Home | History | Annotate | Line # | Download | only in sysinst
bsddisklabel.c revision 1.67
      1 /*	$NetBSD: bsddisklabel.c,v 1.67 2022/12/09 16:54:31 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) || (PART_BOOT_TYPE == FS_EX2FS)
     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) || (PART_BOOT1_TYPE == FS_EX2FS)
    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) || (PART_BOOT2_TYPE == FS_EX2FS)
    123 	  .flags = PUIFLAG_ADD_OUTER,
    124 #endif
    125 #endif
    126 #ifdef PART_BOOT2_SUBT
    127 	  .fs_version = PART_BOOT2_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 	  .fs_type = FS_BSDFFS, .fs_version = 3 },
    148 	{ .def_size = DEFVARSIZE*(MEG/512), .mount = "/var", .type = PT_root,
    149 	  .fs_type = FS_BSDFFS, .fs_version = 3 },
    150 };
    151 
    152 static const char size_separator[] =
    153     "----------------------------------- - --------------------";
    154 static char size_menu_title[STRSIZE];
    155 static char size_menu_exit[MENUSTRSIZE];
    156 
    157 static void
    158 set_pset_exit_str(struct partition_usage_set *pset)
    159 {
    160 	char *str, num[25];
    161 	const char *args[2];
    162 	bool overrun;
    163 	daddr_t free_space = pset->cur_free_space;
    164 
    165 	/* format exit string */
    166 	overrun = free_space < 0;
    167 	if (overrun)
    168 		free_space = -free_space;
    169 
    170 	snprintf(num, sizeof(num), "%" PRIu64, free_space / sizemult);
    171 	args[0] = num;
    172 	args[1] = multname;
    173 	str = str_arg_subst(
    174 	    msg_string(overrun ? MSG_fssizesbad : MSG_fssizesok),
    175 	    2, args);
    176 	strlcpy(size_menu_exit, str, sizeof(size_menu_exit));
    177 	free(str);
    178 }
    179 
    180 static void
    181 draw_size_menu_header(menudesc *m, void *arg)
    182 {
    183 	struct partition_usage_set *pset = arg;
    184 	size_t i;
    185 	char col1[70], desc[MENUSTRSIZE];
    186 	bool need_ext = false, need_existing = false;
    187 
    188 	msg_display(MSG_ptnsizes);
    189 
    190 	for (i = 0; i < pset->num; i++) {
    191 		if (pset->infos[i].flags & PUIFLG_IS_OUTER)
    192 			need_ext = true;
    193 		else if (pset->infos[i].cur_part_id != NO_PART)
    194 			need_existing = true;
    195 	}
    196 	if (need_ext && need_existing)
    197 		snprintf(desc, sizeof desc, "%s, %s",
    198 		    msg_string(MSG_ptnsizes_mark_existing),
    199 		    msg_string(MSG_ptnsizes_mark_external));
    200 	else if (need_existing)
    201 		strlcpy(desc, msg_string(MSG_ptnsizes_mark_existing),
    202 		    sizeof desc);
    203 	else if (need_ext)
    204 		strlcpy(desc, msg_string(MSG_ptnsizes_mark_external),
    205 		    sizeof desc);
    206 	if (need_ext || need_existing) {
    207 		msg_printf("\n");
    208 		msg_display_add_subst(msg_string(MSG_ptnsizes_markers),
    209 		    1, &desc);
    210 	}
    211 	msg_printf("\n\n");
    212 
    213 	/* update menu title */
    214 	snprintf(col1, sizeof col1, "%s (%s)", msg_string(MSG_ptnheaders_size),
    215 	    multname);
    216 	snprintf(size_menu_title, sizeof size_menu_title,
    217 	    "   %-37.37s %s\n   %s", col1,
    218 	    msg_string(MSG_ptnheaders_filesystem), size_separator);
    219 }
    220 
    221 static void
    222 draw_size_menu_line(menudesc *m, int opt, void *arg)
    223 {
    224 	struct partition_usage_set *pset = arg;
    225 	daddr_t size;
    226 	char psize[38], inc_free[16], flag, swap[40];
    227 	const char *mount;
    228 	bool free_mount = false;
    229 
    230 	if (opt < 0 || (size_t)opt >= pset->num)
    231 		return;
    232 
    233 	inc_free[0] = 0;
    234 	if ((pset->infos[opt].flags & PUIFLAG_EXTEND) &&
    235 	     pset->cur_free_space > 0) {
    236 		size = pset->infos[opt].size + pset->cur_free_space;
    237 		snprintf(inc_free, sizeof inc_free, " (%" PRIu64 ")",
    238 		    size / sizemult);
    239 	}
    240 	size = pset->infos[opt].size;
    241 	if (pset->infos[opt].fs_type == FS_TMPFS) {
    242 		if (pset->infos[opt].size < 0)
    243 			snprintf(psize, sizeof psize, "%" PRIu64 "%%", -size);
    244 		else
    245 			snprintf(psize, sizeof psize, "%" PRIu64 " %s", size,
    246 			    msg_string(MSG_megname));
    247 	} else {
    248 		snprintf(psize, sizeof psize, "%" PRIu64 "%s",
    249 		    size / sizemult, inc_free);
    250 	}
    251 
    252 	if (pset->infos[opt].type == PT_swap) {
    253 		snprintf(swap, sizeof swap, "<%s>",
    254 		    msg_string(MSG_swap_display));
    255 		mount = swap;
    256 	} else if (pset->infos[opt].flags & PUIFLG_JUST_MOUNTPOINT) {
    257 		snprintf(swap, sizeof swap, "%s (%s)",
    258 		    pset->infos[opt].mount,
    259 		    getfslabelname(pset->infos[opt].fs_type,
    260 		    pset->infos[opt].fs_version));
    261 		mount = swap;
    262 	} else if (pset->infos[opt].mount[0]) {
    263 		if (pset->infos[opt].instflags & PUIINST_BOOT) {
    264 			snprintf(swap, sizeof swap, "%s <%s>",
    265 			    pset->infos[opt].mount, msg_string(MSG_ptn_boot));
    266 			mount = swap;
    267 		} else {
    268 			mount = pset->infos[opt].mount;
    269 		}
    270 #ifndef NO_CLONES
    271 	} else if (pset->infos[opt].flags & PUIFLG_CLONE_PARTS) {
    272 		snprintf(swap, sizeof swap, "%zu %s",
    273 		    pset->infos[opt].clone_src->num_sel,
    274 		    msg_string(MSG_clone_target_disp));
    275 		mount = swap;
    276 #endif
    277 	} else {
    278 		mount = NULL;
    279 		if (pset->infos[opt].parts->pscheme->other_partition_identifier
    280 		    && pset->infos[opt].cur_part_id != NO_PART)
    281 			mount = pset->infos[opt].parts->pscheme->
    282 			    other_partition_identifier(pset->infos[opt].parts,
    283 			    pset->infos[opt].cur_part_id);
    284 		if (mount == NULL)
    285 			mount = getfslabelname(pset->infos[opt].fs_type,
    286 			    pset->infos[opt].fs_version);
    287 		if (pset->infos[opt].instflags & PUIINST_BOOT) {
    288 			snprintf(swap, sizeof swap, "%s <%s>",
    289 			    mount, msg_string(MSG_ptn_boot));
    290 			mount = swap;
    291 		}
    292 		mount = str_arg_subst(msg_string(MSG_size_ptn_not_mounted),
    293 		    1, &mount);
    294 		free_mount = true;
    295 	}
    296 	flag = ' ';
    297 	if (pset->infos[opt].flags & PUIFLAG_EXTEND)
    298 		flag = '+';
    299 	else if (pset->infos[opt].flags & PUIFLG_IS_OUTER)
    300 		flag = '@';
    301 	else if (pset->infos[opt].cur_part_id != NO_PART)
    302 		flag = '=';
    303 	wprintw(m->mw, "%-35.35s %c %s", psize, flag, mount);
    304 	if (free_mount)
    305 		free(__UNCONST(mount));
    306 
    307 	if (opt == 0)
    308 		set_pset_exit_str(pset);
    309 }
    310 
    311 static int
    312 add_other_ptn_size(menudesc *menu, void *arg)
    313 {
    314 	struct partition_usage_set *pset = arg;
    315 	struct part_usage_info *p;
    316 	struct menu_ent *m;
    317 	char new_mp[MOUNTLEN], *err;
    318 	const char *args;
    319 
    320 	for (;;) {
    321 		msg_prompt_win(partman_go?MSG_askfsmountadv:MSG_askfsmount,
    322 		    -1, 18, 0, 0, NULL,  new_mp, sizeof(new_mp));
    323 		if (new_mp[0] == 0)
    324 			return 0;
    325 		if (new_mp[0] != '/') {
    326 			/* we need absolute mount paths */
    327 			memmove(new_mp+1, new_mp, sizeof(new_mp)-1);
    328 			new_mp[0] = '/';
    329 		}
    330 
    331 		/* duplicates? */
    332 		bool duplicate = false;
    333 		for (size_t i = 0; i < pset->num; i++) {
    334 			if (strcmp(pset->infos[i].mount,
    335 			    new_mp) == 0) {
    336 			    	args = new_mp;
    337 				err = str_arg_subst(
    338 				    msg_string(MSG_mp_already_exists),
    339 				    1, &args);
    340 				err_msg_win(err);
    341 				free(err);
    342 				duplicate = true;
    343 				break;
    344 			}
    345 		}
    346 		if (!duplicate)
    347 			break;
    348 	}
    349 
    350 	m = realloc(pset->menu_opts, (pset->num+5)*sizeof(*pset->menu_opts));
    351 	if (m == NULL)
    352 		return 0;
    353 	p = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
    354 	if (p == NULL)
    355 		return 0;
    356 
    357 	pset->infos = p;
    358 	pset->menu_opts = m;
    359 	menu->opts = m;
    360 	menu->numopts = pset->num+4;
    361 	m += pset->num;
    362 	p += pset->num;
    363 	memset(m, 0, sizeof(*m));
    364 	memset(p, 0, sizeof(*p));
    365 	p->parts = pset->parts;
    366 	p->cur_part_id = NO_PART;
    367 	p->type = PT_root;
    368 	p->fs_type = FS_BSDFFS;
    369 	p->fs_version = 3;
    370 	strncpy(p->mount, new_mp, sizeof(p->mount));
    371 
    372 	menu->cursel = pset->num;
    373 	pset->num++;
    374 	fill_ptn_menu(pset);
    375 
    376 	return -1;
    377 }
    378 
    379 #ifndef NO_CLONES
    380 static int
    381 inst_ext_clone(menudesc *menu, void *arg)
    382 {
    383 	struct selected_partitions selected;
    384 	struct clone_target_menu_data data;
    385 	struct partition_usage_set *pset = arg;
    386 	struct part_usage_info *p;
    387 	menu_ent *men;
    388 	int num_men, i;
    389 
    390 	if (!select_partitions(&selected, pm->parts))
    391 		return 0;
    392 
    393 	num_men = pset->num+1;
    394 	men = calloc(num_men, sizeof *men);
    395 	if (men == NULL)
    396 		return 0;
    397 	for (i = 0; i < num_men; i++)
    398 		men[i].opt_action = clone_target_select;
    399 	men[num_men-1].opt_name = MSG_clone_target_end;
    400 
    401 	memset(&data, 0, sizeof data);
    402 	data.usage = *pset;
    403 	data.res = -1;
    404 
    405 	data.usage.menu = new_menu(MSG_clone_target_hdr,
    406 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
    407 	    NULL, draw_size_menu_line, NULL, NULL, MSG_cancel);
    408 	process_menu(data.usage.menu, &data);
    409 	free_menu(data.usage.menu);
    410 	free(men);
    411 
    412 	if (data.res < 0)
    413 		goto err;
    414 
    415 	/* insert clone record */
    416 	men = realloc(pset->menu_opts, (pset->num+5)*sizeof(*pset->menu_opts));
    417 	if (men == NULL)
    418 		goto err;
    419 	pset->menu_opts = men;
    420 	menu->opts = men;
    421 	menu->numopts = pset->num+4;
    422 
    423 	p = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
    424 	if (p == NULL)
    425 		goto err;
    426 	pset->infos = p;
    427 
    428 	men += data.res;
    429 	p += data.res;
    430 	memmove(men+1, men, sizeof(*men)*((pset->num+4)-data.res));
    431 	memmove(p+1, p, sizeof(*p)*((pset->num)-data.res));
    432 	memset(men, 0, sizeof(*men));
    433 	memset(p, 0, sizeof(*p));
    434 	p->flags = PUIFLG_CLONE_PARTS;
    435 	p->cur_part_id = NO_PART;
    436 	p->clone_src = malloc(sizeof(selected));
    437 	if (p->clone_src != NULL) {
    438 		*p->clone_src = selected;
    439 		p->clone_ndx = ~0U;
    440 		p->size = selected_parts_size(&selected);
    441 		p->parts = pset->parts;
    442 	} else {
    443 		p->clone_ndx = 0;
    444 		free_selected_partitions(&selected);
    445 	}
    446 
    447 	menu->cursel = data.res == 0 ? 1 : 0;
    448 	pset->num++;
    449 	fill_ptn_menu(pset);
    450 
    451 	return -1;
    452 
    453 err:
    454 	free_selected_partitions(&selected);
    455 	return 0;
    456 }
    457 #endif
    458 
    459 static size_t
    460 fill_ptn_menu(struct partition_usage_set *pset)
    461 {
    462 	struct part_usage_info *p;
    463 	struct disk_part_info info;
    464 	menu_ent *m;
    465 	size_t i;
    466 	daddr_t free_space;
    467 
    468 #ifdef NO_CLONES
    469 #define	ADD_ITEMS	3
    470 #else
    471 #define	ADD_ITEMS	4
    472 #endif
    473 
    474 	memset(pset->menu_opts, 0, (pset->num+ADD_ITEMS)
    475 	    *sizeof(*pset->menu_opts));
    476 	for (m = pset->menu_opts, p = pset->infos, i = 0; i < pset->num;
    477 	    m++, p++, i++) {
    478 		if (p->flags & PUIFLG_CLONE_PARTS)
    479 			m->opt_flags = OPT_IGNORE|OPT_NOSHORT;
    480 		else
    481 		m->opt_action = set_ptn_size;
    482 	}
    483 
    484 	m->opt_name = size_separator;
    485 	m->opt_flags = OPT_IGNORE|OPT_NOSHORT;
    486 	m++;
    487 
    488 	m->opt_name = MSG_add_another_ptn;
    489 	m->opt_action = add_other_ptn_size;
    490 	m++;
    491 
    492 #ifndef NO_CLONES
    493 	m->opt_name = MSG_clone_from_elsewhere;
    494 	m->opt_action = inst_ext_clone;
    495 	m++;
    496 #endif
    497 
    498 	m->opt_name = MSG_askunits;
    499 	m->opt_menu = MENU_sizechoice;
    500 	m->opt_flags = OPT_SUB;
    501 	m++;
    502 
    503 	/* calculate free space */
    504 	free_space = pset->parts->free_space - pset->reserved_space;
    505 	for (i = 0; i < pset->parts->num_part; i++) {
    506 		if (!pset->parts->pscheme->get_part_info(pset->parts, i,
    507 		    &info))
    508 			continue;
    509 		if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
    510 		    PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
    511 			continue;
    512 		free_space += info.size;
    513 	}
    514 	for (i = 0; i < pset->num; i++) {
    515 		if (pset->infos[i].flags &
    516 		    (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
    517 			continue;
    518 		free_space -= pset->infos[i].size;
    519 	}
    520 	pset->cur_free_space = free_space;
    521 	set_pset_exit_str(pset);
    522 
    523 	if (pset->menu >= 0)
    524 		set_menu_numopts(pset->menu, m - pset->menu_opts);
    525 
    526 	return m - pset->menu_opts;
    527 }
    528 
    529 static part_id
    530 find_part_at(struct disk_partitions *parts, daddr_t start)
    531 {
    532 	size_t i;
    533 	struct disk_part_info info;
    534 
    535 	for (i = 0; i < parts->num_part; i++) {
    536 		if (!parts->pscheme->get_part_info(parts, i, &info))
    537 			continue;
    538 		if (info.start == start)
    539 			return i;
    540 	}
    541 
    542 	return NO_PART;
    543 }
    544 
    545 static daddr_t
    546 parse_ram_size(const char *str, bool *is_percent)
    547 {
    548 	daddr_t val;
    549 	char *cp;
    550 
    551 	val = strtoull(str, &cp, 10);
    552 	while (*cp && isspace((unsigned char)*cp))
    553 		cp++;
    554 
    555 	*is_percent = *cp == '%';
    556 	return val;
    557 }
    558 
    559 int
    560 set_ptn_size(menudesc *m, void *arg)
    561 {
    562 	struct partition_usage_set *pset = arg;
    563 	struct part_usage_info *p = &pset->infos[m->cursel];
    564 	char answer[16], dflt[16];
    565 	const char *err_msg;
    566 	size_t i, root = ~0U;
    567 	daddr_t size, old_size, new_size_val, mult;
    568 	int rv;
    569 	bool non_zero, extend, is_ram_size, is_percent = false;
    570 
    571 	if (pset->cur_free_space == 0 && p->size == 0 &&
    572 	    !(p->flags & PUIFLG_JUST_MOUNTPOINT))
    573 		/* Don't allow 'free_parts' to go negative */
    574 		return 0;
    575 
    576 	if (p->cur_part_id != NO_PART) {
    577 		rv = 0;
    578 		process_menu(MENU_ptnsize_replace_existing_partition, &rv);
    579 		if (rv == 0)
    580 			return 0;
    581 		if (!pset->parts->pscheme->delete_partition(pset->parts,
    582 		    p->cur_part_id, &err_msg)) {
    583 			if (err_msg)
    584 				err_msg_win(err_msg);
    585 			return 0;
    586 		}
    587 		p->cur_part_id = NO_PART;
    588 		/*
    589 		 * All other part ids are invalid now too - update them!
    590 		 */
    591 		for (i = 0; i < pset->num; i++) {
    592 			if (pset->infos[i].cur_part_id == NO_PART)
    593 				continue;
    594 			pset->infos[i].cur_part_id =
    595 			    find_part_at(pset->parts, pset->infos[i].cur_start);
    596 		}
    597 	}
    598 
    599 	is_ram_size = (p->flags & PUIFLG_JUST_MOUNTPOINT)
    600 	    && p->fs_type == FS_TMPFS;
    601 
    602 	size = p->size;
    603 	if (is_ram_size && size < 0) {
    604 		is_percent = true;
    605 		size = -size;
    606 	}
    607 	old_size = size;
    608 	if (size == 0)
    609 		size = p->def_size;
    610 	if (!is_ram_size)
    611 		size /= sizemult;
    612 
    613 	if (is_ram_size) {
    614 		snprintf(dflt, sizeof dflt, "%" PRIu64 "%s",
    615 		    size, is_percent ? "%" : "");
    616 	} else {
    617 		snprintf(dflt, sizeof dflt, "%" PRIu64 "%s",
    618 		    size, p->flags & PUIFLAG_EXTEND ? "+" : "");
    619 	}
    620 
    621 	for (;;) {
    622 		msg_fmt_prompt_win(MSG_askfssize, -1, 18, 0, 0,
    623 		    dflt, answer, sizeof answer, "%s%s", p->mount,
    624 		    is_ram_size ? msg_string(MSG_megname) : multname);
    625 
    626 		if (is_ram_size) {
    627 			new_size_val = parse_ram_size(answer, &is_percent);
    628 			if (is_percent &&
    629 			    (new_size_val < 0 || new_size_val > 100))
    630 				continue;
    631 			if (!is_percent && new_size_val < 0)
    632 				continue;
    633 			size = new_size_val;
    634 			extend = false;
    635 			break;
    636 		}
    637 		mult = sizemult;
    638 		new_size_val = parse_disk_pos(answer, &mult, pm->sectorsize,
    639 		    pm->dlcylsize, &extend);
    640 
    641 		if (strcmp(answer, dflt) == 0)
    642 			non_zero = p->def_size > 0;
    643 		else
    644 			non_zero = new_size_val > 0;
    645 
    646 		/* Some special cases when /usr is first given a size */
    647 		if (old_size == 0 && non_zero &&
    648 		    strcmp(p->mount, "/usr") == 0) {
    649 			for (i = 0; i < pset->num; i++) {
    650 				if (strcmp(pset->infos[i].mount, "/") == 0) {
    651 					root = i;
    652 					break;
    653 				}
    654 			}
    655 			/* Remove space for /usr from / */
    656 			if (root < pset->num &&
    657 			     pset->infos[root].cur_part_id == NO_PART &&
    658 			     pset->infos[root].size ==
    659 					pset->infos[root].def_size) {
    660 				/*
    661 				 * root partition does not yet exist and
    662 				 * has default size
    663 				 */
    664 				pset->infos[root].size -= p->def_size;
    665 				pset->cur_free_space += p->def_size;
    666 			}
    667 			/*
    668 			 * hack to add free space to /usr if
    669 			 * previously / got it
    670 			 */
    671 			if (pset->infos[root].flags & PUIFLAG_EXTEND)
    672 				extend = true;
    673 		}
    674 		if (new_size_val < 0)
    675 			continue;
    676 		size = new_size_val;
    677 		break;
    678 	}
    679 
    680 	daddr_t align = pset->parts->pscheme->get_part_alignment(pset->parts);
    681 	if (!is_ram_size) {
    682 		size = NUMSEC(size, mult, align);
    683 	}
    684 	if (p->flags & PUIFLAG_EXTEND)
    685 		p->flags &= ~PUIFLAG_EXTEND;
    686 	if (extend && (p->limit == 0 || p->limit > p->size)) {
    687 		for (size_t k = 0; k < pset->num; k++)
    688 			pset->infos[k].flags &= ~PUIFLAG_EXTEND;
    689 		p->flags |= PUIFLAG_EXTEND;
    690 		if (size == 0)
    691 			size = align;
    692 	}
    693 	if (p->limit != 0 && size > p->limit)
    694 		size = p->limit;
    695 	if ((p->flags & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT)) == 0)
    696 		pset->cur_free_space += p->size - size;
    697 	p->size = is_percent ? -size : size;
    698 	set_pset_exit_str(pset);
    699 
    700 	return 0;
    701 }
    702 
    703 /*
    704  * User interface to edit a "wanted" partition layout "pset" as first
    705  * abstract phase (not concrete partitions).
    706  * Make sure to have everything (at least theoretically) fit the
    707  * available space.
    708  * During editing we keep the part_usage_info and the menu_opts
    709  * in pset in sync, that is: we always allocate just enough entries
    710  * in pset->infos as we have usage infos in the list (pset->num),
    711  * and two additional menu entries ("add a partition" and "select units").
    712  * The menu exit string changes depending on content, and implies
    713  * abort while the partition set is not valid (does not fit).
    714  * Return true when the user wants to continue (by editing the concrete
    715  * partitions), return false to abort.
    716  */
    717 bool
    718 get_ptn_sizes(struct partition_usage_set *pset)
    719 {
    720 	size_t num;
    721 
    722 	wclear(stdscr);
    723 	wrefresh(stdscr);
    724 
    725 	if (pset->menu_opts == NULL)
    726 		pset->menu_opts = calloc(pset->num+4, sizeof(*pset->menu_opts));
    727 
    728 	pset->menu = -1;
    729 	num = fill_ptn_menu(pset);
    730 
    731 	pset->menu = new_menu(size_menu_title, pset->menu_opts, num,
    732 			3, -1, 12, 70,
    733 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_NOCLEAR|MC_CONTINUOUS,
    734 			draw_size_menu_header, draw_size_menu_line, NULL,
    735 			NULL, size_menu_exit);
    736 
    737 	if (pset->menu < 0) {
    738 		free(pset->menu_opts);
    739 		pset->menu_opts = NULL;
    740 		return false;
    741 	}
    742 
    743 	pset->ok = true;
    744 	process_menu(pset->menu, pset);
    745 
    746 	free_menu(pset->menu);
    747 	free(pset->menu_opts);
    748 	pset->menu = -1;
    749 	pset->menu_opts = NULL;
    750 
    751 	return pset->ok;
    752 }
    753 
    754 static int
    755 set_keep_existing(menudesc *m, void *arg)
    756 {
    757 	((arg_rep_int*)arg)->rv = LY_KEEPEXISTING;
    758 	return 0;
    759 }
    760 
    761 static int
    762 set_switch_scheme(menudesc *m, void *arg)
    763 {
    764 	((arg_rep_int*)arg)->rv = LY_OTHERSCHEME;
    765 	return 0;
    766 }
    767 
    768 static int
    769 set_edit_part_sizes(menudesc *m, void *arg)
    770 {
    771 	((arg_rep_int*)arg)->rv = LY_SETSIZES;
    772 	return 0;
    773 }
    774 
    775 static int
    776 set_use_default_sizes(menudesc *m, void *arg)
    777 {
    778 	((arg_rep_int*)arg)->rv = LY_USEDEFAULT;
    779 	return 0;
    780 }
    781 
    782 /*
    783  * Check if there is a reasonable pre-existing partition for
    784  * NetBSD.
    785  */
    786 static bool
    787 check_existing_netbsd(struct disk_partitions *parts)
    788 {
    789 	size_t nbsd_parts;
    790 	struct disk_part_info info;
    791 
    792 	nbsd_parts = 0;
    793 	for (part_id p = 0; p < parts->num_part; p++) {
    794 		if (!parts->pscheme->get_part_info(parts, p, &info))
    795 			continue;
    796 		if (info.flags & (PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
    797 			continue;
    798 		if (info.nat_type && info.nat_type->generic_ptype == PT_root)
    799 			nbsd_parts++;
    800 	}
    801 
    802 	return nbsd_parts > 0;
    803 }
    804 
    805 /*
    806  * Query a partition layout type (with available options depending on
    807  * pre-existing partitions).
    808  */
    809 static enum layout_type
    810 ask_layout(struct disk_partitions *parts, bool have_existing)
    811 {
    812 	arg_rep_int ai;
    813 	const char *args[2];
    814 	int menu;
    815 	size_t num_opts;
    816 	menu_ent options[4], *opt;
    817 
    818 	args[0] = msg_string(parts->pscheme->name);
    819 	args[1] = msg_string(parts->pscheme->short_name);
    820 	ai.args.argv = args;
    821 	ai.args.argc = 2;
    822 	ai.rv = LY_ERROR;
    823 
    824 	memset(options, 0, sizeof(options));
    825 	num_opts = 0;
    826 	opt = &options[0];
    827 
    828 	if (have_existing) {
    829 		opt->opt_name = MSG_Keep_existing_partitions;
    830 		opt->opt_flags = OPT_EXIT;
    831 		opt->opt_action = set_keep_existing;
    832 		opt++;
    833 		num_opts++;
    834 	}
    835 	opt->opt_name = MSG_Set_Sizes;
    836 	opt->opt_flags = OPT_EXIT;
    837 	opt->opt_action = set_edit_part_sizes;
    838 	opt++;
    839 	num_opts++;
    840 
    841 	opt->opt_name = MSG_Use_Default_Parts;
    842 	opt->opt_flags = OPT_EXIT;
    843 	opt->opt_action = set_use_default_sizes;
    844 	opt++;
    845 	num_opts++;
    846 
    847 	if (num_available_part_schemes > 1 &&
    848 	    parts->parent == NULL) {
    849 		opt->opt_name = MSG_Use_Different_Part_Scheme;
    850 		opt->opt_flags = OPT_EXIT;
    851 		opt->opt_action = set_switch_scheme;
    852 		opt++;
    853 		num_opts++;
    854 	}
    855 
    856 	menu = new_menu(MSG_Select_your_choice, options, num_opts,
    857 	    -1, -10, 0, 0, 0, NULL, NULL, NULL, NULL, MSG_cancel);
    858 	if (menu != -1) {
    859 		get_menudesc(menu)->expand_act = expand_all_option_texts;
    860 		process_menu(menu, &ai);
    861 		free_menu(menu);
    862 	}
    863 
    864 	return ai.rv;
    865 }
    866 
    867 static void
    868 merge_part_with_wanted(struct disk_partitions *parts, part_id pno,
    869     const struct disk_part_info *info, struct partition_usage_set *wanted,
    870     size_t wanted_num, bool is_outer)
    871 {
    872 	struct part_usage_info *infos;
    873 
    874 	/*
    875 	 * does this partition match something in the wanted set?
    876 	 */
    877 	for (size_t i = 0; i < wanted_num; i++) {
    878 		if (wanted->infos[i].type != info->nat_type->generic_ptype)
    879 			continue;
    880 		if (wanted->infos[i].type == PT_root &&
    881 		    info->last_mounted != NULL && info->last_mounted[0] != 0 &&
    882 		    strcmp(info->last_mounted, wanted->infos[i].mount) != 0)
    883 			continue;
    884 		if (wanted->infos[i].cur_part_id != NO_PART)
    885 			continue;
    886 		wanted->infos[i].cur_part_id = pno;
    887 		wanted->infos[i].parts = parts;
    888 		wanted->infos[i].size = info->size;
    889 		wanted->infos[i].cur_start = info->start;
    890 		wanted->infos[i].flags &= ~PUIFLAG_EXTEND;
    891 		if (wanted->infos[i].fs_type != FS_UNUSED &&
    892 		    wanted->infos[i].type != PT_swap &&
    893 		    info->last_mounted != NULL &&
    894 		    info->last_mounted[0] != 0)
    895 			wanted->infos[i].instflags |= PUIINST_MOUNT;
    896 		if (is_outer)
    897 			wanted->infos[i].flags |= PUIFLG_IS_OUTER;
    898 		else
    899 			wanted->infos[i].flags &= ~PUIFLG_IS_OUTER;
    900 		return;
    901 	}
    902 
    903 	/*
    904 	 * no match - if this is from the outer scheme, we are done.
    905 	 * otherwise it must be inserted into the wanted set.
    906 	 */
    907 	if (is_outer)
    908 		return;
    909 
    910 	/*
    911 	 * create a new entry for this
    912 	 */
    913 	infos = realloc(wanted->infos, sizeof(*infos)*(wanted->num+1));
    914 	if (infos == NULL)
    915 		return;
    916 	wanted->infos = infos;
    917 	infos += wanted->num;
    918 	wanted->num++;
    919 	memset(infos, 0, sizeof(*infos));
    920 	if (info->last_mounted != NULL && info->last_mounted[0] != 0)
    921 		strlcpy(infos->mount, info->last_mounted,
    922 		    sizeof(infos->mount));
    923 	infos->type = info->nat_type->generic_ptype;
    924 	infos->cur_part_id = pno;
    925 	infos->parts = parts;
    926 	infos->size = info->size;
    927 	infos->cur_start = info->start;
    928 	infos->fs_type = info->fs_type;
    929 	infos->fs_version = info->fs_sub_type;
    930 	if (is_outer)
    931 		infos->flags |= PUIFLG_IS_OUTER;
    932 }
    933 
    934 static bool
    935 have_x11_by_default(void)
    936 {
    937 	static const uint8_t def_sets[] = { MD_SETS_SELECTED };
    938 
    939 	for (size_t i = 0; i < __arraycount(def_sets); i++)
    940 		if (def_sets[i] >= SET_X11_FIRST &&
    941 		    def_sets[i] <= SET_X11_LAST)
    942 			return true;
    943 
    944 	return false;
    945 }
    946 
    947 static void
    948 fill_defaults(struct partition_usage_set *wanted, struct disk_partitions *parts,
    949     daddr_t ptstart, daddr_t ptsize)
    950 {
    951 	size_t i, root = ~0U, usr = ~0U, swap = ~0U, def_usr = ~0U;
    952 	daddr_t free_space, dump_space, required;
    953 #if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
    954 	size_t boot = ~0U;
    955 #endif
    956 
    957 	memset(wanted, 0, sizeof(*wanted));
    958 	wanted->parts = parts;
    959 	if (ptstart > parts->disk_start)
    960 		wanted->reserved_space = ptstart - parts->disk_start;
    961 	if ((ptstart + ptsize) < (parts->disk_start+parts->disk_size))
    962 		wanted->reserved_space +=
    963 		    (parts->disk_start+parts->disk_size) -
    964 		    (ptstart + ptsize);
    965 	wanted->num = __arraycount(default_parts_init);
    966 	wanted->infos = calloc(wanted->num, sizeof(*wanted->infos));
    967 	if (wanted->infos == NULL) {
    968 		err_msg_win(err_outofmem);
    969 		return;
    970 	}
    971 
    972 	memcpy(wanted->infos, default_parts_init, sizeof(default_parts_init));
    973 
    974 #ifdef HAVE_TMPFS
    975 	if (get_ramsize() >= SMALL_RAM_SIZE) {
    976 		for (i = 0; i < wanted->num; i++) {
    977 			if (wanted->infos[i].type != PT_root ||
    978 			    wanted->infos[i].fs_type != FS_TMPFS)
    979 				continue;
    980 			/* default tmpfs to 1/4 RAM */
    981 			wanted->infos[i].size = -25;
    982 			wanted->infos[i].def_size = -25;
    983 			break;
    984 		}
    985 	}
    986 #endif
    987 
    988 #ifdef MD_PART_DEFAULTS
    989 	MD_PART_DEFAULTS(pm, wanted->infos, wanted->num);
    990 #endif
    991 
    992 	for (i = 0; i < wanted->num; i++) {
    993 		wanted->infos[i].parts = parts;
    994 		wanted->infos[i].cur_part_id = NO_PART;
    995 		if (wanted->infos[i].type == PT_undef &&
    996 		    wanted->infos[i].fs_type != FS_UNUSED) {
    997 			const struct part_type_desc *pt =
    998 			    parts->pscheme->get_fs_part_type(PT_undef,
    999 			    wanted->infos[i].fs_type,
   1000 			    wanted->infos[i].fs_version);
   1001 			if (pt != NULL)
   1002 				wanted->infos[i].type = pt->generic_ptype;
   1003 		}
   1004 		if (wanted->parts->parent != NULL &&
   1005 		    (wanted->infos[i].fs_type == FS_MSDOS ||
   1006 		     wanted->infos[i].fs_type == FS_EX2FS))
   1007 			wanted->infos[i].flags |=
   1008 			    PUIFLG_ADD_INNER|PUIFLAG_ADD_OUTER;
   1009 
   1010 #if DEFSWAPSIZE == -1
   1011 		if (wanted->infos[i].type == PT_swap) {
   1012 #ifdef	MD_MAY_SWAP_TO
   1013 			if (MD_MAY_SWAP_TO(wanted->parts->disk))
   1014 #endif
   1015 				wanted->infos[i].size =
   1016 				    get_ramsize() * (MEG / 512);
   1017 		}
   1018 #endif
   1019 		if (wanted->infos[i].type == PT_swap && swap > wanted->num)
   1020 			swap = i;
   1021 #if defined(DEFAULT_UFS2) && !defined(HAVE_UFS2_BOOT)
   1022 		if (wanted->infos[i].instflags & PUIINST_BOOT)
   1023 			boot = i;
   1024 #endif
   1025 		if (wanted->infos[i].type == PT_root) {
   1026 			if (strcmp(wanted->infos[i].mount, "/") == 0) {
   1027 				root = i;
   1028 			} else if (
   1029 			    strcmp(wanted->infos[i].mount, "/usr") == 0) {
   1030 				if (wanted->infos[i].size > 0)
   1031 					usr = i;
   1032 				else
   1033 					def_usr = i;
   1034 			}
   1035 			if (wanted->infos[i].fs_type == FS_UNUSED)
   1036 				wanted->infos[i].fs_type = FS_BSDFFS;
   1037 			if (wanted->infos[i].fs_type == FS_BSDFFS) {
   1038 #ifdef DEFAULT_UFS2
   1039 #ifndef HAVE_UFS2_BOOT
   1040 				if (boot < wanted->num || i != root)
   1041 #endif
   1042 					wanted->infos[i].fs_version = 3;
   1043 #endif
   1044 			}
   1045 		}
   1046 	}
   1047 
   1048 	/*
   1049 	 * Now we have the defaults as if we were installing to an
   1050 	 * empty disk. Merge the partitions in target range that are already
   1051 	 * there (match with wanted) or are there additionally.
   1052 	 * The only thing outside of target range that we care for
   1053 	 * are FAT partitions, EXT2FS partitions, and a potential
   1054 	 * swap partition - we assume one is enough.
   1055 	 */
   1056 	size_t num = wanted->num;
   1057 	if (parts->parent) {
   1058 		for (part_id pno = 0; pno < parts->parent->num_part; pno++) {
   1059 			struct disk_part_info info;
   1060 
   1061 			if (!parts->parent->pscheme->get_part_info(
   1062 			    parts->parent, pno, &info))
   1063 				continue;
   1064 			if (info.nat_type->generic_ptype != PT_swap &&
   1065 			    info.fs_type != FS_MSDOS &&
   1066 			    info.fs_type != FS_EX2FS)
   1067 				continue;
   1068 			merge_part_with_wanted(parts->parent, pno, &info,
   1069 			    wanted, num, true);
   1070 			break;
   1071 		}
   1072 	}
   1073 	for (part_id pno = 0; pno < parts->num_part; pno++) {
   1074 		struct disk_part_info info;
   1075 
   1076 		if (!parts->pscheme->get_part_info(parts, pno, &info))
   1077 			continue;
   1078 
   1079 		if (info.flags & PTI_PSCHEME_INTERNAL)
   1080 			continue;
   1081 
   1082 		if (info.nat_type->generic_ptype != PT_swap &&
   1083 		    (info.start < ptstart ||
   1084 		    (info.start + info.size) > (ptstart+ptsize)))
   1085 			continue;
   1086 
   1087 		merge_part_with_wanted(parts, pno, &info,
   1088 		    wanted, num, false);
   1089 	}
   1090 
   1091 	daddr_t align = parts->pscheme->get_part_alignment(parts);
   1092 
   1093 	if (root < wanted->num && wanted->infos[root].cur_part_id == NO_PART) {
   1094 		daddr_t max_root_size = parts->disk_start + parts->disk_size;
   1095 		if (root_limit > 0) {
   1096 			/* Bah - bios can not read all the disk, limit root */
   1097 			max_root_size = root_limit - parts->disk_start;
   1098 		}
   1099 		wanted->infos[root].limit = max_root_size;
   1100 	}
   1101 
   1102 	if (have_x11_by_default()) {
   1103 		daddr_t xsize = XNEEDMB * (MEG / 512);
   1104 		if (usr < wanted->num) {
   1105 			if (wanted->infos[usr].cur_part_id == NO_PART) {
   1106 				wanted->infos[usr].size += xsize;
   1107 				wanted->infos[usr].def_size += xsize;
   1108 			}
   1109 		} else if (root < wanted->num &&
   1110 		    wanted->infos[root].cur_part_id == NO_PART &&
   1111 		    (wanted->infos[root].limit == 0 ||
   1112 		    (wanted->infos[root].size + xsize) <=
   1113 		    wanted->infos[root].limit)) {
   1114 			wanted->infos[root].size += xsize;
   1115 		}
   1116 	}
   1117 	if (wanted->infos[root].limit > 0 &&
   1118 	    wanted->infos[root].size > wanted->infos[root].limit) {
   1119 		if (usr < wanted->num) {
   1120 			/* move space from root to usr */
   1121 			daddr_t spill = wanted->infos[root].size -
   1122 			    wanted->infos[root].limit;
   1123 			spill = roundup(spill, align);
   1124 			wanted->infos[root].size =
   1125 			    wanted->infos[root].limit;
   1126 			wanted->infos[usr].size = spill;
   1127 		} else {
   1128 			wanted->infos[root].size =
   1129 			    wanted->infos[root].limit;
   1130 		}
   1131 	}
   1132 
   1133 	/*
   1134 	 * Preliminary calc additional space to allocate and how much
   1135 	 * we likely will have left over. Use that to do further
   1136 	 * adjustments, so we don't present the user inherently
   1137 	 * impossible defaults.
   1138 	 */
   1139 	free_space = parts->free_space - wanted->reserved_space;
   1140 	required = 0;
   1141 	if (root < wanted->num)
   1142 		required += wanted->infos[root].size;
   1143 	if (usr < wanted->num)
   1144 		required += wanted->infos[usr].size;
   1145 	else if (def_usr < wanted->num)
   1146 		required += wanted->infos[def_usr].def_size;
   1147 	free_space -= required;
   1148 	for (i = 0; i < wanted->num; i++) {
   1149 		if (i == root || i == usr)
   1150 			continue;	/* already accounted above */
   1151 		if (wanted->infos[i].cur_part_id != NO_PART)
   1152 			continue;
   1153 		if (wanted->infos[i].size == 0)
   1154 			continue;
   1155 		if (wanted->infos[i].flags
   1156 		    & (PUIFLG_IS_OUTER|PUIFLG_JUST_MOUNTPOINT))
   1157 			continue;
   1158 		free_space -= wanted->infos[i].size;
   1159 	}
   1160 	if (free_space < 0 && swap < wanted->num &&
   1161 	    get_ramsize() > TINY_RAM_SIZE) {
   1162 		/* steel from swap partition */
   1163 		daddr_t d = wanted->infos[swap].size;
   1164 		daddr_t inc = roundup(-free_space, align);
   1165 		if (inc > d)
   1166 			inc = d;
   1167 		free_space += inc;
   1168 		wanted->infos[swap].size -= inc;
   1169 	}
   1170 	if (root < wanted->num) {
   1171 		/* Add space for 2 system dumps to / (traditional) */
   1172 		dump_space = get_ramsize() * (MEG/512);
   1173 		dump_space = roundup(dump_space, align);
   1174 		if (free_space > dump_space*2)
   1175 			dump_space *= 2;
   1176 		if (free_space > dump_space) {
   1177 			wanted->infos[root].size += dump_space;
   1178 			free_space -= dump_space;
   1179 		}
   1180 	}
   1181 	if (wanted->infos[root].limit > 0 &&
   1182 	    (wanted->infos[root].cur_start + wanted->infos[root].size >
   1183 		wanted->infos[root].limit ||
   1184 	    (wanted->infos[root].flags & PUIFLAG_EXTEND &&
   1185 	    (wanted->infos[root].cur_start + wanted->infos[root].size
   1186 	     + free_space > wanted->infos[root].limit)))) {
   1187 		if (usr >= wanted->num && def_usr < wanted->num) {
   1188 			usr = def_usr;
   1189 			wanted->infos[usr].size = wanted->infos[root].size
   1190 			    - wanted->infos[root].limit;
   1191 			if (wanted->infos[usr].size <= 0)
   1192 				wanted->infos[usr].size = max(1,
   1193 				    wanted->infos[usr].def_size);
   1194 			wanted->infos[root].size =
   1195 			    wanted->infos[root].limit;
   1196 			if (wanted->infos[root].flags & PUIFLAG_EXTEND) {
   1197 				wanted->infos[root].flags &= ~PUIFLAG_EXTEND;
   1198 				wanted->infos[usr].flags |= PUIFLAG_EXTEND;
   1199 			}
   1200 		} else if (usr < wanted->num) {
   1201 			/* move space from root to usr */
   1202 			daddr_t spill = wanted->infos[root].size -
   1203 			    wanted->infos[root].limit;
   1204 			spill = roundup(spill, align);
   1205 			wanted->infos[root].size =
   1206 			    wanted->infos[root].limit;
   1207 			wanted->infos[usr].size = spill;
   1208 		} else {
   1209 			wanted->infos[root].size =
   1210 			    wanted->infos[root].limit;
   1211 		}
   1212 	}
   1213 	wanted->infos[root].def_size = wanted->infos[root].size;
   1214 }
   1215 
   1216 /*
   1217  * We sort pset->infos to sync with pset->parts and
   1218  * the cur_part_id, to allow using the same index into both
   1219  * "array" in later phases. This may include inserting
   1220  * dummy  entries (when we do not actually want the
   1221  * partition, but it is forced upon us, like RAW_PART in
   1222  * disklabel).
   1223  */
   1224 static void
   1225 sort_and_sync_parts(struct partition_usage_set *pset)
   1226 {
   1227 	struct part_usage_info *infos;
   1228 	size_t i, j, no;
   1229 	part_id pno;
   1230 
   1231 	pset->cur_free_space = pset->parts->free_space - pset->reserved_space;
   1232 
   1233 	/* count non-empty entries that are not in pset->parts */
   1234 	no = pset->parts->num_part;
   1235 	for (i = 0; i < pset->num; i++) {
   1236 		if (pset->infos[i].size == 0)
   1237 			continue;
   1238 		if (pset->infos[i].cur_part_id != NO_PART)
   1239 			continue;
   1240 		no++;
   1241 	}
   1242 
   1243 	/* allocate new infos */
   1244 	infos = calloc(no, sizeof *infos);
   1245 	if (infos == NULL)
   1246 		return;
   1247 
   1248 	/* pre-initialize the first entries as dummy entries */
   1249 	for (i = 0; i < pset->parts->num_part; i++) {
   1250 		infos[i].cur_part_id = NO_PART;
   1251 		infos[i].cur_flags = PTI_PSCHEME_INTERNAL;
   1252 	}
   1253 	/*
   1254 	 * Now copy over everything from our old entries that points to
   1255 	 * a real partition.
   1256 	 */
   1257 	for (i = 0; i < pset->num; i++) {
   1258 		pno = pset->infos[i].cur_part_id;
   1259 		if (pno == NO_PART)
   1260 			continue;
   1261 		if (pset->parts != pset->infos[i].parts)
   1262 			continue;
   1263 		if (pset->infos[i].flags & PUIFLG_JUST_MOUNTPOINT)
   1264 			continue;
   1265 		if ((pset->infos[i].flags & (PUIFLG_IS_OUTER|PUIFLG_ADD_INNER))
   1266 		    == PUIFLG_IS_OUTER)
   1267 			continue;
   1268 		if (pno >= pset->parts->num_part)
   1269 			continue;
   1270 		memcpy(infos+pno, pset->infos+i, sizeof(*infos));
   1271 	}
   1272 	/* Fill in the infos for real partitions where we had no data */
   1273 	for (pno = 0; pno < pset->parts->num_part; pno++) {
   1274 		struct disk_part_info info;
   1275 
   1276 		if (infos[pno].cur_part_id != NO_PART)
   1277 			continue;
   1278 
   1279 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
   1280 		    &info))
   1281 			continue;
   1282 
   1283 		infos[pno].parts = pset->parts;
   1284 		infos[pno].cur_part_id = pno;
   1285 		infos[pno].cur_flags = info.flags;
   1286 		infos[pno].size = info.size;
   1287 		infos[pno].type = info.nat_type->generic_ptype;
   1288 		infos[pno].cur_start = info.start;
   1289 		infos[pno].fs_type = info.fs_type;
   1290 		infos[pno].fs_version = info.fs_sub_type;
   1291 	}
   1292 	/* Add the non-partition entries after that */
   1293 	j = pset->parts->num_part;
   1294 	for (i = 0; i < pset->num; i++) {
   1295 		if (j >= no)
   1296 			break;
   1297 		if (pset->infos[i].size == 0)
   1298 			continue;
   1299 		if (pset->infos[i].cur_part_id != NO_PART)
   1300 			continue;
   1301 		memcpy(infos+j, pset->infos+i, sizeof(*infos));
   1302 		j++;
   1303 	}
   1304 
   1305 	/* done, replace infos */
   1306 	free(pset->infos);
   1307 	pset->num = no;
   1308 	pset->infos = infos;
   1309 }
   1310 
   1311 #ifndef NO_CLONES
   1312 /*
   1313  * Convert clone entries with more than one source into
   1314  * several entries with a single source each.
   1315  */
   1316 static void
   1317 normalize_clones(struct part_usage_info **infos, size_t *num)
   1318 {
   1319 	size_t i, j, add_clones;
   1320 	struct part_usage_info *ui, *src, *target;
   1321 	struct disk_part_info info;
   1322 	struct selected_partition *clone;
   1323 
   1324 	for (add_clones = 0, i = 0; i < *num; i++) {
   1325 		if ((*infos)[i].clone_src != NULL &&
   1326 		    (*infos)[i].flags & PUIFLG_CLONE_PARTS &&
   1327 		    (*infos)[i].cur_part_id == NO_PART)
   1328 			add_clones += (*infos)[i].clone_src->num_sel-1;
   1329 	}
   1330 	if (add_clones == 0)
   1331 		return;
   1332 
   1333 	ui = calloc(*num+add_clones, sizeof(**infos));
   1334 	if (ui == NULL)
   1335 		return;	/* can not handle this well here, drop some clones */
   1336 
   1337 	/* walk the list and dedup clones */
   1338 	for (src = *infos, target = ui, i = 0; i < *num; i++) {
   1339 		if (src != target)
   1340 			*target = *src;
   1341 		if (target->clone_src != NULL &&
   1342 		    (target->flags & PUIFLG_CLONE_PARTS) &&
   1343 		    target->cur_part_id == NO_PART) {
   1344 			for (j = 0; j < src->clone_src->num_sel; j++) {
   1345 				if (j > 0) {
   1346 					target++;
   1347 					*target = *src;
   1348 				}
   1349 				target->clone_ndx = j;
   1350 				clone = &target->clone_src->selection[j];
   1351 				clone->parts->pscheme->get_part_info(
   1352 				    clone->parts, clone->id, &info);
   1353 				target->size = info.size;
   1354 			}
   1355 		}
   1356 		target++;
   1357 		src++;
   1358 	}
   1359 	*num += add_clones;
   1360 	assert((target-ui) >= 0 && (size_t)(target-ui) == *num);
   1361 	free(*infos);
   1362 	*infos = ui;
   1363 }
   1364 #endif
   1365 
   1366 static void
   1367 apply_settings_to_partitions(struct disk_partitions *parts,
   1368     struct partition_usage_set *wanted, daddr_t start, daddr_t xsize)
   1369 {
   1370 	size_t i, exp_ndx = ~0U;
   1371 	daddr_t planned_space = 0, nsp, from, align;
   1372 	struct disk_part_info *infos;
   1373 #ifndef NO_CLONES
   1374 	struct disk_part_info cinfo, srcinfo;
   1375 	struct selected_partition *sp;
   1376 #endif
   1377 	struct disk_part_free_space space;
   1378 	struct disk_partitions *ps = NULL;
   1379 	part_id pno, new_part_id;
   1380 
   1381 #ifndef NO_CLONES
   1382 	normalize_clones(&wanted->infos, &wanted->num);
   1383 #endif
   1384 
   1385 	infos = calloc(wanted->num, sizeof(*infos));
   1386 	if (infos == NULL) {
   1387 		err_msg_win(err_outofmem);
   1388 		return;
   1389 	}
   1390 
   1391 	align = wanted->parts->pscheme->get_part_alignment(wanted->parts);
   1392 	/*
   1393 	 * Pass one: calculate space available for expanding
   1394 	 * the marked partition.
   1395 	 */
   1396 	if (parts->free_space != parts->disk_size)
   1397 		planned_space = align;	/* align first part */
   1398 	for (i = 0; i < wanted->num; i++) {
   1399 		if ((wanted->infos[i].flags & PUIFLAG_EXTEND) &&
   1400 		    exp_ndx == ~0U)
   1401 			exp_ndx = i;
   1402 		if (wanted->infos[i].flags & PUIFLG_JUST_MOUNTPOINT)
   1403 			continue;
   1404 		nsp = wanted->infos[i].size;
   1405 		if (wanted->infos[i].cur_part_id != NO_PART) {
   1406 			ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
   1407 			    parts->parent : parts;
   1408 
   1409 			ps->pscheme->get_part_info(ps,
   1410 			     wanted->infos[i].cur_part_id, &infos[i]);
   1411 			if (!(wanted->infos[i].flags & PUIFLG_IS_OUTER))
   1412 				nsp -= infos[i].size;
   1413 		}
   1414 		if (nsp <= 0)
   1415 			continue;
   1416 		planned_space += roundup(nsp, align);
   1417 	}
   1418 
   1419 	/*
   1420 	 * Expand the pool partition (or shrink, if we overran),
   1421 	 * but check size limits.
   1422 	 */
   1423 	if (exp_ndx < wanted->num) {
   1424 		daddr_t free_space = parts->free_space - planned_space;
   1425 		daddr_t new_size = wanted->infos[exp_ndx].size;
   1426 		if (free_space > 0)
   1427 			new_size += roundup(free_space,align);
   1428 
   1429 		if (wanted->infos[exp_ndx].limit > 0 &&
   1430 		    (new_size + wanted->infos[exp_ndx].cur_start)
   1431 		     > wanted->infos[exp_ndx].limit) {
   1432 			wanted->infos[exp_ndx].size =
   1433 			    wanted->infos[exp_ndx].limit
   1434 			    - wanted->infos[exp_ndx].cur_start;
   1435 		} else {
   1436 			wanted->infos[exp_ndx].size = new_size;
   1437 		}
   1438 	}
   1439 
   1440 	/*
   1441 	 * Now it gets tricky: we want the wanted partitions in order
   1442 	 * as defined, but any already existing partitions should not
   1443 	 * be moved. We allow them to change size though.
   1444 	 * To keep it simple, we just assign in order and skip blocked
   1445 	 * spaces. This may shuffle the order of the resulting partitions
   1446 	 * compared to the wanted list.
   1447 	 */
   1448 
   1449 	/* Adjust sizes of existing partitions */
   1450 	for (i = 0; i < wanted->num; i++) {
   1451 		ps = wanted->infos[i].flags & PUIFLG_IS_OUTER ?
   1452 		    parts->parent : parts;
   1453 		const struct part_usage_info *want = &wanted->infos[i];
   1454 
   1455 		if (want->cur_part_id == NO_PART)
   1456 			continue;
   1457 		if (i == exp_ndx)	/* the exp. part. can not exist yet */
   1458 			continue;
   1459 		daddr_t free_size = ps->pscheme->max_free_space_at(ps,
   1460 		    infos[i].start);
   1461 		if (free_size < wanted->infos[i].size)
   1462 			continue;
   1463 		if (infos[i].size != wanted->infos[i].size) {
   1464 			infos[i].size = wanted->infos[i].size;
   1465 			ps->pscheme->set_part_info(ps, want->cur_part_id,
   1466 			    &infos[i], NULL);
   1467 		}
   1468 	}
   1469 
   1470 	from = start > 0 ? start : -1;
   1471 	/*
   1472 	 * First add all outer partitions - we need to align those exactly
   1473 	 * with the inner counterpart later.
   1474 	 */
   1475 	if (parts->parent) {
   1476 		ps = parts->parent;
   1477 		daddr_t outer_align = ps->pscheme->get_part_alignment(ps);
   1478 
   1479 		for (i = 0; i < wanted->num; i++) {
   1480 			struct part_usage_info *want = &wanted->infos[i];
   1481 
   1482 			if (want->cur_part_id != NO_PART)
   1483 				continue;
   1484 			if (!(want->flags & PUIFLAG_ADD_OUTER))
   1485 				continue;
   1486 			if (want->size <= 0)
   1487 				continue;
   1488 
   1489 			size_t cnt = ps->pscheme->get_free_spaces(ps,
   1490 			    &space, 1, want->size-2*outer_align,
   1491 			    outer_align, from, -1);
   1492 
   1493 			if (cnt == 0)	/* no free space for this partition */
   1494 				continue;
   1495 
   1496 			infos[i].start = space.start;
   1497 			infos[i].size = min(want->size, space.size);
   1498 			infos[i].nat_type =
   1499 			    ps->pscheme->get_fs_part_type(
   1500 			        want->type, want->fs_type, want->fs_version);
   1501 			infos[i].last_mounted = want->mount;
   1502 			infos[i].fs_type = want->fs_type;
   1503 			infos[i].fs_sub_type = want->fs_version;
   1504 			infos[i].fs_opt1 = want->fs_opt1;
   1505 			infos[i].fs_opt2 = want->fs_opt2;
   1506 			infos[i].fs_opt3 = want->fs_opt3;
   1507 			new_part_id = ps->pscheme->add_partition(ps,
   1508 			    &infos[i], NULL);
   1509 			if (new_part_id == NO_PART)
   1510 				continue;	/* failed to add, skip */
   1511 
   1512 			ps->pscheme->get_part_info(ps,
   1513 			    new_part_id, &infos[i]);
   1514 			want->cur_part_id = new_part_id;
   1515 
   1516 			want->flags |= PUIFLG_ADD_INNER|PUIFLG_IS_OUTER;
   1517 			from = roundup(infos[i].start +
   1518 			    infos[i].size, outer_align);
   1519 		}
   1520 	}
   1521 
   1522 	/*
   1523 	 * Now add new inner partitions (and cloned partitions)
   1524 	 */
   1525 	for (i = 0; i < wanted->num; i++) {
   1526 
   1527 		daddr_t limit = wanted->parts->disk_size + wanted->parts->disk_start;
   1528 		if (from >= limit)
   1529 			break;
   1530 
   1531 		struct part_usage_info *want = &wanted->infos[i];
   1532 
   1533 		if (want->cur_part_id != NO_PART)
   1534 			continue;
   1535 		if (want->flags & (PUIFLG_JUST_MOUNTPOINT|PUIFLG_IS_OUTER))
   1536 			continue;
   1537 #ifndef NO_CLONES
   1538 		if ((want->flags & PUIFLG_CLONE_PARTS) &&
   1539 		    want->clone_src != NULL &&
   1540 		    want->clone_ndx < want->clone_src->num_sel) {
   1541 			sp = &want->clone_src->selection[want->clone_ndx];
   1542 			if (!sp->parts->pscheme->get_part_info(
   1543 			    sp->parts, sp->id, &srcinfo))
   1544 				continue;
   1545 			if (!wanted->parts->pscheme->
   1546 			    adapt_foreign_part_info(wanted->parts,
   1547 			    &cinfo, sp->parts->pscheme, &srcinfo))
   1548 				continue;
   1549 
   1550 			/* find space for cinfo and add a partition */
   1551 			size_t cnt = wanted->parts->pscheme->get_free_spaces(
   1552 			    wanted->parts, &space, 1, want->size-align, align,
   1553 			    from, -1);
   1554 			if (cnt == 0)
   1555 				cnt = wanted->parts->pscheme->get_free_spaces(
   1556 				    wanted->parts, &space, 1,
   1557 				    want->size-5*align, align, from, -1);
   1558 
   1559 			if (cnt == 0)
   1560 				continue; /* no free space for this clone */
   1561 
   1562 			infos[i] = cinfo;
   1563 			infos[i].start = space.start;
   1564 			new_part_id = wanted->parts->pscheme->add_partition(
   1565 			    wanted->parts, &infos[i], NULL);
   1566 		} else {
   1567 #else
   1568 		{
   1569 #endif
   1570 			if (want->size <= 0)
   1571 				continue;
   1572 			size_t cnt = wanted->parts->pscheme->get_free_spaces(
   1573 			    wanted->parts, &space, 1, want->size-align, align,
   1574 			    from, -1);
   1575 			if (cnt == 0)
   1576 				cnt = wanted->parts->pscheme->get_free_spaces(
   1577 				    wanted->parts, &space, 1,
   1578 				    want->size-5*align, align, from, -1);
   1579 
   1580 			if (cnt == 0)
   1581 				continue; /* no free space for this partition */
   1582 
   1583 			infos[i].start = space.start;
   1584 			infos[i].size = min(want->size, space.size);
   1585 			infos[i].nat_type =
   1586 			    wanted->parts->pscheme->get_fs_part_type(
   1587 			    want->type, want->fs_type, want->fs_version);
   1588 			infos[i].last_mounted = want->mount;
   1589 			infos[i].fs_type = want->fs_type;
   1590 			infos[i].fs_sub_type = want->fs_version;
   1591 			infos[i].fs_opt1 = want->fs_opt1;
   1592 			infos[i].fs_opt2 = want->fs_opt2;
   1593 			infos[i].fs_opt3 = want->fs_opt3;
   1594 			if (want->fs_type != FS_UNUSED &&
   1595 			    want->type != PT_swap) {
   1596 				want->instflags |= PUIINST_NEWFS;
   1597 				if (want->mount[0] != 0)
   1598 					want->instflags |= PUIINST_MOUNT;
   1599 			}
   1600 			new_part_id = wanted->parts->pscheme->add_partition(
   1601 			    wanted->parts, &infos[i], NULL);
   1602 		}
   1603 
   1604 		if (new_part_id == NO_PART)
   1605 			continue;	/* failed to add, skip */
   1606 
   1607 		wanted->parts->pscheme->get_part_info(
   1608 		    wanted->parts, new_part_id, &infos[i]);
   1609 		from = roundup(infos[i].start+infos[i].size, align);
   1610 	}
   1611 
   1612 
   1613 	/*
   1614 	* If there are any outer partitions that we need as inner ones
   1615 	 * too, add them to the inner partitioning scheme.
   1616 	 */
   1617 	for (i = 0; i < wanted->num; i++) {
   1618 		struct part_usage_info *want = &wanted->infos[i];
   1619 
   1620 		if (want->cur_part_id == NO_PART)
   1621 			continue;
   1622 		if (want->flags & PUIFLG_JUST_MOUNTPOINT)
   1623 			continue;
   1624 		if (want->size <= 0)
   1625 			continue;
   1626 
   1627 		if ((want->flags & (PUIFLG_ADD_INNER|PUIFLG_IS_OUTER)) !=
   1628 		    (PUIFLG_ADD_INNER|PUIFLG_IS_OUTER))
   1629 			continue;
   1630 
   1631 		new_part_id = NO_PART;
   1632 		for (part_id j = 0; new_part_id == NO_PART &&
   1633 		    j < wanted->parts->num_part; j++) {
   1634 			struct disk_part_info test;
   1635 
   1636 			if (!wanted->parts->pscheme->get_part_info(
   1637 			    wanted->parts, j, &test))
   1638 				continue;
   1639 			if (test.start == want->cur_start &&
   1640 			    test.size == want->size)
   1641 				new_part_id = j;
   1642 		}
   1643 
   1644 		if (new_part_id == NO_PART) {
   1645 			infos[i].start = want->cur_start;
   1646 			infos[i].size = want->size;
   1647 			infos[i].nat_type = wanted->parts->pscheme->
   1648 			    get_fs_part_type(want->type, want->fs_type,
   1649 			    want->fs_version);
   1650 			infos[i].last_mounted = want->mount;
   1651 			infos[i].fs_type = want->fs_type;
   1652 			infos[i].fs_sub_type = want->fs_version;
   1653 			infos[i].fs_opt1 = want->fs_opt1;
   1654 			infos[i].fs_opt2 = want->fs_opt2;
   1655 			infos[i].fs_opt3 = want->fs_opt3;
   1656 
   1657 			if (wanted->parts->pscheme->add_outer_partition
   1658 			    != NULL)
   1659 				new_part_id = wanted->parts->pscheme->
   1660 				    add_outer_partition(
   1661 				    wanted->parts, &infos[i], NULL);
   1662 			else
   1663 				new_part_id = wanted->parts->pscheme->
   1664 				    add_partition(
   1665 				    wanted->parts, &infos[i], NULL);
   1666 
   1667 			if (new_part_id == NO_PART)
   1668 				continue;	/* failed to add, skip */
   1669 		}
   1670 
   1671 		wanted->parts->pscheme->get_part_info(
   1672 		    wanted->parts, new_part_id, &infos[i]);
   1673 		want->parts = wanted->parts;
   1674 		if (want->fs_type != FS_UNUSED &&
   1675 		    want->type != PT_swap) {
   1676 			want->instflags |= PUIINST_NEWFS;
   1677 			if (want->mount[0] != 0)
   1678 				want->instflags |= PUIINST_MOUNT;
   1679 		}
   1680 	}
   1681 
   1682 	/*
   1683 	 * Note: all part_ids are invalid now, as we have added things!
   1684 	 */
   1685 	for (i = 0; i < wanted->num; i++)
   1686 		wanted->infos[i].cur_part_id = NO_PART;
   1687 	for (pno = 0; pno < parts->num_part; pno++) {
   1688 		struct disk_part_info t;
   1689 
   1690 		if (!parts->pscheme->get_part_info(parts, pno, &t))
   1691 			continue;
   1692 
   1693 		for (i = 0; i < wanted->num; i++) {
   1694 			if (wanted->infos[i].cur_part_id != NO_PART)
   1695 				continue;
   1696 			if (wanted->infos[i].size <= 0)
   1697 				continue;
   1698 			if (t.start == infos[i].start) {
   1699 				wanted->infos[i].cur_part_id = pno;
   1700 				wanted->infos[i].cur_start = infos[i].start;
   1701 				wanted->infos[i].cur_flags = infos[i].flags;
   1702 				break;
   1703 			}
   1704 		}
   1705 	}
   1706 	free(infos);
   1707 
   1708 	/* sort, and sync part ids and wanted->infos[] indices */
   1709 	sort_and_sync_parts(wanted);
   1710 }
   1711 
   1712 static void
   1713 replace_by_default(struct disk_partitions *parts,
   1714     daddr_t start, daddr_t size, struct partition_usage_set *wanted)
   1715 {
   1716 
   1717 	if (start == 0 && size == parts->disk_size)
   1718 		parts->pscheme->delete_all_partitions(parts);
   1719 	else if (parts->pscheme->delete_partitions_in_range != NULL)
   1720 		parts->pscheme->delete_partitions_in_range(parts, start, size);
   1721 	else
   1722 		assert(parts->num_part == 0);
   1723 
   1724 	fill_defaults(wanted, parts, start, size);
   1725 	apply_settings_to_partitions(parts, wanted, start, size);
   1726 }
   1727 
   1728 static bool
   1729 edit_with_defaults(struct disk_partitions *parts,
   1730     daddr_t start, daddr_t size, struct partition_usage_set *wanted)
   1731 {
   1732 	bool ok;
   1733 
   1734 	fill_defaults(wanted, parts, start, size);
   1735 	ok = get_ptn_sizes(wanted);
   1736 	if (ok)
   1737 		apply_settings_to_partitions(parts, wanted, start, size);
   1738 	return ok;
   1739 }
   1740 
   1741 /*
   1742  * md back-end code for menu-driven BSD disklabel editor.
   1743  * returns 0 on failure, 1 on success, -1 for restart.
   1744  * fills the install target with a list for newfs/fstab.
   1745  */
   1746 int
   1747 make_bsd_partitions(struct install_partition_desc *install)
   1748 {
   1749 	struct disk_partitions *parts = pm->parts;
   1750 	const struct disk_partitioning_scheme *pscheme;
   1751 	struct partition_usage_set wanted;
   1752 	daddr_t p_start, p_size;
   1753 	enum layout_type layoutkind = LY_SETSIZES;
   1754 	bool have_existing;
   1755 
   1756 	if (pm && pm->no_part && parts == NULL)
   1757 		return 1;
   1758 	if (parts == NULL) {
   1759 		pscheme = select_part_scheme(pm, NULL, !pm->no_mbr, NULL);
   1760 		if (pscheme == NULL)
   1761 			return 0;
   1762 		parts = pscheme->create_new_for_disk(pm->diskdev,
   1763 		    0, pm->dlsize, true, NULL);
   1764 		if (parts == NULL)
   1765 			return 0;
   1766 		pm->parts = parts;
   1767 	} else {
   1768 		pscheme = parts->pscheme;
   1769 	}
   1770 
   1771 	if (pscheme->secondary_partitions) {
   1772 		struct disk_partitions *p;
   1773 
   1774 		p = pscheme->secondary_partitions(parts, pm->ptstart, false);
   1775 		if (p) {
   1776 			parts = p;
   1777 			pscheme = parts->pscheme;
   1778 		}
   1779 	}
   1780 
   1781 	have_existing = check_existing_netbsd(parts);
   1782 
   1783 	/*
   1784 	 * Make sure the cylinder size multiplier/divisor and disk size are
   1785 	 * valid
   1786 	 */
   1787 	if (pm->current_cylsize == 0)
   1788 		pm->current_cylsize = pm->dlcylsize;
   1789 	if (pm->ptsize == 0)
   1790 		pm->ptsize = pm->dlsize;
   1791 
   1792 	/* Ask for layout type -- standard or special */
   1793 	if (partman_go == 0) {
   1794 		char bsd_size[6], min_size[6], x_size[6];
   1795 
   1796 		humanize_number(bsd_size, sizeof(bsd_size),
   1797 		    (uint64_t)pm->ptsize*pm->sectorsize,
   1798 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1799 		humanize_number(min_size, sizeof(min_size),
   1800 		    (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE)*MEG,
   1801 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1802 		humanize_number(x_size, sizeof(x_size),
   1803 		    (uint64_t)(DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE
   1804 		    + XNEEDMB)*MEG,
   1805 		    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1806 
   1807 		msg_display_subst(
   1808 		    have_existing ? MSG_layout_prologue_existing
   1809 		    : MSG_layout_prologue_none, 6, pm->diskdev,
   1810 		    msg_string(parts->pscheme->name),
   1811 		    msg_string(parts->pscheme->short_name),
   1812 		    bsd_size, min_size, x_size);
   1813 		msg_display_add_subst(MSG_layout_main, 6,
   1814 		    pm->diskdev,
   1815 		    msg_string(parts->pscheme->name),
   1816 		    msg_string(parts->pscheme->short_name),
   1817 		    bsd_size, min_size, x_size);
   1818 		msg_display_add("\n\n");
   1819 		layoutkind = ask_layout(parts, have_existing);
   1820 		if (layoutkind == LY_ERROR)
   1821 			return 0;
   1822 	}
   1823 
   1824 	if (layoutkind == LY_USEDEFAULT || layoutkind == LY_SETSIZES) {
   1825 		/* calc available disk area for the NetBSD partitions */
   1826 		p_start = pm->ptstart;
   1827 		p_size = pm->ptsize;
   1828 		if (parts->parent != NULL &&
   1829 		    parts->parent->pscheme->guess_install_target != NULL)
   1830 			parts->parent->pscheme->guess_install_target(
   1831 			    parts->parent, &p_start, &p_size);
   1832 	}
   1833 	if (layoutkind == LY_OTHERSCHEME) {
   1834 		parts->pscheme->destroy_part_scheme(parts);
   1835 		return -1;
   1836 	} else if (layoutkind == LY_USEDEFAULT) {
   1837 		replace_by_default(parts, p_start, p_size,
   1838 		    &wanted);
   1839 	} else if (layoutkind == LY_SETSIZES) {
   1840 		if (!edit_with_defaults(parts, p_start, p_size,
   1841 		    &wanted)) {
   1842 			free_usage_set(&wanted);
   1843 			return 0;
   1844 		}
   1845 	} else {
   1846 		usage_set_from_parts(&wanted, parts);
   1847 	}
   1848 
   1849 	/*
   1850 	 * Make sure the target root partition is properly marked,
   1851 	 * check for existing EFI boot partition.
   1852 	 */
   1853 	bool have_inst_target = false;
   1854 #ifdef HAVE_EFI_BOOT
   1855 	daddr_t target_start = -1;
   1856 #endif
   1857 	for (size_t i = 0; i < wanted.num; i++) {
   1858 		if (wanted.infos[i].cur_flags & PTI_INSTALL_TARGET) {
   1859 			have_inst_target = true;
   1860 #ifdef HAVE_EFI_BOOT
   1861 			target_start = wanted.infos[i].cur_start;
   1862 #endif
   1863 			break;
   1864 		 }
   1865 	}
   1866 	if (!have_inst_target) {
   1867 		for (size_t i = 0; i < wanted.num; i++) {
   1868 			struct disk_part_info info;
   1869 
   1870 			if (wanted.infos[i].type != PT_root ||
   1871 			    strcmp(wanted.infos[i].mount, "/") != 0)
   1872 				continue;
   1873 			wanted.infos[i].cur_flags |= PTI_INSTALL_TARGET;
   1874 
   1875 			if (!wanted.parts->pscheme->get_part_info(wanted.parts,
   1876 			    wanted.infos[i].cur_part_id, &info))
   1877 				break;
   1878 			info.flags |= PTI_INSTALL_TARGET;
   1879 			wanted.parts->pscheme->set_part_info(wanted.parts,
   1880 			    wanted.infos[i].cur_part_id, &info, NULL);
   1881 #ifdef HAVE_EFI_BOOT
   1882 			target_start = wanted.infos[i].cur_start;
   1883 #endif
   1884 			break;
   1885 		}
   1886 	}
   1887 #ifdef HAVE_EFI_BOOT
   1888 	size_t boot_part = ~0U;
   1889 	for (part_id i = 0; i < wanted.num; i++) {
   1890 		if ((wanted.infos[i].cur_flags & PTI_BOOT) != 0 ||
   1891 		    wanted.infos[i].type ==  PT_EFI_SYSTEM) {
   1892 			boot_part = i;
   1893 			break;
   1894 		}
   1895 	}
   1896 	if (boot_part == ~0U) {
   1897 		for (part_id i = 0; i < wanted.num; i++) {
   1898 			/*
   1899 			 * heuristic to recognize existing MBR FAT
   1900 			 * partitions as EFI without looking for
   1901 			 * details
   1902 			 */
   1903 			if ((wanted.infos[i].type != PT_FAT &&
   1904 			    wanted.infos[i].type != PT_EFI_SYSTEM) ||
   1905 			    wanted.infos[i].fs_type != FS_MSDOS)
   1906 				continue;
   1907 			daddr_t ps = wanted.infos[i].cur_start;
   1908 			daddr_t pe = ps + wanted.infos[i].size;
   1909 			if (target_start >= 0 &&
   1910 			   (ps >= target_start || pe >= target_start))
   1911 				continue;
   1912 			boot_part = i;
   1913 			break;
   1914 		}
   1915 	}
   1916 	if (boot_part != ~0U) {
   1917 		struct disk_part_info info;
   1918 
   1919 		if (wanted.parts->pscheme->get_part_info(wanted.parts,
   1920 		    wanted.infos[boot_part].cur_part_id, &info)) {
   1921 			info.flags |= PTI_BOOT;
   1922 			wanted.parts->pscheme->set_part_info(wanted.parts,
   1923 			    wanted.infos[boot_part].cur_part_id, &info, NULL);
   1924 		}
   1925 		wanted.infos[boot_part].instflags |= PUIINST_BOOT;
   1926 	}
   1927 #endif
   1928 
   1929 	/*
   1930 	 * OK, we have a partition table. Give the user the chance to
   1931 	 * edit it and verify it's OK, or abort altogether.
   1932 	 */
   1933  	for (;;) {
   1934 		int rv = edit_and_check_label(pm, &wanted, true);
   1935 		if (rv == 0) {
   1936 			msg_display(MSG_abort_part);
   1937 			free_usage_set(&wanted);
   1938 			return 0;
   1939 		}
   1940 		/* update install infos */
   1941 		install->num = wanted.num;
   1942 		install->infos = wanted.infos;
   1943 		install->write_back = wanted.write_back;
   1944 		install->num_write_back = wanted.num_write_back;
   1945 		/* and check them */
   1946 		if (check_partitions(install))
   1947 			break;
   1948 	}
   1949 
   1950 	/* we moved infos from wanted to install target */
   1951 	wanted.infos = NULL;
   1952 	wanted.write_back = NULL;
   1953 	free_usage_set(&wanted);
   1954 
   1955 	/* Everything looks OK. */
   1956 	return 1;
   1957 }
   1958 
   1959 #ifndef MD_NEED_BOOTBLOCK
   1960 #define MD_NEED_BOOTBLOCK(A)	true
   1961 #endif
   1962 
   1963 /*
   1964  * check that there is at least a / somewhere.
   1965  */
   1966 bool
   1967 check_partitions(struct install_partition_desc *install)
   1968 {
   1969 #ifdef HAVE_BOOTXX_xFS
   1970 	int rv = 1;
   1971 	char *bootxx;
   1972 #endif
   1973 #ifndef HAVE_UFS2_BOOT
   1974 	size_t i;
   1975 #endif
   1976 
   1977 #ifdef HAVE_BOOTXX_xFS
   1978 	if (MD_NEED_BOOTBLOCK(install)) {
   1979 		/* check if we have boot code for the root partition type */
   1980 		bootxx = bootxx_name(install);
   1981 		if (bootxx != NULL) {
   1982 			rv = access(bootxx, R_OK);
   1983 			free(bootxx);
   1984 		} else
   1985 			rv = -1;
   1986 		if (rv != 0) {
   1987 			hit_enter_to_continue(NULL, MSG_No_Bootcode);
   1988 			return false;
   1989 		}
   1990 	}
   1991 #endif
   1992 #ifndef HAVE_UFS2_BOOT
   1993 	if (MD_NEED_BOOTBLOCK(install)) {
   1994 		for (i = 0; i < install->num; i++) {
   1995 			if (install->infos[i].type != PT_root)
   1996 				continue;
   1997 			if (strcmp(install->infos[i].mount, "/") != 0)
   1998 				continue;
   1999 			if (install->infos[i].fs_type != FS_BSDFFS)
   2000 				continue;
   2001 			if (install->infos[i].fs_version < 2)
   2002 				continue;
   2003 			hit_enter_to_continue(NULL, MSG_cannot_ufs2_root);
   2004 			return false;
   2005 		}
   2006 	}
   2007 #endif
   2008 
   2009 	return md_check_partitions(install);
   2010 }
   2011