Home | History | Annotate | Line # | Download | only in sysinst
bsddisklabel.c revision 1.25
      1 /*	$NetBSD: bsddisklabel.c,v 1.25 2019/08/01 17:49:12 martin 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 <assert.h>
     46 #include <stdio.h>
     47 #include <stddef.h>
     48 #include <util.h>
     49 #include <dirent.h>
     50 #include "defs.h"
     51 #include "md.h"
     52 #include "defsizes.h"
     53 #include "endian.h"
     54 #include "msg_defs.h"
     55 #include "menu_defs.h"
     56 
     57 static size_t fill_ptn_menu(struct partition_usage_set *pset);
     58 
     59 /*
     60  * The default partition layout.
     61  */
     62 static const struct part_usage_info
     63 default_parts_init[] =
     64 {
     65 	/*
     66 	 * Pretty complex setup for boot partitions.
     67 	 * This is copy&pasted below, please keep in sync!
     68 	 */
     69 #ifdef PART_BOOT
     70 	{ .size = PART_BOOT/512,	/* PART_BOOT is in BYTE, not MB! */
     71 #ifdef PART_BOOT_MOUNT
     72 	  .mount = PART_BOOT_MOUNT,
     73 	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
     74 #else
     75 	  .instflags = PUIINST_BOOT,
     76 #endif
     77 #ifdef PART_BOOT_TYPE
     78 	  .fs_type = PART_BOOT_TYPE,
     79 #if PART_BOOT_TYPE == FS_MSDOS
     80 	  .flags = PUIFLAG_ADD_OUTER,
     81 #endif
     82 #endif
     83 #ifdef PART_BOOT_SUBT
     84 	  .fs_version = PART_BOOT_SUBT,
     85 #endif
     86 	},
     87 #endif
     88 
     89 	/*
     90 	 * Two more copies of above for _BOOT1 and _BOOT2, please
     91 	 * keep in sync!
     92 	 */
     93 #ifdef PART_BOOT1
     94 	{ .size = PART_BOOT1/512,	/* PART_BOOT1 is in BYTE, not MB! */
     95 #ifdef PART_BOOT1_MOUNT
     96 	  .mount = PART_BOOT1_MOUNT,
     97 	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
     98 #else
     99 	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
    100 #endif
    101 #ifdef PART_BOOT1_TYPE
    102 	  .fs_type = PART_BOOT1_TYPE,
    103 #if PART_BOOT1_TYPE == FS_MSDOS
    104 	  .flags = PUIFLAG_ADD_OUTER,
    105 #endif
    106 #endif
    107 #ifdef PART_BOOT1_SUBT
    108 	  .fs_version = PART_BOOT1_SUBT,
    109 #endif
    110 	},
    111 #endif
    112 #ifdef PART_BOOT2
    113 	{ .size = PART_BOOT2/512,	/* PART_BOOT2 is in BYTE, not MB! */
    114 #ifdef PART_BOOT2_MOUNT
    115 	  .mount = PART_BOOT2_MOUNT,
    116 	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
    117 #else
    118 	  .instflags = PUIINST_MOUNT|PUIINST_BOOT,
    119 #endif
    120 #ifdef PART_BOOT2_TYPE
    121 	  .fs_type = PART_BOOT2_TYPE,
    122 #if PART_BOOT2_TYPE == FS_MSDOS
    123 	  .flags = PUIFLAG_ADD_OUTER,
    124 #endif
    125 #endif
    126 #ifdef PART_BOOT2_SUBT
    127 	  .fs_version = PART_BOOT1_SUBT,
    128 #endif
    129 	},
    130 #endif
    131 
    132 	{ .size = DEFROOTSIZE*(MEG/512), .mount = "/", .type = PT_root,
    133 	  .flags = PUIFLAG_EXTEND },
    134 	{
    135 #if DEFSWAPSIZE > 0
    136 	  .size = DEFSWAPSIZE*(MEG/512),
    137 #endif
    138 	  .type = PT_swap, .fs_type = FS_SWAP },
    139 #ifdef HAVE_TMPFS
    140 	{ .type = PT_root, .mount = "/tmp", .fs_type = FS_TMPFS,
    141 	  .flags = PUIFLG_JUST_MOUNTPOINT },
    142 #else
    143 	{ .type = PT_root, .mount = "/tmp", .fs_type = FS_MFS,
    144 	  .flags = PUIFLG_JUST_MOUNTPOINT },
    145 #endif
    146 	{ .def_size = DEFUSRSIZE*(MEG/512), .mount = "/usr", .type = PT_root },
    147 	{ .def_size = DEFVARSIZE*(MEG/512), .mount = "/var", .type = PT_root },
    148 };
    149 
    150 #if 0 // XXX
    151 static int
    152 save_ptn(int ptn, daddr_t start, daddr_t size, int fstype, const char *mountpt)
    153 {
    154 	static int maxptn;
    155 	partinfo *p;
    156 	int pp;
    157 	char *buf;
    158 
    159 	if (maxptn == 0)
    160 		maxptn = getmaxpartitions();
    161 
    162 	if (ptn < 0 || PI_FSTYPE(&pm->bsdlabel[ptn]) != FS_UNUSED) {
    163 		ptn = getrawpartition() + 1;
    164 #ifdef PART_FIRST_FREE
    165 		if (ptn < PART_FIRST_FREE)
    166 			ptn = PART_FIRST_FREE;
    167 #endif
    168 		for (;; ptn++) {
    169 			if (ptn >= maxptn)
    170 				return -1;
    171 // XXX			if (ptn == PART_USR)
    172 //				continue;
    173 			if (PI_FSTYPE(&pm->bsdlabel[ptn]) == FS_UNUSED)
    174 				break;
    175 		}
    176 	}
    177 
    178 	if (fstype == FS_UNUSED)
    179 		return ptn;
    180 
    181 	p = pm->bsdlabel + ptn;
    182 	PI_SET_OFFSET(p, start);
    183 	PI_SET_SIZE(p, size);
    184 	set_ptype(p, fstype, mountpt ? PIF_NEWFS : 0);
    185 
    186 	/* Hack because we does not have something like FS_LVMPV */
    187 	p->lvmpv = 0;
    188 	if (mountpt != NULL && strcmp(mountpt, "lvm") == 0)
    189 		p->lvmpv = 1;
    190 	else if (mountpt != NULL) {
    191 		for (pp = 0; pp < maxptn; pp++) {
    192 			if (strcmp(pm->bsdlabel[pp].pi_mount, mountpt) == 0)
    193 				pm->bsdlabel[pp].pi_flags &= ~PIF_MOUNT;
    194 		}
    195 		if (mountpt[0] != '/')
    196 			asprintf(&buf, "/%s", mountpt);
    197 		else
    198 			asprintf(&buf, "%s", mountpt);
    199 		strlcpy(p->pi_mount, buf, sizeof p->pi_mount);
    200 		p->pi_flags |= PIF_MOUNT;
    201 		/* Default to UFS2. */
    202 		if (PI_FSTYPE(p) == FS_BSDFFS) {
    203 #ifdef DEFAULT_UFS2
    204 #ifndef HAVE_UFS2_BOOT
    205 			if (strcmp(mountpt, "/") != 0)
    206 #endif
    207 				p->pi_flags |= PIF_FFSv2;
    208 #endif
    209 		}
    210 		free(buf);
    211 	}
    212 	return ptn;
    213 }
    214 #endif
    215 
    216 
    217 static const char size_separator[] =
    218     "----------------------------------- - --------------------";
    219 static char size_menu_title[STRSIZE];
    220 static char size_menu_exit[MENUSTRSIZE];
    221 
    222 static void
    223 set_pset_exit_str(struct partition_usage_set *pset)
    224 {
    225 	char *str, num[25];
    226 	const char *args[2];
    227 	bool overrun;
    228 	daddr_t free_space = pset->cur_free_space;
    229 
    230 	/* format exit string */
    231 	overrun = free_space < 0;
    232 	if (overrun)
    233 		free_space = -free_space;
    234 
    235 	snprintf(num, sizeof(num), "%" PRIu64, free_space / sizemult);
    236 	args[0] = num;
    237 	args[1] = multname;
    238 	str = str_arg_subst(
    239 	    msg_string(overrun ? MSG_fssizesbad : MSG_fssizesok),
    240 	    2, args);
    241 	strlcpy(size_menu_exit, str, sizeof(size_menu_exit));
    242 	free(str);
    243 }
    244 
    245 static void
    246 draw_size_menu_header(menudesc *m, void *arg)
    247 {
    248 	struct partition_usage_set *pset = arg;
    249 	size_t i;
    250 	char col1[70], desc[MENUSTRSIZE];
    251 	bool need_ext = false, need_existing = false;
    252 
    253 	msg_display(MSG_ptnsizes);
    254 
    255 	for (i = 0; i < pset->num; i++) {
    256 		if (pset->infos[i].flags & PUIFLG_IS_OUTER)
    257 			need_ext = true;
    258 		else if (pset->infos[i].cur_part_id != NO_PART)
    259 			need_existing = true;
    260 	}
    261 	if (need_ext && need_existing)
    262 		snprintf(desc, sizeof desc, "%s, %s",
    263 		    msg_string(MSG_ptnsizes_mark_existing),
    264 		    msg_string(MSG_ptnsizes_mark_external));
    265 	else if (need_existing)
    266 		strlcpy(desc, msg_string(MSG_ptnsizes_mark_existing),
    267 		    sizeof desc);
    268 	else if (need_ext)
    269 		strlcpy(desc, msg_string(MSG_ptnsizes_mark_external),
    270 		    sizeof desc);
    271 	if (need_ext || need_existing) {
    272 		msg_printf("\n");
    273 		msg_display_add_subst(msg_string(MSG_ptnsizes_markers),
    274 		    1, &desc);
    275 	}
    276 	msg_printf("\n\n");
    277 
    278 	/* update menu title */
    279 	snprintf(col1, sizeof col1, "%s (%s)", msg_string(MSG_ptnheaders_size),
    280 	    multname);
    281 	snprintf(size_menu_title, sizeof size_menu_title,
    282 	    "   %-37.37s %s\n   %s", col1,
    283 	    msg_string(MSG_ptnheaders_filesystem), size_separator);
    284 }
    285 
    286 static void
    287 draw_size_menu_line(menudesc *m, int opt, void *arg)
    288 {
    289 	struct partition_usage_set *pset = arg;
    290 	daddr_t size;
    291 	char psize[38], inc_free[16], flag, swap[40];
    292 	const char *mount;
    293 	bool free_mount = false;
    294 
    295 	if (opt < 0 || (size_t)opt >= pset->num)
    296 		return;
    297 
    298 	inc_free[0] = 0;
    299 	if ((pset->infos[opt].flags & PUIFLAG_EXTEND) &&
    300 	     pset->cur_free_space > 0) {
    301 		size = pset->infos[opt].size + pset->cur_free_space;
    302 		snprintf(inc_free, sizeof inc_free, " (%" PRIu64 ")",
    303 		    size / sizemult);
    304 	}
    305 	size = pset->infos[opt].size;
    306 	snprintf(psize, sizeof psize, "%" PRIu64 "%s",
    307 	    size / sizemult, inc_free);
    308 
    309 	if (pset->infos[opt].type == PT_swap) {
    310 		snprintf(swap, sizeof swap, "<%s>",
    311 		    msg_string(MSG_swap_display));
    312 		mount = swap;
    313 	} else if (pset->infos[opt].flags & PUIFLG_JUST_MOUNTPOINT) {
    314 		snprintf(swap, sizeof swap, "%s (%s)",
    315 		    pset->infos[opt].mount,
    316 		    getfslabelname(pset->infos[opt].fs_type,
    317 		    pset->infos[opt].fs_version));
    318 		mount = swap;
    319 	} else if (pset->infos[opt].mount[0]) {
    320 		mount = pset->infos[opt].mount;
    321 	} else {
    322 		mount = NULL;
    323 		if (pset->infos[opt].parts->pscheme->other_partition_identifier
    324 		    && pset->infos[opt].cur_part_id != NO_PART)
    325 			mount = pset->infos[opt].parts->pscheme->
    326 			    other_partition_identifier(pset->infos[opt].parts,
    327 			    pset->infos[opt].cur_part_id);
    328 		if (mount == NULL)
    329 			mount = getfslabelname(pset->infos[opt].fs_type,
    330 			    pset->infos[opt].fs_version);
    331 		mount = str_arg_subst(msg_string(MSG_size_ptn_not_mounted),
    332 		    1, &mount);
    333 		free_mount = true;
    334 	}
    335 	flag = ' ';
    336 	if (pset->infos[opt].flags & PUIFLAG_EXTEND)
    337 		flag = '+';
    338 	else if (pset->infos[opt].flags & PUIFLG_IS_OUTER)
    339 		flag = '@';
    340 	else if (pset->infos[opt].cur_part_id != NO_PART)
    341 		flag = '=';
    342 	wprintw(m->mw, "%-35.35s %c %s", psize, flag, mount);
    343 	if (free_mount)
    344 		free(__UNCONST(mount));
    345 
    346 	if (opt == 0)
    347 		set_pset_exit_str(pset);
    348 }
    349 
    350 static int
    351 add_other_ptn_size(menudesc *menu, void *arg)
    352 {
    353 	struct partition_usage_set *pset = arg;
    354 	struct part_usage_info *p;
    355 	struct menu_ent *m;
    356 	char new_mp[MOUNTLEN], *err;
    357 	const char *args;
    358 
    359 	for (;;) {
    360 		msg_prompt_win(partman_go?MSG_askfsmountadv:MSG_askfsmount,
    361 		    -1, 18, 0, 0, NULL,  new_mp, sizeof(new_mp));
    362 		if (new_mp[0] == 0)
    363 			return 0;
    364 		if (new_mp[0] != '/') {
    365 			/* we need absolute mount paths */
    366 			memmove(new_mp+1, new_mp, sizeof(new_mp)-1);
    367 			new_mp[0] = '/';
    368 		}
    369 
    370 		/* duplicates? */
    371 		bool duplicate = false;
    372 		for (size_t i = 0; i < pset->num; i++) {
    373 			if (strcmp(pset->infos[i].mount,
    374 			    new_mp) == 0) {
    375 			    	args = new_mp;
    376 				err = str_arg_subst(
    377 				    msg_string(MSG_mp_already_exists),
    378 				    1, &args);
    379 				err_msg_win(err);
    380 				free(err);
    381 				duplicate = true;
    382 				break;
    383 			}
    384 		}
    385 		if (!duplicate)
    386 			break;
    387 	}
    388 
    389 	m = realloc(pset->menu_opts, (pset->num+4)*sizeof(*pset->menu_opts));
    390 	if (m == NULL)
    391 		return 0;
    392 	p = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
    393 	if (p == NULL)
    394 		return 0;
    395 
    396 	pset->infos = p;
    397 	pset->menu_opts = m;
    398 	menu->opts = m;
    399 	menu->numopts = pset->num+4;
    400 	m += pset->num;
    401 	p += pset->num;
    402 	memset(m, 0, sizeof(*m));
    403 	memset(p, 0, sizeof(*p));
    404 	p->parts = pset->parts;
    405 	p->cur_part_id = NO_PART;
    406 	p->type = PT_root;
    407 	p->fs_type = FS_BSDFFS;
    408 	p->fs_version = 2;
    409 	strncpy(p->mount, new_mp, sizeof(p->mount));
    410 
    411 	menu->cursel = pset->num;
    412 	pset->num++;
    413 	fill_ptn_menu(pset);
    414 
    415 	return -1;
    416 }
    417 
    418 static size_t
    419 fill_ptn_menu(struct partition_usage_set *pset)
    420 {
    421 	struct part_usage_info *p;
    422 	struct disk_part_info info;
    423 	menu_ent *m;
    424 	size_t i;
    425 	daddr_t free_space;
    426 
    427 	memset(pset->menu_opts, 0, (pset->num+3)*sizeof(*pset->menu_opts));
    428 	for (m = pset->menu_opts, p = pset->infos, i = 0; i < pset->num;
    429 	    m++, p++, i++) {
    430 		m->opt_action = set_ptn_size;
    431 	}
    432 
    433 	m->opt_name = size_separator;
    434 	m->opt_flags = OPT_IGNORE|OPT_NOSHORT;
    435 	m++;
    436 
    437 	m->opt_name = MSG_add_another_ptn;
    438 	m->opt_action = add_other_ptn_size;
    439 	m++;
    440 
    441 	m->opt_name = MSG_askunits;
    442 	m->opt_menu = MENU_sizechoice;
    443 	m->opt_flags = OPT_SUB;
    444 	m++;
    445 
    446 	/* calculate free space */
    447 	free_space = pset->parts->free_space;
    448 	for (i = 0; i < pset->parts->num_part; i++) {
    449 		if (!pset->parts->pscheme->get_part_info(pset->parts, i,
    450 		    &info))
    451 			continue;
    452 		if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
    453 		    PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
    454 			continue;
    455 		free_space += info.size;
    456 	}
    457 	for (i = 0; i < pset->num; i++) {
    458 		if (pset->infos[i].flags &
    459 		    (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
    460 			continue;
    461 		free_space -= pset->infos[i].size;
    462 	}
    463 	pset->cur_free_space = free_space;
    464 	set_pset_exit_str(pset);
    465 
    466 	if (pset->menu >= 0)
    467 		set_menu_numopts(pset->menu, m - pset->menu_opts);
    468 
    469 	return m - pset->menu_opts;
    470 }
    471 
    472 static part_id
    473 find_part_at(struct disk_partitions *parts, daddr_t start)
    474 {
    475 	size_t i;
    476 	struct disk_part_info info;
    477 
    478 	for (i = 0; i < parts->num_part; i++) {
    479 		if (!parts->pscheme->get_part_info(parts, i, &info))
    480 			continue;
    481 		if (info.start == start)
    482 			return i;
    483 	}
    484 
    485 	return NO_PART;
    486 }
    487 
    488 int
    489 set_ptn_size(menudesc *m, void *arg)
    490 {
    491 	struct partition_usage_set *pset = arg;
    492 	struct part_usage_info *p = &pset->infos[m->cursel];
    493 	char answer[16], dflt[16];
    494 	const char *err_msg;
    495 	size_t i, root = ~0U;
    496 	daddr_t size, old_size, new_size_val, mult;
    497 	int rv;
    498 	bool non_zero, extend;
    499 
    500 	if (pset->cur_free_space == 0 && p->size == 0 &&
    501 	    !(p->flags & PUIFLG_JUST_MOUNTPOINT))
    502 		/* Don't allow 'free_parts' to go negative */
    503 		return 0;
    504 
    505 	if (p->cur_part_id != NO_PART) {
    506 		rv = 0;
    507 		process_menu(MENU_ptnsize_replace_existing_partition, &rv);
    508 		if (rv == 0)
    509 			return 0;
    510 		if (!pset->parts->pscheme->delete_partition(pset->parts,
    511 		    p->cur_part_id, &err_msg)) {
    512 			if (err_msg)
    513 				err_msg_win(err_msg);
    514 			return 0;
    515 		}
    516 		p->cur_part_id = NO_PART;
    517 		/*
    518 		 * All other part ids are invalid now too - update them!
    519 		 */
    520 		for (i = 0; i < pset->num; i++) {
    521 			if (pset->infos[i].cur_part_id == NO_PART)
    522 				continue;
    523 			pset->infos[i].cur_part_id =
    524 			    find_part_at(pset->parts, pset->infos[i].cur_start);
    525 		}
    526 	}
    527 
    528 	size = p->size;
    529 	old_size = size;
    530 	if (size == 0)
    531 		size = p->def_size;
    532 	size /= sizemult;
    533 	snprintf(dflt, sizeof dflt, "%" PRIu64 "%s",
    534 	    size, p->flags & PUIFLAG_EXTEND ? "+" : "");
    535 
    536 	for (;;) {
    537 		msg_fmt_prompt_win(MSG_askfssize, -1, 18, 0, 0,
    538 		    dflt, answer, sizeof answer, "%s%s", p->mount, multname);
    539 
    540 		/* cp will be checked below */
    541 		mult = sizemult;
    542 		new_size_val = parse_disk_pos(answer, &mult, pm->dlcylsize,
    543 		    &extend);
    544 
    545 		if (strcmp(answer, dflt) == 0)
    546 			non_zero = p->def_size > 0;
    547 		else
    548 			non_zero = new_size_val > 0;
    549 
    550 		/* Some special cases when /usr is first given a size */
    551 		if (old_size == 0 && non_zero &&
    552 		    strcmp(p->mount, "/usr") == 0) {
    553 			for (i = 0; i < pset->num; i++) {
    554 				if (strcmp(pset->infos[i].mount, "/") == 0) {
    555 					root = i;
    556 					break;
    557 				}
    558 			}
    559 			/* Remove space for /usr from / */
    560 			if (root < pset->num && pset->infos[i].cur_part_id ==
    561 			    NO_PART) {
    562 			    	pset->infos[root].size -= p->def_size;
    563 				pset->cur_free_space += p->def_size;
    564 			}
    565 			/* hack to add free space to default sized /usr */
    566 			if (strcmp(answer, dflt) == 0) {
    567 				size = p->def_size;
    568 				pset->infos[root].flags &= ~PUIFLAG_EXTEND;
    569 				p->flags |= PUIFLAG_EXTEND;
    570 				goto adjust_free;
    571 			}
    572 		}
    573 		if (new_size_val < 0)
    574 			continue;
    575 		size = new_size_val;
    576 		break;
    577 	}
    578 
    579 	daddr_t align = pset->parts->pscheme->get_part_alignment(pset->parts);
    580 	size = NUMSEC(size, mult, align);
    581 	if (p->flags & PUIFLAG_EXTEND)
    582 		p->flags &= ~PUIFLAG_EXTEND;
    583 	if (extend && (p->limit == 0 || p->limit > p->size)) {
    584 		p->flags |= PUIFLAG_EXTEND;
    585 		if (size == 0)
    586 			size = align;
    587 	}
    588 	if (p->limit != 0 && size > p->limit)
    589 		size = p->limit;
    590     adjust_free:
    591 	if ((p->flags & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT)) == 0)
    592 		pset->cur_free_space += p->size - size;
    593 	p->size = size;
    594 	set_pset_exit_str(pset);
    595 
    596 	return 0;
    597 }
    598 
    599 /*
    600  * User interface to edit a "wanted" partition layout "pset" as first
    601  * abstract phase (not concrete partitions).
    602  * Make sure to have everything (at least theoretically) fit the
    603  * available space.
    604  * During editing we keep the part_usage_info and the menu_opts
    605  * in pset in sync, that is: we always allocate just enough entries
    606  * in pset->infos as we have usage infos in the list (pset->num),
    607  * and two additional menu entries ("add a partition" and "select units").
    608  * The menu exit string changes depending on content, and implies
    609  * abort while the partition set is not valid (does not fit).
    610  * Return true when the user wants to continue (by editing the concrete
    611  * partitions), return false to abort.
    612  */
    613 bool
    614 get_ptn_sizes(struct partition_usage_set *pset)
    615 {
    616 	size_t num;
    617 
    618 	wclear(stdscr);
    619 	wrefresh(stdscr);
    620 
    621 	if (pset->menu_opts == NULL)
    622 		pset->menu_opts = calloc(pset->num+3, sizeof(*pset->menu_opts));
    623 
    624 	pset->menu = -1;
    625 	num = fill_ptn_menu(pset);
    626 
    627 	pset->menu = new_menu(size_menu_title, pset->menu_opts, num,
    628 			3, -1, 12, 70,
    629 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_NOCLEAR|MC_CONTINUOUS,
    630 			draw_size_menu_header, draw_size_menu_line, NULL,
    631 			NULL, size_menu_exit);
    632 
    633 	if (pset->menu < 0) {
    634 		free(pset->menu_opts);
    635 		pset->menu_opts = NULL;
    636 		return false;
    637 	}
    638 
    639 	pset->ok = true;
    640 	process_menu(pset->menu, pset);
    641 
    642 	free_menu(pset->menu);
    643 	free(pset->menu_opts);
    644 	pset->menu = -1;
    645 	pset->menu_opts = NULL;
    646 
    647 	if (!pset->ok)
    648 		return false;
    649 
    650 #if 0
    651 	if (cur_ptns.menu_no < 0) {
    652 		/* If there is a swap partition elsewhere, don't add one here.*/
    653 		if (no_swap || (swap_created && partman_go)) {
    654 			cur_ptns.ptn_sizes[PI_SWAP].size = 0;
    655 		} else {
    656 #if DEFSWAPSIZE == -1
    657 			/* Dynamic swap size. */
    658 			cur_ptns.ptn_sizes[PI_SWAP].dflt_size = get_ramsize();
    659 			cur_ptns.ptn_sizes[PI_SWAP].size =
    660 			    cur_ptns.ptn_sizes[PI_SWAP].dflt_size;
    661 #endif
    662 		}
    663 
    664 		/* If installing X increase default size of /usr */
    665 		if (set_X11_selected())
    666 			cur_ptns.ptn_sizes[PI_USR].dflt_size += XNEEDMB;
    667 
    668 		/* Start of planning to give free space to / */
    669 		cur_ptns.pool_part = &cur_ptns.ptn_sizes[PI_ROOT];
    670 		/* Make size of root include default size of /usr */
    671 		cur_ptns.ptn_sizes[PI_ROOT].size += cur_ptns.ptn_sizes[PI_USR].dflt_size;
    672 
    673 		sm = MEG / pm->sectorsize;
    674 
    675 		if (root_limit != 0) {
    676 			/* Bah - bios can not read all the disk, limit root */
    677 			cur_ptns.ptn_sizes[PI_ROOT].limit = root_limit -
    678 			    part_start;
    679 			/* Allocate a /usr partition if bios can't read
    680 			 * everything except swap.
    681 			 */
    682 			if (cur_ptns.ptn_sizes[PI_ROOT].limit
    683 			    < sectors - cur_ptns.ptn_sizes[PI_SWAP].size * sm) {
    684 				/* Root won't be able to access all the space */
    685 				/* Claw back space for /usr */
    686 				cur_ptns.ptn_sizes[PI_USR].size =
    687 						cur_ptns.ptn_sizes[PI_USR].dflt_size;
    688 				cur_ptns.ptn_sizes[PI_ROOT].size -=
    689 						cur_ptns.ptn_sizes[PI_USR].dflt_size;
    690 				cur_ptns.ptn_sizes[PI_ROOT].changed = 1;
    691 				/* Give free space to /usr */
    692 				cur_ptns.pool_part = &cur_ptns.ptn_sizes[PI_USR];
    693 			}
    694 		}
    695 
    696 		/* Change preset sizes from MB to sectors */
    697 		cur_ptns.free_space = sectors;
    698 		for (p = cur_ptns.ptn_sizes; p->mount[0]; p++) {
    699 			p->size = NUMSEC(p->size, sm, pm->dlcylsize);
    700 			p->dflt_size = NUMSEC(p->dflt_size, sm, pm->dlcylsize);
    701 			cur_ptns.free_space -= p->size;
    702 		}
    703 
    704 		/* Steal space from swap to make things fit.. */
    705 		if (cur_ptns.free_space < 0) {
    706 			i = roundup(-cur_ptns.free_space, pm->dlcylsize);
    707 			if (i > cur_ptns.ptn_sizes[PI_SWAP].size)
    708 				i = cur_ptns.ptn_sizes[PI_SWAP].size;
    709 			cur_ptns.ptn_sizes[PI_SWAP].size -= i;
    710 			cur_ptns.free_space += i;
    711 		}
    712 
    713 		/* Add space for 2 system dumps to / (traditional) */
    714 		i = get_ramsize() * sm;
    715 		i = roundup(i, pm->dlcylsize);
    716 		if (cur_ptns.free_space > i * 2)
    717 			i *= 2;
    718 		if (cur_ptns.free_space > i) {
    719 			cur_ptns.ptn_sizes[PI_ROOT].size += i;
    720 			cur_ptns.free_space -= i;
    721 		}
    722 
    723 		if (root_created && partman_go) {
    724 			cur_ptns.ptn_sizes[PI_ROOT].size = 0;
    725 			cur_ptns.pool_part = 0;
    726 		}
    727 
    728 		/* Ensure all of / is readable by the system boot code */
    729 		i = cur_ptns.ptn_sizes[PI_ROOT].limit;
    730 		if (i != 0 && (i -= cur_ptns.ptn_sizes[PI_ROOT].size) < 0) {
    731 			cur_ptns.ptn_sizes[PI_ROOT].size += i;
    732 			cur_ptns.free_space -= i;
    733 		}
    734 
    735 		/* Count free partition slots */
    736 		cur_ptns.free_parts = 0;
    737 #if 0 // XXX
    738 		for (i = 0; i < maxpart; i++) {
    739 			if (pm->bsdlabel[i].pi_size == 0)
    740 				cur_ptns.free_parts++;
    741 		}
    742 #endif
    743 		for (i = 0; i < MAXPARTITIONS; i++) {
    744 			p = &cur_ptns.ptn_sizes[i];
    745 			if (i != 0 && p->use == 0)
    746 				p->use = PART_EXTRA;
    747 			if (p->size != 0)
    748 				cur_ptns.free_parts--;
    749 		}
    750 
    751 		cur_ptns.menu_no = new_menu(0, cur_ptns.ptn_menus,
    752 			__arraycount(cur_ptns.ptn_menus),
    753 			3, -1, 12, 70,
    754 			MC_ALWAYS_SCROLL | MC_NOBOX | MC_NOCLEAR,
    755 			NULL, set_ptn_titles, NULL,
    756 			"help", cur_ptns.exit_msg);
    757 
    758 		if (cur_ptns.menu_no < 0)
    759 			return;
    760 	}
    761 
    762 	do {
    763 		set_ptn_menu(&cur_ptns);
    764 		pm->current_cylsize = pm->dlcylsize;
    765 		process_menu(cur_ptns.menu_no, &cur_ptns);
    766 	} while (cur_ptns.free_space < 0 || cur_ptns.free_parts < 0);
    767 
    768 	/* Give any cylinder fragment to last partition */
    769 	if (cur_ptns.pool_part != NULL || cur_ptns.free_space < pm->dlcylsize) {
    770 		for (p = cur_ptns.ptn_sizes + __arraycount(cur_ptns.ptn_sizes) - 1; ;p--) {
    771 			if (p->size == 0) {
    772 				if (p == cur_ptns.ptn_sizes)
    773 					break;
    774 				continue;
    775 			}
    776 			if (p->use == PART_TMP_RAMDISK)
    777 				continue;
    778 			p->size += cur_ptns.free_space % pm->dlcylsize;
    779 			cur_ptns.free_space -= cur_ptns.free_space % pm->dlcylsize;
    780 			break;
    781 		}
    782 	}
    783 
    784 	for (p = cur_ptns.ptn_sizes; p->mount[0]; p++, part_start += size) {
    785 		size = p->size;
    786 		if (p == cur_ptns.pool_part) {
    787 			size += rounddown(cur_ptns.free_space, pm->dlcylsize);
    788 			if (p->limit != 0 && size > p->limit)
    789 				size = p->limit;
    790 		}
    791 		i = p->use;
    792 		if (i == PART_TMP_RAMDISK) {
    793 			tmp_ramdisk_size = size;
    794 			size = 0;
    795 			continue;
    796 		}
    797 		if (size == 0)
    798 			continue;
    799 		if (i == PART_ROOT && size > 0)
    800 			root_created = 1;
    801 		if (i == PART_SWAP) {
    802 			if (size > 0)
    803 				swap_created = 1;
    804 			save_ptn(i, part_start, size, FS_SWAP, NULL);
    805 			continue;
    806 		}
    807 		if (!strcmp(p->mount, "raid")) {
    808 			save_ptn(i, part_start, size, FS_RAID, NULL);
    809 			continue;
    810 		} else if (!strcmp(p->mount, "cgd")) {
    811 			save_ptn(i, part_start, size, FS_CGD, NULL);
    812 			continue;
    813 		}
    814 		save_ptn(i, part_start, size, FS_BSDFFS, p->mount);
    815 	}
    816 #endif
    817 	return true;
    818 }
    819 
    820 static int
    821 set_keep_existing(menudesc *m, void *arg)
    822 {
    823 	((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
    824 	return 0;
    825 }
    826 
    827 static int
    828 set_edit_part_sizes(menudesc *m, void *arg)
    829 {
    830 	((arg_rep_int*)arg)->rv = LY_SETSIZES;
    831 	return 0;
    832 }
    833 
    834 static int
    835 set_use_default_sizes(menudesc *m, void *arg)
    836 {
    837 	((arg_rep_int*)arg)->rv = LY_USEDEFAULT;
    838 	return 0;
    839 }
    840 
    841 /*
    842  * Check if there is a reasonable pre-existing partition for
    843  * NetBSD.
    844  */
    845 static bool
    846 check_existing_netbsd(struct disk_partitions *parts)
    847 {
    848 	size_t nbsd_parts;
    849 	struct disk_part_info info;
    850 
    851 	nbsd_parts = 0;
    852 	for (part_id p = 0; p < parts->num_part; p++) {
    853 		if (!parts->pscheme->get_part_info(parts, p, &info))
    854 			continue;
    855 		if (info.flags & (PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
    856 			continue;
    857 		if (info.nat_type && info.nat_type->generic_ptype == PT_root)
    858 			nbsd_parts++;
    859 	}
    860 
    861 	return nbsd_parts > 0;
    862 }
    863 
    864 /*
    865  * Query a partition layout type (with available options depending on
    866  * pre-existing partitions).
    867  */
    868 static enum layout_type
    869 ask_layout(struct disk_partitions *parts, bool have_existing)
    870 {
    871 	arg_rep_int ai;
    872 	const char *args[2];
    873 	int menu;
    874 	size_t num_opts;
    875 	menu_ent options[3], *opt;
    876 
    877 	args[0] = msg_string(parts->pscheme->name);
    878 	args[1] = msg_string(parts->pscheme->short_name);
    879 	ai.args.argv = args;
    880 	ai.args.argc = 2;
    881 	ai.rv = LY_SETSIZES;
    882 
    883 	memset(options, 0, sizeof(options));
    884 	num_opts = 0;
    885 	opt = &options[0];
    886 
    887 	if (have_existing) {
    888 		opt->opt_name = MSG_Keep_existing_partitions;
    889 		opt->opt_flags = OPT_EXIT;
    890 		opt->opt_action = set_keep_existing;
    891 		opt++;
    892 		num_opts++;
    893 	}
    894 	opt->opt_name = MSG_Set_Sizes;
    895 	opt->opt_flags = OPT_EXIT;
    896 	opt->opt_action = set_edit_part_sizes;
    897 	opt++;
    898 	num_opts++;
    899 
    900 	opt->opt_name = MSG_Use_Default_Parts;
    901 	opt->opt_flags = OPT_EXIT;
    902 	opt->opt_action = set_use_default_sizes;
    903 	opt++;
    904 	num_opts++;
    905 
    906 	menu = new_menu(MSG_Select_your_choice, options, num_opts,
    907 	    -1, -10, 0, 0, MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
    908 	if (menu != -1) {
    909 		get_menudesc(menu)->expand_act = expand_all_option_texts;
    910 		process_menu(menu, &ai);
    911 		free_menu(menu);
    912 	}
    913 
    914 	return ai.rv;
    915 }
    916 
    917 static void
    918 merge_part_with_wanted(struct disk_partitions *parts, part_id pno,
    919     const struct disk_part_info *info, struct partition_usage_set *wanted,
    920     size_t wanted_num, bool is_outer)
    921 {
    922 	struct part_usage_info *infos;
    923 
    924 	/*
    925 	 * does this partition match something in the wanted set?
    926 	 */
    927 	for (size_t i = 0; i < wanted_num; i++) {
    928 		if (wanted->infos[i].type != info->nat_type->generic_ptype)
    929 			continue;
    930 		if (wanted->infos[i].type == PT_root &&
    931 		    info->last_mounted != NULL && info->last_mounted[0] != 0 &&
    932 		    strcmp(info->last_mounted, wanted->infos[i].mount) != 0)
    933 			continue;
    934 		if (wanted->infos[i].cur_part_id != NO_PART)
    935 			continue;
    936 		wanted->infos[i].cur_part_id = pno;
    937 		wanted->infos[i].parts = parts;
    938 		wanted->infos[i].size = info->size;
    939 		wanted->infos[i].cur_start = info->start;
    940 		wanted->infos[i].flags &= ~PUIFLAG_EXTEND;
    941 		if (wanted->infos[i].fs_type != FS_UNUSED &&
    942 		    wanted->infos[i].type != PT_swap)
    943 			wanted->infos[i].instflags |= PUIINST_MOUNT;
    944 		if (is_outer)
    945 			wanted->infos[i].flags |= PUIFLG_IS_OUTER;
    946 		else
    947 			wanted->infos[i].flags &= ~PUIFLG_IS_OUTER;
    948 		return;
    949 	}
    950 
    951 	/*
    952 	 * no match - if this is fromt the outer scheme, we are done.
    953 	 * otherwise it must be inserted into the wanted set.
    954 	 */
    955 	if (is_outer)
    956 		return;
    957 
    958 	/*
    959 	 * create a new entry for this
    960 	 */
    961 	infos = realloc(wanted->infos, sizeof(*infos)*(wanted->num+1));
    962 	if (infos == NULL)
    963 		return;
    964 	wanted->infos = infos;
    965 	infos += wanted->num;
    966 	wanted->num++;
    967 	memset(infos, 0, sizeof(*infos));
    968 	if (info->last_mounted != NULL && info->last_mounted[0] != 0)
    969 		strlcpy(infos->mount, info->last_mounted,
    970 		    sizeof(infos->mount));
    971 	infos->type = info->nat_type->generic_ptype;
    972 	infos->cur_part_id = pno;
    973 	infos->parts = parts;
    974 	infos->size = info->size;
    975 	infos->cur_start = info->start;
    976 	infos->fs_type = info->fs_type;
    977 	infos->fs_version = info->fs_sub_type;
    978 	if (is_outer)
    979 		infos->flags |= PUIFLG_IS_OUTER;
    980 }
    981 
    982 static bool
    983 have_x11_by_default(void)
    984 {
    985 	static const uint8_t def_sets[] = { MD_SETS_SELECTED };
    986 
    987 	for (size_t i = 0; i < __arraycount(def_sets); i++)
    988 		if (def_sets[i] >= SET_X11_FIRST &&
    989 		    def_sets[i] <= SET_X11_LAST)
    990 			return true;
    991 
    992 	return false;
    993 }
    994 
    995 static void
    996 fill_defaults(struct partition_usage_set *wanted, struct disk_partitions *parts,
    997     daddr_t ptstart, daddr_t ptsize)
    998 {
    999 	size_t i, root = ~0U, usr = ~0U, swap = ~0U, def_usr = ~0U;
   1000 	daddr_t free_space, dump_space, required;
   1001 #if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
   1002 	size_t boot = ~0U;
   1003 #endif
   1004 
   1005 	memset(wanted, 0, sizeof(*wanted));
   1006 	wanted->parts = parts;
   1007 	wanted->num = __arraycount(default_parts_init);
   1008 	wanted->infos = calloc(wanted->num, sizeof(*wanted->infos));
   1009 	if (wanted->infos == NULL) {
   1010 		err_msg_win(err_outofmem);
   1011 		return;
   1012 	}
   1013 
   1014 	memcpy(wanted->infos, default_parts_init, sizeof(default_parts_init));
   1015 
   1016 #ifdef MD_PART_DEFAULTS
   1017 	MD_PART_DEFAULTS(pm, wanted->infos, wanted->num);
   1018 #endif
   1019 
   1020 	for (i = 0; i < wanted->num; i++) {
   1021 		wanted->infos[i].parts = parts;
   1022 		wanted->infos[i].cur_part_id = NO_PART;
   1023 
   1024 #if DEFSWAPSIZE == -1
   1025 		if (wanted->infos[i].type == PT_swap)
   1026 			wanted->infos[i].size = get_ramsize() * (MEG / 512);
   1027 #endif
   1028 		if (wanted->infos[i].type == PT_swap && swap > wanted->num)
   1029 			swap = i;
   1030 #if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
   1031 		if (wanted->infos[i].instflags & PUIINST_BOOT)
   1032 			boot = i;
   1033 #endif
   1034 		if (wanted->infos[i].type == PT_root) {
   1035 			if (strcmp(wanted->infos[i].mount, "/") == 0) {
   1036 				root = i;
   1037 			} else if (
   1038 			    strcmp(wanted->infos[i].mount, "/usr") == 0) {
   1039 				if (wanted->infos[i].size > 0)
   1040 					usr = i;
   1041 				else
   1042 					def_usr = i;
   1043 			}
   1044 			if (wanted->infos[i].fs_type == FS_UNUSED)
   1045 				wanted->infos[i].fs_type = FS_BSDFFS;
   1046 			if (wanted->infos[i].fs_type == FS_BSDFFS) {
   1047 #ifdef DEFAULT_UFS2
   1048 #ifndef HAVE_UFS2_BOOT
   1049 				if (boot < wanted->num || i != root)
   1050 #endif
   1051 					wanted->infos[i].fs_version = 2;
   1052 #endif
   1053 			}
   1054 		}
   1055 		if ((wanted->infos[i].flags & PUIFLG_JUST_MOUNTPOINT) &&
   1056 		    wanted->infos[i].size == 0)
   1057 			/* default tmpfs to 1/4 RAM */
   1058 			wanted->infos[i].def_size =
   1059 			    get_ramsize() * (MEG/512/4);
   1060 	}
   1061 
   1062 	/*
   1063 	 * Now we have the defaults as if we were installing to an
   1064 	 * empty disk. Merge the partitions in target range that are already
   1065 	 * there (match with wanted) or are there additionaly.
   1066 	 * The only thing outside of target range that we care for
   1067 	 * is a potential swap partition - we assume one is enough.
   1068 	 */
   1069 	size_t num = wanted->num;
   1070 	if (parts->parent) {
   1071 		for (part_id pno = 0; pno < parts->parent->num_part; pno++) {
   1072 			struct disk_part_info info;
   1073 
   1074 			if (!parts->parent->pscheme->get_part_info(
   1075 			    parts->parent, pno, &info))
   1076 				continue;
   1077 			if (info.nat_type->generic_ptype != PT_swap)
   1078 				continue;
   1079 			merge_part_with_wanted(parts->parent, pno, &info,
   1080 			    wanted, num, true);
   1081 			break;
   1082 		}
   1083 	}
   1084 	for (part_id pno = 0; pno < parts->num_part; pno++) {
   1085 		struct disk_part_info info;
   1086 
   1087 		if (!parts->pscheme->get_part_info(parts, pno, &info))
   1088 			continue;
   1089 
   1090 		if (info.flags & PTI_PSCHEME_INTERNAL)
   1091 			continue;
   1092 
   1093 		if (info.nat_type->generic_ptype != PT_swap &&
   1094 		    (info.start < ptstart ||
   1095 		    (info.start + info.size) > (ptstart+ptsize)))
   1096 			continue;
   1097 
   1098 		merge_part_with_wanted(parts, pno, &info,
   1099 		    wanted, num, false);
   1100 	}
   1101 
   1102 	daddr_t align = parts->pscheme->get_part_alignment(parts);
   1103 
   1104 	if (root < wanted->num && wanted->infos[root].cur_part_id == NO_PART) {
   1105 		daddr_t max_root_size = parts->disk_start + parts->disk_size;
   1106 		if (root_limit > 0) {
   1107 			/* Bah - bios can not read all the disk, limit root */
   1108 			max_root_size = root_limit - parts->disk_start;
   1109 		}
   1110 		wanted->infos[root].limit = max_root_size;
   1111 	}
   1112 
   1113 	if (have_x11_by_default()) {
   1114 		daddr_t xsize = XNEEDMB * (MEG / 512);
   1115 		if (usr < wanted->num) {
   1116 			if (wanted->infos[usr].cur_part_id == NO_PART) {
   1117 				wanted->infos[usr].size += xsize;
   1118 				wanted->infos[usr].def_size += xsize;
   1119 			}
   1120 		} else if (root < wanted->num &&
   1121 		    wanted->infos[root].cur_part_id == NO_PART &&
   1122 		    (wanted->infos[root].limit == 0 ||
   1123 		    (wanted->infos[root].size + xsize) <=
   1124 		    wanted->infos[root].limit)) {
   1125 			wanted->infos[root].size += xsize;
   1126 		}
   1127 	}
   1128 	if (wanted->infos[root].limit > 0 &&
   1129 	    wanted->infos[root].size > wanted->infos[root].limit) {
   1130 		if (usr < wanted->num) {
   1131 			/* move space from root to usr */
   1132 			daddr_t spill = wanted->infos[root].size -
   1133 			    wanted->infos[root].limit;
   1134 			spill = roundup(spill, align);
   1135 			wanted->infos[root].size =
   1136 			    wanted->infos[root].limit;
   1137 			wanted->infos[usr].size = spill;
   1138 		} else {
   1139 			wanted->infos[root].size =
   1140 			    wanted->infos[root].limit;
   1141 		}
   1142 	}
   1143 
   1144 	/*
   1145 	 * Preliminary calc additional space to allocate and how much
   1146 	 * we likely will have left over. Use that to do further
   1147 	 * adjustments, so we don't present the user inherently
   1148 	 * impossible defaults.
   1149 	 */
   1150 	free_space = parts->free_space;
   1151 	required = 0;
   1152 	if (root < wanted->num)
   1153 		required += wanted->infos[root].size;
   1154 	if (usr < wanted->num)
   1155 		required += wanted->infos[usr].size;
   1156 	else if (def_usr < wanted->num)
   1157 			required += wanted->infos[def_usr].def_size;
   1158 	free_space -= required;
   1159 	for (i = 0; i < wanted->num; i++) {
   1160 		if (i == root || i == usr)
   1161 			continue;	/* already accounted above */
   1162 		if (wanted->infos[i].cur_part_id != NO_PART)
   1163 			continue;
   1164 		if (wanted->infos[i].size == 0)
   1165 			continue;
   1166 		if (wanted->infos[i].flags
   1167 		    & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
   1168 			continue;
   1169 		free_space -= wanted->infos[i].size;
   1170 	}
   1171 	if (free_space < 0 && swap < wanted->num) {
   1172 		/* steel from swap partition */
   1173 		daddr_t d = wanted->infos[swap].size;
   1174 		daddr_t inc = roundup(-free_space, align);
   1175 		if (inc > d)
   1176 			inc = d;
   1177 		free_space += inc;
   1178 		wanted->infos[swap].size -= inc;
   1179 	}
   1180 	if (root < wanted->num) {
   1181 		/* Add space for 2 system dumps to / (traditional) */
   1182 		dump_space = get_ramsize() * (MEG/512);
   1183 		dump_space = roundup(dump_space, align);
   1184 		if (free_space > dump_space*2)
   1185 			dump_space *= 2;
   1186 		if (free_space > dump_space)
   1187 			wanted->infos[root].size += dump_space;
   1188 	}
   1189 }
   1190 
   1191 /*
   1192  * We sort pset->infos to sync with pset->parts and
   1193  * the cur_part_id, to allow using the same index into both
   1194  * "array" in later phases. This may include inserting
   1195  * dummy  entries (when we do not actually want the
   1196  * partition, but it is forced upon us, like RAW_PART in
   1197  * disklabel).
   1198  */
   1199 static void
   1200 sort_and_sync_parts(struct partition_usage_set *pset)
   1201 {
   1202 	struct part_usage_info *infos;
   1203 	size_t i, j, no;
   1204 	part_id pno;
   1205 
   1206 	pset->cur_free_space = pset->parts->free_space;
   1207 
   1208 	/* count non-empty entries that are not in pset->parts */
   1209 	no = pset->parts->num_part;
   1210 	for (i = 0; i < pset->num; i++) {
   1211 		if (pset->infos[i].size == 0)
   1212 			continue;
   1213 		if (pset->infos[i].cur_part_id != NO_PART)
   1214 			continue;
   1215 		no++;
   1216 	}
   1217 
   1218 	/* allocate new infos */
   1219 	infos = calloc(no, sizeof *infos);
   1220 	if (infos == NULL)
   1221 		return;
   1222 
   1223 	/* pre-initialize the first entires as dummy entries */
   1224 	for (i = 0; i < pset->parts->num_part; i++) {
   1225 		infos[i].cur_part_id = NO_PART;
   1226 		infos[i].cur_flags = PTI_PSCHEME_INTERNAL;
   1227 	}
   1228 	/*
   1229 	 * Now copy over eveything from our old entries that points to
   1230 	 * a real partition.
   1231 	 */
   1232 	for (i = 0; i < pset->num; i++) {
   1233 		pno = pset->infos[i].cur_part_id;
   1234 		if (pno == NO_PART)
   1235 			continue;
   1236 		if (pset->parts != pset->infos[i].parts)
   1237 			continue;
   1238 		if (pset->infos[i].flags & PUIFLG_JUST_MOUNTPOINT)
   1239 			continue;
   1240 		if ((pset->infos[i].flags & (PUIFLG_IS_OUTER|PUIFLAG_ADD_INNER))
   1241 		    == PUIFLG_IS_OUTER)
   1242 			continue;
   1243 		if (pno >= pset->parts->num_part)
   1244 			continue;
   1245 		memcpy(infos+pno, pset->infos+i, sizeof(*infos));
   1246 	}
   1247 	/* Fill in the infos for real partitions where we had no data */
   1248 	for (pno = 0; pno < pset->parts->num_part; pno++) {
   1249 		struct disk_part_info info;
   1250 
   1251 		if (infos[pno].cur_part_id != NO_PART)
   1252 			continue;
   1253 
   1254 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
   1255 		    &info))
   1256 			continue;
   1257 
   1258 		infos[pno].parts = pset->parts;
   1259 		infos[pno].cur_part_id = pno;
   1260 		infos[pno].cur_flags = info.flags;
   1261 		infos[pno].size = info.size;
   1262 		infos[pno].type = info.nat_type->generic_ptype;
   1263 		infos[pno].cur_start = info.start;
   1264 		infos[pno].fs_type = info.fs_type;
   1265 		infos[pno].fs_version = info.fs_sub_type;
   1266 	}
   1267 	/* Add the non-partition entires after that */
   1268 	j = pset->num;
   1269 	for (i = 0; i < pset->num; i++) {
   1270 		if (j >= no)
   1271 			break;
   1272 		if (pset->infos[i].size == 0)
   1273 			continue;
   1274 		if (pset->infos[i].cur_part_id != NO_PART)
   1275 			continue;
   1276 		memcpy(infos+j, pset->infos+i, sizeof(*infos));
   1277 		j++;
   1278 	}
   1279 
   1280 	/* done, replace infos */
   1281 	free(pset->infos);
   1282 	pset->num = no;
   1283 	pset->infos = infos;
   1284 }
   1285 
   1286 static void
   1287 apply_settings_to_partitions(struct pm_devs *p, struct disk_partitions *parts,
   1288     struct partition_usage_set *wanted, daddr_t start, daddr_t size)
   1289 {
   1290 	size_t i, exp_ndx = ~0U;
   1291 	daddr_t planned_space = 0, nsp, from, align;
   1292 	struct disk_part_info *infos;
   1293 	struct disk_part_free_space space;
   1294 	struct disk_partitions *ps = NULL;
   1295 	part_id pno, new_part_id;
   1296 
   1297 	infos = calloc(wanted->num, sizeof(*infos));
   1298 	if (infos == NULL) {
   1299 		err_msg_win(err_outofmem);
   1300 		return;
   1301 	}
   1302 
   1303 	align = wanted->parts->pscheme->get_part_alignment(wanted->parts);
   1304 
   1305 	/*
   1306 	 * Pass one: calculate space available for expanding
   1307 	 * the marked partition.
   1308 	 */
   1309 	for (i = 0; i < wanted->num; i++) {
   1310 		if ((wanted->infos[i].flags & PUIFLAG_EXTEND) &&
   1311 		    exp_ndx == ~0U)
   1312 			exp_ndx = i;
   1313 		if (wanted->infos[i].flags &
   1314 		    (PUIFLG_JUST_MOUNTPOINT|PUIFLG_IS_OUTER))
   1315 			continue;
   1316 		nsp = wanted->infos[i].size;
   1317 		if (wanted->infos[i].cur_part_id != NO_PART) {
   1318 			ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
   1319 			    parts->parent : parts;
   1320 
   1321 			if (ps->pscheme->get_part_info(ps,
   1322 			     wanted->infos[i].cur_part_id, &infos[i]))
   1323 				nsp -= infos[i].size;
   1324 		}
   1325 		if (nsp > 0)
   1326 			planned_space += roundup(nsp, align);
   1327 	}
   1328 
   1329 	/*
   1330 	 * Expand the pool partition (or shrink, if we overran),
   1331 	 */
   1332 	if (exp_ndx < wanted->num)
   1333 		wanted->infos[exp_ndx].size +=
   1334 		    parts->free_space - planned_space;
   1335 
   1336 	/*
   1337 	 * Now it gets tricky: we want the wanted partitions in order
   1338 	 * as defined, but any already existing partitions should not
   1339 	 * be moved. We allow them to change size though.
   1340 	 * To keep it simple, we just assign in order and skip blocked
   1341 	 * spaces. This may shuffle the order of the resulting partitions
   1342 	 * compared to the wanted list.
   1343 	 */
   1344 
   1345 	/* Adjust sizes of existing partitions */
   1346 	for (i = 0; i < wanted->num; i++) {
   1347 		ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
   1348 		    parts->parent : parts;
   1349 		const struct part_usage_info *want = &wanted->infos[i];
   1350 
   1351 		if (want->cur_part_id == NO_PART)
   1352 			continue;
   1353 		if (i == exp_ndx)	/* the exp. part. can not exist yet */
   1354 			continue;
   1355 		daddr_t free_size = ps->pscheme->max_free_space_at(ps,
   1356 		    infos[i].start);
   1357 		if (free_size < wanted->infos[i].size)
   1358 			continue;
   1359 		infos[i].size = wanted->infos[i].size;
   1360 		ps->pscheme->set_part_info(ps, want->cur_part_id,
   1361 		    &infos[i], NULL);
   1362 	}
   1363 
   1364 	from = -1;
   1365 	/*
   1366 	 * First add all outer partitions - we need to align those exactly
   1367 	 * with the inner counterpart later.
   1368 	 */
   1369 	if (parts->parent) {
   1370 		ps = parts->parent;
   1371 		daddr_t outer_align = ps->pscheme->get_part_alignment(ps);
   1372 
   1373 		for (i = 0; i < wanted->num; i++) {
   1374 			struct part_usage_info *want = &wanted->infos[i];
   1375 
   1376 			if (want->cur_part_id != NO_PART)
   1377 				continue;
   1378 			if (!(want->flags & PUIFLAG_ADD_OUTER))
   1379 				continue;
   1380 			if (want->size <= 0)
   1381 				continue;
   1382 
   1383 			size_t cnt = ps->pscheme->get_free_spaces(ps,
   1384 			    &space, 1, want->size-2*outer_align,
   1385 			    outer_align, from, -1);
   1386 
   1387 			if (cnt == 0)	/* no free space for this partition */
   1388 				continue;
   1389 
   1390 			infos[i].start = space.start;
   1391 			infos[i].size = min(want->size, space.size);
   1392 			infos[i].nat_type =
   1393 			    ps->pscheme->get_fs_part_type(
   1394 			        want->fs_type, want->fs_version);
   1395 			infos[i].last_mounted = want->mount;
   1396 			infos[i].fs_type = want->fs_type;
   1397 			infos[i].fs_sub_type = want->fs_version;
   1398 			new_part_id = ps->pscheme->add_partition(ps,
   1399 			    &infos[i], NULL);
   1400 			if (new_part_id == NO_PART)
   1401 				continue;	/* failed to add, skip */
   1402 
   1403 			ps->pscheme->get_part_info(ps,
   1404 			    new_part_id, &infos[i]);
   1405 			want->cur_part_id = new_part_id;
   1406 
   1407 			want->flags |= PUIFLAG_ADD_INNER|PUIFLG_IS_OUTER;
   1408 			from = rounddown(infos[i].start +
   1409 			    infos[i].size+outer_align, outer_align);
   1410 		}
   1411 	}
   1412 
   1413 	/*
   1414 	 * Now add new inner partitions
   1415 	 */
   1416 	for (i = 0; i < wanted->num && from < wanted->parts->disk_size; i++) {
   1417 		struct part_usage_info *want = &wanted->infos[i];
   1418 
   1419 		if (want->cur_part_id != NO_PART)
   1420 			continue;
   1421 		if (want->flags & (PUIFLG_JUST_MOUNTPOINT|PUIFLG_IS_OUTER))
   1422 			continue;
   1423 		if (want->size <= 0)
   1424 			continue;
   1425 
   1426 		size_t cnt = wanted->parts->pscheme->get_free_spaces(
   1427 		    wanted->parts, &space, 1, want->size-2*align, align, from,
   1428 		    -1);
   1429 
   1430 		if (cnt == 0)
   1431 			continue;	/* no free space for this partition */
   1432 
   1433 		infos[i].start = space.start;
   1434 		infos[i].size = min(want->size, space.size);
   1435 		infos[i].nat_type =
   1436 		    wanted->parts->pscheme->get_fs_part_type(want->fs_type,
   1437 		    want->fs_version);
   1438 		infos[i].last_mounted = want->mount;
   1439 		infos[i].fs_type = want->fs_type;
   1440 		infos[i].fs_sub_type = want->fs_version;
   1441 		if (want->fs_type != FS_UNUSED && want->type != PT_swap) {
   1442 			want->instflags |= PUIINST_NEWFS;
   1443 			if (want->mount[0] != 0)
   1444 				want->instflags |= PUIINST_MOUNT;
   1445 		}
   1446 		new_part_id = wanted->parts->pscheme->add_partition(
   1447 		    wanted->parts, &infos[i], NULL);
   1448 		if (new_part_id == NO_PART)
   1449 			continue;	/* failed to add, skip */
   1450 
   1451 		wanted->parts->pscheme->get_part_info(
   1452 		    wanted->parts, new_part_id, &infos[i]);
   1453 		from = rounddown(infos[i].start+infos[i].size+align, align);
   1454 	}
   1455 
   1456 
   1457 	/*
   1458 	* If there are any outer partitions that we need as inner ones
   1459 	 * too, add them to the inner partitioning scheme.
   1460 	 */
   1461 	for (i = 0; i < wanted->num; i++) {
   1462 		struct part_usage_info *want = &wanted->infos[i];
   1463 
   1464 		if (want->cur_part_id != NO_PART)
   1465 			continue;
   1466 		if (want->flags & PUIFLG_JUST_MOUNTPOINT)
   1467 			continue;
   1468 		if (want->size <= 0)
   1469 			continue;
   1470 
   1471 		if ((want->flags & (PUIFLAG_ADD_INNER|PUIFLG_IS_OUTER)) !=
   1472 		    (PUIFLAG_ADD_INNER|PUIFLG_IS_OUTER))
   1473 			continue;
   1474 
   1475 		infos[i].start = want->cur_start;
   1476 		infos[i].size = want->size;
   1477 		infos[i].nat_type = wanted->parts->pscheme->get_fs_part_type(
   1478 		    want->fs_type, want->fs_version);
   1479 		infos[i].last_mounted = want->mount;
   1480 		infos[i].fs_type = want->fs_type;
   1481 		infos[i].fs_sub_type = want->fs_version;
   1482 
   1483 		if (wanted->parts->pscheme->add_outer_partition
   1484 		    != NULL)
   1485 			new_part_id = wanted->parts->pscheme->
   1486 			    add_outer_partition(
   1487 			    wanted->parts, &infos[i], NULL);
   1488 		else
   1489 			new_part_id = wanted->parts->pscheme->
   1490 			    add_partition(
   1491 			    wanted->parts, &infos[i], NULL);
   1492 
   1493 		if (new_part_id == NO_PART)
   1494 			continue;	/* failed to add, skip */
   1495 
   1496 		wanted->parts->pscheme->get_part_info(
   1497 		    wanted->parts, new_part_id, &infos[i]);
   1498 	}
   1499 
   1500 	/*
   1501 	 * Note: all part_ids are invalid now, as we have added things!
   1502 	 */
   1503 	for (i = 0; i < wanted->num; i++)
   1504 		wanted->infos[i].cur_part_id = NO_PART;
   1505 	for (pno = 0; pno < parts->num_part; pno++) {
   1506 		struct disk_part_info t;
   1507 
   1508 		if (!parts->pscheme->get_part_info(parts, pno, &t))
   1509 			continue;
   1510 
   1511 		for (i = 0; i < wanted->num; i++) {
   1512 			if (wanted->infos[i].cur_part_id != NO_PART)
   1513 				continue;
   1514 			if (t.start == infos[i].start) {
   1515 				wanted->infos[i].cur_part_id = pno;
   1516 				wanted->infos[i].cur_start = infos[i].start;
   1517 				wanted->infos[i].cur_flags = infos[i].flags;
   1518 				break;
   1519 			}
   1520 		}
   1521 	}
   1522 	free(infos);
   1523 
   1524 	/* sort, and sync part ids and wanted->infos[] indices */
   1525 	sort_and_sync_parts(wanted);
   1526 }
   1527 
   1528 static void
   1529 replace_by_default(struct pm_devs *p, struct disk_partitions *parts,
   1530     daddr_t start, daddr_t size, struct partition_usage_set *wanted)
   1531 {
   1532 
   1533 	if (start == 0 && size == parts->disk_size)
   1534 		parts->pscheme->delete_all_partitions(parts);
   1535 	else if (parts->pscheme->delete_partitions_in_range != NULL)
   1536 		parts->pscheme->delete_partitions_in_range(parts, start, size);
   1537 	else
   1538 		assert(parts->num_part == 0);
   1539 
   1540 	fill_defaults(wanted, parts, start, size);
   1541 	apply_settings_to_partitions(p, parts, wanted, start, size);
   1542 }
   1543 
   1544 static bool
   1545 edit_with_defaults(struct pm_devs *p, struct disk_partitions *parts,
   1546     daddr_t start, daddr_t size, struct partition_usage_set *wanted)
   1547 {
   1548 	bool ok;
   1549 
   1550 	fill_defaults(wanted, parts, start, size);
   1551 	ok = get_ptn_sizes(wanted);
   1552 	if (ok)
   1553 		apply_settings_to_partitions(p, parts, wanted, start, size);
   1554 	return ok;
   1555 }
   1556 
   1557 /*
   1558  * md back-end code for menu-driven BSD disklabel editor.
   1559  * returns 0 on failure, 1 on success.
   1560  * fills the install target with a list for newfs/fstab.
   1561  */
   1562 bool
   1563 make_bsd_partitions(struct install_partition_desc *install)
   1564 {
   1565 	struct disk_partitions *parts = pm->parts;
   1566 	const struct disk_partitioning_scheme *pscheme;
   1567 	struct partition_usage_set wanted;
   1568 	enum layout_type layoutkind = LY_SETSIZES;
   1569 	bool have_existing;
   1570 
   1571 	if (pm && pm->no_part && parts == NULL)
   1572 		return true;
   1573 
   1574 	if (parts == NULL) {
   1575 		pscheme = select_part_scheme(pm, NULL, true, NULL);
   1576 		if (pscheme == NULL)
   1577 			return false;
   1578 		parts = pscheme->create_new_for_disk(pm->diskdev,
   1579 		    0, pm->dlsize, pm->dlsize, true);
   1580 		if (parts == NULL)
   1581 			return false;
   1582 		pm->parts = parts;
   1583 	} else {
   1584 		pscheme = parts->pscheme;
   1585 	}
   1586 
   1587 	if (pscheme->secondary_partitions) {
   1588 		struct disk_partitions *p;
   1589 
   1590 		p = pscheme->secondary_partitions(parts, pm->ptstart, false);
   1591 		if (p) {
   1592 			parts = p;
   1593 			pscheme = parts->pscheme;
   1594 		}
   1595 	}
   1596 
   1597 	have_existing = check_existing_netbsd(parts);
   1598 
   1599 	/*
   1600 	 * Initialize global variables that track space used on this disk.
   1601 	 */
   1602 	if (pm->ptsize == 0)
   1603 		pm->ptsize = pm->dlsize - pm->ptstart;
   1604 	if (pm->dlsize == 0)
   1605 		pm->dlsize = pm->ptstart + pm->ptsize;
   1606 
   1607 	if (logfp) fprintf(logfp, "dlsize=%" PRId64 " ptsize=%" PRId64
   1608 	    " ptstart=%" PRId64 "\n",
   1609 	    pm->dlsize, pm->ptsize, pm->ptstart);
   1610 
   1611 	if (pm->current_cylsize == 0)
   1612 		pm->current_cylsize = pm->dlcylsize;
   1613 
   1614 	/* Ask for layout type -- standard or special */
   1615 	if (partman_go == 0) {
   1616 		char bsd_size[6], min_size[6], x_size[6];
   1617 
   1618 		humanize_number(bsd_size, sizeof(bsd_size),
   1619 		    (uint64_t)pm->ptsize*pm->sectorsize,
   1620 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1621 		humanize_number(min_size, sizeof(min_size),
   1622 		    (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE)*MEG,
   1623 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1624 		humanize_number(x_size, sizeof(x_size),
   1625 		    (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE
   1626 		    + XNEEDMB)*MEG,
   1627 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1628 
   1629 		msg_display_subst(
   1630 		    have_existing ? MSG_layout_prologue_existing
   1631 		    : MSG_layout_prologue_none, 6, pm->diskdev,
   1632 		    msg_string(parts->pscheme->name),
   1633 		    msg_string(parts->pscheme->short_name),
   1634 		    bsd_size, min_size, x_size);
   1635 		msg_display_add_subst(MSG_layout_main, 6,
   1636 		    pm->diskdev,
   1637 		    msg_string(parts->pscheme->name),
   1638 		    msg_string(parts->pscheme->short_name),
   1639 		    bsd_size, min_size, x_size);
   1640 		msg_display_add("\n\n");
   1641 		layoutkind = ask_layout(parts, have_existing);
   1642 	}
   1643 
   1644 	if (layoutkind == LY_USEDEFAULT) {
   1645 		replace_by_default(pm, parts, pm->ptstart, pm->ptsize,
   1646 		    &wanted);
   1647 	} else if (layoutkind == LY_SETSIZES) {
   1648 		if (!edit_with_defaults(pm, parts, pm->ptstart, pm->ptsize,
   1649 		    &wanted)) {
   1650 			free_usage_set(&wanted);
   1651 			return false;
   1652 		}
   1653 	} else {
   1654 		usage_set_from_parts(&wanted, parts);
   1655 	}
   1656 
   1657 	/*
   1658 	 * OK, we have a partition table. Give the user the chance to
   1659 	 * edit it and verify it's OK, or abort altogether.
   1660 	 */
   1661  	for (;;) {
   1662 		int rv = edit_and_check_label(pm, &wanted);
   1663 		if (rv == 0) {
   1664 			msg_display(MSG_abort_part);
   1665 			free_usage_set(&wanted);
   1666 			return false;
   1667 		}
   1668 		/* update install infos */
   1669 		install->num = wanted.num;
   1670 		install->infos = wanted.infos;
   1671 		/* and check them */
   1672 		if (check_partitions(install))
   1673 			break;
   1674 	}
   1675 
   1676 	/* we moved infos from wanted to install target */
   1677 	wanted.infos = NULL;
   1678 	free_usage_set(&wanted);
   1679 
   1680 	/* Everything looks OK. */
   1681 	return true;
   1682 }
   1683 
   1684 #ifndef MD_NEED_BOOTBLOCK
   1685 #define MD_NEED_BOOTBLOCK(A)	true
   1686 #endif
   1687 
   1688 /*
   1689  * check that there is at least a / somewhere.
   1690  */
   1691 bool
   1692 check_partitions(struct install_partition_desc *install)
   1693 {
   1694 #ifdef HAVE_BOOTXX_xFS
   1695 	int rv = 1;
   1696 	char *bootxx;
   1697 #endif
   1698 #ifndef HAVE_UFS2_BOOT
   1699 	size_t i;
   1700 #endif
   1701 
   1702 #ifdef HAVE_BOOTXX_xFS
   1703 	if (MD_NEED_BOOTBLOCK(install)) {
   1704 		/* check if we have boot code for the root partition type */
   1705 		bootxx = bootxx_name(install);
   1706 		if (bootxx != NULL) {
   1707 			rv = access(bootxx, R_OK);
   1708 			free(bootxx);
   1709 		} else
   1710 			rv = -1;
   1711 		if (rv != 0) {
   1712 			hit_enter_to_continue(NULL, MSG_No_Bootcode);
   1713 			return false;
   1714 		}
   1715 	}
   1716 #endif
   1717 #ifndef HAVE_UFS2_BOOT
   1718 	if (MD_NEED_BOOTBLOCK(install)) {
   1719 		for (i = 0; i < install->num; i++) {
   1720 			if (install->infos[i].type != PT_root)
   1721 				continue;
   1722 			if (strcmp(install->infos[i].mount, "/") != 0)
   1723 				continue;
   1724 			if (install->infos[i].fs_type != FS_BSDFFS)
   1725 				continue;
   1726 			if (install->infos[i].fs_version != 2)
   1727 				continue;
   1728 			hit_enter_to_continue(NULL, MSG_cannot_ufs2_root);
   1729 			return false;
   1730 		}
   1731 	}
   1732 #endif
   1733 
   1734 	return md_check_partitions(install);
   1735 }
   1736