xref: /freebsd/usr.sbin/bsdinstall/partedit/gpart_ops.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <libutil.h>
36 #include <inttypes.h>
37 
38 #include <libgeom.h>
39 #include <dialog.h>
40 #include <dlg_keys.h>
41 
42 #include "partedit.h"
43 
44 #define GPART_FLAGS "x" /* Do not commit changes by default */
45 
46 static void
47 gpart_show_error(const char *title, const char *explanation, const char *errstr)
48 {
49 	char *errmsg;
50 	char message[512];
51 	int error;
52 
53 	if (explanation == NULL)
54 		explanation = "";
55 
56 	error = strtol(errstr, &errmsg, 0);
57 	if (errmsg != errstr) {
58 		while (errmsg[0] == ' ')
59 			errmsg++;
60 		if (errmsg[0] != '\0')
61 			sprintf(message, "%s%s. %s", explanation,
62 			    strerror(error), errmsg);
63 		else
64 			sprintf(message, "%s%s", explanation, strerror(error));
65 	} else {
66 		sprintf(message, "%s%s", explanation, errmsg);
67 	}
68 
69 	dialog_msgbox(title, message, 0, 0, TRUE);
70 }
71 
72 static int
73 scheme_supports_labels(const char *scheme)
74 {
75 	if (strcmp(scheme, "APM") == 0)
76 		return (1);
77 	if (strcmp(scheme, "GPT") == 0)
78 		return (1);
79 
80 	return (0);
81 }
82 
83 static void
84 newfs_command(const char *fstype, char *command, int use_default)
85 {
86 	if (strcmp(fstype, "freebsd-ufs") == 0) {
87 		int i;
88 		DIALOG_LISTITEM items[] = {
89 			{"UFS1", "UFS Version 1",
90 			    "Use version 1 of the UFS file system instead "
91 			    "of version 2 (not recommended)", 0 },
92 			{"SU", "Softupdates",
93 			    "Enable softupdates (default)", 1 },
94 			{"SUJ", "Softupdates journaling",
95 			    "Enable file system journaling (default - "
96 			    "turn off for SSDs)", 1 },
97 			{"TRIM", "Enable SSD TRIM support",
98 			    "Enable TRIM support, useful on solid-state drives",
99 			    0 },
100 		};
101 
102 		if (!use_default) {
103 			int choice;
104 			choice = dlg_checklist("UFS Options", "", 0, 0, 0,
105 			    nitems(items), items, NULL,
106 			    FLAG_CHECK, &i);
107 			if (choice == 1) /* Cancel */
108 				return;
109 		}
110 
111 		strcpy(command, "newfs ");
112 		for (i = 0; i < (int)nitems(items); i++) {
113 			if (items[i].state == 0)
114 				continue;
115 			if (strcmp(items[i].name, "UFS1") == 0)
116 				strcat(command, "-O1 ");
117 			else if (strcmp(items[i].name, "SU") == 0)
118 				strcat(command, "-U ");
119 			else if (strcmp(items[i].name, "SUJ") == 0)
120 				strcat(command, "-j ");
121 			else if (strcmp(items[i].name, "TRIM") == 0)
122 				strcat(command, "-t ");
123 		}
124 	} else if (strcmp(fstype, "freebsd-zfs") == 0) {
125 		int i;
126 		DIALOG_LISTITEM items[] = {
127 			{"fletcher4", "checksum algorithm: fletcher4",
128 			    "Use fletcher4 for data integrity checking. "
129 			    "(default)", 1 },
130 			{"fletcher2", "checksum algorithm: fletcher2",
131 			    "Use fletcher2 for data integrity checking. "
132 			    "(not recommended)", 0 },
133 			{"sha256", "checksum algorithm: sha256",
134 			    "Use sha256 for data integrity checking. "
135 			    "(not recommended)", 0 },
136 			{"atime", "Update atimes for files",
137 			    "Disable atime update", 0 },
138 		};
139 
140 		if (!use_default) {
141 			int choice;
142 			choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
143 			    nitems(items), items, NULL,
144 			    FLAG_CHECK, &i);
145 			if (choice == 1) /* Cancel */
146 				return;
147 		}
148 
149 		strcpy(command, "zpool create -f -m none ");
150 		if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
151 			char zfsboot_path[MAXPATHLEN];
152 			snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs",
153 			    getenv("BSDINSTALL_TMPBOOT"));
154 			mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
155 			    S_IROTH | S_IXOTH);
156 			sprintf(command, "%s -o cachefile=%s/zpool.cache ",
157 			    command, zfsboot_path);
158 		}
159 		for (i = 0; i < (int)nitems(items); i++) {
160 			if (items[i].state == 0)
161 				continue;
162 			if (strcmp(items[i].name, "fletcher4") == 0)
163 				strcat(command, "-O checksum=fletcher4 ");
164 			else if (strcmp(items[i].name, "fletcher2") == 0)
165 				strcat(command, "-O checksum=fletcher2 ");
166 			else if (strcmp(items[i].name, "sha256") == 0)
167 				strcat(command, "-O checksum=sha256 ");
168 			else if (strcmp(items[i].name, "atime") == 0)
169 				strcat(command, "-O atime=off ");
170 		}
171 	} else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0 ||
172 	     strcmp(fstype, "ms-basic-data") == 0) {
173 		int i;
174 		DIALOG_LISTITEM items[] = {
175 			{"FAT32", "FAT Type 32",
176 			    "Create a FAT32 filesystem (default)", 1 },
177 			{"FAT16", "FAT Type 16",
178 			    "Create a FAT16 filesystem", 0 },
179 			{"FAT12", "FAT Type 12",
180 			    "Create a FAT12 filesystem", 0 },
181 		};
182 
183 		if (!use_default) {
184 			int choice;
185 			choice = dlg_checklist("FAT Options", "", 0, 0, 0,
186 			    nitems(items), items, NULL,
187 			    FLAG_RADIO, &i);
188 			if (choice == 1) /* Cancel */
189 				return;
190 		}
191 
192 		strcpy(command, "newfs_msdos ");
193 		for (i = 0; i < (int)nitems(items); i++) {
194 			if (items[i].state == 0)
195 				continue;
196 			if (strcmp(items[i].name, "FAT32") == 0)
197 				strcat(command, "-F 32 -c 1");
198 			else if (strcmp(items[i].name, "FAT16") == 0)
199 				strcat(command, "-F 16 ");
200 			else if (strcmp(items[i].name, "FAT12") == 0)
201 				strcat(command, "-F 12 ");
202 		}
203 	} else {
204 		if (!use_default)
205 			dialog_msgbox("Error", "No configurable options exist "
206 			    "for this filesystem.", 0, 0, TRUE);
207 		command[0] = '\0';
208 	}
209 }
210 
211 const char *
212 choose_part_type(const char *def_scheme)
213 {
214 	int cancel, choice;
215 	const char *scheme = NULL;
216 
217 	DIALOG_LISTITEM items[] = {
218 		{"APM", "Apple Partition Map",
219 		    "Bootable on PowerPC Apple Hardware", 0 },
220 		{"BSD", "BSD Labels",
221 		    "Bootable on most x86 systems", 0 },
222 		{"GPT", "GUID Partition Table",
223 		    "Bootable on most x86 systems and EFI aware ARM64", 0 },
224 		{"MBR", "DOS Partitions",
225 		    "Bootable on most x86 systems", 0 },
226 		{"VTOC8", "Sun VTOC8 Partition Table",
227 		    "Bootable on Sun SPARC systems", 0 },
228 	};
229 
230 parttypemenu:
231 	dialog_vars.default_item = __DECONST(char *, def_scheme);
232 	cancel = dlg_menu("Partition Scheme",
233 	    "Select a partition scheme for this volume:", 0, 0, 0,
234 	    nitems(items), items, &choice, NULL);
235 	dialog_vars.default_item = NULL;
236 
237 	if (cancel)
238 		return NULL;
239 
240 	if (!is_scheme_bootable(items[choice].name)) {
241 		char message[512];
242 		sprintf(message, "This partition scheme (%s) is not "
243 		    "bootable on this platform. Are you sure you want "
244 		    "to proceed?", items[choice].name);
245 		dialog_vars.defaultno = TRUE;
246 		cancel = dialog_yesno("Warning", message, 0, 0);
247 		dialog_vars.defaultno = FALSE;
248 		if (cancel) /* cancel */
249 			goto parttypemenu;
250 	}
251 
252 	scheme = items[choice].name;
253 
254 	return scheme;
255 }
256 
257 int
258 gpart_partition(const char *lg_name, const char *scheme)
259 {
260 	int cancel;
261 	struct gctl_req *r;
262 	const char *errstr;
263 
264 schememenu:
265 	if (scheme == NULL) {
266 		scheme = choose_part_type(default_scheme());
267 
268 		if (scheme == NULL)
269 			return (-1);
270 
271 		if (!is_scheme_bootable(scheme)) {
272 			char message[512];
273 			sprintf(message, "This partition scheme (%s) is not "
274 			    "bootable on this platform. Are you sure you want "
275 			    "to proceed?", scheme);
276 			dialog_vars.defaultno = TRUE;
277 			cancel = dialog_yesno("Warning", message, 0, 0);
278 			dialog_vars.defaultno = FALSE;
279 			if (cancel) { /* cancel */
280 				/* Reset scheme so user can choose another */
281 				scheme = NULL;
282 				goto schememenu;
283 			}
284 		}
285 	}
286 
287 	r = gctl_get_handle();
288 	gctl_ro_param(r, "class", -1, "PART");
289 	gctl_ro_param(r, "arg0", -1, lg_name);
290 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
291 	gctl_ro_param(r, "scheme", -1, scheme);
292 	gctl_ro_param(r, "verb", -1, "create");
293 
294 	errstr = gctl_issue(r);
295 	if (errstr != NULL && errstr[0] != '\0') {
296 		gpart_show_error("Error", NULL, errstr);
297 		gctl_free(r);
298 		scheme = NULL;
299 		goto schememenu;
300 	}
301 	gctl_free(r);
302 
303 	if (bootcode_path(scheme) != NULL)
304 		get_part_metadata(lg_name, 1)->bootcode = 1;
305 	return (0);
306 }
307 
308 static void
309 gpart_activate(struct gprovider *pp)
310 {
311 	struct gconfig *gc;
312 	struct gctl_req *r;
313 	const char *errstr, *scheme;
314 	const char *attribute = NULL;
315 	intmax_t idx;
316 
317 	/*
318 	 * Some partition schemes need this partition to be marked 'active'
319 	 * for it to be bootable.
320 	 */
321 	LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
322 		if (strcmp(gc->lg_name, "scheme") == 0) {
323 			scheme = gc->lg_val;
324 			break;
325 		}
326 	}
327 
328 	if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
329 		attribute = "active";
330 	else
331 		return;
332 
333 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
334 		if (strcmp(gc->lg_name, "index") == 0) {
335 			idx = atoi(gc->lg_val);
336 			break;
337 		}
338 	}
339 
340 	r = gctl_get_handle();
341 	gctl_ro_param(r, "class", -1, "PART");
342 	gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
343 	gctl_ro_param(r, "verb", -1, "set");
344 	gctl_ro_param(r, "attrib", -1, attribute);
345 	gctl_ro_param(r, "index", sizeof(idx), &idx);
346 
347 	errstr = gctl_issue(r);
348 	if (errstr != NULL && errstr[0] != '\0')
349 		gpart_show_error("Error", "Error marking partition active:",
350 		    errstr);
351 	gctl_free(r);
352 }
353 
354 void
355 gpart_set_root(const char *lg_name, const char *attribute)
356 {
357 	struct gctl_req *r;
358 	const char *errstr;
359 
360 	r = gctl_get_handle();
361 	gctl_ro_param(r, "class", -1, "PART");
362 	gctl_ro_param(r, "arg0", -1, lg_name);
363 	gctl_ro_param(r, "flags", -1, "C");
364 	gctl_ro_param(r, "verb", -1, "set");
365 	gctl_ro_param(r, "attrib", -1, attribute);
366 
367 	errstr = gctl_issue(r);
368 	if (errstr != NULL && errstr[0] != '\0')
369 		gpart_show_error("Error", "Error setting parameter on disk:",
370 		    errstr);
371 	gctl_free(r);
372 }
373 
374 static void
375 gpart_bootcode(struct ggeom *gp)
376 {
377 	const char *bootcode;
378 	struct gconfig *gc;
379 	struct gctl_req *r;
380 	const char *errstr, *scheme;
381 	uint8_t *boot;
382 	size_t bootsize, bytes;
383 	int bootfd;
384 
385 	/*
386 	 * Write default bootcode to the newly partitioned disk, if that
387 	 * applies on this platform.
388 	 */
389 	LIST_FOREACH(gc, &gp->lg_config, lg_config) {
390 		if (strcmp(gc->lg_name, "scheme") == 0) {
391 			scheme = gc->lg_val;
392 			break;
393 		}
394 	}
395 
396 	bootcode = bootcode_path(scheme);
397 	if (bootcode == NULL)
398 		return;
399 
400 	bootfd = open(bootcode, O_RDONLY);
401 	if (bootfd < 0) {
402 		dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
403 		    TRUE);
404 		return;
405 	}
406 
407 	bootsize = lseek(bootfd, 0, SEEK_END);
408 	boot = malloc(bootsize);
409 	lseek(bootfd, 0, SEEK_SET);
410 	bytes = 0;
411 	while (bytes < bootsize)
412 		bytes += read(bootfd, boot + bytes, bootsize - bytes);
413 	close(bootfd);
414 
415 	r = gctl_get_handle();
416 	gctl_ro_param(r, "class", -1, "PART");
417 	gctl_ro_param(r, "arg0", -1, gp->lg_name);
418 	gctl_ro_param(r, "verb", -1, "bootcode");
419 	gctl_ro_param(r, "bootcode", bootsize, boot);
420 
421 	errstr = gctl_issue(r);
422 	if (errstr != NULL && errstr[0] != '\0')
423 		gpart_show_error("Bootcode Error", NULL, errstr);
424 	gctl_free(r);
425 	free(boot);
426 }
427 
428 static void
429 gpart_partcode(struct gprovider *pp, const char *fstype)
430 {
431 	struct gconfig *gc;
432 	const char *scheme;
433 	const char *indexstr;
434 	char message[255], command[255];
435 
436 	LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
437 		if (strcmp(gc->lg_name, "scheme") == 0) {
438 			scheme = gc->lg_val;
439 			break;
440 		}
441 	}
442 
443 	/* Make sure this partition scheme needs partcode on this platform */
444 	if (partcode_path(scheme, fstype) == NULL)
445 		return;
446 
447 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
448 		if (strcmp(gc->lg_name, "index") == 0) {
449 			indexstr = gc->lg_val;
450 			break;
451 		}
452 	}
453 
454 	/* Shell out to gpart for partcode for now */
455 	sprintf(command, "gpart bootcode -p %s -i %s %s",
456 	    partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
457 	if (system(command) != 0) {
458 		sprintf(message, "Error installing partcode on partition %s",
459 		    pp->lg_name);
460 		dialog_msgbox("Error", message, 0, 0, TRUE);
461 	}
462 }
463 
464 void
465 gpart_destroy(struct ggeom *lg_geom)
466 {
467 	struct gctl_req *r;
468 	struct gprovider *pp;
469 	const char *errstr;
470 	int force = 1;
471 
472 	/* Delete all child metadata */
473 	LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
474 		gpart_delete(pp);
475 
476 	/* Revert any local changes to get this geom into a pristine state */
477 	r = gctl_get_handle();
478 	gctl_ro_param(r, "class", -1, "PART");
479 	gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
480 	gctl_ro_param(r, "verb", -1, "undo");
481 	gctl_issue(r); /* Ignore errors -- these are non-fatal */
482 	gctl_free(r);
483 
484 	/* Now destroy the geom itself */
485 	r = gctl_get_handle();
486 	gctl_ro_param(r, "class", -1, "PART");
487 	gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
488 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
489 	gctl_ro_param(r, "force", sizeof(force), &force);
490 	gctl_ro_param(r, "verb", -1, "destroy");
491 	errstr = gctl_issue(r);
492 	if (errstr != NULL && errstr[0] != '\0') {
493 		/*
494 		 * Check if we reverted away the existence of the geom
495 		 * altogether. Show all other errors to the user.
496 		 */
497 		if (strtol(errstr, NULL, 0) != EINVAL)
498 			gpart_show_error("Error", NULL, errstr);
499 	}
500 	gctl_free(r);
501 
502 	/* And any metadata associated with the partition scheme itself */
503 	delete_part_metadata(lg_geom->lg_name);
504 }
505 
506 void
507 gpart_edit(struct gprovider *pp)
508 {
509 	struct gctl_req *r;
510 	struct gconfig *gc;
511 	struct gconsumer *cp;
512 	struct ggeom *geom;
513 	const char *errstr, *oldtype, *scheme;
514 	struct partition_metadata *md;
515 	char sizestr[32];
516 	char newfs[255];
517 	intmax_t idx;
518 	int hadlabel, choice, junk, nitems;
519 	unsigned i;
520 
521 	DIALOG_FORMITEM items[] = {
522 		{0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
523 		    FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
524 		    "freebsd-swap)", FALSE},
525 		{0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
526 		    FALSE, "Partition size. Append K, M, G for kilobytes, "
527 		    "megabytes or gigabytes.", FALSE},
528 		{0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
529 		    FALSE, "Path at which to mount this partition (leave blank "
530 		    "for swap, set to / for root filesystem)", FALSE},
531 		{0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
532 		    "Partition name. Not all partition schemes support this.",
533 		    FALSE},
534 	};
535 
536 	/*
537 	 * Find the PART geom we are manipulating. This may be a consumer of
538 	 * this provider, or its parent. Check the consumer case first.
539 	 */
540 	geom = NULL;
541 	LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
542 		if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
543 			/* Check for zombie geoms, treating them as blank */
544 			scheme = NULL;
545 			LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
546 				if (strcmp(gc->lg_name, "scheme") == 0) {
547 					scheme = gc->lg_val;
548 					break;
549 				}
550 			}
551 			if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
552 				gpart_partition(cp->lg_geom->lg_name, NULL);
553 				return;
554 			}
555 
556 			/* If this is a nested partition, edit as usual */
557 			if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
558 				break;
559 
560 			/* Destroy the geom and all sub-partitions */
561 			gpart_destroy(cp->lg_geom);
562 
563 			/* Now re-partition and return */
564 			gpart_partition(cp->lg_geom->lg_name, NULL);
565 			return;
566 		}
567 
568 	if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
569 		geom = pp->lg_geom;
570 
571 	if (geom == NULL) {
572 		/* Disk not partitioned, so partition it */
573 		gpart_partition(pp->lg_name, NULL);
574 		return;
575 	}
576 
577 	LIST_FOREACH(gc, &geom->lg_config, lg_config) {
578 		if (strcmp(gc->lg_name, "scheme") == 0) {
579 			scheme = gc->lg_val;
580 			break;
581 		}
582 	}
583 
584 	nitems = scheme_supports_labels(scheme) ? 4 : 3;
585 
586 	/* Edit editable parameters of a partition */
587 	hadlabel = 0;
588 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
589 		if (strcmp(gc->lg_name, "type") == 0) {
590 			oldtype = gc->lg_val;
591 			items[0].text = gc->lg_val;
592 		}
593 		if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
594 			hadlabel = 1;
595 			items[3].text = gc->lg_val;
596 		}
597 		if (strcmp(gc->lg_name, "index") == 0)
598 			idx = atoi(gc->lg_val);
599 	}
600 
601 	TAILQ_FOREACH(md, &part_metadata, metadata) {
602 		if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
603 			if (md->fstab != NULL)
604 				items[2].text = md->fstab->fs_file;
605 			break;
606 		}
607 	}
608 
609 	humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
610 	    HN_NOSPACE | HN_DECIMAL);
611 	items[1].text = sizestr;
612 
613 editpart:
614 	choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
615 
616 	if (choice) /* Cancel pressed */
617 		goto endedit;
618 
619 	/* If this is the root partition, check that this fs is bootable */
620 	if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
621 	    items[0].text)) {
622 		char message[512];
623 		sprintf(message, "This file system (%s) is not bootable "
624 		    "on this system. Are you sure you want to proceed?",
625 		    items[0].text);
626 		dialog_vars.defaultno = TRUE;
627 		choice = dialog_yesno("Warning", message, 0, 0);
628 		dialog_vars.defaultno = FALSE;
629 		if (choice == 1) /* cancel */
630 			goto editpart;
631 	}
632 
633 	/* Check if the label has a / in it */
634 	if (strchr(items[3].text, '/') != NULL) {
635 		dialog_msgbox("Error", "Label contains a /, which is not an "
636 		    "allowed character.", 0, 0, TRUE);
637 		goto editpart;
638 	}
639 
640 	r = gctl_get_handle();
641 	gctl_ro_param(r, "class", -1, "PART");
642 	gctl_ro_param(r, "arg0", -1, geom->lg_name);
643 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
644 	gctl_ro_param(r, "verb", -1, "modify");
645 	gctl_ro_param(r, "index", sizeof(idx), &idx);
646 	if (hadlabel || items[3].text[0] != '\0')
647 		gctl_ro_param(r, "label", -1, items[3].text);
648 	gctl_ro_param(r, "type", -1, items[0].text);
649 	errstr = gctl_issue(r);
650 	if (errstr != NULL && errstr[0] != '\0') {
651 		gpart_show_error("Error", NULL, errstr);
652 		gctl_free(r);
653 		goto editpart;
654 	}
655 	gctl_free(r);
656 
657 	newfs_command(items[0].text, newfs, 1);
658 	set_default_part_metadata(pp->lg_name, scheme, items[0].text,
659 	    items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
660 	    newfs : NULL);
661 
662 endedit:
663 	if (strcmp(oldtype, items[0].text) != 0 && cp != NULL)
664 		gpart_destroy(cp->lg_geom);
665 	if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text,
666 	    "freebsd") == 0)
667 		gpart_partition(pp->lg_name, "BSD");
668 
669 	for (i = 0; i < nitems(items); i++)
670 		if (items[i].text_free)
671 			free(items[i].text);
672 }
673 
674 void
675 set_default_part_metadata(const char *name, const char *scheme,
676     const char *type, const char *mountpoint, const char *newfs)
677 {
678 	struct partition_metadata *md;
679 	char *zpool_name = NULL;
680 	const char *default_bootmount = NULL;
681 	int i;
682 
683 	/* Set part metadata */
684 	md = get_part_metadata(name, 1);
685 
686 	if (newfs) {
687 		if (md->newfs != NULL) {
688 			free(md->newfs);
689 			md->newfs = NULL;
690 		}
691 
692 		if (newfs != NULL && newfs[0] != '\0') {
693 			md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
694 			    strlen(mountpoint) + 5 + strlen(name) + 1);
695 			if (strcmp("freebsd-zfs", type) == 0) {
696 				zpool_name = strdup((strlen(mountpoint) == 1) ?
697 				    "root" : &mountpoint[1]);
698 				for (i = 0; zpool_name[i] != 0; i++)
699 					if (!isalnum(zpool_name[i]))
700 						zpool_name[i] = '_';
701 				sprintf(md->newfs, "%s %s /dev/%s", newfs,
702 				    zpool_name, name);
703 			} else {
704 				sprintf(md->newfs, "%s /dev/%s", newfs, name);
705 			}
706 		}
707 	}
708 
709 	if (strcmp(type, "freebsd-swap") == 0)
710 		mountpoint = "none";
711 	if (strcmp(type, bootpart_type(scheme, &default_bootmount)) == 0) {
712 		if (default_bootmount == NULL) {
713 
714 			int fd = open("/tmp/bsdinstall-esps", O_CREAT | O_WRONLY | O_APPEND,
715 						  0600);
716 			if (fd > 0) {
717 				write(fd, md->name, strlen(md->name));
718 				close(fd);
719 			}
720 
721 			md->bootcode = 1;
722 		}
723 		else if (mountpoint == NULL || strlen(mountpoint) == 0)
724 			mountpoint = default_bootmount;
725 	}
726 
727 	/* VTOC8 needs partcode at the start of partitions */
728 	if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
729 	    || strcmp(type, "freebsd-zfs") == 0))
730 		md->bootcode = 1;
731 
732 	if (mountpoint == NULL || mountpoint[0] == '\0') {
733 		if (md->fstab != NULL) {
734 			free(md->fstab->fs_spec);
735 			free(md->fstab->fs_file);
736 			free(md->fstab->fs_vfstype);
737 			free(md->fstab->fs_mntops);
738 			free(md->fstab->fs_type);
739 			free(md->fstab);
740 			md->fstab = NULL;
741 		}
742 	} else {
743 		if (md->fstab == NULL) {
744 			md->fstab = malloc(sizeof(struct fstab));
745 		} else {
746 			free(md->fstab->fs_spec);
747 			free(md->fstab->fs_file);
748 			free(md->fstab->fs_vfstype);
749 			free(md->fstab->fs_mntops);
750 			free(md->fstab->fs_type);
751 		}
752 		if (strcmp("freebsd-zfs", type) == 0) {
753 			md->fstab->fs_spec = strdup(zpool_name);
754 		} else {
755 			md->fstab->fs_spec = malloc(strlen(name) +
756 			    strlen("/dev/") + 1);
757 			sprintf(md->fstab->fs_spec, "/dev/%s", name);
758 		}
759 		md->fstab->fs_file = strdup(mountpoint);
760 		/* Get VFS from text after freebsd-, if possible */
761 		if (strncmp("freebsd-", type, 8) == 0)
762 			md->fstab->fs_vfstype = strdup(&type[8]);
763 		else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0
764 	     	    || strcmp("ms-basic-data", type) == 0)
765 			md->fstab->fs_vfstype = strdup("msdosfs");
766 		else
767 			md->fstab->fs_vfstype = strdup(type); /* Guess */
768 		if (strcmp(type, "freebsd-swap") == 0) {
769 			md->fstab->fs_type = strdup(FSTAB_SW);
770 			md->fstab->fs_freq = 0;
771 			md->fstab->fs_passno = 0;
772 		} else if (strcmp(type, "freebsd-zfs") == 0) {
773 			md->fstab->fs_type = strdup(FSTAB_RW);
774 			md->fstab->fs_freq = 0;
775 			md->fstab->fs_passno = 0;
776 		} else {
777 			md->fstab->fs_type = strdup(FSTAB_RW);
778 			if (strcmp(mountpoint, "/") == 0) {
779 				md->fstab->fs_freq = 1;
780 				md->fstab->fs_passno = 1;
781 			} else {
782 				md->fstab->fs_freq = 2;
783 				md->fstab->fs_passno = 2;
784 			}
785 		}
786 		md->fstab->fs_mntops = strdup(md->fstab->fs_type);
787 	}
788 
789 	if (zpool_name != NULL)
790 		free(zpool_name);
791 }
792 
793 static
794 int part_compare(const void *xa, const void *xb)
795 {
796 	struct gprovider **a = (struct gprovider **)xa;
797 	struct gprovider **b = (struct gprovider **)xb;
798 	intmax_t astart, bstart;
799 	struct gconfig *gc;
800 
801 	astart = bstart = 0;
802 	LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
803 		if (strcmp(gc->lg_name, "start") == 0) {
804 			astart = strtoimax(gc->lg_val, NULL, 0);
805 			break;
806 		}
807 	LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
808 		if (strcmp(gc->lg_name, "start") == 0) {
809 			bstart = strtoimax(gc->lg_val, NULL, 0);
810 			break;
811 		}
812 
813 	if (astart < bstart)
814 		return -1;
815 	else if (astart > bstart)
816 		return 1;
817 	else
818 		return 0;
819 }
820 
821 intmax_t
822 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
823 {
824 	struct gconfig *gc;
825 	struct gprovider *pp, **providers;
826 	intmax_t sectorsize, stripesize, offset;
827 	intmax_t lastend;
828 	intmax_t start, end;
829 	intmax_t maxsize, maxstart;
830 	intmax_t partstart, partend;
831 	int i, nparts;
832 
833 	/* Now get the maximum free size and free start */
834 	start = end = 0;
835 	LIST_FOREACH(gc, &geom->lg_config, lg_config) {
836 		if (strcmp(gc->lg_name, "first") == 0)
837 			start = strtoimax(gc->lg_val, NULL, 0);
838 		if (strcmp(gc->lg_name, "last") == 0)
839 			end = strtoimax(gc->lg_val, NULL, 0);
840 	}
841 
842 	i = nparts = 0;
843 	LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
844 		nparts++;
845 	providers = calloc(nparts, sizeof(providers[0]));
846 	LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
847 		providers[i++] = pp;
848 	qsort(providers, nparts, sizeof(providers[0]), part_compare);
849 
850 	lastend = start - 1;
851 	maxsize = 0;
852 	for (i = 0; i < nparts; i++) {
853 		pp = providers[i];
854 
855 		LIST_FOREACH(gc, &pp->lg_config, lg_config) {
856 			if (strcmp(gc->lg_name, "start") == 0)
857 				partstart = strtoimax(gc->lg_val, NULL, 0);
858 			if (strcmp(gc->lg_name, "end") == 0)
859 				partend = strtoimax(gc->lg_val, NULL, 0);
860 		}
861 
862 		if (partstart - lastend > maxsize) {
863 			maxsize = partstart - lastend - 1;
864 			maxstart = lastend + 1;
865 		}
866 
867 		lastend = partend;
868 	}
869 
870 	if (end - lastend > maxsize) {
871 		maxsize = end - lastend;
872 		maxstart = lastend + 1;
873 	}
874 
875 	pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
876 
877 	/*
878 	 * Round the start and size of the largest available space up to
879 	 * the nearest multiple of the adjusted stripe size.
880 	 *
881 	 * The adjusted stripe size is the least common multiple of the
882 	 * actual stripe size, or the sector size if no stripe size was
883 	 * reported, and 4096.  The reason for this is that contemporary
884 	 * disks often have 4096-byte physical sectors but report 512
885 	 * bytes instead for compatibility with older / broken operating
886 	 * systems and BIOSes.  For the same reasons, virtualized storage
887 	 * may also report a 512-byte stripe size, or none at all.
888 	 */
889 	sectorsize = pp->lg_sectorsize;
890 	if ((stripesize = pp->lg_stripesize) == 0)
891 		stripesize = sectorsize;
892 	while (stripesize % 4096 != 0)
893 		stripesize *= 2;
894 	if ((offset = maxstart * sectorsize % stripesize) != 0) {
895 		offset = (stripesize - offset) / sectorsize;
896 		maxstart += offset;
897 		maxsize -= offset;
898 	}
899 
900 	if (npartstart != NULL)
901 		*npartstart = maxstart;
902 
903 	return (maxsize);
904 }
905 
906 static size_t
907 add_boot_partition(struct ggeom *geom, struct gprovider *pp,
908     const char *scheme, int interactive)
909 {
910 	struct gconfig *gc;
911 	struct gprovider *ppi;
912 	int choice;
913 
914 	/* Check for existing freebsd-boot partition */
915 	LIST_FOREACH(ppi, &geom->lg_provider, lg_provider) {
916 		struct partition_metadata *md;
917 		const char *bootmount = NULL;
918 
919 		LIST_FOREACH(gc, &ppi->lg_config, lg_config)
920 			if (strcmp(gc->lg_name, "type") == 0)
921 				break;
922 		if (gc == NULL)
923 			continue;
924 		if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)
925 			continue;
926 
927 		/*
928 		 * If the boot partition is not mountable and needs partcode,
929 		 * but doesn't have it, it doesn't satisfy our requirements.
930 		 */
931 		md = get_part_metadata(ppi->lg_name, 0);
932 		if (bootmount == NULL && (md == NULL || !md->bootcode))
933 			continue;
934 
935 		/* If it is mountable, but mounted somewhere else, remount */
936 		if (bootmount != NULL && md != NULL && md->fstab != NULL
937 		    && strlen(md->fstab->fs_file) > 0
938 		    && strcmp(md->fstab->fs_file, bootmount) != 0)
939 			continue;
940 
941 		/* If it is mountable, but mountpoint is not set, mount it */
942 		if (bootmount != NULL && md == NULL)
943 			set_default_part_metadata(ppi->lg_name, scheme,
944 			    gc->lg_val, bootmount, NULL);
945 
946 		/* Looks good at this point, no added data needed */
947 		return (0);
948 	}
949 
950 	if (interactive)
951 		choice = dialog_yesno("Boot Partition",
952 		    "This partition scheme requires a boot partition "
953 		    "for the disk to be bootable. Would you like to "
954 		    "make one now?", 0, 0);
955 	else
956 		choice = 0;
957 
958 	if (choice == 0) { /* yes */
959 		struct partition_metadata *md;
960 		const char *bootmount = NULL;
961 		char *bootpartname = NULL;
962 		char sizestr[7];
963 
964 		humanize_number(sizestr, 7,
965 		    bootpart_size(scheme), "B", HN_AUTOSCALE,
966 		    HN_NOSPACE | HN_DECIMAL);
967 
968 		gpart_create(pp, bootpart_type(scheme, &bootmount),
969 		    sizestr, bootmount, &bootpartname, 0);
970 
971 		if (bootpartname == NULL) /* Error reported to user already */
972 			return 0;
973 
974 		/* If the part is not mountable, make sure newfs isn't set */
975 		if (bootmount == NULL) {
976 			md = get_part_metadata(bootpartname, 0);
977 			if (md != NULL && md->newfs != NULL) {
978 				free(md->newfs);
979 				md->newfs = NULL;
980 			}
981 		}
982 
983 		free(bootpartname);
984 
985 		return (bootpart_size(scheme));
986 	}
987 
988 	return (0);
989 }
990 
991 void
992 gpart_create(struct gprovider *pp, const char *default_type,
993     const char *default_size, const char *default_mountpoint,
994     char **partname, int interactive)
995 {
996 	struct gctl_req *r;
997 	struct gconfig *gc;
998 	struct gconsumer *cp;
999 	struct ggeom *geom;
1000 	const char *errstr, *scheme;
1001 	char sizestr[32], startstr[32], output[64], *newpartname;
1002 	char newfs[255], options_fstype[64];
1003 	intmax_t maxsize, size, sector, firstfree, stripe;
1004 	uint64_t bytes;
1005 	int nitems, choice, junk;
1006 	unsigned i;
1007 
1008 	DIALOG_FORMITEM items[] = {
1009 		{0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
1010 		    FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
1011 		    "freebsd-swap)", FALSE},
1012 		{0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
1013 		    FALSE, "Partition size. Append K, M, G for kilobytes, "
1014 		    "megabytes or gigabytes.", FALSE},
1015 		{0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
1016 		    FALSE, "Path at which to mount partition (blank for "
1017 		    "swap, / for root filesystem)", FALSE},
1018 		{0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
1019 		    "Partition name. Not all partition schemes support this.",
1020 		    FALSE},
1021 	};
1022 
1023 	if (partname != NULL)
1024 		*partname = NULL;
1025 
1026 	/* Record sector and stripe sizes */
1027 	sector = pp->lg_sectorsize;
1028 	stripe = pp->lg_stripesize;
1029 
1030 	/*
1031 	 * Find the PART geom we are manipulating. This may be a consumer of
1032 	 * this provider, or its parent. Check the consumer case first.
1033 	 */
1034 	geom = NULL;
1035 	LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1036 		if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1037 			geom = cp->lg_geom;
1038 			break;
1039 		}
1040 
1041 	if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
1042 		geom = pp->lg_geom;
1043 
1044 	/* Now get the partition scheme */
1045 	scheme = NULL;
1046 	if (geom != NULL) {
1047 		LIST_FOREACH(gc, &geom->lg_config, lg_config)
1048 			if (strcmp(gc->lg_name, "scheme") == 0)
1049 				scheme = gc->lg_val;
1050 	}
1051 
1052 	if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
1053 		if (gpart_partition(pp->lg_name, NULL) == 0)
1054 			dialog_msgbox("",
1055 			    "The partition table has been successfully created."
1056 			    " Please press Create again to create partitions.",
1057 			    0, 0, TRUE);
1058 
1059 		return;
1060 	}
1061 
1062 	/*
1063 	 * If we still don't have a geom, either the user has
1064 	 * canceled partitioning or there has been an error which has already
1065 	 * been displayed, so bail.
1066 	 */
1067 	if (geom == NULL)
1068 		return;
1069 
1070 	maxsize = size = gpart_max_free(geom, &firstfree);
1071 	if (size <= 0) {
1072 		dialog_msgbox("Error", "No free space left on device.", 0, 0,
1073 		    TRUE);
1074 		return;
1075 	}
1076 
1077 	humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
1078 	    HN_NOSPACE | HN_DECIMAL);
1079 	items[1].text = sizestr;
1080 
1081 	/* Special-case the MBR default type for nested partitions */
1082 	if (strcmp(scheme, "MBR") == 0) {
1083 		items[0].text = "freebsd";
1084 		items[0].help = "Filesystem type (e.g. freebsd, fat32)";
1085 	}
1086 
1087 	nitems = scheme_supports_labels(scheme) ? 4 : 3;
1088 
1089 	if (default_type != NULL)
1090 		items[0].text = (char *)default_type;
1091 	if (default_size != NULL)
1092 		items[1].text = (char *)default_size;
1093 	if (default_mountpoint != NULL)
1094 		items[2].text = (char *)default_mountpoint;
1095 
1096 	/* Default options */
1097 	strncpy(options_fstype, items[0].text,
1098 	    sizeof(options_fstype));
1099 	newfs_command(options_fstype, newfs, 1);
1100 addpartform:
1101 	if (interactive) {
1102 		dialog_vars.extra_label = "Options";
1103 		dialog_vars.extra_button = TRUE;
1104 		choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1105 		    items, &junk);
1106 		dialog_vars.extra_button = FALSE;
1107 		switch (choice) {
1108 		case 0: /* OK */
1109 			break;
1110 		case 1: /* Cancel */
1111 			return;
1112 		case 3: /* Options */
1113 			strncpy(options_fstype, items[0].text,
1114 			    sizeof(options_fstype));
1115 			newfs_command(options_fstype, newfs, 0);
1116 			goto addpartform;
1117 		}
1118 	}
1119 
1120 	/*
1121 	 * If the user changed the fs type after specifying options, undo
1122 	 * their choices in favor of the new filesystem's defaults.
1123 	 */
1124 	if (strcmp(options_fstype, items[0].text) != 0) {
1125 		strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1126 		newfs_command(options_fstype, newfs, 1);
1127 	}
1128 
1129 	size = maxsize;
1130 	if (strlen(items[1].text) > 0) {
1131 		if (expand_number(items[1].text, &bytes) != 0) {
1132 			char error[512];
1133 
1134 			sprintf(error, "Invalid size: %s\n", strerror(errno));
1135 			dialog_msgbox("Error", error, 0, 0, TRUE);
1136 			goto addpartform;
1137 		}
1138 		size = MIN((intmax_t)(bytes/sector), maxsize);
1139 	}
1140 
1141 	/* Check if the label has a / in it */
1142 	if (strchr(items[3].text, '/') != NULL) {
1143 		dialog_msgbox("Error", "Label contains a /, which is not an "
1144 		    "allowed character.", 0, 0, TRUE);
1145 		goto addpartform;
1146 	}
1147 
1148 	/* Warn if no mountpoint set */
1149 	if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1150 	    items[2].text[0] != '/') {
1151 		choice = 0;
1152 		if (interactive) {
1153 			dialog_vars.defaultno = TRUE;
1154 			choice = dialog_yesno("Warning",
1155 			    "This partition does not have a valid mountpoint "
1156 			    "(for the partition from which you intend to boot the "
1157 			    "operating system, the mountpoint should be /). Are you "
1158 			    "sure you want to continue?"
1159 			, 0, 0);
1160 			dialog_vars.defaultno = FALSE;
1161 		}
1162 		if (choice == 1) /* cancel */
1163 			goto addpartform;
1164 	}
1165 
1166 	/*
1167 	 * Error if this scheme needs nested partitions, this is one, and
1168 	 * a mountpoint was set.
1169 	 */
1170 	if (strcmp(items[0].text, "freebsd") == 0 &&
1171 	    strlen(items[2].text) > 0) {
1172 		dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1173 		    "nested BSD-type partition schemes and cannot have "
1174 		    "mountpoints. After creating one, select it and press "
1175 		    "Create again to add the actual file systems.", 0, 0, TRUE);
1176 		goto addpartform;
1177 	}
1178 
1179 	/* If this is the root partition, check that this scheme is bootable */
1180 	if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1181 		char message[512];
1182 		sprintf(message, "This partition scheme (%s) is not bootable "
1183 		    "on this platform. Are you sure you want to proceed?",
1184 		    scheme);
1185 		dialog_vars.defaultno = TRUE;
1186 		choice = dialog_yesno("Warning", message, 0, 0);
1187 		dialog_vars.defaultno = FALSE;
1188 		if (choice == 1) /* cancel */
1189 			goto addpartform;
1190 	}
1191 
1192 	/* If this is the root partition, check that this fs is bootable */
1193 	if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1194 	    items[0].text)) {
1195 		char message[512];
1196 		sprintf(message, "This file system (%s) is not bootable "
1197 		    "on this system. Are you sure you want to proceed?",
1198 		    items[0].text);
1199 		dialog_vars.defaultno = TRUE;
1200 		choice = dialog_yesno("Warning", message, 0, 0);
1201 		dialog_vars.defaultno = FALSE;
1202 		if (choice == 1) /* cancel */
1203 			goto addpartform;
1204 	}
1205 
1206 	/*
1207 	 * If this is the root partition, and we need a boot partition, ask
1208 	 * the user to add one.
1209 	 */
1210 
1211 	if ((strcmp(items[0].text, "freebsd") == 0 ||
1212 	    strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0) {
1213 		size_t bytes = add_boot_partition(geom, pp, scheme,
1214 		    interactive);
1215 
1216 		/* Now adjust the part we are really adding forward */
1217 		if (bytes > 0) {
1218 			firstfree += bytes / sector;
1219 			size -= (bytes + stripe)/sector;
1220 			if (stripe > 0 && (firstfree*sector % stripe) != 0)
1221 				firstfree += (stripe - ((firstfree*sector) %
1222 				    stripe)) / sector;
1223 		}
1224 	}
1225 
1226 	r = gctl_get_handle();
1227 	gctl_ro_param(r, "class", -1, "PART");
1228 	gctl_ro_param(r, "arg0", -1, geom->lg_name);
1229 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1230 	gctl_ro_param(r, "verb", -1, "add");
1231 
1232 	gctl_ro_param(r, "type", -1, items[0].text);
1233 	snprintf(sizestr, sizeof(sizestr), "%jd", size);
1234 	gctl_ro_param(r, "size", -1, sizestr);
1235 	snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1236 	gctl_ro_param(r, "start", -1, startstr);
1237 	if (items[3].text[0] != '\0')
1238 		gctl_ro_param(r, "label", -1, items[3].text);
1239 	gctl_rw_param(r, "output", sizeof(output), output);
1240 	errstr = gctl_issue(r);
1241 	if (errstr != NULL && errstr[0] != '\0') {
1242 		gpart_show_error("Error", NULL, errstr);
1243 		gctl_free(r);
1244 		goto addpartform;
1245 	}
1246 	newpartname = strtok(output, " ");
1247 	gctl_free(r);
1248 
1249 	/*
1250 	 * Try to destroy any geom that gpart picked up already here from
1251 	 * dirty blocks.
1252 	 */
1253 	r = gctl_get_handle();
1254 	gctl_ro_param(r, "class", -1, "PART");
1255 	gctl_ro_param(r, "arg0", -1, newpartname);
1256 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1257 	junk = 1;
1258 	gctl_ro_param(r, "force", sizeof(junk), &junk);
1259 	gctl_ro_param(r, "verb", -1, "destroy");
1260 	gctl_issue(r); /* Error usually expected and non-fatal */
1261 	gctl_free(r);
1262 
1263 
1264 	if (strcmp(items[0].text, "freebsd") == 0)
1265 		gpart_partition(newpartname, "BSD");
1266 	else
1267 		set_default_part_metadata(newpartname, scheme,
1268 		    items[0].text, items[2].text, newfs);
1269 
1270 	for (i = 0; i < nitems(items); i++)
1271 		if (items[i].text_free)
1272 			free(items[i].text);
1273 
1274 	if (partname != NULL)
1275 		*partname = strdup(newpartname);
1276 }
1277 
1278 void
1279 gpart_delete(struct gprovider *pp)
1280 {
1281 	struct gconfig *gc;
1282 	struct ggeom *geom;
1283 	struct gconsumer *cp;
1284 	struct gctl_req *r;
1285 	const char *errstr;
1286 	intmax_t idx;
1287 	int is_partition;
1288 
1289 	/* Is it a partition? */
1290 	is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1291 
1292 	/* Find out if this is the root of a gpart geom */
1293 	geom = NULL;
1294 	LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1295 		if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1296 			geom = cp->lg_geom;
1297 			break;
1298 		}
1299 
1300 	/* If so, destroy all children */
1301 	if (geom != NULL) {
1302 		gpart_destroy(geom);
1303 
1304 		/* If this is a partition, revert it, so it can be deleted */
1305 		if (is_partition) {
1306 			r = gctl_get_handle();
1307 			gctl_ro_param(r, "class", -1, "PART");
1308 			gctl_ro_param(r, "arg0", -1, geom->lg_name);
1309 			gctl_ro_param(r, "verb", -1, "undo");
1310 			gctl_issue(r); /* Ignore non-fatal errors */
1311 			gctl_free(r);
1312 		}
1313 	}
1314 
1315 	/*
1316 	 * If this is not a partition, see if that is a problem, complain if
1317 	 * necessary, and return always, since we need not do anything further,
1318 	 * error or no.
1319 	 */
1320 	if (!is_partition) {
1321 		if (geom == NULL)
1322 			dialog_msgbox("Error",
1323 			    "Only partitions can be deleted.", 0, 0, TRUE);
1324 		return;
1325 	}
1326 
1327 	r = gctl_get_handle();
1328 	gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1329 	gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1330 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1331 	gctl_ro_param(r, "verb", -1, "delete");
1332 
1333 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1334 		if (strcmp(gc->lg_name, "index") == 0) {
1335 			idx = atoi(gc->lg_val);
1336 			gctl_ro_param(r, "index", sizeof(idx), &idx);
1337 			break;
1338 		}
1339 	}
1340 
1341 	errstr = gctl_issue(r);
1342 	if (errstr != NULL && errstr[0] != '\0') {
1343 		gpart_show_error("Error", NULL, errstr);
1344 		gctl_free(r);
1345 		return;
1346 	}
1347 
1348 	gctl_free(r);
1349 
1350 	delete_part_metadata(pp->lg_name);
1351 }
1352 
1353 void
1354 gpart_revert_all(struct gmesh *mesh)
1355 {
1356 	struct gclass *classp;
1357 	struct gconfig *gc;
1358 	struct ggeom *gp;
1359 	struct gctl_req *r;
1360 	const char *modified;
1361 
1362 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1363 		if (strcmp(classp->lg_name, "PART") == 0)
1364 			break;
1365 	}
1366 
1367 	if (strcmp(classp->lg_name, "PART") != 0) {
1368 		dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1369 		return;
1370 	}
1371 
1372 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1373 		modified = "true"; /* XXX: If we don't know (kernel too old),
1374 				    * assume there are modifications. */
1375 		LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1376 			if (strcmp(gc->lg_name, "modified") == 0) {
1377 				modified = gc->lg_val;
1378 				break;
1379 			}
1380 		}
1381 
1382 		if (strcmp(modified, "false") == 0)
1383 			continue;
1384 
1385 		r = gctl_get_handle();
1386 		gctl_ro_param(r, "class", -1, "PART");
1387 		gctl_ro_param(r, "arg0", -1, gp->lg_name);
1388 		gctl_ro_param(r, "verb", -1, "undo");
1389 
1390 		gctl_issue(r);
1391 		gctl_free(r);
1392 	}
1393 }
1394 
1395 void
1396 gpart_commit(struct gmesh *mesh)
1397 {
1398 	struct partition_metadata *md;
1399 	struct gclass *classp;
1400 	struct ggeom *gp;
1401 	struct gconfig *gc;
1402 	struct gconsumer *cp;
1403 	struct gprovider *pp;
1404 	struct gctl_req *r;
1405 	const char *errstr;
1406 	const char *modified;
1407 	const char *rootfs;
1408 
1409 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1410 		if (strcmp(classp->lg_name, "PART") == 0)
1411 			break;
1412 	}
1413 
1414 	/* Figure out what filesystem / uses */
1415 	rootfs = "ufs"; /* Assume ufs if nothing else present */
1416 	TAILQ_FOREACH(md, &part_metadata, metadata) {
1417 		if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1418 			rootfs = md->fstab->fs_vfstype;
1419 			break;
1420 		}
1421 	}
1422 
1423 	if (strcmp(classp->lg_name, "PART") != 0) {
1424 		dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1425 		return;
1426 	}
1427 
1428 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1429 		modified = "true"; /* XXX: If we don't know (kernel too old),
1430 				    * assume there are modifications. */
1431 		LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1432 			if (strcmp(gc->lg_name, "modified") == 0) {
1433 				modified = gc->lg_val;
1434 				break;
1435 			}
1436 		}
1437 
1438 		if (strcmp(modified, "false") == 0)
1439 			continue;
1440 
1441 		/* Add bootcode if necessary, before the commit */
1442 		md = get_part_metadata(gp->lg_name, 0);
1443 		if (md != NULL && md->bootcode)
1444 			gpart_bootcode(gp);
1445 
1446 		/* Now install partcode on its partitions, if necessary */
1447 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1448 			md = get_part_metadata(pp->lg_name, 0);
1449 			if (md == NULL || !md->bootcode)
1450 				continue;
1451 
1452 			/* Mark this partition active if that's required */
1453 			gpart_activate(pp);
1454 
1455 			/* Check if the partition has sub-partitions */
1456 			LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1457 				if (strcmp(cp->lg_geom->lg_class->lg_name,
1458 				    "PART") == 0)
1459 					break;
1460 
1461 			if (cp == NULL) /* No sub-partitions */
1462 				gpart_partcode(pp, rootfs);
1463 		}
1464 
1465 		r = gctl_get_handle();
1466 		gctl_ro_param(r, "class", -1, "PART");
1467 		gctl_ro_param(r, "arg0", -1, gp->lg_name);
1468 		gctl_ro_param(r, "verb", -1, "commit");
1469 
1470 		errstr = gctl_issue(r);
1471 		if (errstr != NULL && errstr[0] != '\0')
1472 			gpart_show_error("Error", NULL, errstr);
1473 		gctl_free(r);
1474 	}
1475 }
1476 
1477