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