bsddisklabel.c revision 1.4 1 1.4 martin /* $NetBSD: bsddisklabel.c,v 1.4 2018/06/03 13:16:30 martin Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Based on code written by Philip A. Nelson for Piermont Information
8 1.1 dholland * Systems Inc.
9 1.1 dholland *
10 1.1 dholland * Redistribution and use in source and binary forms, with or without
11 1.1 dholland * modification, are permitted provided that the following conditions
12 1.1 dholland * are met:
13 1.1 dholland * 1. Redistributions of source code must retain the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer.
15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dholland * notice, this list of conditions and the following disclaimer in the
17 1.1 dholland * documentation and/or other materials provided with the distribution.
18 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 1.1 dholland * or promote products derived from this software without specific prior
20 1.1 dholland * written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* bsddisklabel.c -- generate standard BSD disklabel */
36 1.1 dholland /* Included by appropriate arch/XXXX/md.c */
37 1.1 dholland
38 1.1 dholland #include <sys/param.h>
39 1.1 dholland #include <sys/sysctl.h>
40 1.1 dholland #include <sys/exec.h>
41 1.1 dholland #include <sys/utsname.h>
42 1.1 dholland #include <sys/types.h>
43 1.1 dholland #include <sys/stat.h>
44 1.1 dholland #include <machine/cpu.h>
45 1.1 dholland #include <stdio.h>
46 1.1 dholland #include <stddef.h>
47 1.1 dholland #include <util.h>
48 1.1 dholland #include <dirent.h>
49 1.1 dholland #include "defs.h"
50 1.1 dholland #include "md.h"
51 1.1 dholland #include "endian.h"
52 1.1 dholland #include "msg_defs.h"
53 1.1 dholland #include "menu_defs.h"
54 1.1 dholland
55 1.1 dholland /* For the current state of this file blame abs (at) NetBSD.org */
56 1.1 dholland /* Even though he wasn't the last to hack it, but he did admit doing so :-) */
57 1.1 dholland
58 1.1 dholland #define PART_ANY -1
59 1.1 dholland #define PART_EXTRA -2
60 1.1 dholland #define PART_TMP_RAMDISK -3
61 1.1 dholland
62 1.1 dholland /* Defaults for things that might be defined in md.h */
63 1.1 dholland #ifndef PART_ROOT
64 1.1 dholland #define PART_ROOT PART_A
65 1.1 dholland #endif
66 1.1 dholland #ifndef PART_SWAP
67 1.1 dholland #define PART_SWAP PART_B
68 1.1 dholland #endif
69 1.1 dholland #ifndef PART_USR
70 1.1 dholland #define PART_USR PART_ANY
71 1.1 dholland #endif
72 1.1 dholland
73 1.1 dholland #ifndef DEFVARSIZE
74 1.1 dholland #define DEFVARSIZE 32
75 1.1 dholland #endif
76 1.1 dholland #ifndef DEFROOTSIZE
77 1.1 dholland #define DEFROOTSIZE 32
78 1.1 dholland #endif
79 1.1 dholland #ifndef DEFUSRSIZE
80 1.1 dholland #define DEFUSRSIZE 128
81 1.1 dholland #endif
82 1.1 dholland #ifndef DEFSWAPSIZE
83 1.1 dholland #define DEFSWAPSIZE 128
84 1.1 dholland #endif
85 1.1 dholland
86 1.1 dholland int
87 1.1 dholland save_ptn(int ptn, daddr_t start, daddr_t size, int fstype, const char *mountpt)
88 1.1 dholland {
89 1.1 dholland static int maxptn;
90 1.1 dholland partinfo *p;
91 1.1 dholland int pp;
92 1.2 martin char *buf;
93 1.1 dholland
94 1.1 dholland if (maxptn == 0)
95 1.1 dholland maxptn = getmaxpartitions();
96 1.1 dholland
97 1.2 martin if (ptn < 0 || pm->bsdlabel[ptn].pi_fstype != FS_UNUSED) {
98 1.1 dholland ptn = getrawpartition() + 1;
99 1.1 dholland #ifdef PART_FIRST_FREE
100 1.1 dholland if (ptn < PART_FIRST_FREE)
101 1.1 dholland ptn = PART_FIRST_FREE;
102 1.1 dholland #endif
103 1.1 dholland for (;; ptn++) {
104 1.1 dholland if (ptn >= maxptn)
105 1.1 dholland return -1;
106 1.1 dholland if (ptn == PART_USR)
107 1.1 dholland continue;
108 1.2 martin if (pm->bsdlabel[ptn].pi_fstype == FS_UNUSED)
109 1.1 dholland break;
110 1.1 dholland }
111 1.1 dholland }
112 1.1 dholland
113 1.1 dholland if (fstype == FS_UNUSED)
114 1.1 dholland return ptn;
115 1.1 dholland
116 1.2 martin p = pm->bsdlabel + ptn;
117 1.1 dholland p->pi_offset = start;
118 1.1 dholland p->pi_size = size;
119 1.1 dholland set_ptype(p, fstype, mountpt ? PIF_NEWFS : 0);
120 1.1 dholland
121 1.2 martin /* Hack because we does not have something like FS_LVMPV */
122 1.2 martin p->lvmpv = 0;
123 1.2 martin if (mountpt != NULL && strcmp(mountpt, "lvm") == 0)
124 1.2 martin p->lvmpv = 1;
125 1.2 martin else if (mountpt != NULL) {
126 1.1 dholland for (pp = 0; pp < maxptn; pp++) {
127 1.2 martin if (strcmp(pm->bsdlabel[pp].pi_mount, mountpt) == 0)
128 1.2 martin pm->bsdlabel[pp].pi_flags &= ~PIF_MOUNT;
129 1.1 dholland }
130 1.2 martin if (mountpt[0] != '/')
131 1.2 martin asprintf(&buf, "/%s", mountpt);
132 1.2 martin else
133 1.2 martin asprintf(&buf, "%s", mountpt);
134 1.2 martin strlcpy(p->pi_mount, buf, sizeof p->pi_mount);
135 1.1 dholland p->pi_flags |= PIF_MOUNT;
136 1.1 dholland /* Default to UFS2. */
137 1.1 dholland if (p->pi_fstype == FS_BSDFFS) {
138 1.1 dholland #ifdef DEFAULT_UFS2
139 1.1 dholland #ifndef HAVE_UFS2_BOOT
140 1.1 dholland if (strcmp(mountpt, "/") != 0)
141 1.1 dholland #endif
142 1.1 dholland p->pi_flags |= PIF_FFSv2;
143 1.1 dholland #endif
144 1.1 dholland }
145 1.2 martin free(buf);
146 1.1 dholland }
147 1.1 dholland
148 1.1 dholland return ptn;
149 1.1 dholland }
150 1.1 dholland
151 1.1 dholland void
152 1.1 dholland set_ptn_titles(menudesc *m, int opt, void *arg)
153 1.1 dholland {
154 1.1 dholland struct ptn_info *pi = arg;
155 1.1 dholland struct ptn_size *p;
156 1.2 martin int sm = MEG / pm->sectorsize;
157 1.1 dholland daddr_t size;
158 1.1 dholland char inc_free[12];
159 1.1 dholland
160 1.1 dholland p = &pi->ptn_sizes[opt];
161 1.1 dholland if (p->mount[0] == 0) {
162 1.1 dholland wprintw(m->mw, "%s", msg_string(MSG_add_another_ptn));
163 1.1 dholland return;
164 1.1 dholland }
165 1.1 dholland size = p->size;
166 1.1 dholland if (p == pi->pool_part)
167 1.1 dholland snprintf(inc_free, sizeof inc_free, "(%" PRIi64 ")",
168 1.1 dholland (size + pi->free_space) / sm);
169 1.1 dholland else
170 1.1 dholland inc_free[0] = 0;
171 1.1 dholland wprintw(m->mw, "%6" PRIi64 "%8s%10" PRIi64 "%10" PRIi64 " %c %s",
172 1.2 martin size / sm, inc_free, size / pm->dlcylsize, size,
173 1.1 dholland p == pi->pool_part ? '+' : ' ', p->mount);
174 1.1 dholland }
175 1.1 dholland
176 1.1 dholland void
177 1.1 dholland set_ptn_menu(struct ptn_info *pi)
178 1.1 dholland {
179 1.1 dholland struct ptn_size *p;
180 1.1 dholland menu_ent *m;
181 1.1 dholland
182 1.1 dholland for (m = pi->ptn_menus, p = pi->ptn_sizes;; m++, p++) {
183 1.1 dholland m->opt_name = NULL;
184 1.1 dholland m->opt_menu = OPT_NOMENU;
185 1.1 dholland m->opt_flags = 0;
186 1.1 dholland m->opt_action = set_ptn_size;
187 1.1 dholland if (p->mount[0] == 0)
188 1.1 dholland break;
189 1.1 dholland }
190 1.1 dholland if (pi->free_parts != 0)
191 1.1 dholland m++;
192 1.1 dholland m->opt_name = MSG_askunits;
193 1.1 dholland m->opt_menu = MENU_sizechoice;
194 1.1 dholland m->opt_flags = OPT_SUB;
195 1.1 dholland m->opt_action = NULL;
196 1.1 dholland m++;
197 1.1 dholland
198 1.1 dholland if (pi->free_space >= 0)
199 1.1 dholland snprintf(pi->exit_msg, sizeof pi->exit_msg,
200 1.1 dholland msg_string(MSG_fssizesok),
201 1.1 dholland (int)(pi->free_space / sizemult), multname, pi->free_parts);
202 1.1 dholland else
203 1.1 dholland snprintf(pi->exit_msg, sizeof pi->exit_msg,
204 1.1 dholland msg_string(MSG_fssizesbad),
205 1.1 dholland (int)(-pi->free_space / sizemult), multname, (uint) -pi->free_space);
206 1.1 dholland
207 1.1 dholland set_menu_numopts(pi->menu_no, m - pi->ptn_menus);
208 1.1 dholland }
209 1.1 dholland
210 1.1 dholland int
211 1.1 dholland set_ptn_size(menudesc *m, void *arg)
212 1.1 dholland {
213 1.1 dholland struct ptn_info *pi = arg;
214 1.1 dholland struct ptn_size *p;
215 1.1 dholland char answer[10];
216 1.1 dholland char dflt[10];
217 1.1 dholland char *cp;
218 1.1 dholland daddr_t size, old_size;
219 1.1 dholland int mult;
220 1.1 dholland
221 1.1 dholland p = pi->ptn_sizes + m->cursel;
222 1.1 dholland
223 1.1 dholland if (pi->free_parts == 0 && p->size == 0)
224 1.1 dholland /* Don't allow 'free_parts' to go negative */
225 1.1 dholland return 0;
226 1.1 dholland
227 1.1 dholland if (p->mount[0] == 0) {
228 1.2 martin msg_prompt_win(partman_go?MSG_askfsmountadv:MSG_askfsmount,
229 1.2 martin -1, 18, 0, 0, NULL, p->mount, sizeof p->mount);
230 1.1 dholland if (p->mount[0] == 0)
231 1.1 dholland return 0;
232 1.1 dholland }
233 1.1 dholland
234 1.1 dholland size = p->size;
235 1.1 dholland old_size = size;
236 1.1 dholland if (size == 0)
237 1.1 dholland size = p->dflt_size;
238 1.1 dholland size /= sizemult;
239 1.1 dholland snprintf(dflt, sizeof dflt, "%" PRIi64 "%s",
240 1.1 dholland size, p == pi->pool_part ? "+" : "");
241 1.1 dholland
242 1.1 dholland for (;;) {
243 1.1 dholland mult = sizemult;
244 1.1 dholland msg_prompt_win(MSG_askfssize, -1, 18, 0, 0,
245 1.1 dholland dflt, answer, sizeof answer,
246 1.1 dholland p->mount, multname);
247 1.1 dholland /* Some special cases when /usr is first given a size */
248 1.1 dholland if (old_size == 0 && !strcmp(p->mount, "/usr")) {
249 1.1 dholland /* Remove space for /usr from / */
250 1.1 dholland if (!pi->ptn_sizes[0].changed) {
251 1.1 dholland pi->ptn_sizes[0].size -= p->dflt_size;
252 1.1 dholland pi->free_space += p->dflt_size;
253 1.1 dholland pi->ptn_sizes[0].changed = 1;
254 1.1 dholland }
255 1.1 dholland /* hack to add free space to default sized /usr */
256 1.1 dholland if (!strcmp(answer, dflt)) {
257 1.1 dholland size = p->dflt_size;
258 1.1 dholland pi->pool_part = p;
259 1.1 dholland goto adjust_free;
260 1.1 dholland }
261 1.1 dholland }
262 1.1 dholland size = strtoul(answer, &cp, 0);
263 1.1 dholland switch (*cp++) {
264 1.1 dholland default:
265 1.1 dholland continue;
266 1.1 dholland case 's':
267 1.1 dholland mult = 1;
268 1.1 dholland break;
269 1.1 dholland case 'c':
270 1.2 martin mult = pm->dlcylsize;
271 1.1 dholland break;
272 1.1 dholland case 'm':
273 1.1 dholland case 'M':
274 1.2 martin mult = MEG / pm->sectorsize;
275 1.1 dholland break;
276 1.1 dholland case 'g':
277 1.1 dholland case 'G':
278 1.2 martin mult = 1024 * MEG / pm->sectorsize;
279 1.1 dholland break;
280 1.1 dholland case '+':
281 1.1 dholland cp--;
282 1.1 dholland if (cp != answer)
283 1.1 dholland break;
284 1.1 dholland mult = 1;
285 1.1 dholland size = old_size;
286 1.1 dholland break;
287 1.1 dholland case 0:
288 1.1 dholland cp--;
289 1.1 dholland break;
290 1.1 dholland }
291 1.1 dholland if (*cp == 0 || *cp == '+')
292 1.1 dholland break;
293 1.1 dholland }
294 1.1 dholland
295 1.2 martin size = NUMSEC(size, mult, pm->dlcylsize);
296 1.1 dholland if (p->ptn_id == PART_TMP_RAMDISK) {
297 1.1 dholland p->size = size;
298 1.1 dholland return 0;
299 1.1 dholland }
300 1.1 dholland if (p == pi->pool_part)
301 1.1 dholland pi->pool_part = NULL;
302 1.1 dholland if (*cp == '+' && p->limit == 0) {
303 1.1 dholland pi->pool_part = p;
304 1.1 dholland if (size == 0)
305 1.2 martin size = pm->dlcylsize;
306 1.1 dholland }
307 1.1 dholland if (p->limit != 0 && size > p->limit)
308 1.1 dholland size = p->limit;
309 1.1 dholland adjust_free:
310 1.1 dholland if (size != old_size)
311 1.1 dholland p->changed = 1;
312 1.1 dholland pi->free_space += old_size - size;
313 1.1 dholland p->size = size;
314 1.1 dholland if (size == 0) {
315 1.1 dholland if (old_size != 0)
316 1.1 dholland pi->free_parts++;
317 1.1 dholland if (p->ptn_id == PART_EXTRA)
318 1.1 dholland memmove(p, p + 1,
319 1.1 dholland (char *)&pi->ptn_sizes[MAXPARTITIONS]
320 1.1 dholland - (char *)p);
321 1.1 dholland } else {
322 1.1 dholland int f = pi->free_space;
323 1.1 dholland if (old_size == 0)
324 1.1 dholland pi->free_parts--;
325 1.1 dholland if (f < mult && -f < mult) {
326 1.1 dholland /*
327 1.1 dholland * Round size to end of available space,
328 1.1 dholland * but keep cylinder alignment
329 1.1 dholland */
330 1.1 dholland if (f < 0)
331 1.2 martin f = -roundup(-f, pm->dlcylsize);
332 1.1 dholland else
333 1.2 martin f = rounddown(f, pm->dlcylsize);
334 1.1 dholland size += f;
335 1.1 dholland if (size != 0) {
336 1.1 dholland pi->free_space -= f;
337 1.1 dholland p->size += f;
338 1.1 dholland }
339 1.1 dholland }
340 1.1 dholland }
341 1.1 dholland
342 1.1 dholland set_ptn_menu(pi);
343 1.1 dholland
344 1.1 dholland return 0;
345 1.1 dholland }
346 1.1 dholland
347 1.2 martin /* Menu to change sizes of /, /usr, /home and etc. partitions */
348 1.1 dholland void
349 1.1 dholland get_ptn_sizes(daddr_t part_start, daddr_t sectors, int no_swap)
350 1.1 dholland {
351 1.1 dholland int i;
352 1.1 dholland int maxpart = getmaxpartitions();
353 1.1 dholland int sm; /* sectors in 1MB */
354 1.1 dholland struct ptn_size *p;
355 1.1 dholland daddr_t size;
356 1.2 martin static int swap_created = 0, root_created = 0;
357 1.1 dholland
358 1.2 martin if (pm->pi.menu_no < 0)
359 1.2 martin pm->pi = (struct ptn_info) { -1, {
360 1.1 dholland #define PI_ROOT 0
361 1.1 dholland { PART_ROOT, { '/', '\0' },
362 1.1 dholland DEFROOTSIZE, DEFROOTSIZE , 0, 0},
363 1.1 dholland #define PI_SWAP 1
364 1.1 dholland { PART_SWAP, { 's', 'w', 'a', 'p', '\0' },
365 1.1 dholland DEFSWAPSIZE, DEFSWAPSIZE, 0, 0 },
366 1.1 dholland { PART_TMP_RAMDISK,
367 1.1 dholland #ifdef HAVE_TMPFS
368 1.1 dholland { '/', 't', 'm', 'p', ' ', '(', 't', 'm', 'p', 'f', 's', ')', '\0' },
369 1.1 dholland #else
370 1.1 dholland { '/', 't', 'm', 'p', ' ', '(', 'm', 'f', 's', ')', '\0' },
371 1.1 dholland #endif
372 1.1 dholland 64, 0, 0, 0 },
373 1.1 dholland #define PI_USR 3
374 1.1 dholland { PART_USR, { '/', 'u', 's', 'r', '\0' }, DEFUSRSIZE,
375 1.1 dholland 0, 0, 0 },
376 1.1 dholland { PART_ANY, { '/', 'v', 'a', 'r', '\0' }, DEFVARSIZE,
377 1.1 dholland 0, 0, 0 },
378 1.1 dholland { PART_ANY, { '/', 'h', 'o', 'm', 'e', '\0' }, 0,
379 1.1 dholland 0, 0, 0 },
380 1.1 dholland }, {
381 1.1 dholland { NULL, OPT_NOMENU, 0, set_ptn_size },
382 1.1 dholland { MSG_askunits, MENU_sizechoice, OPT_SUB, NULL },
383 1.1 dholland }, 0, 0, NULL, { 0 } };
384 1.1 dholland
385 1.1 dholland if (maxpart > MAXPARTITIONS)
386 1.1 dholland maxpart = MAXPARTITIONS; /* sanity */
387 1.1 dholland
388 1.1 dholland msg_display(MSG_ptnsizes);
389 1.1 dholland msg_table_add(MSG_ptnheaders);
390 1.1 dholland
391 1.2 martin if (pm->pi.menu_no < 0) {
392 1.1 dholland /* If there is a swap partition elsewhere, don't add one here.*/
393 1.2 martin if (no_swap || (swap_created && partman_go)) {
394 1.2 martin pm->pi.ptn_sizes[PI_SWAP].size = 0;
395 1.1 dholland } else {
396 1.1 dholland #if DEFSWAPSIZE == -1
397 1.1 dholland /* Dynamic swap size. */
398 1.2 martin pm->pi.ptn_sizes[PI_SWAP].dflt_size = get_ramsize();
399 1.2 martin pm->pi.ptn_sizes[PI_SWAP].size =
400 1.2 martin pm->pi.ptn_sizes[PI_SWAP].dflt_size;
401 1.1 dholland #endif
402 1.1 dholland }
403 1.1 dholland
404 1.1 dholland /* If installing X increase default size of /usr */
405 1.1 dholland if (set_X11_selected())
406 1.2 martin pm->pi.ptn_sizes[PI_USR].dflt_size += XNEEDMB;
407 1.1 dholland
408 1.1 dholland /* Start of planning to give free space to / */
409 1.2 martin pm->pi.pool_part = &pm->pi.ptn_sizes[PI_ROOT];
410 1.1 dholland /* Make size of root include default size of /usr */
411 1.2 martin pm->pi.ptn_sizes[PI_ROOT].size += pm->pi.ptn_sizes[PI_USR].dflt_size;
412 1.1 dholland
413 1.2 martin sm = MEG / pm->sectorsize;
414 1.1 dholland
415 1.1 dholland if (root_limit != 0) {
416 1.1 dholland /* Bah - bios can not read all the disk, limit root */
417 1.2 martin pm->pi.ptn_sizes[PI_ROOT].limit = root_limit - part_start;
418 1.1 dholland /* Allocate a /usr partition if bios can't read
419 1.1 dholland * everything except swap.
420 1.1 dholland */
421 1.2 martin if (pm->pi.ptn_sizes[PI_ROOT].limit
422 1.2 martin < sectors - pm->pi.ptn_sizes[PI_SWAP].size * sm) {
423 1.1 dholland /* Root won't be able to access all the space */
424 1.1 dholland /* Claw back space for /usr */
425 1.2 martin pm->pi.ptn_sizes[PI_USR].size =
426 1.2 martin pm->pi.ptn_sizes[PI_USR].dflt_size;
427 1.2 martin pm->pi.ptn_sizes[PI_ROOT].size -=
428 1.2 martin pm->pi.ptn_sizes[PI_USR].dflt_size;
429 1.2 martin pm->pi.ptn_sizes[PI_ROOT].changed = 1;
430 1.1 dholland /* Give free space to /usr */
431 1.2 martin pm->pi.pool_part = &pm->pi.ptn_sizes[PI_USR];
432 1.1 dholland }
433 1.1 dholland }
434 1.1 dholland
435 1.1 dholland /* Change preset sizes from MB to sectors */
436 1.2 martin pm->pi.free_space = sectors;
437 1.2 martin for (p = pm->pi.ptn_sizes; p->mount[0]; p++) {
438 1.2 martin p->size = NUMSEC(p->size, sm, pm->dlcylsize);
439 1.2 martin p->dflt_size = NUMSEC(p->dflt_size, sm, pm->dlcylsize);
440 1.2 martin pm->pi.free_space -= p->size;
441 1.1 dholland }
442 1.1 dholland
443 1.1 dholland /* Steal space from swap to make things fit.. */
444 1.2 martin if (pm->pi.free_space < 0) {
445 1.2 martin i = roundup(-pm->pi.free_space, pm->dlcylsize);
446 1.2 martin if (i > pm->pi.ptn_sizes[PI_SWAP].size)
447 1.2 martin i = pm->pi.ptn_sizes[PI_SWAP].size;
448 1.2 martin pm->pi.ptn_sizes[PI_SWAP].size -= i;
449 1.2 martin pm->pi.free_space += i;
450 1.1 dholland }
451 1.1 dholland
452 1.1 dholland /* Add space for 2 system dumps to / (traditional) */
453 1.1 dholland i = get_ramsize() * sm;
454 1.2 martin i = roundup(i, pm->dlcylsize);
455 1.2 martin if (pm->pi.free_space > i * 2)
456 1.1 dholland i *= 2;
457 1.2 martin if (pm->pi.free_space > i) {
458 1.2 martin pm->pi.ptn_sizes[PI_ROOT].size += i;
459 1.2 martin pm->pi.free_space -= i;
460 1.2 martin }
461 1.2 martin
462 1.2 martin if (root_created && partman_go) {
463 1.2 martin pm->pi.ptn_sizes[PI_ROOT].size = 0;
464 1.2 martin pm->pi.pool_part = 0;
465 1.1 dholland }
466 1.1 dholland
467 1.1 dholland /* Ensure all of / is readable by the system boot code */
468 1.2 martin i = pm->pi.ptn_sizes[PI_ROOT].limit;
469 1.2 martin if (i != 0 && (i -= pm->pi.ptn_sizes[PI_ROOT].size) < 0) {
470 1.2 martin pm->pi.ptn_sizes[PI_ROOT].size += i;
471 1.2 martin pm->pi.free_space -= i;
472 1.1 dholland }
473 1.1 dholland
474 1.1 dholland /* Count free partition slots */
475 1.2 martin pm->pi.free_parts = 0;
476 1.1 dholland for (i = 0; i < maxpart; i++) {
477 1.2 martin if (pm->bsdlabel[i].pi_size == 0)
478 1.2 martin pm->pi.free_parts++;
479 1.1 dholland }
480 1.1 dholland for (i = 0; i < MAXPARTITIONS; i++) {
481 1.2 martin p = &pm->pi.ptn_sizes[i];
482 1.1 dholland if (i != 0 && p->ptn_id == 0)
483 1.1 dholland p->ptn_id = PART_EXTRA;
484 1.1 dholland if (p->size != 0)
485 1.2 martin pm->pi.free_parts--;
486 1.1 dholland }
487 1.1 dholland
488 1.2 martin pm->pi.menu_no = new_menu(0, pm->pi.ptn_menus, nelem(pm->pi.ptn_menus),
489 1.1 dholland 3, -1, 12, 70,
490 1.1 dholland MC_ALWAYS_SCROLL | MC_NOBOX | MC_NOCLEAR,
491 1.1 dholland NULL, set_ptn_titles, NULL,
492 1.2 martin "help", pm->pi.exit_msg);
493 1.1 dholland
494 1.2 martin if (pm->pi.menu_no < 0)
495 1.1 dholland return;
496 1.1 dholland }
497 1.1 dholland
498 1.1 dholland do {
499 1.2 martin set_ptn_menu(&pm->pi);
500 1.2 martin pm->current_cylsize = pm->dlcylsize;
501 1.2 martin process_menu(pm->pi.menu_no, &pm->pi);
502 1.2 martin } while (pm->pi.free_space < 0 || pm->pi.free_parts < 0);
503 1.1 dholland
504 1.1 dholland /* Give any cylinder fragment to last partition */
505 1.2 martin if (pm->pi.pool_part != NULL || pm->pi.free_space < pm->dlcylsize) {
506 1.2 martin for (p = pm->pi.ptn_sizes + nelem(pm->pi.ptn_sizes) - 1; ;p--) {
507 1.1 dholland if (p->size == 0) {
508 1.2 martin if (p == pm->pi.ptn_sizes)
509 1.1 dholland break;
510 1.1 dholland continue;
511 1.1 dholland }
512 1.1 dholland if (p->ptn_id == PART_TMP_RAMDISK)
513 1.1 dholland continue;
514 1.2 martin p->size += pm->pi.free_space % pm->dlcylsize;
515 1.2 martin pm->pi.free_space -= pm->pi.free_space % pm->dlcylsize;
516 1.1 dholland break;
517 1.1 dholland }
518 1.1 dholland }
519 1.1 dholland
520 1.2 martin for (p = pm->pi.ptn_sizes; p->mount[0]; p++, part_start += size) {
521 1.1 dholland size = p->size;
522 1.2 martin if (p == pm->pi.pool_part) {
523 1.2 martin size += rounddown(pm->pi.free_space, pm->dlcylsize);
524 1.1 dholland if (p->limit != 0 && size > p->limit)
525 1.1 dholland size = p->limit;
526 1.1 dholland }
527 1.1 dholland i = p->ptn_id;
528 1.1 dholland if (i == PART_TMP_RAMDISK) {
529 1.1 dholland tmp_ramdisk_size = size;
530 1.1 dholland size = 0;
531 1.1 dholland continue;
532 1.1 dholland }
533 1.1 dholland if (size == 0)
534 1.1 dholland continue;
535 1.2 martin if (i == PART_ROOT && size > 0)
536 1.2 martin root_created = 1;
537 1.1 dholland if (i == PART_SWAP) {
538 1.2 martin if (size > 0)
539 1.2 martin swap_created = 1;
540 1.1 dholland save_ptn(i, part_start, size, FS_SWAP, NULL);
541 1.1 dholland continue;
542 1.1 dholland }
543 1.2 martin if (!strcmp(p->mount, "raid")) {
544 1.2 martin save_ptn(i, part_start, size, FS_RAID, NULL);
545 1.2 martin continue;
546 1.2 martin } else if (!strcmp(p->mount, "cgd")) {
547 1.2 martin save_ptn(i, part_start, size, FS_CGD, NULL);
548 1.2 martin continue;
549 1.2 martin }
550 1.1 dholland save_ptn(i, part_start, size, FS_BSDFFS, p->mount);
551 1.1 dholland }
552 1.1 dholland }
553 1.1 dholland
554 1.1 dholland /*
555 1.1 dholland * md back-end code for menu-driven BSD disklabel editor.
556 1.1 dholland */
557 1.1 dholland int
558 1.1 dholland make_bsd_partitions(void)
559 1.1 dholland {
560 1.1 dholland int i;
561 1.1 dholland int part;
562 1.1 dholland int maxpart = getmaxpartitions();
563 1.1 dholland daddr_t partstart;
564 1.1 dholland int part_raw, part_bsd;
565 1.1 dholland daddr_t ptend;
566 1.1 dholland int no_swap = 0, valid_part = -1;
567 1.2 martin partinfo *p, savedlabel[MAXPARTITIONS];
568 1.2 martin
569 1.4 martin if (pm && pm->no_part)
570 1.4 martin return 1;
571 1.4 martin
572 1.2 martin memcpy(&savedlabel, &pm->bsdlabel, sizeof savedlabel);
573 1.1 dholland
574 1.1 dholland /*
575 1.1 dholland * Initialize global variables that track space used on this disk.
576 1.1 dholland * Standard 4.4BSD 8-partition labels always cover whole disk.
577 1.1 dholland */
578 1.2 martin if (pm->ptsize == 0)
579 1.2 martin pm->ptsize = pm->dlsize - pm->ptstart;
580 1.2 martin if (pm->dlsize == 0)
581 1.2 martin pm->dlsize = pm->ptstart + pm->ptsize;
582 1.2 martin if (logfp) fprintf(logfp, "dlsize=%" PRId64 " ptsize=%" PRId64
583 1.2 martin " ptstart=%" PRId64 "\n",
584 1.2 martin pm->dlsize, pm->ptsize, pm->ptstart);
585 1.1 dholland
586 1.2 martin partstart = pm->ptstart;
587 1.2 martin ptend = pm->ptstart + pm->ptsize;
588 1.1 dholland
589 1.1 dholland /* Ask for layout type -- standard or special */
590 1.2 martin if (partman_go == 0) {
591 1.2 martin msg_display(MSG_layout,
592 1.2 martin (int) (pm->ptsize / (MEG / pm->sectorsize)),
593 1.1 dholland DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE,
594 1.1 dholland DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB);
595 1.2 martin process_menu(MENU_layout, NULL);
596 1.2 martin }
597 1.1 dholland
598 1.1 dholland /* Set so we use the 'real' geometry for rounding, input in MB */
599 1.2 martin pm->current_cylsize = pm->dlcylsize;
600 1.1 dholland set_sizemultname_meg();
601 1.1 dholland
602 1.1 dholland /* Build standard partitions */
603 1.2 martin memset(&pm->bsdlabel, 0, sizeof pm->bsdlabel);
604 1.1 dholland
605 1.1 dholland /* Set initial partition types to unused */
606 1.1 dholland for (part = 0 ; part < maxpart ; ++part)
607 1.2 martin pm->bsdlabel[part].pi_fstype = FS_UNUSED;
608 1.1 dholland
609 1.1 dholland /* Whole disk partition */
610 1.1 dholland part_raw = getrawpartition();
611 1.1 dholland if (part_raw == -1)
612 1.1 dholland part_raw = PART_C; /* for sanity... */
613 1.2 martin pm->bsdlabel[part_raw].pi_offset = 0;
614 1.2 martin pm->bsdlabel[part_raw].pi_size = pm->dlsize;
615 1.1 dholland
616 1.1 dholland if (part_raw == PART_D) {
617 1.1 dholland /* Probably a system that expects an i386 style mbr */
618 1.1 dholland part_bsd = PART_C;
619 1.2 martin pm->bsdlabel[PART_C].pi_offset = pm->ptstart;
620 1.2 martin pm->bsdlabel[PART_C].pi_size = pm->ptsize;
621 1.1 dholland } else {
622 1.1 dholland part_bsd = part_raw;
623 1.1 dholland }
624 1.1 dholland
625 1.1 dholland #if defined(PART_BOOT) && defined(BOOT_SIZE)
626 1.1 dholland i = BOOT_SIZE;
627 1.1 dholland if (i >= 1024) {
628 1.1 dholland /* Treat big numbers as a byte count */
629 1.2 martin i = (i + pm->dlcylsize * pm->sectorsize - 1) / (pm->dlcylsize * pm->sectorsize);
630 1.2 martin i *= pm->dlcylsize;
631 1.1 dholland }
632 1.1 dholland #if defined(PART_BOOT_FFS)
633 1.2 martin pm->bsdlabel[PART_BOOT].pi_fstype = FS_BSDFFS;
634 1.2 martin pm->bsdlabel[PART_BOOT].pi_flags = PIF_NEWFS;
635 1.1 dholland #else
636 1.2 martin pm->bsdlabel[PART_BOOT].pi_fstype = FS_BOOT;
637 1.1 dholland #endif
638 1.2 martin pm->bsdlabel[PART_BOOT].pi_size = i;
639 1.1 dholland #ifdef BOOT_HIGH
640 1.2 martin pm->bsdlabel[PART_BOOT].pi_offset = ptend - i;
641 1.1 dholland ptend -= i;
642 1.1 dholland #else
643 1.2 martin pm->bsdlabel[PART_BOOT].pi_offset = pm->ptstart;
644 1.1 dholland partstart += i;
645 1.1 dholland #endif
646 1.1 dholland #elif defined(PART_BOOT)
647 1.2 martin if (pm->bootsize != 0) {
648 1.1 dholland #if defined(PART_BOOT_MSDOS)
649 1.2 martin pm->bsdlabel[PART_BOOT].pi_fstype = FS_MSDOS;
650 1.1 dholland #else
651 1.2 martin pm->bsdlabel[PART_BOOT].pi_fstype = FS_BOOT;
652 1.1 dholland #endif
653 1.2 martin pm->bsdlabel[PART_BOOT].pi_size = pm->bootsize;
654 1.2 martin pm->bsdlabel[PART_BOOT].pi_offset = pm->bootstart;
655 1.1 dholland #if defined(PART_BOOT_PI_FLAGS)
656 1.2 martin pm->bsdlabel[PART_BOOT].pi_flags |= PART_BOOT_PI_FLAGS;
657 1.1 dholland #endif
658 1.1 dholland #if defined(PART_BOOT_PI_MOUNT)
659 1.2 martin strlcpy(pm->bsdlabel[PART_BOOT].pi_mount, PART_BOOT_PI_MOUNT,
660 1.2 martin sizeof pm->bsdlabel[PART_BOOT].pi_mount);
661 1.1 dholland #endif
662 1.1 dholland }
663 1.1 dholland #endif /* PART_BOOT w/o BOOT_SIZE */
664 1.1 dholland
665 1.1 dholland #if defined(PART_SYSVBFS) && defined(SYSVBFS_SIZE)
666 1.2 martin pm->bsdlabel[PART_SYSVBFS].pi_offset = partstart;
667 1.2 martin pm->bsdlabel[PART_SYSVBFS].pi_fstype = FS_SYSVBFS;
668 1.2 martin pm->bsdlabel[PART_SYSVBFS].pi_size = SYSVBFS_SIZE;
669 1.2 martin pm->bsdlabel[PART_SYSVBFS].pi_flags |= PIF_NEWFS | PIF_MOUNT;
670 1.2 martin strlcpy(pm->bsdlabel[PART_SYSVBFS].pi_mount, "/stand",
671 1.2 martin sizeof pm->bsdlabel[PART_SYSVBFS].pi_mount);
672 1.1 dholland partstart += SYSVBFS_SIZE;
673 1.1 dholland #endif
674 1.1 dholland
675 1.1 dholland #ifdef PART_REST
676 1.2 martin pm->bsdlabel[PART_REST].pi_offset = 0;
677 1.2 martin pm->bsdlabel[PART_REST].pi_size = pm->ptstart;
678 1.1 dholland #endif
679 1.1 dholland
680 1.2 martin if (layoutkind == LY_USEEXIST) {
681 1.1 dholland /*
682 1.2 martin * If 'pm->oldlabel' is a default label created by the kernel it
683 1.1 dholland * will have exactly one valid partition besides raw_part
684 1.1 dholland * which covers the whole disk - but might lie outside the
685 1.1 dholland * mbr partition we (by now) have offset by a few sectors.
686 1.1 dholland * Check for this and and fix ut up.
687 1.1 dholland */
688 1.1 dholland valid_part = -1;
689 1.1 dholland for (i = 0; i < maxpart; i++) {
690 1.1 dholland if (i == part_raw)
691 1.1 dholland continue;
692 1.2 martin if (pm->oldlabel[i].pi_size > 0 && PI_ISBSDFS(&pm->oldlabel[i])) {
693 1.1 dholland if (valid_part >= 0) {
694 1.1 dholland /* nope, not the default case */
695 1.1 dholland valid_part = -1;
696 1.1 dholland break;
697 1.1 dholland }
698 1.1 dholland valid_part = i;
699 1.1 dholland }
700 1.1 dholland }
701 1.2 martin if (valid_part >= 0 && pm->oldlabel[valid_part].pi_offset < pm->ptstart) {
702 1.2 martin pm->oldlabel[valid_part].pi_offset = pm->ptstart;
703 1.2 martin pm->oldlabel[valid_part].pi_size -= pm->ptstart;
704 1.1 dholland }
705 1.1 dholland }
706 1.1 dholland
707 1.1 dholland /*
708 1.1 dholland * Save any partitions that are outside the area we are
709 1.1 dholland * going to use.
710 1.1 dholland * In particular this saves details of the other MBR
711 1.1 dholland * partitions on a multiboot i386 system.
712 1.1 dholland */
713 1.1 dholland for (i = maxpart; i--;) {
714 1.2 martin if (pm->bsdlabel[i].pi_size != 0)
715 1.1 dholland /* Don't overwrite special partitions */
716 1.1 dholland continue;
717 1.2 martin p = &pm->oldlabel[i];
718 1.1 dholland if (p->pi_fstype == FS_UNUSED || p->pi_size == 0)
719 1.1 dholland continue;
720 1.2 martin if (layoutkind == LY_USEEXIST) {
721 1.1 dholland if (PI_ISBSDFS(p)) {
722 1.1 dholland p->pi_flags |= PIF_MOUNT;
723 1.2 martin if (layoutkind == LY_USEEXIST && i == valid_part) {
724 1.1 dholland int fstype = p->pi_fstype;
725 1.1 dholland p->pi_fstype = 0;
726 1.1 dholland strcpy(p->pi_mount, "/");
727 1.1 dholland set_ptype(p, fstype, PIF_NEWFS);
728 1.1 dholland }
729 1.1 dholland }
730 1.1 dholland } else {
731 1.2 martin if (p->pi_offset < pm->ptstart + pm->ptsize &&
732 1.2 martin p->pi_offset + p->pi_size > pm->ptstart)
733 1.1 dholland /* Not outside area we are allocating */
734 1.1 dholland continue;
735 1.1 dholland if (p->pi_fstype == FS_SWAP)
736 1.1 dholland no_swap = 1;
737 1.1 dholland }
738 1.2 martin pm->bsdlabel[i] = pm->oldlabel[i];
739 1.1 dholland }
740 1.1 dholland
741 1.2 martin if (layoutkind == LY_SETNEW)
742 1.1 dholland get_ptn_sizes(partstart, ptend - partstart, no_swap);
743 1.2 martin
744 1.2 martin else if (layoutkind == LY_NEWRAID) {
745 1.2 martin set_ptype(&(pm->bsdlabel[PART_E]), FS_RAID, 0);
746 1.2 martin pm->bsdlabel[PART_E].pi_size = pm->ptsize;
747 1.2 martin }
748 1.2 martin else if (layoutkind == LY_NEWCGD) {
749 1.2 martin set_ptype(&(pm->bsdlabel[PART_E]), FS_CGD, 0);
750 1.2 martin pm->bsdlabel[PART_E].pi_size = pm->ptsize;
751 1.2 martin }
752 1.2 martin else if (layoutkind == LY_NEWLVM) {
753 1.2 martin set_ptype(&(pm->bsdlabel[PART_E]), FS_BSDFFS, 0);
754 1.2 martin pm->bsdlabel[PART_E].pi_size = pm->ptsize;
755 1.2 martin pm->bsdlabel[PART_E].lvmpv = 1;
756 1.2 martin }
757 1.1 dholland
758 1.1 dholland /*
759 1.1 dholland * OK, we have a partition table. Give the user the chance to
760 1.1 dholland * edit it and verify it's OK, or abort altogether.
761 1.1 dholland */
762 1.2 martin do {
763 1.2 martin if (edit_and_check_label(pm->bsdlabel, maxpart, part_raw, part_bsd) == 0) {
764 1.2 martin msg_display(MSG_abort);
765 1.2 martin memcpy(&pm->bsdlabel, &savedlabel, sizeof pm->bsdlabel);
766 1.2 martin return 0;
767 1.2 martin }
768 1.2 martin } while (partman_go == 0 && check_partitions() == 0);
769 1.1 dholland
770 1.1 dholland /* Disk name */
771 1.2 martin if (!partman_go)
772 1.2 martin msg_prompt(MSG_packname, pm->bsddiskname, pm->bsddiskname,
773 1.2 martin sizeof pm->bsddiskname);
774 1.1 dholland
775 1.1 dholland /* save label to disk for MI code to update. */
776 1.2 martin if (! partman_go)
777 1.2 martin (void) savenewlabel(pm->bsdlabel, maxpart);
778 1.1 dholland
779 1.1 dholland /* Everything looks OK. */
780 1.1 dholland return (1);
781 1.1 dholland }
782 1.1 dholland
783 1.1 dholland /*
784 1.1 dholland * check that there is at least a / somewhere.
785 1.1 dholland */
786 1.2 martin int
787 1.1 dholland check_partitions(void)
788 1.1 dholland {
789 1.1 dholland #ifdef HAVE_BOOTXX_xFS
790 1.2 martin int rv = 1;
791 1.1 dholland char *bootxx;
792 1.1 dholland #endif
793 1.1 dholland #ifndef HAVE_UFS2_BOOT
794 1.1 dholland int fstype;
795 1.1 dholland #endif
796 1.1 dholland
797 1.1 dholland #ifdef HAVE_BOOTXX_xFS
798 1.1 dholland /* check if we have boot code for the root partition type */
799 1.1 dholland bootxx = bootxx_name();
800 1.1 dholland if (bootxx != NULL) {
801 1.1 dholland rv = access(bootxx, R_OK);
802 1.1 dholland free(bootxx);
803 1.1 dholland } else
804 1.1 dholland rv = -1;
805 1.1 dholland if (rv != 0) {
806 1.3 joerg process_menu(MENU_ok, __UNCONST(MSG_No_Bootcode));
807 1.1 dholland return 0;
808 1.1 dholland }
809 1.1 dholland #endif
810 1.1 dholland #ifndef HAVE_UFS2_BOOT
811 1.2 martin fstype = pm->bsdlabel[pm->rootpart].pi_fstype;
812 1.1 dholland if (fstype == FS_BSDFFS &&
813 1.2 martin (pm->bsdlabel[pm->rootpart].pi_flags & PIF_FFSv2) != 0) {
814 1.3 joerg process_menu(MENU_ok, __UNCONST(MSG_cannot_ufs2_root));
815 1.1 dholland return 0;
816 1.1 dholland }
817 1.1 dholland #endif
818 1.1 dholland
819 1.1 dholland return md_check_partitions();
820 1.1 dholland }
821