Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.34
      1 /*	$NetBSD: label.c,v 1.34 2022/06/09 18:26:06 martin Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Jonathan Stone
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Jonathan Stone.
     19  * 4. The name of Jonathan Stone may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
     24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     33  * THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 __RCSID("$NetBSD: label.c,v 1.34 2022/06/09 18:26:06 martin Exp $");
     40 #endif
     41 
     42 #include <sys/types.h>
     43 #include <stddef.h>
     44 #include <assert.h>
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <fcntl.h>
     48 #include <util.h>
     49 #include <unistd.h>
     50 #include <sys/dkio.h>
     51 #include <sys/param.h>
     52 #include <sys/bootblock.h>
     53 #include <sys/bitops.h>
     54 #include <ufs/ffs/fs.h>
     55 
     56 #include "defs.h"
     57 #include "msg_defs.h"
     58 #include "menu_defs.h"
     59 
     60 /*
     61  * local prototypes
     62  */
     63 static bool boringpart(const struct disk_part_info *info);
     64 static bool checklabel(struct disk_partitions*, char *, char *);
     65 static void show_partition_adder(menudesc *, struct partition_usage_set*);
     66 
     67 /*
     68  * Return 1 if a partition should be ignored when checking
     69  * for overlapping partitions.
     70  */
     71 static bool
     72 boringpart(const struct disk_part_info *info)
     73 {
     74 
     75 	if (info->size == 0)
     76 		return true;
     77 	if (info->flags &
     78 	     (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
     79 	     PTI_RAW_PART))
     80 		return true;
     81 
     82 	return false;
     83 }
     84 
     85 /*
     86  * We have some partitions in our "wanted" list that we may not edit,
     87  * like the RAW_PART in disklabel, some that just represent external
     88  * mount entries for the final fstab or similar.
     89  * We have previously sorted pset->parts and pset->infos to be in sync,
     90  * but the former "array" may be shorter.
     91  * Here are a few quick predicates to check for them.
     92  */
     93 static bool
     94 real_partition(const struct partition_usage_set *pset, int index)
     95 {
     96 	if (index < 0 || (size_t)index >= pset->num)
     97 		return false;
     98 
     99 	return pset->infos[index].cur_part_id != NO_PART;
    100 }
    101 
    102 /*
    103  * Check partitioning for overlapping partitions.
    104  * Returns 0 if no overlapping partition found, nonzero otherwise.
    105  * Sets reference arguments ovly1 and ovly2 to the indices of
    106  * overlapping partitions if any are found.
    107  */
    108 static bool
    109 checklabel(struct disk_partitions *parts,
    110     char *ovl1, char *ovl2)
    111 {
    112 	part_id i, j;
    113 	struct disk_part_info info;
    114 	daddr_t istart, iend, jstart, jend;
    115 	unsigned int fs_type, fs_sub_type;
    116 
    117 	for (i = 0; i < parts->num_part - 1; i ++ ) {
    118 		if (!parts->pscheme->get_part_info(parts, i, &info))
    119 			continue;
    120 
    121 		/* skip unused or reserved partitions */
    122 		if (boringpart(&info))
    123 			continue;
    124 
    125 		/*
    126 		 * check succeeding partitions for overlap.
    127 		 * O(n^2), but n is small.
    128 		 */
    129 		istart = info.start;
    130 		iend = istart + info.size;
    131 		fs_type = info.fs_type;
    132 		fs_sub_type = info.fs_sub_type;
    133 
    134 		for (j = i+1; j < parts->num_part; j++) {
    135 
    136 			if (!parts->pscheme->get_part_info(parts, j, &info))
    137 				continue;
    138 
    139 			/* skip unused or reserved partitions */
    140 			if (boringpart(&info))
    141 				continue;
    142 
    143 			jstart = info.start;
    144 			jend = jstart + info.size;
    145 
    146 			/* overlap? */
    147 			if ((istart <= jstart && jstart < iend) ||
    148 			    (jstart <= istart && istart < jend)) {
    149 				snprintf(ovl1, MENUSTRSIZE,
    150 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    151 				    istart / sizemult, iend / sizemult,
    152 				    multname,
    153 				    getfslabelname(fs_type, fs_sub_type));
    154 				snprintf(ovl2, MENUSTRSIZE,
    155 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    156 				    jstart / sizemult, jend / sizemult,
    157 				    multname,
    158 				    getfslabelname(info.fs_type,
    159 				        info.fs_sub_type));
    160 				return false;
    161 			}
    162 		}
    163 	}
    164 
    165 	return true;
    166 }
    167 
    168 int
    169 checkoverlap(struct disk_partitions *parts)
    170 {
    171 	char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
    172 	if (!checklabel(parts, desc1, desc2)) {
    173 		msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
    174 		return 1;
    175 	}
    176 	return 0;
    177 }
    178 
    179 /*
    180  * return (see post_edit_verify):
    181  *  0 -> abort
    182  *  1 -> re-edit
    183  *  2 -> continue installation
    184  */
    185 static int
    186 verify_parts(struct partition_usage_set *pset, bool install)
    187 {
    188 	struct part_usage_info *wanted;
    189 	struct disk_partitions *parts;
    190 	size_t i, num_root;
    191 	daddr_t first_bsdstart, inst_start;
    192 	int rv;
    193 
    194 	first_bsdstart = inst_start = -1;
    195 	num_root = 0;
    196 	parts = pset->parts;
    197 	for (i = 0; i < pset->num; i++) {
    198 		wanted = &pset->infos[i];
    199 
    200 		if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
    201 			continue;
    202 		if (wanted->cur_part_id == NO_PART)
    203 			continue;
    204 		if (!(wanted->instflags & PUIINST_MOUNT))
    205 			continue;
    206 		if (strcmp(wanted->mount, "/") != 0)
    207 			continue;
    208 		num_root++;
    209 
    210 		if (first_bsdstart <= 0) {
    211 			first_bsdstart = wanted->cur_start;
    212 		}
    213 		if (inst_start < 0 &&
    214 		    (wanted->cur_flags & PTI_INSTALL_TARGET)) {
    215 			inst_start = wanted->cur_start;
    216 		}
    217 	}
    218 
    219 	if ((num_root == 0 && install) ||
    220 	    (num_root > 1 && inst_start < 0)) {
    221 		if (num_root == 0 && install)
    222 			msg_display_subst(MSG_must_be_one_root, 2,
    223 			    msg_string(parts->pscheme->name),
    224 			    msg_string(parts->pscheme->short_name));
    225 		else
    226 			msg_display_subst(MSG_multbsdpart, 2,
    227 			    msg_string(parts->pscheme->name),
    228 			    msg_string(parts->pscheme->short_name));
    229 		rv = ask_reedit(parts);
    230 		if (rv != 2)
    231 			return rv;
    232 	}
    233 
    234 	/* Check for overlaps */
    235 	if (checkoverlap(parts) != 0) {
    236 		rv = ask_reedit(parts);
    237 		if (rv != 2)
    238 			return rv;
    239 	}
    240 
    241 	/*
    242 	 * post_edit_verify returns:
    243 	 *  0 -> abort
    244 	 *  1 -> re-edit
    245 	 *  2 -> continue installation
    246 	 */
    247 	if (parts->pscheme->post_edit_verify)
    248 		return parts->pscheme->post_edit_verify(parts, false);
    249 
    250 	return 2;
    251 }
    252 
    253 static int
    254 edit_fs_start(menudesc *m, void *arg)
    255 {
    256 	struct single_part_fs_edit *edit = arg;
    257 	daddr_t start, end;
    258 
    259 	start = getpartoff(edit->pset->parts, edit->info.start);
    260 	if (edit->info.size != 0) {
    261 		/* Try to keep end in the same place */
    262 		end = edit->info.start + edit->info.size;
    263 		if (end < start)
    264 			edit->info.size = edit->pset->parts->pscheme->
    265 			    max_free_space_at(edit->pset->parts,
    266 			    edit->info.start);
    267 		else
    268 			edit->info.size = end - start;
    269 	}
    270 	edit->info.start = start;
    271 	return 0;
    272 }
    273 
    274 static int
    275 edit_fs_size(menudesc *m, void *arg)
    276 {
    277 	struct single_part_fs_edit *edit = arg;
    278 	struct disk_part_info pinfo;
    279 	daddr_t size;
    280 
    281 	/* get original partition data, in case start moved already */
    282 	edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
    283 	    edit->id, &pinfo);
    284 	/* ask for new size with old start and current values */
    285 	size = getpartsize(edit->pset->parts, pinfo.start,
    286 	    edit->info.start, edit->info.size);
    287 	if (size < 0)
    288 		return 0;
    289 	if (size > edit->pset->parts->disk_size)
    290 		size = edit->pset->parts->disk_size - edit->info.start;
    291 	edit->info.size = size;
    292 	return 0;
    293 }
    294 
    295 static int
    296 set_ffs_opt_pow2(menudesc *m, void *arg)
    297 {
    298 	struct single_part_fs_edit *edit = arg;
    299 	size_t val = 1 << (edit->offset+m->cursel);
    300 
    301 	if (edit->mode == 1) {
    302 		edit->info.fs_opt1 = val;
    303 		edit->wanted->fs_opt1 = val;
    304 	} else if (edit->mode == 2) {
    305 		edit->info.fs_opt2 = val;
    306 		edit->wanted->fs_opt2 = val;
    307 	}
    308 	return 0;
    309 }
    310 
    311 static int
    312 edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
    313     size_t min_val, size_t max_val)
    314 {
    315 	struct single_part_fs_edit *edit = arg;
    316 	menu_ent opts[min(MAXPHYS/4096, 8)];
    317 	char names[min(MAXPHYS/4096, 8)][20];
    318 	size_t i, val;
    319 	int menu;
    320 
    321 	edit->offset = ilog2(min_val);
    322 	memset(opts, 0, sizeof opts);
    323 	for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
    324 		snprintf(names[i], sizeof names[i], "%zu", val);
    325 		opts[i].opt_name = names[i];
    326 		opts[i].opt_action = set_ffs_opt_pow2;
    327 		opts[i].opt_flags = OPT_EXIT;
    328 	}
    329 	menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
    330 	    NULL, NULL, NULL, NULL, NULL);
    331 	if (menu < 0)
    332 		return 1;
    333 	process_menu(menu, arg);
    334 	free_menu(menu);
    335 	return 0;
    336 }
    337 
    338 static int
    339 edit_fs_ffs_block(menudesc *m, void *arg)
    340 {
    341 	struct single_part_fs_edit *edit = arg;
    342 
    343 	edit->mode = 1;		/* edit fs_opt1 */
    344 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
    345 	    4096, MAXPHYS);
    346 }
    347 
    348 static int
    349 edit_fs_ffs_frag(menudesc *m, void *arg)
    350 {
    351 	struct single_part_fs_edit *edit = arg;
    352 	size_t bsize, sec_size;
    353 
    354 	edit->mode = 2;		/* edit fs_opt2 */
    355 	bsize = edit->info.fs_opt1;
    356 	if (bsize == 0) {
    357 		sec_size = edit->wanted->parts->bytes_per_sector;
    358 		if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
    359 			bsize = 32*1024;
    360 		else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
    361 			bsize = 16*1024;
    362 		else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
    363 			bsize = 8*1024;
    364 		else
    365 			bsize = 4+1024;
    366 	}
    367 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
    368 		bsize / 8, bsize);
    369 }
    370 
    371 static int
    372 edit_fs_ffs_avg_size(menudesc *m, void *arg)
    373 {
    374 	struct single_part_fs_edit *edit = arg;
    375 	char answer[12];
    376 
    377 	snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
    378 	msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
    379 		answer, answer, sizeof answer);
    380 	edit->info.fs_opt3 = atol(answer);
    381 	edit->wanted->fs_opt3 = edit->info.fs_opt3;
    382 
    383 	return 0;
    384 }
    385 
    386 static int
    387 edit_fs_preserve(menudesc *m, void *arg)
    388 {
    389 	struct single_part_fs_edit *edit = arg;
    390 
    391 	edit->wanted->instflags ^= PUIINST_NEWFS;
    392 	return 0;
    393 }
    394 
    395 static int
    396 edit_install(menudesc *m, void *arg)
    397 {
    398 	struct single_part_fs_edit *edit = arg;
    399 
    400 	edit->info.flags ^= PTI_INSTALL_TARGET;
    401 	return 0;
    402 }
    403 
    404 static int
    405 edit_fs_mount(menudesc *m, void *arg)
    406 {
    407 	struct single_part_fs_edit *edit = arg;
    408 
    409 	edit->wanted->instflags ^= PUIINST_MOUNT;
    410 	return 0;
    411 }
    412 
    413 static int
    414 edit_fs_mountpt(menudesc *m, void *arg)
    415 {
    416 	struct single_part_fs_edit *edit = arg;
    417 	char *p, *first, *last, buf[MOUNTLEN];
    418 
    419 	strlcpy(buf, edit->wanted->mount, sizeof buf);
    420 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
    421 		buf, buf, MOUNTLEN);
    422 
    423 	/*
    424 	 * Trim all leading and trailing whitespace
    425 	 */
    426 	for (first = NULL, last = NULL, p = buf; *p; p++) {
    427 		if (isspace((unsigned char)*p))
    428 			continue;
    429 		if (first == NULL)
    430 			first = p;
    431 		last = p;
    432 	}
    433 	if (last != NULL)
    434 		last[1] = 0;
    435 
    436 	if (first == NULL || *first == 0 || strcmp(first, "none") == 0) {
    437 		edit->wanted->mount[0] = 0;
    438 		edit->wanted->instflags &= ~PUIINST_MOUNT;
    439 		return 0;
    440 	}
    441 
    442 	if (*first != '/') {
    443 		edit->wanted->mount[0] = '/';
    444 		strlcpy(&edit->wanted->mount[1], first,
    445 		    sizeof(edit->wanted->mount)-1);
    446 	} else {
    447 		strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
    448 	}
    449 
    450 	return 0;
    451 }
    452 
    453 static int
    454 edit_restore(menudesc *m, void *arg)
    455 {
    456 	struct single_part_fs_edit *edit = arg;
    457 
    458 	edit->info = edit->old_info;
    459 	*edit->wanted = edit->old_usage;
    460 	return 0;
    461 }
    462 
    463 static int
    464 edit_cancel(menudesc *m, void *arg)
    465 {
    466 	struct single_part_fs_edit *edit = arg;
    467 
    468 	edit->rv = -1;
    469 	return 1;
    470 }
    471 
    472 static int
    473 edit_delete_ptn(menudesc *m, void *arg)
    474 {
    475 	struct single_part_fs_edit *edit = arg;
    476 
    477 	edit->rv = -2;
    478 	return 1;
    479 }
    480 
    481 /*
    482  * We have added/removed partitions, all cur_part_id values are
    483  * out of sync. Re-fetch and reorder partitions accordingly.
    484  */
    485 static void
    486 renumber_partitions(struct partition_usage_set *pset)
    487 {
    488 	struct part_usage_info *ninfos;
    489 	struct disk_part_info info;
    490 	size_t i;
    491 	part_id pno;
    492 
    493 	ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
    494 	if (ninfos == NULL) {
    495 		err_msg_win(err_outofmem);
    496 		return;
    497 	}
    498 
    499 	for (pno = 0; pno < pset->parts->num_part; pno++) {
    500 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
    501 		    &info))
    502 			continue;
    503 		for (i = 0; i < pset->parts->num_part; i++) {
    504 			if (pset->infos[i].cur_start != info.start)
    505 				continue;
    506 			if (pset->infos[i].cur_flags != info.flags)
    507 				continue;
    508 			if ((info.fs_type != FS_UNUSED &&
    509 			    info.fs_type == pset->infos[i].fs_type) ||
    510 			    (pset->infos[i].type ==
    511 			    info.nat_type->generic_ptype)) {
    512 				memcpy(&ninfos[pno], &pset->infos[i],
    513 				    sizeof(ninfos[pno]));
    514 				ninfos[pno].cur_part_id = pno;
    515 				break;
    516 			}
    517 		}
    518 	}
    519 
    520 	memcpy(pset->infos, ninfos, sizeof(*pset->infos)*pset->parts->num_part);
    521 	free(ninfos);
    522 }
    523 
    524 /*
    525  * Most often used file system types, we offer them in a first level menu.
    526  */
    527 static const uint edit_fs_common_types[] =
    528     { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_EFI_SP, FS_BSDLFS, FS_EX2FS };
    529 
    530 /*
    531  * Functions for uncommon file system types - we offer the full list,
    532  * but put FFSv2 and FFSv1 at the front and duplicat FS_MSDOS as
    533  * EFI system partition.
    534  */
    535 static void
    536 init_fs_type_ext(menudesc *menu, void *arg)
    537 {
    538 	struct single_part_fs_edit *edit = arg;
    539 	uint t = edit->info.fs_type;
    540 	size_t i, ndx, max = menu->numopts;
    541 
    542 	if (t == FS_BSDFFS) {
    543 		if (edit->info.fs_sub_type == 2)
    544 			menu->cursel = 0;
    545 		else
    546 			menu->cursel = 1;
    547 		return;
    548 	} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
    549 		menu->cursel = FSMAXTYPES;
    550 		return;
    551 	}
    552 	/* skip the two FFS entries, and do not add FFS later again */
    553 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    554 		if (i == FS_UNUSED)
    555 			continue;
    556 		if (i == FS_BSDFFS)
    557 			continue;
    558 		if (fstypenames[i] == NULL)
    559 			continue;
    560 
    561 		if (i == t) {
    562 			menu->cursel = ndx;
    563 			break;
    564 		}
    565 		if (i == FS_MSDOS) {
    566 			ndx++;
    567 			if (t == FS_EFI_SP) {
    568 				menu->cursel = ndx;
    569 				break;
    570 			}
    571 		}
    572 		ndx++;
    573 	}
    574 }
    575 
    576 static int
    577 set_fstype_ext(menudesc *menu, void *arg)
    578 {
    579 	struct single_part_fs_edit *edit = arg;
    580 	size_t i, ndx, max = menu->numopts;
    581 	enum part_type pt;
    582 
    583 	if (menu->cursel == 0 || menu->cursel == 1) {
    584 		edit->info.fs_type = FS_BSDFFS;
    585 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    586 		goto found_type;
    587 	} else if (menu->cursel == FSMAXTYPES) {
    588 		edit->info.fs_type = FS_EX2FS;
    589 		edit->info.fs_sub_type = 1;
    590 		goto found_type;
    591 	}
    592 
    593 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    594 		if (i == FS_UNUSED)
    595 			continue;
    596 		if (i == FS_BSDFFS)
    597 			continue;
    598 		if (fstypenames[i] == NULL)
    599 			continue;
    600 
    601 		if (ndx == (size_t)menu->cursel) {
    602 			edit->info.fs_type = i;
    603 			edit->info.fs_sub_type = 0;
    604 			goto found_type;
    605 		}
    606 		ndx++;
    607 		if (i == FS_MSDOS) {
    608 			ndx++;
    609 			if (ndx == (size_t)menu->cursel) {
    610 				edit->info.fs_type = FS_EFI_SP;
    611 				edit->info.fs_sub_type = 0;
    612 				goto found_type;
    613 			}
    614 		}
    615 	}
    616 	return 1;
    617 
    618 found_type:
    619 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
    620 	edit->info.nat_type = edit->pset->parts->pscheme->
    621 	    get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
    622 	if (edit->info.nat_type == NULL)
    623 		edit->info.nat_type = edit->pset->parts->pscheme->
    624 		    get_generic_part_type(PT_root);
    625 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    626 	edit->wanted->fs_type = edit->info.fs_type;
    627 	edit->wanted->fs_version = edit->info.fs_sub_type;
    628 	return 1;
    629 }
    630 
    631 /*
    632  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
    633  * skip later FFS entry in the generic list.
    634  */
    635 static int
    636 edit_fs_type_ext(menudesc *menu, void *arg)
    637 {
    638 	menu_ent *opts;
    639 	int m;
    640 	size_t i, ndx, cnt;
    641 
    642 	cnt = __arraycount(fstypenames)+1;
    643 	opts = calloc(cnt, sizeof(*opts));
    644 	if (opts == NULL)
    645 		return 1;
    646 
    647 	ndx = 0;
    648 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
    649 	opts[ndx].opt_action = set_fstype_ext;
    650 	ndx++;
    651 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
    652 	opts[ndx].opt_action = set_fstype_ext;
    653 	ndx++;
    654 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
    655 		if (i == FS_UNUSED)
    656 			continue;
    657 		if (i == FS_BSDFFS)
    658 			continue;
    659 		if (fstypenames[i] == NULL)
    660 			continue;
    661 		opts[ndx].opt_name = fstypenames[i];
    662 		opts[ndx].opt_action = set_fstype_ext;
    663 		ndx++;
    664 		if (i == FS_MSDOS) {
    665 			opts[ndx] = opts[ndx-1];
    666 			opts[ndx].opt_name = getfslabelname(FS_EFI_SP, 0);
    667 			ndx++;
    668 		}
    669 	}
    670 	opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
    671 	opts[ndx].opt_action = set_fstype_ext;
    672 	ndx++;
    673 	assert(ndx == cnt);
    674 	m = new_menu(MSG_Select_the_type, opts, ndx,
    675 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    676 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
    677 
    678 	if (m < 0)
    679 		return 1;
    680 	process_menu(m, arg);
    681 	free_menu(m);
    682 	free(opts);
    683 
    684 	return 1;
    685 }
    686 
    687 static void
    688 init_fs_type(menudesc *menu, void *arg)
    689 {
    690 	struct single_part_fs_edit *edit = arg;
    691 	size_t i;
    692 
    693 	/* init menu->cursel from fs type in arg */
    694 	if (edit->info.fs_type == FS_BSDFFS) {
    695 		if (edit->info.fs_sub_type == 2)
    696 			menu->cursel = 0;
    697 		else
    698 			menu->cursel = 1;
    699 	}
    700 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
    701 		if (edit->info.fs_type == edit_fs_common_types[i]) {
    702 			menu->cursel = i+1;
    703 			break;
    704 		}
    705 	}
    706 }
    707 
    708 static int
    709 set_fstype(menudesc *menu, void *arg)
    710 {
    711 	struct single_part_fs_edit *edit = arg;
    712 	enum part_type pt;
    713 	int ndx;
    714 
    715 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
    716 	if (menu->cursel < 2) {
    717 		edit->info.fs_type = FS_BSDFFS;
    718 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    719 		edit->info.nat_type = edit->pset->parts->pscheme->
    720 		    get_fs_part_type(pt, FS_BSDFFS, 2);
    721 		if (edit->info.nat_type == NULL)
    722 			edit->info.nat_type = edit->pset->parts->
    723 			    pscheme->get_generic_part_type(PT_root);
    724 		edit->wanted->type = edit->info.nat_type->generic_ptype;
    725 		edit->wanted->fs_type = edit->info.fs_type;
    726 		edit->wanted->fs_version = edit->info.fs_sub_type;
    727 		return 1;
    728 	}
    729 	ndx = menu->cursel-1;
    730 
    731 	if (ndx < 0 ||
    732 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
    733 		return 1;
    734 
    735 	edit->info.fs_type = edit_fs_common_types[ndx];
    736 	edit->info.fs_sub_type = 0;
    737 	edit->info.nat_type = edit->pset->parts->pscheme->
    738 	    get_fs_part_type(pt, edit->info.fs_type, 0);
    739 	if (edit->info.nat_type == NULL)
    740 		edit->info.nat_type = edit->pset->parts->
    741 		    pscheme->get_generic_part_type(PT_root);
    742 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    743 	edit->wanted->fs_type = edit->info.fs_type;
    744 	edit->wanted->fs_version = edit->info.fs_sub_type;
    745 	return 1;
    746 }
    747 
    748 /*
    749  * Offer a menu selecting the common file system types
    750  */
    751 static int
    752 edit_fs_type(menudesc *menu, void *arg)
    753 {
    754 	struct single_part_fs_edit *edit = arg;
    755 	menu_ent *opts;
    756 	int m, cnt;
    757 	size_t i;
    758 
    759 	/*
    760 	 * Shortcut to full menu if we have an exotic value
    761 	 */
    762 	if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
    763 		edit_fs_type_ext(menu, arg);
    764 		return 0;
    765 	}
    766 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
    767 		if (edit->info.fs_type == edit_fs_common_types[i])
    768 			break;
    769 	if (i >= __arraycount(edit_fs_common_types)) {
    770 		edit_fs_type_ext(menu, arg);
    771 		return 0;
    772 	}
    773 
    774 	/*
    775 	 * Starting with a common type, show short menu first
    776 	 */
    777 	cnt = __arraycount(edit_fs_common_types) + 2;
    778 	opts = calloc(cnt, sizeof(*opts));
    779 	if (opts == NULL)
    780 		return 0;
    781 
    782 	/* special case entry 0: two FFS entries */
    783 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
    784 		opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
    785 		opts[i+1].opt_action = set_fstype;
    786 	}
    787 	/* duplicate FFS (at offset 1) into first entry */
    788 	opts[0] = opts[1];
    789 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
    790 	opts[1].opt_name = msg_string(MSG_fs_type_ffs);
    791 	/* add secondary sub-menu */
    792 	assert(i+1 < (size_t)cnt);
    793 	opts[i+1].opt_name = msg_string(MSG_other_fs_type);
    794 	opts[i+1].opt_action = edit_fs_type_ext;
    795 
    796 	m = new_menu(MSG_Select_the_type, opts, cnt,
    797 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
    798 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
    799 
    800 	if (m < 0)
    801 		return 0;
    802 	process_menu(m, arg);
    803 	free_menu(m);
    804 	free(opts);
    805 
    806 	return 0;
    807 }
    808 
    809 
    810 static void update_edit_ptn_menu(menudesc *m, void *arg);
    811 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
    812 static int edit_ptn_custom_type(menudesc *m, void *arg);
    813 
    814 static void
    815 remember_deleted(struct partition_usage_set *pset,
    816     struct disk_partitions *parts)
    817 {
    818 	size_t i, num;
    819 	struct disk_partitions **tab;
    820 
    821 	/* do we have parts on record already? */
    822 	for (i = 0; i < pset->num_write_back; i++)
    823 		if (pset->write_back[i] == parts)
    824 			return;
    825 	/*
    826 	 * Need to record this partition table for write back
    827 	 */
    828 	num = pset->num_write_back + 1;
    829 	tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
    830 	if (!tab)
    831 		return;
    832 	tab[pset->num_write_back] = parts;
    833 	pset->write_back = tab;
    834 	pset->num_write_back = num;
    835 }
    836 
    837 int
    838 edit_ptn(menudesc *menu, void *arg)
    839 {
    840 	struct partition_usage_set *pset = arg;
    841 	struct single_part_fs_edit edit;
    842 	int fspart_menu, num_opts;
    843 	const char *err;
    844 	menu_ent *mopts, *popt;
    845 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
    846 
    847 	static const menu_ent edit_ptn_fields_head[] = {
    848 		{ .opt_action=edit_fs_type },
    849 		{ .opt_action=edit_fs_start },
    850 		{ .opt_action=edit_fs_size },
    851 		{ .opt_flags=OPT_IGNORE },
    852 	};
    853 
    854 	static const menu_ent edit_ptn_fields_head_add[] = {
    855 		{ .opt_action=edit_install },
    856 	};
    857 
    858 	static const menu_ent edit_ptn_fields_head2[] = {
    859 		{ .opt_action=edit_fs_preserve },
    860 		{ .opt_action=edit_fs_mount },
    861 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
    862 		{ .opt_action=edit_fs_mountpt },
    863 	};
    864 
    865 	static const menu_ent edit_ptn_fields_ffs[] = {
    866 		{ .opt_action=edit_fs_ffs_avg_size },
    867 		{ .opt_action=edit_fs_ffs_block },
    868 		{ .opt_action=edit_fs_ffs_frag },
    869 	};
    870 
    871 	static const menu_ent edit_ptn_fields_tail[] = {
    872 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    873 		  .opt_flags=OPT_SUB },
    874 		{ .opt_name=MSG_restore,
    875 		  .opt_action=edit_restore},
    876 		{ .opt_name=MSG_Delete_partition,
    877 		  .opt_action=edit_delete_ptn},
    878 		{ .opt_name=MSG_cancel,
    879 		  .opt_action=edit_cancel},
    880 	};
    881 
    882 	memset(&edit, 0, sizeof edit);
    883 	edit.pset = pset;
    884 	edit.index = menu->cursel;
    885 	edit.wanted = &pset->infos[edit.index];
    886 
    887 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
    888 		return 0;
    889 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
    890 
    891 	num_opts = __arraycount(edit_ptn_fields_head) +
    892 	    __arraycount(edit_ptn_fields_head2) +
    893 	    __arraycount(edit_ptn_fields_tail);
    894 	if (edit.wanted->fs_type == FS_BSDFFS ||
    895 	    edit.wanted->fs_type == FS_BSDLFS)
    896 		num_opts += __arraycount(edit_ptn_fields_ffs);
    897 	if (with_inst_opt)
    898 		num_opts += __arraycount(edit_ptn_fields_head_add);
    899 	if (is_new_part)
    900 		num_opts--;
    901 	else
    902 		num_opts += pset->parts->pscheme->custom_attribute_count;
    903 
    904 	mopts = calloc(num_opts, sizeof(*mopts));
    905 	if (mopts == NULL) {
    906 		err_msg_win(err_outofmem);
    907 		return 0;
    908 	}
    909 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
    910 	popt = mopts + __arraycount(edit_ptn_fields_head);
    911 	if (with_inst_opt) {
    912 		memcpy(popt, edit_ptn_fields_head_add,
    913 		    sizeof(edit_ptn_fields_head_add));
    914 		popt +=  __arraycount(edit_ptn_fields_head_add);
    915 	}
    916 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
    917 	popt +=  __arraycount(edit_ptn_fields_head2);
    918 	if (edit.wanted->fs_type == FS_BSDFFS ||
    919 	    edit.wanted->fs_type == FS_BSDLFS) {
    920 		memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
    921 		popt +=  __arraycount(edit_ptn_fields_ffs);
    922 	}
    923 	edit.first_custom_attr = popt - mopts;
    924 	if (!is_new_part) {
    925 		for (size_t i = 0;
    926 		    i < pset->parts->pscheme->custom_attribute_count;
    927 		    i++, popt++) {
    928 			popt->opt_action = edit_ptn_custom_type;
    929 		}
    930 	}
    931 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
    932 	popt += __arraycount(edit_ptn_fields_tail) - 1;
    933 	if (is_new_part)
    934 		memcpy(popt-1, popt, sizeof(*popt));
    935 
    936 	if (is_new_part) {
    937 		struct disk_part_free_space space;
    938 		daddr_t align = pset->parts->pscheme->get_part_alignment(
    939 		    pset->parts);
    940 
    941 		edit.id = NO_PART;
    942 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
    943 		    &space, 1, align, align, -1, -1) == 1) {
    944 			edit.info.start = space.start;
    945 			edit.info.size = space.size;
    946 			edit.info.fs_type = FS_BSDFFS;
    947 			edit.info.fs_sub_type = 2;
    948 			edit.info.nat_type = pset->parts->pscheme->
    949 			    get_fs_part_type(PT_root, edit.info.fs_type,
    950 			    edit.info.fs_sub_type);
    951 			edit.wanted->instflags = PUIINST_NEWFS;
    952 		}
    953 	} else {
    954 		edit.id = pset->infos[edit.index].cur_part_id;
    955 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
    956 		    &edit.info)) {
    957 			free(mopts);
    958 			return 0;
    959 		}
    960 	}
    961 
    962 	edit.old_usage = *edit.wanted;
    963 	edit.old_info = edit.info;
    964 
    965 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
    966 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
    967 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
    968 		NULL, MSG_OK);
    969 
    970 	process_menu(fspart_menu, &edit);
    971 	free(mopts);
    972 	free_menu(fspart_menu);
    973 
    974 	if (edit.rv == 0) {	/* OK, set new data */
    975 		edit.info.last_mounted = edit.wanted->mount;
    976 		if (is_new_part) {
    977 			edit.wanted->parts = pset->parts;
    978 			edit.wanted->cur_part_id = pset->parts->pscheme->
    979 			    add_partition(pset->parts, &edit.info, &err);
    980 			if (edit.wanted->cur_part_id == NO_PART)
    981 				err_msg_win(err);
    982 			else {
    983 				pset->parts->pscheme->get_part_info(
    984 				    pset->parts, edit.wanted->cur_part_id,
    985 				    &edit.info);
    986 				edit.wanted->cur_start = edit.info.start;
    987 				edit.wanted->size = edit.info.size;
    988 				edit.wanted->type =
    989 				    edit.info.nat_type->generic_ptype;
    990 				edit.wanted->fs_type = edit.info.fs_type;
    991 				edit.wanted->fs_version = edit.info.fs_sub_type;
    992 				/* things have changed, re-sort */
    993 				renumber_partitions(pset);
    994 			}
    995 		} else {
    996 			if (!pset->parts->pscheme->set_part_info(pset->parts,
    997 			    edit.id, &edit.info, &err))
    998 				err_msg_win(err);
    999 		}
   1000 
   1001 		/*
   1002 		 * if size has changed, we may need to add or remove
   1003 		 * the option to add partitions
   1004 		 */
   1005 		show_partition_adder(menu, pset);
   1006 	} else if (edit.rv == -1) {	/* cancel edit */
   1007 		if (is_new_part) {
   1008 			memmove(pset->infos+edit.index,
   1009 			    pset->infos+edit.index+1,
   1010 			    sizeof(*pset->infos)*(pset->num-edit.index));
   1011 			memmove(menu->opts+edit.index,
   1012 			    menu->opts+edit.index+1,
   1013 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
   1014 			menu->numopts--;
   1015 			menu->cursel = 0;
   1016 			pset->num--;
   1017 			return -1;
   1018 		}
   1019 		pset->infos[edit.index] = edit.old_usage;
   1020 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
   1021 		if (!pset->parts->pscheme->delete_partition(pset->parts,
   1022 		    edit.id, &err)) {
   1023 			err_msg_win(err);
   1024 			return 0;
   1025 		}
   1026 		remember_deleted(pset,
   1027 		    pset->infos[edit.index].parts);
   1028 		pset->cur_free_space += pset->infos[edit.index].size;
   1029 		memmove(pset->infos+edit.index,
   1030 		    pset->infos+edit.index+1,
   1031 		    sizeof(*pset->infos)*(pset->num-edit.index));
   1032 		memmove(menu->opts+edit.index,
   1033 		    menu->opts+edit.index+1,
   1034 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
   1035 		menu->numopts--;
   1036 		menu->cursel = 0;
   1037 		if (pset->parts->num_part == 0)
   1038 			menu->cursel = 1;	/* skip sentinel line */
   1039 
   1040 		/* things have changed, re-sort */
   1041 		pset->num--;
   1042 		renumber_partitions(pset);
   1043 
   1044 		/* we can likely add new partitions now */
   1045 		show_partition_adder(menu, pset);
   1046 
   1047 		return -1;
   1048 	}
   1049 
   1050 	return 0;
   1051 }
   1052 
   1053 static void
   1054 update_edit_ptn_menu(menudesc *m, void *arg)
   1055 {
   1056 	struct single_part_fs_edit *edit = arg;
   1057 	int i;
   1058 	uint t = edit->info.fs_type;
   1059 	size_t attr_no;
   1060 
   1061 	/* Determine which of the properties can be changed */
   1062 	for (i = 0; i < m->numopts; i++) {
   1063 		if (m->opts[i].opt_action == NULL &&
   1064 		    m->opts[i].opt_menu != MENU_mountoptions)
   1065 			continue;
   1066 
   1067 		/* Default to disabled... */
   1068 		m->opts[i].opt_flags |= OPT_IGNORE;
   1069 		if ((t == FS_UNUSED || t == FS_SWAP) &&
   1070 		    (m->opts[i].opt_action == edit_fs_preserve ||
   1071 		     m->opts[i].opt_action == edit_fs_mount ||
   1072 		     m->opts[i].opt_action == edit_fs_mountpt ||
   1073 		     m->opts[i].opt_menu == MENU_mountoptions))
   1074 			continue;
   1075 		if (m->opts[i].opt_action == edit_install &&
   1076 		    edit->info.nat_type &&
   1077 		    edit->info.nat_type->generic_ptype != PT_root)
   1078 			/* can only install onto PT_root partitions */
   1079 			continue;
   1080 		if (m->opts[i].opt_action == edit_fs_preserve &&
   1081 		    t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS &&
   1082 		    t != FS_MSDOS && t != FS_EX2FS) {
   1083 			/* Can not newfs this filesystem */
   1084 			edit->wanted->instflags &= ~PUIINST_NEWFS;
   1085 			continue;
   1086 		}
   1087 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
   1088 			attr_no = (size_t)i - edit->first_custom_attr;
   1089 			if (!edit->pset->parts->pscheme->
   1090 			    custom_attribute_writable(
   1091 			    edit->pset->parts, edit->id, attr_no))
   1092 				continue;
   1093 		}
   1094 		/*
   1095 		 * Do not allow editing of size/start/type when partition
   1096 		 * is defined in some outer partition table already
   1097 		 */
   1098 		if ((edit->pset->infos[edit->index].flags & PUIFLG_IS_OUTER)
   1099 		    && (m->opts[i].opt_action == edit_fs_type
   1100 			|| m->opts[i].opt_action == edit_fs_start
   1101 			|| m->opts[i].opt_action == edit_fs_size))
   1102 				continue;
   1103 		/* Ok: we want this one */
   1104 		m->opts[i].opt_flags &= ~OPT_IGNORE;
   1105 	}
   1106 
   1107 	/* Avoid starting at a (now) disabled menu item */
   1108 	while (m->cursel >= 0 && m->cursel < m->numopts
   1109 	    && (m->opts[m->cursel].opt_flags & OPT_IGNORE))
   1110 		m->cursel++;
   1111 }
   1112 
   1113 static void
   1114 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
   1115 {
   1116 	struct single_part_fs_edit *edit = arg;
   1117 	static int col_width;
   1118 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
   1119 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
   1120 	     *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
   1121 	const char *c;
   1122 	char val[MENUSTRSIZE];
   1123 	const char *attrname;
   1124 	size_t attr_no;
   1125 
   1126 	if (col_width == 0) {
   1127 		int l;
   1128 
   1129 #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
   1130 			if (l > col_width) col_width = l
   1131 
   1132 		LOAD(ptn_type);
   1133 		LOAD(ptn_start);
   1134 		LOAD(ptn_size);
   1135 		LOAD(ptn_end);
   1136 		LOAD(ptn_install);
   1137 		LOAD(ptn_newfs);
   1138 		LOAD(ptn_mount);
   1139 		LOAD(ptn_mount_options);
   1140 		LOAD(ptn_mountpt);
   1141 		LOAD(ptn_bsize);
   1142 		LOAD(ptn_fsize);
   1143 		LOAD(ptn_isize);
   1144 #undef LOAD
   1145 
   1146 		for (size_t i = 0;
   1147 		    i < edit->pset->parts->pscheme->custom_attribute_count;
   1148 		    i++) {
   1149 			attrname = msg_string(
   1150 			    edit->pset->parts->pscheme->custom_attributes[i]
   1151 			    .label);
   1152 			l = strlen(attrname);
   1153 			if (l > col_width) col_width = l;
   1154 		}
   1155 
   1156 		col_width += 3;
   1157 	}
   1158 
   1159 	if (m->opts[opt].opt_flags & OPT_IGNORE
   1160 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
   1161 	    && m->opts[opt].opt_action != edit_ptn_custom_type
   1162 	    && m->opts[opt].opt_action != edit_fs_type
   1163 	    && m->opts[opt].opt_action != edit_fs_start
   1164 	    && m->opts[opt].opt_action != edit_fs_size) {
   1165 		wprintw(m->mw, "%*s -", col_width, "");
   1166 		return;
   1167 	}
   1168 
   1169 	if (opt < 4) {
   1170 		switch (opt) {
   1171 		case 0:
   1172 			if (edit->info.fs_type == FS_BSDFFS)
   1173 				if (edit->info.fs_sub_type == 2)
   1174 					c = msg_string(MSG_fs_type_ffsv2);
   1175 				else
   1176 					c = msg_string(MSG_fs_type_ffs);
   1177 			else
   1178 				c = getfslabelname(edit->info.fs_type,
   1179 				    edit->info.fs_sub_type);
   1180 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
   1181 			return;
   1182 		case 1:
   1183 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1184 			    ptn_start, edit->info.start / sizemult, multname);
   1185 			return;
   1186 		case 2:
   1187 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1188 			    ptn_size, edit->info.size / sizemult, multname);
   1189 			return;
   1190 		case 3:
   1191 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1192 			    ptn_end, (edit->info.start + edit->info.size)
   1193 			    / sizemult, multname);
   1194 			return;
   1195 		 }
   1196 	}
   1197 	if (m->opts[opt].opt_action == edit_install) {
   1198 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
   1199 			msg_string((edit->info.flags & PTI_INSTALL_TARGET)
   1200 			    ? MSG_Yes : MSG_No));
   1201 		return;
   1202 	}
   1203 	if (m->opts[opt].opt_action == edit_fs_preserve) {
   1204 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
   1205 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
   1206 			    ? MSG_Yes : MSG_No));
   1207 		return;
   1208 	}
   1209 	if (m->opts[opt].opt_action == edit_fs_mount) {
   1210 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
   1211 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
   1212 			    ? MSG_Yes : MSG_No));
   1213 		return;
   1214 	}
   1215 	if (m->opts[opt].opt_action == edit_fs_ffs_block) {
   1216 		wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
   1217 			edit->wanted->fs_opt1);
   1218 		return;
   1219 	}
   1220 	if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
   1221 		wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
   1222 			edit->wanted->fs_opt2);
   1223 		return;
   1224 	}
   1225 	if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
   1226 		if (edit->wanted->fs_opt3 == 0)
   1227 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
   1228 				msg_string(MSG_ptn_isize_dflt));
   1229 		else {
   1230         	        char buf[24], *line;
   1231 			const char *t = buf;
   1232 
   1233 			snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
   1234 			line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
   1235 			    1, &t);
   1236 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
   1237 				line);
   1238 			free(line);
   1239 		}
   1240 		return;
   1241 	}
   1242 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
   1243 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
   1244 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
   1245 			wprintw(m->mw, "async ");
   1246 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
   1247 			wprintw(m->mw, "noatime ");
   1248 		if (edit->wanted->mountflags & PUIMNT_NODEV)
   1249 			wprintw(m->mw, "nodev ");
   1250 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
   1251 			wprintw(m->mw, "nodevmtime ");
   1252 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
   1253 			wprintw(m->mw, "noexec ");
   1254 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
   1255 			wprintw(m->mw, "nosuid ");
   1256 		if (edit->wanted->mountflags & PUIMNT_LOG)
   1257 			wprintw(m->mw, "log ");
   1258 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
   1259 			wprintw(m->mw, "noauto  ");
   1260 		return;
   1261 	}
   1262 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
   1263 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
   1264 		    edit->wanted->mount);
   1265 		return;
   1266 	}
   1267 
   1268 	attr_no = opt - edit->first_custom_attr;
   1269 	edit->pset->parts->pscheme->format_custom_attribute(
   1270 	    edit->pset->parts, edit->id, attr_no, &edit->info,
   1271 	    val, sizeof val);
   1272 	attrname = msg_string(edit->pset->parts->pscheme->
   1273 	    custom_attributes[attr_no].label);
   1274 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
   1275 }
   1276 
   1277 static int
   1278 edit_ptn_custom_type(menudesc *m, void *arg)
   1279 {
   1280 	struct single_part_fs_edit *edit = arg;
   1281 	size_t attr_no = m->cursel - edit->first_custom_attr;
   1282 	char line[STRSIZE];
   1283 
   1284 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
   1285 	case pet_bool:
   1286 		edit->pset->parts->pscheme->custom_attribute_toggle(
   1287 		    edit->pset->parts, edit->id, attr_no);
   1288 		break;
   1289 	case pet_cardinal:
   1290 	case pet_str:
   1291 		edit->pset->parts->pscheme->format_custom_attribute(
   1292 		    edit->pset->parts, edit->id, attr_no, &edit->info,
   1293 		    line, sizeof(line));
   1294 		msg_prompt_win(
   1295 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
   1296 		    label, -1, 18, 0, 0, line, line, sizeof(line));
   1297 		edit->pset->parts->pscheme->custom_attribute_set_str(
   1298 		    edit->pset->parts, edit->id, attr_no, line);
   1299 		break;
   1300 	}
   1301 
   1302 	return 0;
   1303 }
   1304 
   1305 
   1306 /*
   1307  * Some column width depend on translation, we will set these in
   1308  * fmt_fspart_header and later use it when formatting single entries
   1309  * in fmt_fspart_row.
   1310  * The table consist of 3 "size like" columns, all fixed width, then
   1311  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
   1312  * (which is variable width).
   1313  */
   1314 static int fstype_width, flags_width;
   1315 static char fspart_separator[MENUSTRSIZE];
   1316 static char fspart_title[2*MENUSTRSIZE];
   1317 
   1318 /*
   1319  * Format the header of the main partition editor menu.
   1320  */
   1321 static void
   1322 fmt_fspart_header(menudesc *menu, void *arg)
   1323 {
   1324 	struct partition_usage_set *pset = arg;
   1325 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
   1326 	    sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
   1327 	const char *fstype, *flags;
   1328 	int i;
   1329 	size_t ptn;
   1330 	bool with_clone, with_inst_flag = pset->parts->parent == NULL;
   1331 
   1332 	with_clone = false;
   1333 	for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
   1334 		if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1335 			with_clone = true;
   1336 	humanize_number(total, sizeof total,
   1337 	    pset->parts->disk_size * pset->parts->bytes_per_sector,
   1338 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1339 	humanize_number(free_space, sizeof free_space,
   1340 	    pset->cur_free_space * pset->parts->bytes_per_sector,
   1341 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1342 
   1343 	if (with_clone)
   1344 		strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
   1345 	else
   1346 		desc[0] = 0;
   1347 	if (pset->parts->pscheme->part_flag_desc)
   1348 		strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
   1349 		sizeof desc);
   1350 
   1351 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
   1352 	    msg_string(pset->parts->pscheme->name),
   1353 	    msg_string(pset->parts->pscheme->short_name),
   1354 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
   1355 	    desc, total, free_space);
   1356 
   1357 	snprintf(scol, sizeof scol, "%s (%s)",
   1358 	    msg_string(MSG_ptnheaders_start), multname);
   1359 	snprintf(ecol, sizeof ecol, "%s (%s)",
   1360 	    msg_string(MSG_ptnheaders_end), multname);
   1361 	snprintf(szcol, sizeof szcol, "%s (%s)",
   1362 	    msg_string(MSG_ptnheaders_size), multname);
   1363 
   1364 	fstype = msg_string(MSG_ptnheaders_fstype);
   1365 	flags = msg_string(MSG_part_header_col_flag);
   1366 	fstype_width = max(strlen(fstype), 8);
   1367 	flags_width = strlen(flags);
   1368 	for (i = 0, p = sepline; i < fstype_width; i++)
   1369 		*p++ = '-';
   1370 	for (i = 0, *p++ = ' '; i < flags_width; i++)
   1371 		*p++ = '-';
   1372 	*p = 0;
   1373 
   1374 	snprintf(fspart_separator, sizeof(fspart_separator),
   1375 	    "------------ ------------ ------------ %s ----------------",
   1376 	    sepline);
   1377 
   1378 	snprintf(fspart_title, sizeof(fspart_title),
   1379 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
   1380 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
   1381 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
   1382 	    fspart_separator);
   1383 
   1384 	msg_table_add("\n\n");
   1385 }
   1386 
   1387 /*
   1388  * Format one partition entry in the main partition editor.
   1389  */
   1390 static void
   1391 fmt_fspart_row(menudesc *m, int ptn, void *arg)
   1392 {
   1393 	struct partition_usage_set *pset = arg;
   1394 	struct disk_part_info info;
   1395 	daddr_t poffset, psize, pend;
   1396 	const char *desc;
   1397 	static const char *Yes;
   1398 	char flag_str[MENUSTRSIZE], *fp;
   1399 	unsigned inst_flags;
   1400 #ifndef NO_CLONES
   1401 	size_t clone_cnt;
   1402 #endif
   1403 	bool with_inst_flag = pset->parts->parent == NULL;
   1404 
   1405 	if (Yes == NULL)
   1406 		Yes = msg_string(MSG_Yes);
   1407 
   1408 #ifndef NO_CLONES
   1409 	if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
   1410 	   pset->infos[ptn].cur_part_id == NO_PART) {
   1411 		psize = pset->infos[ptn].size / sizemult;
   1412 		if (pset->infos[ptn].clone_ndx <
   1413 		    pset->infos[ptn].clone_src->num_sel)
   1414 			clone_cnt = 1;
   1415 		else
   1416 			clone_cnt = pset->infos[ptn].clone_src->num_sel;
   1417 		if (pset->infos[ptn].cur_part_id == NO_PART)
   1418 			wprintw(m->mw, "                          %12" PRIu64
   1419 			    " [%zu %s]", psize, clone_cnt,
   1420 			    msg_string(MSG_clone_target_disp));
   1421 		else {
   1422 			poffset = pset->infos[ptn].cur_start / sizemult;
   1423 			pend = (pset->infos[ptn].cur_start +
   1424 			    pset->infos[ptn].size) / sizemult - 1;
   1425 			wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1426 			    " [%zu %s]",
   1427 			    poffset, pend, psize, clone_cnt,
   1428 			    msg_string(MSG_clone_target_disp));
   1429 		}
   1430 		if (m->title == fspart_title)
   1431 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1432 		else
   1433 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1434 		return;
   1435 	}
   1436 #endif
   1437 
   1438 	if (!real_partition(pset, ptn))
   1439 		return;
   1440 
   1441 	if (!pset->parts->pscheme->get_part_info(pset->parts,
   1442 	    pset->infos[ptn].cur_part_id, &info))
   1443 		return;
   1444 
   1445 	/*
   1446 	 * We use this function in multiple menus, but only want it
   1447 	 * to play with enable/disable in a single one:
   1448 	 */
   1449 	if (m->title == fspart_title) {
   1450 		/*
   1451 		 * Enable / disable this line if it is something
   1452 		 * like RAW_PART
   1453 		 */
   1454 		if ((info.flags &
   1455 		    (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   1456 		    || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
   1457 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1458 		else
   1459 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1460 	}
   1461 
   1462 	poffset = info.start / sizemult;
   1463 	psize = info.size / sizemult;
   1464 	if (psize == 0)
   1465 		pend = 0;
   1466 	else
   1467 		pend = (info.start + info.size) / sizemult - 1;
   1468 
   1469 	if (info.flags & PTI_WHOLE_DISK)
   1470 		desc = msg_string(MSG_NetBSD_partition_cant_change);
   1471 	else if (info.flags & PTI_RAW_PART)
   1472 		desc = msg_string(MSG_Whole_disk_cant_change);
   1473 	else if (info.flags & PTI_BOOT)
   1474 		desc = msg_string(MSG_Boot_partition_cant_change);
   1475 	else
   1476 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
   1477 
   1478 	fp = flag_str;
   1479 	inst_flags = pset->infos[ptn].instflags;
   1480 	if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
   1481 	    info.nat_type->generic_ptype == PT_root) {
   1482 		static char inst_flag;
   1483 
   1484 		if (inst_flag == 0)
   1485 			inst_flag = msg_string(MSG_install_flag)[0];
   1486 		*fp++ = inst_flag;
   1487 	}
   1488 	if (inst_flags & PUIINST_NEWFS)
   1489 		*fp++ = msg_string(MSG_newfs_flag)[0];
   1490 	if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1491 		*fp++ = msg_string(MSG_clone_flag)[0];
   1492 	*fp = 0;
   1493 	if (pset->parts->pscheme->get_part_attr_str != NULL)
   1494 		pset->parts->pscheme->get_part_attr_str(pset->parts,
   1495 		    pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
   1496 
   1497 	/* if the fstype description does not fit, check if we can overrun */
   1498 	if (strlen(desc) > (size_t)fstype_width &&
   1499 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
   1500 	     info.last_mounted[0] == 0))
   1501 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1502 		    " %s",
   1503 		    poffset, pend, psize, desc);
   1504 	else
   1505 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1506 		    " %*.*s %*s %s",
   1507 		    poffset, pend, psize, fstype_width, fstype_width, desc,
   1508 		    -flags_width, flag_str,
   1509 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
   1510 		     info.last_mounted[0] ? info.last_mounted : "");
   1511 }
   1512 
   1513 #ifndef NO_CLONES
   1514 static int
   1515 part_ext_clone(menudesc *m, void *arg)
   1516 {
   1517 	struct selected_partitions selected, *clone_src;
   1518 	struct clone_target_menu_data data;
   1519 	struct partition_usage_set *pset = arg;
   1520 	struct part_usage_info *p;
   1521 	struct disk_part_info sinfo, cinfo;
   1522 	struct disk_partitions *csrc;
   1523 	struct disk_part_free_space space;
   1524 	menu_ent *men;
   1525 	daddr_t clone_size, free_size, offset, align;
   1526 	int num_men, i;
   1527 	size_t s, clone_cnt;
   1528 	part_id cid;
   1529 	struct clone_data {
   1530 		struct disk_part_info info;
   1531 		part_id new_id;
   1532 		size_t ndx;
   1533 	};
   1534 	struct clone_data *clones = NULL;
   1535 
   1536 	if (!select_partitions(&selected, pm->parts))
   1537 		return 0;
   1538 
   1539 	clone_size = selected_parts_size(&selected);
   1540 	num_men = pset->num+1;
   1541 	men = calloc(num_men, sizeof *men);
   1542 	if (men == NULL)
   1543 		return 0;
   1544 	for (i = 0; i < num_men; i++) {
   1545 		men[i].opt_action = clone_target_select;
   1546 		if (i == 0)
   1547 			free_size = pset->infos[i].cur_start;
   1548 		else if (i > 0 && (size_t)i < pset->num)
   1549 			free_size = pset->infos[i].cur_start -
   1550 			    pset->infos[i-1].cur_start - pset->infos[i-1].size;
   1551 		else
   1552 			free_size = pset->parts->free_space;
   1553 		if (free_size < clone_size)
   1554 			men[i].opt_flags = OPT_IGNORE;
   1555 	}
   1556 	men[num_men-1].opt_name = MSG_clone_target_end;
   1557 
   1558 	memset(&data, 0, sizeof data);
   1559 	data.usage = *pset;
   1560 	data.res = -1;
   1561 
   1562 	data.usage.menu = new_menu(MSG_clone_target_hdr,
   1563 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
   1564 	    NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
   1565 	process_menu(data.usage.menu, &data);
   1566 	free_menu(data.usage.menu);
   1567 	free(men);
   1568 
   1569 	if (data.res < 0)
   1570 		goto err;
   1571 
   1572 	/* create temporary infos for all clones that work out */
   1573 	clone_cnt = 0;
   1574 	clones = calloc(selected.num_sel, sizeof(*clones));
   1575 	if (clones == NULL)
   1576 		goto err;
   1577 
   1578 	clone_src = malloc(sizeof(selected));
   1579 	if (clone_src == NULL)
   1580 		goto err;
   1581 	*clone_src = selected;
   1582 
   1583 	/* find selected offset from data.res and insert clones there */
   1584 	align = pset->parts->pscheme->get_part_alignment(pset->parts);
   1585 	offset = -1;
   1586 	if (data.res > 0)
   1587 		offset = pset->infos[data.res-1].cur_start
   1588 		    + pset->infos[data.res-1].size;
   1589 	else
   1590 		offset = 0;
   1591 	for (s = 0; s < selected.num_sel; s++) {
   1592 		csrc = selected.selection[s].parts;
   1593 		cid = selected.selection[s].id;
   1594 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
   1595 		if (!pset->parts->pscheme->adapt_foreign_part_info(
   1596 		    pset->parts, &cinfo, csrc->pscheme, &sinfo))
   1597 			continue;
   1598 		size_t cnt = pset->parts->pscheme->get_free_spaces(
   1599 		    pset->parts, &space, 1, cinfo.size-align, align,
   1600 		    offset, -1);
   1601 		if (cnt == 0)
   1602 			continue;
   1603 		cinfo.start = space.start;
   1604 		cid = pset->parts->pscheme->add_partition(
   1605 		    pset->parts, &cinfo, NULL);
   1606 		if (cid == NO_PART)
   1607 			continue;
   1608 		pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
   1609 		clones[clone_cnt].info = cinfo;
   1610 		clones[clone_cnt].new_id = cid;
   1611 		clones[clone_cnt].ndx = s;
   1612 		clone_cnt++;
   1613 		offset = roundup(cinfo.start+cinfo.size, align);
   1614 	}
   1615 
   1616 	/* insert new clone records at offset data.res */
   1617 	men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
   1618 	if (men == NULL)
   1619 		goto err;
   1620 	pset->menu_opts = men;
   1621 	m->opts = men;
   1622 	m->numopts += clone_cnt;
   1623 
   1624 	p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
   1625 	if (p == NULL)
   1626 		goto err;
   1627 	pset->infos = p;
   1628 
   1629 	men += data.res;
   1630 	p += data.res;
   1631 	memmove(men+clone_cnt, men,
   1632 	    sizeof(*men)*(m->numopts-data.res-clone_cnt));
   1633 	if (pset->num > (size_t)data.res)
   1634 		memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
   1635 	memset(men, 0, sizeof(*men)*clone_cnt);
   1636 	memset(p, 0, sizeof(*p)*clone_cnt);
   1637 	for (s = 0; s < clone_cnt; s++) {
   1638 		p[s].cur_part_id = clones[s].new_id;
   1639 		p[s].cur_start = clones[s].info.start;
   1640 		p[s].size = clones[s].info.size;
   1641 		p[s].cur_flags = clones[s].info.flags;
   1642 		p[s].flags = PUIFLG_CLONE_PARTS;
   1643 		p[s].parts = pset->parts;
   1644 		p[s].clone_src = clone_src;
   1645 		p[s].clone_ndx = s;
   1646 	}
   1647 	free(clones);
   1648 	m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
   1649 	pset->num += clone_cnt;
   1650 	m->h = 0;
   1651 	resize_menu_height(m);
   1652 
   1653 	return -1;
   1654 
   1655 err:
   1656 	free(clones);
   1657 	free_selected_partitions(&selected);
   1658 	return 0;
   1659 }
   1660 #endif
   1661 
   1662 static int
   1663 edit_fspart_pack(menudesc *m, void *arg)
   1664 {
   1665 	struct partition_usage_set *pset = arg;
   1666 	char buf[STRSIZE];
   1667 
   1668 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
   1669 	    buf, sizeof buf))
   1670 		return 0;
   1671 
   1672 	msg_prompt_win(MSG_edit_disk_pack_hdr,
   1673 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
   1674 
   1675 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
   1676 	return 0;
   1677 }
   1678 
   1679 static int
   1680 edit_fspart_add(menudesc *m, void *arg)
   1681 {
   1682 	struct partition_usage_set *pset = arg;
   1683 	struct part_usage_info *ninfo;
   1684 	menu_ent *nmenopts;
   1685 	size_t cnt, off;
   1686 
   1687 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1688 	if (ninfo == NULL)
   1689 		return 0;
   1690 	pset->infos = ninfo;
   1691 	off = pset->parts->num_part;
   1692 	cnt = pset->num-pset->parts->num_part;
   1693 	if (cnt > 0)
   1694 		memmove(pset->infos+off+1,pset->infos+off,
   1695 		    cnt*sizeof(*pset->infos));
   1696 	memset(pset->infos+off, 0, sizeof(*pset->infos));
   1697 
   1698 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1699 	if (nmenopts == NULL)
   1700 		return 0;
   1701 	memmove(nmenopts+off+1, nmenopts+off,
   1702 	    (m->numopts-off)*sizeof(*nmenopts));
   1703 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1704 	nmenopts[off].opt_action = edit_ptn;
   1705 	pset->menu_opts = m->opts = nmenopts;
   1706 	m->numopts++;
   1707 	m->cursel = off;
   1708 	pset->num++;
   1709 
   1710 	/* now update edit menu to fit */
   1711 	m->h = 0;
   1712 	resize_menu_height(m);
   1713 
   1714 	/* and directly invoke the partition editor for the new part */
   1715 	edit_ptn(m, arg);
   1716 
   1717 	show_partition_adder(m, pset);
   1718 
   1719 	return -1;
   1720 }
   1721 
   1722 static void
   1723 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1724 {
   1725 	struct part_usage_info *ninfo;
   1726 	menu_ent *nmenopts;
   1727 	size_t off;
   1728 
   1729 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1730 	if (ninfo == NULL)
   1731 		return;
   1732 	pset->infos = ninfo;
   1733 	off = pset->parts->num_part+1;
   1734 
   1735 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1736 	if (nmenopts == NULL)
   1737 		return;
   1738 	memmove(nmenopts+off+1, nmenopts+off,
   1739 	    (m->numopts-off)*sizeof(*nmenopts));
   1740 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1741 
   1742 	nmenopts[off].opt_name = MSG_addpart;
   1743 	nmenopts[off].opt_flags = OPT_SUB;
   1744 	nmenopts[off].opt_action = edit_fspart_add;
   1745 
   1746 	m->opts = nmenopts;
   1747 	m->numopts++;
   1748 }
   1749 
   1750 static void
   1751 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1752 {
   1753 	size_t off;
   1754 
   1755 	off = pset->parts->num_part+1;
   1756 	memmove(m->opts+off, m->opts+off+1,
   1757 	    (m->numopts-off-1)*sizeof(*m->opts));
   1758 	m->numopts--;
   1759 }
   1760 
   1761 /*
   1762  * Called whenever the "add a partition" option may need to be removed
   1763  * or added
   1764  */
   1765 static void
   1766 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1767 {
   1768 	if (m->opts == NULL)
   1769 		return;
   1770 
   1771 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
   1772 	    pset->parts);
   1773 	bool part_adder_present =
   1774 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
   1775 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
   1776 
   1777 	if (can_add_partition == part_adder_present)
   1778 		return;
   1779 
   1780 	if (can_add_partition)
   1781 		add_partition_adder(m, pset);
   1782 	else
   1783 		remove_partition_adder(m, pset);
   1784 
   1785 	/* now update edit menu to fit */
   1786 	m->h = 0;
   1787 	resize_menu_height(m);
   1788 }
   1789 
   1790 static int
   1791 edit_fspart_abort(menudesc *m, void *arg)
   1792 {
   1793 	struct partition_usage_set *pset = arg;
   1794 
   1795 	pset->ok = false;
   1796 	return 1;
   1797 }
   1798 
   1799 /*
   1800  * Check a disklabel.
   1801  * If there are overlapping active partitions,
   1802  * Ask the user if they want to edit the partition or give up.
   1803  */
   1804 int
   1805 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
   1806     bool install)
   1807 {
   1808 	menu_ent *op;
   1809 	size_t cnt, i;
   1810 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
   1811 	bool may_edit_pack =
   1812 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
   1813 	    pset->parts->pscheme->set_disk_pack_name != NULL;
   1814 
   1815 #ifdef NO_CLONES
   1816 #define	C_M_ITEMS	0
   1817 #else
   1818 #define	C_M_ITEMS	1
   1819 #endif
   1820 	pset->menu_opts = calloc(pset->parts->num_part
   1821 	     +3+C_M_ITEMS+may_add+may_edit_pack,
   1822 	     sizeof *pset->menu_opts);
   1823 	if (pset->menu_opts == NULL)
   1824 		return 0;
   1825 
   1826 	op = pset->menu_opts;
   1827 	for (i = 0; i < pset->parts->num_part; i++) {
   1828 		op->opt_action = edit_ptn;
   1829 		op++;
   1830 	}
   1831 	/* separator line between partitions and actions */
   1832 	op->opt_name = fspart_separator;
   1833 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
   1834 	op++;
   1835 
   1836 	/* followed by new partition adder */
   1837 	if (may_add) {
   1838 		op->opt_name = MSG_addpart;
   1839 		op->opt_flags = OPT_SUB;
   1840 		op->opt_action = edit_fspart_add;
   1841 		op++;
   1842 	}
   1843 
   1844 	/* and unit changer */
   1845 	op->opt_name = MSG_askunits;
   1846 	op->opt_menu = MENU_sizechoice;
   1847 	op->opt_flags = OPT_SUB;
   1848 	op->opt_action = NULL;
   1849 	op++;
   1850 
   1851 	if (may_edit_pack) {
   1852 		op->opt_name = MSG_editpack;
   1853 		op->opt_flags = OPT_SUB;
   1854 		op->opt_action = edit_fspart_pack;
   1855 		op++;
   1856 	}
   1857 
   1858 #ifndef NO_CLONES
   1859 	/* add a clone-from-elsewhere option */
   1860 	op->opt_name = MSG_clone_from_elsewhere;
   1861 	op->opt_action = part_ext_clone;
   1862 	op++;
   1863 #endif
   1864 
   1865 	/* and abort option */
   1866 	op->opt_name = MSG_cancel;
   1867 	op->opt_flags = OPT_EXIT;
   1868 	op->opt_action = edit_fspart_abort;
   1869 	op++;
   1870 	cnt = op - pset->menu_opts;
   1871 	assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
   1872 
   1873 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
   1874 			0, -1, 0, 74,
   1875 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
   1876 			MC_NOCLEAR|MC_CONTINUOUS,
   1877 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
   1878 			MSG_partition_sizes_ok);
   1879 
   1880 	if (pset->menu < 0) {
   1881 		free(pset->menu_opts);
   1882 		pset->menu_opts = NULL;
   1883 		return 0;
   1884 	}
   1885 
   1886 	p->current_cylsize = p->dlcylsize;
   1887 
   1888 	for (;;) {
   1889 		/* first give the user the option to edit the label... */
   1890 		pset->ok = true;
   1891 		process_menu(pset->menu, pset);
   1892 		if (!pset->ok) {
   1893 			i = 0;
   1894 			break;
   1895 		}
   1896 
   1897 		/* User thinks the label is OK. */
   1898 		i = verify_parts(pset, install);
   1899 		if (i == 1)
   1900 			continue;
   1901 		break;
   1902 	}
   1903 	free(pset->menu_opts);
   1904 	pset->menu_opts = NULL;
   1905 	free_menu(pset->menu);
   1906 	pset->menu = -1;
   1907 
   1908 	return i != 0;
   1909 }
   1910 
   1911 /*
   1912  * strip trailing / to avoid confusion in path comparisons later
   1913  */
   1914 void
   1915 canonicalize_last_mounted(char *path)
   1916 {
   1917 	char *p;
   1918 
   1919 	if (path == NULL)
   1920 		return;
   1921 
   1922 	if (strcmp(path, "/") == 0)
   1923 		return;	/* in this case a "trailing" slash is allowed */
   1924 
   1925 	for (;;) {
   1926 		p = strrchr(path, '/');
   1927 		if (p == NULL)
   1928 			return;
   1929 		if (p[1] != 0)
   1930 			return;
   1931 		p[0] = 0;
   1932 	}
   1933 }
   1934 
   1935 /*
   1936  * Try to get 'last mounted on' (or equiv) from fs superblock.
   1937  */
   1938 const char *
   1939 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
   1940     uint flags)
   1941 {
   1942 	static char sblk[SBLOCKSIZE];		/* is this enough? */
   1943 	struct fs *SB = (struct fs *)sblk;
   1944 	static const off_t sblocks[] = SBLOCKSEARCH;
   1945 	const off_t *sbp;
   1946 	const char *mnt = NULL;
   1947 	int len;
   1948 
   1949 	if (fd == -1)
   1950 		return "";
   1951 
   1952 	if (fs_type)
   1953 		*fs_type = 0;
   1954 	if (fs_sub_type)
   1955 		*fs_sub_type = 0;
   1956 
   1957 	/* Check UFS1/2 (and hence LFS) superblock */
   1958 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
   1959 		if (pread(fd, sblk, sizeof sblk,
   1960 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
   1961 			continue;
   1962 
   1963 		/*
   1964 		 * If start of partition and allowed by flags check
   1965 		 * for other fs types
   1966 		 */
   1967 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
   1968 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
   1969 			/* Probably a FAT filesystem, report volume name */
   1970 			size_t i;
   1971 			for (i = 0x51; i >= 0x47; i--) {
   1972 				if (sblk[i] != ' ')
   1973 					break;
   1974 				sblk[i] = 0;
   1975 			}
   1976 			sblk[0x52] = 0;
   1977 			if (fs_type)
   1978 				*fs_type = FS_MSDOS;
   1979 			if (fs_sub_type)
   1980 				*fs_sub_type = sblk[0x53];
   1981 			return sblk + 0x47;
   1982 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
   1983 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
   1984 			if (fs_type)
   1985 				*fs_type = FS_NTFS;
   1986 			if (fs_sub_type)
   1987 				*fs_sub_type = MBR_PTYPE_NTFS;
   1988 			/* XXX dig for volume name attribute ? */
   1989 			return "";
   1990 		}
   1991 
   1992 		if (!(flags & GLM_LIKELY_FFS))
   1993 			continue;
   1994 
   1995 		/* Maybe we should validate the checksum??? */
   1996 		switch (SB->fs_magic) {
   1997 		case FS_UFS1_MAGIC:
   1998 		case FS_UFS1_MAGIC_SWAPPED:
   1999 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
   2000 				if (*sbp == SBLOCK_UFS1)
   2001 					mnt = (const char *)SB->fs_fsmnt;
   2002 			} else {
   2003 				/* Check we have the main superblock */
   2004 				if (SB->fs_sblockloc == *sbp)
   2005 					mnt = (const char *)SB->fs_fsmnt;
   2006 			}
   2007 			if (fs_type)
   2008 				*fs_type = FS_BSDFFS;
   2009 			if (fs_sub_type)
   2010 				*fs_sub_type = 1;
   2011 			continue;
   2012 		case FS_UFS2_MAGIC:
   2013 		case FS_UFS2_MAGIC_SWAPPED:
   2014 			/* Check we have the main superblock */
   2015 			if (SB->fs_sblockloc == *sbp) {
   2016 				mnt = (const char *)SB->fs_fsmnt;
   2017 				if (fs_type)
   2018 					*fs_type = FS_BSDFFS;
   2019 				if (fs_sub_type)
   2020 					*fs_sub_type = 2;
   2021 			}
   2022 			continue;
   2023 		}
   2024 	}
   2025 
   2026 	if (mnt == NULL)
   2027 		return "";
   2028 
   2029 	/* If sysinst mounted this last then strip prefix */
   2030 	len = strlen(targetroot_mnt);
   2031 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
   2032 		if (mnt[len] == 0)
   2033 			return "/";
   2034 		if (mnt[len] == '/')
   2035 			return mnt + len;
   2036 	}
   2037 	return mnt;
   2038 	#undef SB
   2039 }
   2040 
   2041 /* Ask for a partition offset, check bounds and do the needed roundups */
   2042 daddr_t
   2043 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
   2044 {
   2045 	char defstart[24], isize[24], maxpart, minspace, maxspace,
   2046 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
   2047 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
   2048 	size_t num_freespace, spaces, ndx;
   2049 	struct disk_part_free_space *freespace;
   2050 	daddr_t i, localsizemult, ptn_alignment, min, max;
   2051 	part_id partn;
   2052 	struct disk_part_info info;
   2053 	const char *errmsg = NULL;
   2054 
   2055 	min = parts->disk_start;
   2056 	max = min + parts->disk_size;
   2057 
   2058 	/* upper bound on the number of free spaces, plus some slope */
   2059 	num_freespace = parts->num_part * 2 + 5;
   2060 	freespace = calloc(num_freespace, sizeof(*freespace));
   2061 	if (freespace == NULL)
   2062 		return -1;
   2063 
   2064 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
   2065 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
   2066 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
   2067 	    defpartstart);
   2068 
   2069 	maxpart = 'a' + parts->num_part -1;
   2070 	if (parts->num_part > 1) {
   2071 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
   2072 	} else if (parts->num_part == 1) {
   2073 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
   2074 	} else {
   2075 		strcpy(valid_parts, "---");
   2076 	}
   2077 	if (spaces > 1) {
   2078 		minspace = maxpart + 1;
   2079 		maxspace = minspace + spaces -1;
   2080 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
   2081 		    maxspace);
   2082 	} else if (spaces == 1) {
   2083 		maxspace = minspace = maxpart + 1;
   2084 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
   2085 	} else {
   2086 		minspace = 0;
   2087 		maxspace = 0;
   2088 		strcpy(valid_spaces, "---");
   2089 	}
   2090 
   2091 	/* Add description of start/size to user prompt */
   2092 	const char *mstr = msg_string(MSG_free_space_line);
   2093         space_prompt[0] = 0;
   2094         for (ndx = 0; ndx < spaces; ndx++) {
   2095                 char str_start[40], str_end[40], str_size[40], str_tag[4];
   2096 
   2097 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
   2098                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
   2099                 sprintf(str_end, "%" PRIu64,
   2100                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
   2101                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
   2102                 const char *args[4] = { str_start, str_end, str_size,
   2103                     multname };
   2104                 char *line = str_arg_subst(mstr, 4, args);
   2105 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
   2106                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
   2107                 free (line);
   2108                 if (len >= sizeof space_prompt)
   2109                         break;
   2110         }
   2111 
   2112 	const char *args[] = { valid_parts, valid_spaces, multname };
   2113 	hint_part = NULL;
   2114 	hint_space = NULL;
   2115 	head = str_arg_subst(msg_string(MSG_label_offset_head),
   2116 	    __arraycount(args), args);
   2117 	if (parts->num_part)
   2118 		hint_part = str_arg_subst(msg_string(
   2119 		    MSG_label_offset_part_hint), __arraycount(args), args);
   2120 	if (spaces)
   2121 		hint_space = str_arg_subst(msg_string(
   2122 		    MSG_label_offset_space_hint), __arraycount(args), args);
   2123 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
   2124 	    __arraycount(args), args);
   2125 
   2126 	if (hint_part && hint_space)
   2127 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
   2128 		    head, hint_part, hint_space, space_prompt, tail);
   2129 	else if (hint_part)
   2130 		asprintf(&label_msg, "%s\n%s\n\n%s",
   2131 		    head, hint_part, tail);
   2132 	else if (hint_space)
   2133 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
   2134 		    head, hint_space, space_prompt, tail);
   2135 	else
   2136 		asprintf(&label_msg, "%s\n\n%s",
   2137 		    head, tail);
   2138 	free(head); free(hint_part); free(hint_space); free(tail);
   2139 
   2140 	localsizemult = sizemult;
   2141 	errmsg = NULL;
   2142 	for (;;) {
   2143 		snprintf(defstart, sizeof defstart, "%" PRIu64,
   2144 		    defpartstart/sizemult);
   2145 		if (errmsg != NULL && errmsg[0] != 0)
   2146 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2147 		else
   2148 			prompt = label_msg;
   2149 		msg_prompt_win(prompt, -1, 13, 70, -1,
   2150 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
   2151 		if (label_msg != prompt)
   2152 			free(prompt);
   2153 		if (strcmp(defstart, isize) == 0) {
   2154 			/* Don't do rounding if default accepted */
   2155 			i = defpartstart;
   2156 			break;
   2157 		}
   2158 		if (isize[1] == '\0' && isize[0] >= 'a' &&
   2159 		    isize[0] <= maxpart) {
   2160 			partn = isize[0] - 'a';
   2161 			if (parts->pscheme->get_part_info(parts, partn,
   2162 			    &info)) {
   2163 				i = info.start + info.size;
   2164 				localsizemult = 1;
   2165 			} else {
   2166 				errmsg = msg_string(MSG_invalid_sector_number);
   2167 				continue;
   2168 			}
   2169 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
   2170 		    isize[0] <= maxspace) {
   2171 			ndx = isize[0] - minspace;
   2172 			i = freespace[ndx].start;
   2173 			localsizemult = 1;
   2174 		} else if (atoi(isize) == -1) {
   2175 			i = min;
   2176 			localsizemult = 1;
   2177 		} else {
   2178 			i = parse_disk_pos(isize, &localsizemult,
   2179 			    parts->bytes_per_sector,
   2180 			    parts->pscheme->get_cylinder_size(parts), NULL);
   2181 			if (i < 0) {
   2182 				errmsg = msg_string(MSG_invalid_sector_number);
   2183 				continue;
   2184 			}
   2185 		}
   2186 		/* round to cylinder size if localsizemult != 1 */
   2187 		int cylsize = parts->pscheme->get_cylinder_size(parts);
   2188 		i = NUMSEC(i, localsizemult, cylsize);
   2189 		/* Adjust to start of slice if needed */
   2190 		if ((i < min && (min - i) < localsizemult) ||
   2191 		    (i > min && (i - min) < localsizemult)) {
   2192 			i = min;
   2193 		}
   2194 		if (max == 0 || i <= max)
   2195 			break;
   2196 		errmsg = msg_string(MSG_startoutsidedisk);
   2197 	}
   2198 	free(label_msg);
   2199 	free(freespace);
   2200 
   2201 	return i;
   2202 }
   2203 
   2204 
   2205 /* Ask for a partition size, check bounds and do the needed roundups */
   2206 daddr_t
   2207 getpartsize(struct disk_partitions *parts, daddr_t orig_start,
   2208     daddr_t partstart, daddr_t dflt)
   2209 {
   2210 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
   2211 	    *label_msg, *prompt, *head, *hint, *tail;
   2212 	const char *errmsg = NULL;
   2213 	daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
   2214 	struct disk_part_info info;
   2215 	part_id partn;
   2216 
   2217 	diskend = parts->disk_start + parts->disk_size;
   2218 	max = parts->pscheme->max_free_space_at(parts, orig_start);
   2219 	max += orig_start - partstart;
   2220 	if (sizemult == 1)
   2221 		max--;	/* with hugher scale proper rounding later will be ok */
   2222 
   2223 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
   2224 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
   2225 	if (max == dflt)
   2226 		max_r = dflt_r;
   2227 	else
   2228 		max_r = max / sizemult;
   2229 	/* the partition may have been moved and now not fit any longer */
   2230 	if (dflt > max)
   2231 		dflt = max;
   2232 	if (dflt_r > max_r)
   2233 		dflt_r = max_r;
   2234 
   2235 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
   2236 
   2237 	maxpartc = 'a' + parts->num_part -1;
   2238 	if (parts->num_part > 1) {
   2239 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
   2240 	} else if (parts->num_part == 1) {
   2241 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
   2242 	} else {
   2243 		strcpy(valid_parts, "---");
   2244 	}
   2245 
   2246 	const char *args[] = { valid_parts, max_size, multname };
   2247 	hint = NULL;
   2248 	head = str_arg_subst(msg_string(MSG_label_size_head),
   2249 	    __arraycount(args), args);
   2250 	if (parts->num_part)
   2251 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
   2252 		    __arraycount(args), args);
   2253 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
   2254 	    __arraycount(args), args);
   2255 
   2256 	if (hint != NULL)
   2257 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
   2258 	else
   2259 		asprintf(&label_msg, "%s\n\n%s", head, tail);
   2260 	free(head); free(hint); free(tail);
   2261 
   2262 	localsizemult = sizemult;
   2263 	i = -1;
   2264 	for (;;) {
   2265 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
   2266 
   2267 		if (errmsg != NULL && errmsg[0] != 0)
   2268 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2269 		else
   2270 			prompt = label_msg;
   2271 		msg_prompt_win(prompt, -1, 12, 70, -1,
   2272 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
   2273 		if (prompt != label_msg)
   2274 			free(prompt);
   2275 
   2276 		if (strcmp(isize, dsize) == 0) {
   2277 			free(label_msg);
   2278 			return dflt;
   2279 		}
   2280 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
   2281 		    isize[0] <= maxpartc) {
   2282 			partn = isize[0] - 'a';
   2283 			if (parts->pscheme->get_part_info(parts, partn,
   2284 			    &info)) {
   2285 				i = info.start - partstart -1;
   2286 				localsizemult = 1;
   2287 				max_r = max;
   2288 			}
   2289 		} else if (atoi(isize) == -1) {
   2290 			i = max;
   2291 			localsizemult = 1;
   2292 			max_r = max;
   2293 		} else {
   2294 			i = parse_disk_pos(isize, &localsizemult,
   2295 			    parts->bytes_per_sector,
   2296 			    parts->pscheme->get_cylinder_size(parts), NULL);
   2297 			if (localsizemult != sizemult)
   2298 				max_r = max;
   2299 		}
   2300 		if (i < 0) {
   2301 			errmsg = msg_string(MSG_Invalid_numeric);
   2302 			continue;
   2303 		} else if (i > max_r) {
   2304 			errmsg = msg_string(MSG_Too_large);
   2305 			continue;
   2306 		}
   2307 		/*
   2308 		 * partend is aligned to a cylinder if localsizemult
   2309 		 * is not 1 sector
   2310 		 */
   2311 		int cylsize = parts->pscheme->get_cylinder_size(parts);
   2312 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
   2313 		    localsizemult, cylsize);
   2314 		/* Align to end-of-disk or end-of-slice if close enough */
   2315 		if (partend > (diskend - sizemult)
   2316 		    && partend < (diskend + sizemult))
   2317 			partend = diskend;
   2318 		if (partend > (partstart + max - sizemult)
   2319 		    && partend < (partstart + max + sizemult))
   2320 			partend = partstart + max;
   2321 		/* sanity checks */
   2322 		if (partend > diskend) {
   2323 			partend = diskend;
   2324 			errmsg = msg_string(MSG_endoutsidedisk);
   2325 			continue;
   2326 		}
   2327 		free(label_msg);
   2328 		if (partend < partstart)
   2329 			return 0;
   2330 		return (partend - partstart);
   2331 	}
   2332 	/* NOTREACHED */
   2333 }
   2334 
   2335 /*
   2336  * convert a string to a number of sectors, with a possible unit
   2337  * 150M = 150 Megabytes
   2338  * 2000c = 2000 cylinders
   2339  * 150256s = 150256 sectors
   2340  * Without units, use the default (sizemult).
   2341  * returns the raw input value, and the unit used. Caller needs to multiply!
   2342  * On invalid inputs, returns -1.
   2343  */
   2344 daddr_t
   2345 parse_disk_pos(
   2346 	const char *str,
   2347 	daddr_t *localsizemult,
   2348 	daddr_t bps,
   2349 	daddr_t cyl_size,
   2350 	bool *extend_this)
   2351 {
   2352 	daddr_t val;
   2353 	char *cp;
   2354 	bool mult_found;
   2355 
   2356 	if (str[0] == '\0') {
   2357 		return -1;
   2358 	}
   2359 	val = strtoull(str, &cp, 10);
   2360 	mult_found = false;
   2361 	if (extend_this)
   2362 		*extend_this = false;
   2363 	while (*cp != 0) {
   2364 		if (*cp == 'G' || *cp == 'g') {
   2365 			if (mult_found)
   2366 				return -1;
   2367 			*localsizemult = GIG / bps;
   2368 			goto next;
   2369 		}
   2370 		if (*cp == 'M' || *cp == 'm') {
   2371 			if (mult_found)
   2372 				return -1;
   2373 			*localsizemult = MEG / bps;
   2374 			goto next;
   2375 		}
   2376 		if (*cp == 'c' || *cp == 'C') {
   2377 			if (mult_found)
   2378 				return -1;
   2379 			*localsizemult = cyl_size;
   2380 			goto next;
   2381 		}
   2382 		if (*cp == 's' || *cp == 'S') {
   2383 			if (mult_found)
   2384 				return -1;
   2385 			*localsizemult = 1;
   2386 			goto next;
   2387 		}
   2388 		if (*cp == '+' && extend_this) {
   2389 			*extend_this = true;
   2390 			cp++;
   2391 			break;
   2392 		}
   2393 
   2394 		/* not a known unit */
   2395 		return -1;
   2396 
   2397 next:
   2398 		mult_found = true;
   2399 		cp++;
   2400 		continue;
   2401 	}
   2402 	if (*cp != 0)
   2403 		return -1;
   2404 
   2405 	return val;
   2406 }
   2407