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