xref: /freebsd/usr.sbin/bsdinstall/partedit/gpart_ops.c (revision 47ead00d5ba986ee615123b2fba1d2af3e7bf1f5)
12118f387SNathan Whitehorn /*-
22118f387SNathan Whitehorn  * Copyright (c) 2011 Nathan Whitehorn
32118f387SNathan Whitehorn  * All rights reserved.
42118f387SNathan Whitehorn  *
52118f387SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
62118f387SNathan Whitehorn  * modification, are permitted provided that the following conditions
72118f387SNathan Whitehorn  * are met:
82118f387SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
92118f387SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
102118f387SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
112118f387SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
122118f387SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
132118f387SNathan Whitehorn  *
142118f387SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152118f387SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162118f387SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172118f387SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182118f387SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192118f387SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202118f387SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212118f387SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222118f387SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232118f387SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242118f387SNathan Whitehorn  * SUCH DAMAGE.
252118f387SNathan Whitehorn  *
262118f387SNathan Whitehorn  * $FreeBSD$
272118f387SNathan Whitehorn  */
282118f387SNathan Whitehorn 
292118f387SNathan Whitehorn #include <sys/param.h>
306e15678aSNathan Whitehorn #include <sys/stat.h>
312118f387SNathan Whitehorn #include <errno.h>
322118f387SNathan Whitehorn #include <libutil.h>
332118f387SNathan Whitehorn #include <inttypes.h>
342118f387SNathan Whitehorn 
352118f387SNathan Whitehorn #include <libgeom.h>
362118f387SNathan Whitehorn #include <dialog.h>
372118f387SNathan Whitehorn #include <dlg_keys.h>
382118f387SNathan Whitehorn 
392118f387SNathan Whitehorn #include "partedit.h"
402118f387SNathan Whitehorn 
412118f387SNathan Whitehorn #define GPART_FLAGS "x" /* Do not commit changes by default */
422118f387SNathan Whitehorn 
432118f387SNathan Whitehorn static void
442118f387SNathan Whitehorn gpart_show_error(const char *title, const char *explanation, const char *errstr)
452118f387SNathan Whitehorn {
462118f387SNathan Whitehorn 	char *errmsg;
472118f387SNathan Whitehorn 	char message[512];
482118f387SNathan Whitehorn 	int error;
492118f387SNathan Whitehorn 
502118f387SNathan Whitehorn 	if (explanation == NULL)
512118f387SNathan Whitehorn 		explanation = "";
522118f387SNathan Whitehorn 
532118f387SNathan Whitehorn 	error = strtol(errstr, &errmsg, 0);
542118f387SNathan Whitehorn 	if (errmsg != errstr) {
552118f387SNathan Whitehorn 		while (errmsg[0] == ' ')
562118f387SNathan Whitehorn 			errmsg++;
572118f387SNathan Whitehorn 		if (errmsg[0] != '\0')
582118f387SNathan Whitehorn 			sprintf(message, "%s%s. %s", explanation,
592118f387SNathan Whitehorn 			    strerror(error), errmsg);
602118f387SNathan Whitehorn 		else
612118f387SNathan Whitehorn 			sprintf(message, "%s%s", explanation, strerror(error));
622118f387SNathan Whitehorn 	} else {
632118f387SNathan Whitehorn 		sprintf(message, "%s%s", explanation, errmsg);
642118f387SNathan Whitehorn 	}
652118f387SNathan Whitehorn 
662118f387SNathan Whitehorn 	dialog_msgbox(title, message, 0, 0, TRUE);
672118f387SNathan Whitehorn }
682118f387SNathan Whitehorn 
69c67f41d0SNathan Whitehorn static int
70c67f41d0SNathan Whitehorn scheme_supports_labels(const char *scheme)
71c67f41d0SNathan Whitehorn {
72c67f41d0SNathan Whitehorn 	if (strcmp(scheme, "APM") == 0)
73c67f41d0SNathan Whitehorn 		return (1);
74c67f41d0SNathan Whitehorn 	if (strcmp(scheme, "GPT") == 0)
75c67f41d0SNathan Whitehorn 		return (1);
76c67f41d0SNathan Whitehorn 	if (strcmp(scheme, "PC98") == 0)
77c67f41d0SNathan Whitehorn 		return (1);
78c67f41d0SNathan Whitehorn 
79c67f41d0SNathan Whitehorn 	return (0);
80c67f41d0SNathan Whitehorn }
81c67f41d0SNathan Whitehorn 
8250de5d07SNathan Whitehorn static void
8350de5d07SNathan Whitehorn newfs_command(const char *fstype, char *command, int use_default)
8450de5d07SNathan Whitehorn {
8550de5d07SNathan Whitehorn 	if (strcmp(fstype, "freebsd-ufs") == 0) {
8650de5d07SNathan Whitehorn 		int i;
8750de5d07SNathan Whitehorn 		DIALOG_LISTITEM items[] = {
8850de5d07SNathan Whitehorn 			{"UFS1", "UFS Version 1",
8950de5d07SNathan Whitehorn 			    "Use version 1 of the UFS file system instead "
9050de5d07SNathan Whitehorn 			    "of version 2 (not recommended)", 0 },
9150de5d07SNathan Whitehorn 			{"SU", "Softupdates",
9250de5d07SNathan Whitehorn 			    "Enable softupdates (default)", 1 },
9350de5d07SNathan Whitehorn 			{"SUJ", "Softupdates journaling",
9450de5d07SNathan Whitehorn 			    "Enable file system journaling (default - "
9550de5d07SNathan Whitehorn 			    "turn off for SSDs)", 1 },
9650de5d07SNathan Whitehorn 			{"TRIM", "Enable SSD TRIM support",
9750de5d07SNathan Whitehorn 			    "Enable TRIM support, useful on solid-state drives",
9850de5d07SNathan Whitehorn 			    0 },
9950de5d07SNathan Whitehorn 		};
10050de5d07SNathan Whitehorn 
10150de5d07SNathan Whitehorn 		if (!use_default) {
10250de5d07SNathan Whitehorn 			int choice;
10350de5d07SNathan Whitehorn 			choice = dlg_checklist("UFS Options", "", 0, 0, 0,
10450de5d07SNathan Whitehorn 			    sizeof(items)/sizeof(items[0]), items, NULL,
10550de5d07SNathan Whitehorn 			    FLAG_CHECK, &i);
10650de5d07SNathan Whitehorn 			if (choice == 1) /* Cancel */
10750de5d07SNathan Whitehorn 				return;
10850de5d07SNathan Whitehorn 		}
10950de5d07SNathan Whitehorn 
11050de5d07SNathan Whitehorn 		strcpy(command, "newfs ");
11150de5d07SNathan Whitehorn 		for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
11250de5d07SNathan Whitehorn 			if (items[i].state == 0)
11350de5d07SNathan Whitehorn 				continue;
11450de5d07SNathan Whitehorn 			if (strcmp(items[i].name, "UFS1") == 0)
11550de5d07SNathan Whitehorn 				strcat(command, "-O1 ");
11650de5d07SNathan Whitehorn 			else if (strcmp(items[i].name, "SU") == 0)
11750de5d07SNathan Whitehorn 				strcat(command, "-U ");
11850de5d07SNathan Whitehorn 			else if (strcmp(items[i].name, "SUJ") == 0)
11950de5d07SNathan Whitehorn 				strcat(command, "-j ");
12050de5d07SNathan Whitehorn 			else if (strcmp(items[i].name, "TRIM") == 0)
12150de5d07SNathan Whitehorn 				strcat(command, "-t ");
12250de5d07SNathan Whitehorn 		}
1236e15678aSNathan Whitehorn 	} else if (strcmp(fstype, "freebsd-zfs") == 0) {
1246e15678aSNathan Whitehorn 		int i;
1256e15678aSNathan Whitehorn 		DIALOG_LISTITEM items[] = {
1266e15678aSNathan Whitehorn 			{"fletcher4", "checksum algorithm: fletcher4",
1276e15678aSNathan Whitehorn 			    "Use fletcher4 for data integrity checking. "
1286e15678aSNathan Whitehorn 			    "(default)", 1 },
1296e15678aSNathan Whitehorn 			{"fletcher2", "checksum algorithm: fletcher2",
1306e15678aSNathan Whitehorn 			    "Use fletcher2 for data integrity checking. "
1316e15678aSNathan Whitehorn 			    "(not recommended)", 0 },
1326e15678aSNathan Whitehorn 			{"sha256", "checksum algorithm: sha256",
1336e15678aSNathan Whitehorn 			    "Use sha256 for data integrity checking. "
1346e15678aSNathan Whitehorn 			    "(not recommended)", 0 },
1356e15678aSNathan Whitehorn 			{"atime", "Update atimes for files",
1366e15678aSNathan Whitehorn 			    "Disable atime update", 0 },
1376e15678aSNathan Whitehorn 		};
1386e15678aSNathan Whitehorn 
1396e15678aSNathan Whitehorn 		if (!use_default) {
1406e15678aSNathan Whitehorn 			int choice;
1416e15678aSNathan Whitehorn 			choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
1426e15678aSNathan Whitehorn 			    sizeof(items)/sizeof(items[0]), items, NULL,
1436e15678aSNathan Whitehorn 			    FLAG_CHECK, &i);
1446e15678aSNathan Whitehorn 			if (choice == 1) /* Cancel */
1456e15678aSNathan Whitehorn 				return;
1466e15678aSNathan Whitehorn 		}
1476e15678aSNathan Whitehorn 
1486e15678aSNathan Whitehorn 		strcpy(command, "zpool create -f -m none ");
1496e15678aSNathan Whitehorn 		if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
1506e15678aSNathan Whitehorn 			char zfsboot_path[MAXPATHLEN];
1516e15678aSNathan Whitehorn 			sprintf(zfsboot_path, "%s/zfs",
1526e15678aSNathan Whitehorn 			    getenv("BSDINSTALL_TMPBOOT"));
1536e15678aSNathan Whitehorn 			mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
1546e15678aSNathan Whitehorn 			    S_IROTH | S_IXOTH);
1556e15678aSNathan Whitehorn 			sprintf(command, "%s -o cachefile=%s/zpool.cache ",
1566e15678aSNathan Whitehorn 			    command, zfsboot_path);
1576e15678aSNathan Whitehorn 		}
1586e15678aSNathan Whitehorn 		for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
1596e15678aSNathan Whitehorn 			if (items[i].state == 0)
1606e15678aSNathan Whitehorn 				continue;
1616e15678aSNathan Whitehorn 			if (strcmp(items[i].name, "fletcher4") == 0)
1626e15678aSNathan Whitehorn 				strcat(command, "-O checksum=fletcher4 ");
1636e15678aSNathan Whitehorn 			else if (strcmp(items[i].name, "fletcher2") == 0)
1646e15678aSNathan Whitehorn 				strcat(command, "-O checksum=fletcher2 ");
1656e15678aSNathan Whitehorn 			else if (strcmp(items[i].name, "sha256") == 0)
1666e15678aSNathan Whitehorn 				strcat(command, "-O checksum=sha256 ");
1676e15678aSNathan Whitehorn 			else if (strcmp(items[i].name, "atime") == 0)
1686e15678aSNathan Whitehorn 				strcat(command, "-O atime=off ");
1696e15678aSNathan Whitehorn 		}
17050de5d07SNathan Whitehorn 	} else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) {
17150de5d07SNathan Whitehorn 		int i;
17250de5d07SNathan Whitehorn 		DIALOG_LISTITEM items[] = {
17350de5d07SNathan Whitehorn 			{"FAT32", "FAT Type 32",
17450de5d07SNathan Whitehorn 			    "Create a FAT32 filesystem (default)", 1 },
17550de5d07SNathan Whitehorn 			{"FAT16", "FAT Type 16",
17650de5d07SNathan Whitehorn 			    "Create a FAT16 filesystem", 0 },
17750de5d07SNathan Whitehorn 			{"FAT12", "FAT Type 12",
17850de5d07SNathan Whitehorn 			    "Create a FAT12 filesystem", 0 },
17950de5d07SNathan Whitehorn 		};
18050de5d07SNathan Whitehorn 
18150de5d07SNathan Whitehorn 		if (!use_default) {
18250de5d07SNathan Whitehorn 			int choice;
18350de5d07SNathan Whitehorn 			choice = dlg_checklist("FAT Options", "", 0, 0, 0,
18450de5d07SNathan Whitehorn 			    sizeof(items)/sizeof(items[0]), items, NULL,
18550de5d07SNathan Whitehorn 			    FLAG_RADIO, &i);
18650de5d07SNathan Whitehorn 			if (choice == 1) /* Cancel */
18750de5d07SNathan Whitehorn 				return;
18850de5d07SNathan Whitehorn 		}
18950de5d07SNathan Whitehorn 
19050de5d07SNathan Whitehorn 		strcpy(command, "newfs_msdos ");
19150de5d07SNathan Whitehorn 		for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
19250de5d07SNathan Whitehorn 			if (items[i].state == 0)
19350de5d07SNathan Whitehorn 				continue;
19450de5d07SNathan Whitehorn 			if (strcmp(items[i].name, "FAT32") == 0)
19550de5d07SNathan Whitehorn 				strcat(command, "-F 32 ");
19650de5d07SNathan Whitehorn 			else if (strcmp(items[i].name, "FAT16") == 0)
19750de5d07SNathan Whitehorn 				strcat(command, "-F 16 ");
198ae2e7abfSNathan Whitehorn 			else if (strcmp(items[i].name, "FAT12") == 0)
19950de5d07SNathan Whitehorn 				strcat(command, "-F 12 ");
20050de5d07SNathan Whitehorn 		}
20150de5d07SNathan Whitehorn 	} else {
20250de5d07SNathan Whitehorn 		if (!use_default)
20350de5d07SNathan Whitehorn 			dialog_msgbox("Error", "No configurable options exist "
20450de5d07SNathan Whitehorn 			    "for this filesystem.", 0, 0, TRUE);
20550de5d07SNathan Whitehorn 		command[0] = '\0';
20650de5d07SNathan Whitehorn 	}
20750de5d07SNathan Whitehorn }
20850de5d07SNathan Whitehorn 
2097059fa6fSAllan Jude const char *
2107059fa6fSAllan Jude choose_part_type(const char *def_scheme)
2112118f387SNathan Whitehorn {
2122118f387SNathan Whitehorn 	int cancel, choice;
2137059fa6fSAllan Jude 	const char *scheme = NULL;
2142118f387SNathan Whitehorn 
2152118f387SNathan Whitehorn 	DIALOG_LISTITEM items[] = {
2162118f387SNathan Whitehorn 		{"APM", "Apple Partition Map",
2172118f387SNathan Whitehorn 		    "Bootable on PowerPC Apple Hardware", 0 },
2182118f387SNathan Whitehorn 		{"BSD", "BSD Labels",
2192118f387SNathan Whitehorn 		    "Bootable on most x86 systems", 0 },
2202118f387SNathan Whitehorn 		{"GPT", "GUID Partition Table",
2210fbcaf76SWojciech Macek 		    "Bootable on most x86 systems and EFI aware ARM64", 0 },
2222118f387SNathan Whitehorn 		{"MBR", "DOS Partitions",
2232118f387SNathan Whitehorn 		    "Bootable on most x86 systems", 0 },
2242118f387SNathan Whitehorn 		{"PC98", "NEC PC9801 Partition Table",
2252118f387SNathan Whitehorn 		    "Bootable on NEC PC9801 systems", 0 },
2262118f387SNathan Whitehorn 		{"VTOC8", "Sun VTOC8 Partition Table",
2272118f387SNathan Whitehorn 		    "Bootable on Sun SPARC systems", 0 },
2282118f387SNathan Whitehorn 	};
2292118f387SNathan Whitehorn 
2307059fa6fSAllan Jude parttypemenu:
2317059fa6fSAllan Jude 	dialog_vars.default_item = __DECONST(char *, def_scheme);
2322118f387SNathan Whitehorn 	cancel = dlg_menu("Partition Scheme",
2332118f387SNathan Whitehorn 	    "Select a partition scheme for this volume:", 0, 0, 0,
2342118f387SNathan Whitehorn 	    sizeof(items) / sizeof(items[0]), items, &choice, NULL);
2352118f387SNathan Whitehorn 	dialog_vars.default_item = NULL;
2362118f387SNathan Whitehorn 
2372118f387SNathan Whitehorn 	if (cancel)
2387059fa6fSAllan Jude 		return NULL;
2392118f387SNathan Whitehorn 
2402118f387SNathan Whitehorn 	if (!is_scheme_bootable(items[choice].name)) {
2412118f387SNathan Whitehorn 		char message[512];
2422118f387SNathan Whitehorn 		sprintf(message, "This partition scheme (%s) is not "
2432118f387SNathan Whitehorn 		    "bootable on this platform. Are you sure you want "
2442118f387SNathan Whitehorn 		    "to proceed?", items[choice].name);
2452118f387SNathan Whitehorn 		dialog_vars.defaultno = TRUE;
2462118f387SNathan Whitehorn 		cancel = dialog_yesno("Warning", message, 0, 0);
2472118f387SNathan Whitehorn 		dialog_vars.defaultno = FALSE;
2482118f387SNathan Whitehorn 		if (cancel) /* cancel */
2497059fa6fSAllan Jude 			goto parttypemenu;
2502118f387SNathan Whitehorn 	}
2512118f387SNathan Whitehorn 
2522118f387SNathan Whitehorn 	scheme = items[choice].name;
2537059fa6fSAllan Jude 
2547059fa6fSAllan Jude 	return scheme;
2557059fa6fSAllan Jude }
2567059fa6fSAllan Jude 
2577059fa6fSAllan Jude int
2587059fa6fSAllan Jude gpart_partition(const char *lg_name, const char *scheme)
2597059fa6fSAllan Jude {
2607059fa6fSAllan Jude 	int cancel;
2617059fa6fSAllan Jude 	struct gctl_req *r;
2627059fa6fSAllan Jude 	const char *errstr;
2637059fa6fSAllan Jude 
2647059fa6fSAllan Jude schememenu:
2657059fa6fSAllan Jude 	if (scheme == NULL) {
2667059fa6fSAllan Jude 		scheme = choose_part_type(default_scheme());
2677059fa6fSAllan Jude 
2687059fa6fSAllan Jude 		if (scheme == NULL)
2697059fa6fSAllan Jude 			return (-1);
2707059fa6fSAllan Jude 
2717059fa6fSAllan Jude 		if (!is_scheme_bootable(scheme)) {
2727059fa6fSAllan Jude 			char message[512];
2737059fa6fSAllan Jude 			sprintf(message, "This partition scheme (%s) is not "
2747059fa6fSAllan Jude 			    "bootable on this platform. Are you sure you want "
2757059fa6fSAllan Jude 			    "to proceed?", scheme);
2767059fa6fSAllan Jude 			dialog_vars.defaultno = TRUE;
2777059fa6fSAllan Jude 			cancel = dialog_yesno("Warning", message, 0, 0);
2787059fa6fSAllan Jude 			dialog_vars.defaultno = FALSE;
2797059fa6fSAllan Jude 			if (cancel) { /* cancel */
2807059fa6fSAllan Jude 				/* Reset scheme so user can choose another */
2817059fa6fSAllan Jude 				scheme = NULL;
2827059fa6fSAllan Jude 				goto schememenu;
2837059fa6fSAllan Jude 			}
2847059fa6fSAllan Jude 		}
2852118f387SNathan Whitehorn 	}
2862118f387SNathan Whitehorn 
2872118f387SNathan Whitehorn 	r = gctl_get_handle();
2882118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
2892118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, lg_name);
2902118f387SNathan Whitehorn 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
2912118f387SNathan Whitehorn 	gctl_ro_param(r, "scheme", -1, scheme);
2922118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "create");
2932118f387SNathan Whitehorn 
2942118f387SNathan Whitehorn 	errstr = gctl_issue(r);
2952118f387SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0') {
2962118f387SNathan Whitehorn 		gpart_show_error("Error", NULL, errstr);
2972118f387SNathan Whitehorn 		gctl_free(r);
2982118f387SNathan Whitehorn 		scheme = NULL;
2992118f387SNathan Whitehorn 		goto schememenu;
3002118f387SNathan Whitehorn 	}
3012118f387SNathan Whitehorn 	gctl_free(r);
3022118f387SNathan Whitehorn 
3032118f387SNathan Whitehorn 	if (bootcode_path(scheme) != NULL)
3042118f387SNathan Whitehorn 		get_part_metadata(lg_name, 1)->bootcode = 1;
3052118f387SNathan Whitehorn 	return (0);
3062118f387SNathan Whitehorn }
3072118f387SNathan Whitehorn 
3082118f387SNathan Whitehorn static void
3092118f387SNathan Whitehorn gpart_activate(struct gprovider *pp)
3102118f387SNathan Whitehorn {
3112118f387SNathan Whitehorn 	struct gconfig *gc;
3122118f387SNathan Whitehorn 	struct gctl_req *r;
3132118f387SNathan Whitehorn 	const char *errstr, *scheme;
3142118f387SNathan Whitehorn 	const char *attribute = NULL;
3152118f387SNathan Whitehorn 	intmax_t idx;
3162118f387SNathan Whitehorn 
3172118f387SNathan Whitehorn 	/*
3182118f387SNathan Whitehorn 	 * Some partition schemes need this partition to be marked 'active'
3192118f387SNathan Whitehorn 	 * for it to be bootable.
3202118f387SNathan Whitehorn 	 */
3212118f387SNathan Whitehorn 	LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
3222118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "scheme") == 0) {
3232118f387SNathan Whitehorn 			scheme = gc->lg_val;
3242118f387SNathan Whitehorn 			break;
3252118f387SNathan Whitehorn 		}
3262118f387SNathan Whitehorn 	}
3272118f387SNathan Whitehorn 
3282118f387SNathan Whitehorn 	if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0 ||
3292118f387SNathan Whitehorn 	    strcmp(scheme, "PC98") == 0)
3302118f387SNathan Whitehorn 		attribute = "active";
3312118f387SNathan Whitehorn 	else
3322118f387SNathan Whitehorn 		return;
3332118f387SNathan Whitehorn 
3342118f387SNathan Whitehorn 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
3352118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "index") == 0) {
3362118f387SNathan Whitehorn 			idx = atoi(gc->lg_val);
3372118f387SNathan Whitehorn 			break;
3382118f387SNathan Whitehorn 		}
3392118f387SNathan Whitehorn 	}
3402118f387SNathan Whitehorn 
3412118f387SNathan Whitehorn 	r = gctl_get_handle();
3422118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
3432118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
3442118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "set");
3452118f387SNathan Whitehorn 	gctl_ro_param(r, "attrib", -1, attribute);
3462118f387SNathan Whitehorn 	gctl_ro_param(r, "index", sizeof(idx), &idx);
3472118f387SNathan Whitehorn 
3482118f387SNathan Whitehorn 	errstr = gctl_issue(r);
3492118f387SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0')
3502118f387SNathan Whitehorn 		gpart_show_error("Error", "Error marking partition active:",
3512118f387SNathan Whitehorn 		    errstr);
3522118f387SNathan Whitehorn 	gctl_free(r);
3532118f387SNathan Whitehorn }
3542118f387SNathan Whitehorn 
3557059fa6fSAllan Jude void
3567059fa6fSAllan Jude gpart_set_root(const char *lg_name, const char *attribute)
3577059fa6fSAllan Jude {
3587059fa6fSAllan Jude 	struct gctl_req *r;
3597059fa6fSAllan Jude 	const char *errstr;
3607059fa6fSAllan Jude 
3617059fa6fSAllan Jude 	r = gctl_get_handle();
3627059fa6fSAllan Jude 	gctl_ro_param(r, "class", -1, "PART");
3637059fa6fSAllan Jude 	gctl_ro_param(r, "arg0", -1, lg_name);
3647059fa6fSAllan Jude 	gctl_ro_param(r, "flags", -1, "C");
3657059fa6fSAllan Jude 	gctl_ro_param(r, "verb", -1, "set");
3667059fa6fSAllan Jude 	gctl_ro_param(r, "attrib", -1, attribute);
3677059fa6fSAllan Jude 
3687059fa6fSAllan Jude 	errstr = gctl_issue(r);
3697059fa6fSAllan Jude 	if (errstr != NULL && errstr[0] != '\0')
3707059fa6fSAllan Jude 		gpart_show_error("Error", "Error setting parameter on disk:",
3717059fa6fSAllan Jude 		    errstr);
3727059fa6fSAllan Jude 	gctl_free(r);
3737059fa6fSAllan Jude }
3747059fa6fSAllan Jude 
3752118f387SNathan Whitehorn static void
3762118f387SNathan Whitehorn gpart_bootcode(struct ggeom *gp)
3772118f387SNathan Whitehorn {
3782118f387SNathan Whitehorn 	const char *bootcode;
3792118f387SNathan Whitehorn 	struct gconfig *gc;
3802118f387SNathan Whitehorn 	struct gctl_req *r;
3812118f387SNathan Whitehorn 	const char *errstr, *scheme;
3822118f387SNathan Whitehorn 	uint8_t *boot;
3832118f387SNathan Whitehorn 	size_t bootsize, bytes;
3842118f387SNathan Whitehorn 	int bootfd;
3852118f387SNathan Whitehorn 
3862118f387SNathan Whitehorn 	/*
3872118f387SNathan Whitehorn 	 * Write default bootcode to the newly partitioned disk, if that
3882118f387SNathan Whitehorn 	 * applies on this platform.
3892118f387SNathan Whitehorn 	 */
3902118f387SNathan Whitehorn 	LIST_FOREACH(gc, &gp->lg_config, lg_config) {
3912118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "scheme") == 0) {
3922118f387SNathan Whitehorn 			scheme = gc->lg_val;
3932118f387SNathan Whitehorn 			break;
3942118f387SNathan Whitehorn 		}
3952118f387SNathan Whitehorn 	}
3962118f387SNathan Whitehorn 
3972118f387SNathan Whitehorn 	bootcode = bootcode_path(scheme);
3982118f387SNathan Whitehorn 	if (bootcode == NULL)
3992118f387SNathan Whitehorn 		return;
4002118f387SNathan Whitehorn 
4012118f387SNathan Whitehorn 	bootfd = open(bootcode, O_RDONLY);
402c725e3efSKevin Lo 	if (bootfd < 0) {
4032118f387SNathan Whitehorn 		dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
4042118f387SNathan Whitehorn 		    TRUE);
4052118f387SNathan Whitehorn 		return;
4062118f387SNathan Whitehorn 	}
4072118f387SNathan Whitehorn 
4082118f387SNathan Whitehorn 	bootsize = lseek(bootfd, 0, SEEK_END);
4092118f387SNathan Whitehorn 	boot = malloc(bootsize);
4102118f387SNathan Whitehorn 	lseek(bootfd, 0, SEEK_SET);
4112118f387SNathan Whitehorn 	bytes = 0;
4122118f387SNathan Whitehorn 	while (bytes < bootsize)
4132118f387SNathan Whitehorn 		bytes += read(bootfd, boot + bytes, bootsize - bytes);
4142118f387SNathan Whitehorn 	close(bootfd);
4152118f387SNathan Whitehorn 
4162118f387SNathan Whitehorn 	r = gctl_get_handle();
4172118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
4182118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, gp->lg_name);
4192118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "bootcode");
4202118f387SNathan Whitehorn 	gctl_ro_param(r, "bootcode", bootsize, boot);
4212118f387SNathan Whitehorn 
4222118f387SNathan Whitehorn 	errstr = gctl_issue(r);
4232118f387SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0')
4242118f387SNathan Whitehorn 		gpart_show_error("Bootcode Error", NULL, errstr);
4252118f387SNathan Whitehorn 	gctl_free(r);
4262118f387SNathan Whitehorn 	free(boot);
4272118f387SNathan Whitehorn }
4282118f387SNathan Whitehorn 
4292118f387SNathan Whitehorn static void
4306e15678aSNathan Whitehorn gpart_partcode(struct gprovider *pp, const char *fstype)
4312118f387SNathan Whitehorn {
4322118f387SNathan Whitehorn 	struct gconfig *gc;
4332118f387SNathan Whitehorn 	const char *scheme;
4342118f387SNathan Whitehorn 	const char *indexstr;
4352118f387SNathan Whitehorn 	char message[255], command[255];
4362118f387SNathan Whitehorn 
4372118f387SNathan Whitehorn 	LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
4382118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "scheme") == 0) {
4392118f387SNathan Whitehorn 			scheme = gc->lg_val;
4402118f387SNathan Whitehorn 			break;
4412118f387SNathan Whitehorn 		}
4422118f387SNathan Whitehorn 	}
4432118f387SNathan Whitehorn 
4442118f387SNathan Whitehorn 	/* Make sure this partition scheme needs partcode on this platform */
4456e15678aSNathan Whitehorn 	if (partcode_path(scheme, fstype) == NULL)
4462118f387SNathan Whitehorn 		return;
4472118f387SNathan Whitehorn 
4482118f387SNathan Whitehorn 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
4492118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "index") == 0) {
4502118f387SNathan Whitehorn 			indexstr = gc->lg_val;
4512118f387SNathan Whitehorn 			break;
4522118f387SNathan Whitehorn 		}
4532118f387SNathan Whitehorn 	}
4542118f387SNathan Whitehorn 
4552118f387SNathan Whitehorn 	/* Shell out to gpart for partcode for now */
4562118f387SNathan Whitehorn 	sprintf(command, "gpart bootcode -p %s -i %s %s",
4576e15678aSNathan Whitehorn 	    partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
4582118f387SNathan Whitehorn 	if (system(command) != 0) {
4592118f387SNathan Whitehorn 		sprintf(message, "Error installing partcode on partition %s",
4602118f387SNathan Whitehorn 		    pp->lg_name);
4612118f387SNathan Whitehorn 		dialog_msgbox("Error", message, 0, 0, TRUE);
4622118f387SNathan Whitehorn 	}
4632118f387SNathan Whitehorn }
4642118f387SNathan Whitehorn 
4652118f387SNathan Whitehorn void
466f36a5e0fSNathan Whitehorn gpart_destroy(struct ggeom *lg_geom)
4672118f387SNathan Whitehorn {
4682118f387SNathan Whitehorn 	struct gctl_req *r;
469f36a5e0fSNathan Whitehorn 	struct gprovider *pp;
4702118f387SNathan Whitehorn 	const char *errstr;
471f36a5e0fSNathan Whitehorn 	int force = 1;
4722118f387SNathan Whitehorn 
473f36a5e0fSNathan Whitehorn 	/* Delete all child metadata */
4742118f387SNathan Whitehorn 	LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
4752118f387SNathan Whitehorn 		gpart_delete(pp);
4762118f387SNathan Whitehorn 
477f36a5e0fSNathan Whitehorn 	/* Revert any local changes to get this geom into a pristine state */
478f36a5e0fSNathan Whitehorn 	r = gctl_get_handle();
479f36a5e0fSNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
480f36a5e0fSNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
481f36a5e0fSNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "undo");
482f36a5e0fSNathan Whitehorn 	gctl_issue(r); /* Ignore errors -- these are non-fatal */
483f36a5e0fSNathan Whitehorn 	gctl_free(r);
484f36a5e0fSNathan Whitehorn 
4852118f387SNathan Whitehorn 	/* Now destroy the geom itself */
4862118f387SNathan Whitehorn 	r = gctl_get_handle();
4872118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
4882118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
4892118f387SNathan Whitehorn 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
490f36a5e0fSNathan Whitehorn 	gctl_ro_param(r, "force", sizeof(force), &force);
4912118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "destroy");
4922118f387SNathan Whitehorn 	errstr = gctl_issue(r);
493987415b1SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0') {
494987415b1SNathan Whitehorn 		/*
495987415b1SNathan Whitehorn 		 * Check if we reverted away the existence of the geom
496987415b1SNathan Whitehorn 		 * altogether. Show all other errors to the user.
497987415b1SNathan Whitehorn 		 */
498987415b1SNathan Whitehorn 		if (strtol(errstr, NULL, 0) != EINVAL)
4992118f387SNathan Whitehorn 			gpart_show_error("Error", NULL, errstr);
500987415b1SNathan Whitehorn 	}
5012118f387SNathan Whitehorn 	gctl_free(r);
5022118f387SNathan Whitehorn 
5032118f387SNathan Whitehorn 	/* And any metadata associated with the partition scheme itself */
5042118f387SNathan Whitehorn 	delete_part_metadata(lg_geom->lg_name);
5052118f387SNathan Whitehorn }
5062118f387SNathan Whitehorn 
5072118f387SNathan Whitehorn void
5082118f387SNathan Whitehorn gpart_edit(struct gprovider *pp)
5092118f387SNathan Whitehorn {
5102118f387SNathan Whitehorn 	struct gctl_req *r;
5112118f387SNathan Whitehorn 	struct gconfig *gc;
5122118f387SNathan Whitehorn 	struct gconsumer *cp;
5132118f387SNathan Whitehorn 	struct ggeom *geom;
5142118f387SNathan Whitehorn 	const char *errstr, *oldtype, *scheme;
5152118f387SNathan Whitehorn 	struct partition_metadata *md;
5162118f387SNathan Whitehorn 	char sizestr[32];
5176e15678aSNathan Whitehorn 	char newfs[255];
5182118f387SNathan Whitehorn 	intmax_t idx;
5192118f387SNathan Whitehorn 	int hadlabel, choice, junk, nitems;
5202118f387SNathan Whitehorn 	unsigned i;
5212118f387SNathan Whitehorn 
5222118f387SNathan Whitehorn 	DIALOG_FORMITEM items[] = {
5232118f387SNathan Whitehorn 		{0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
5246e15678aSNathan Whitehorn 		    FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
5256e15678aSNathan Whitehorn 		    "freebsd-swap)", FALSE},
5262118f387SNathan Whitehorn 		{0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
5272118f387SNathan Whitehorn 		    FALSE, "Partition size. Append K, M, G for kilobytes, "
5282118f387SNathan Whitehorn 		    "megabytes or gigabytes.", FALSE},
5292118f387SNathan Whitehorn 		{0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
5302118f387SNathan Whitehorn 		    FALSE, "Path at which to mount this partition (leave blank "
5312118f387SNathan Whitehorn 		    "for swap, set to / for root filesystem)", FALSE},
5322118f387SNathan Whitehorn 		{0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
5332118f387SNathan Whitehorn 		    "Partition name. Not all partition schemes support this.",
5342118f387SNathan Whitehorn 		    FALSE},
5352118f387SNathan Whitehorn 	};
5362118f387SNathan Whitehorn 
5372118f387SNathan Whitehorn 	/*
5382118f387SNathan Whitehorn 	 * Find the PART geom we are manipulating. This may be a consumer of
5392118f387SNathan Whitehorn 	 * this provider, or its parent. Check the consumer case first.
5402118f387SNathan Whitehorn 	 */
5412118f387SNathan Whitehorn 	geom = NULL;
5422118f387SNathan Whitehorn 	LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
5432118f387SNathan Whitehorn 		if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
544f36a5e0fSNathan Whitehorn 			/* Check for zombie geoms, treating them as blank */
545f36a5e0fSNathan Whitehorn 			scheme = NULL;
546f36a5e0fSNathan Whitehorn 			LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
547f36a5e0fSNathan Whitehorn 				if (strcmp(gc->lg_name, "scheme") == 0) {
548f36a5e0fSNathan Whitehorn 					scheme = gc->lg_val;
549f36a5e0fSNathan Whitehorn 					break;
550f36a5e0fSNathan Whitehorn 				}
551f36a5e0fSNathan Whitehorn 			}
552f36a5e0fSNathan Whitehorn 			if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
553f36a5e0fSNathan Whitehorn 				gpart_partition(cp->lg_geom->lg_name, NULL);
5542118f387SNathan Whitehorn 				return;
555f36a5e0fSNathan Whitehorn 			}
5562118f387SNathan Whitehorn 
557987415b1SNathan Whitehorn 			/* If this is a nested partition, edit as usual */
558987415b1SNathan Whitehorn 			if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
559987415b1SNathan Whitehorn 				break;
560987415b1SNathan Whitehorn 
5612118f387SNathan Whitehorn 			/* Destroy the geom and all sub-partitions */
562f36a5e0fSNathan Whitehorn 			gpart_destroy(cp->lg_geom);
5632118f387SNathan Whitehorn 
5642118f387SNathan Whitehorn 			/* Now re-partition and return */
5652118f387SNathan Whitehorn 			gpart_partition(cp->lg_geom->lg_name, NULL);
5662118f387SNathan Whitehorn 			return;
5672118f387SNathan Whitehorn 		}
5682118f387SNathan Whitehorn 
5692118f387SNathan Whitehorn 	if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
5702118f387SNathan Whitehorn 		geom = pp->lg_geom;
5712118f387SNathan Whitehorn 
5722118f387SNathan Whitehorn 	if (geom == NULL) {
5732118f387SNathan Whitehorn 		/* Disk not partitioned, so partition it */
574bcc25b7eSNathan Whitehorn 		gpart_partition(pp->lg_name, NULL);
5752118f387SNathan Whitehorn 		return;
5762118f387SNathan Whitehorn 	}
5772118f387SNathan Whitehorn 
5782118f387SNathan Whitehorn 	LIST_FOREACH(gc, &geom->lg_config, lg_config) {
5792118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "scheme") == 0) {
5802118f387SNathan Whitehorn 			scheme = gc->lg_val;
5812118f387SNathan Whitehorn 			break;
5822118f387SNathan Whitehorn 		}
5832118f387SNathan Whitehorn 	}
5842118f387SNathan Whitehorn 
585c67f41d0SNathan Whitehorn 	nitems = scheme_supports_labels(scheme) ? 4 : 3;
5862118f387SNathan Whitehorn 
5872118f387SNathan Whitehorn 	/* Edit editable parameters of a partition */
5882118f387SNathan Whitehorn 	hadlabel = 0;
5892118f387SNathan Whitehorn 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
5902118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "type") == 0) {
5912118f387SNathan Whitehorn 			oldtype = gc->lg_val;
5922118f387SNathan Whitehorn 			items[0].text = gc->lg_val;
5932118f387SNathan Whitehorn 		}
5942118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
5952118f387SNathan Whitehorn 			hadlabel = 1;
5962118f387SNathan Whitehorn 			items[3].text = gc->lg_val;
5972118f387SNathan Whitehorn 		}
5982118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "index") == 0)
5992118f387SNathan Whitehorn 			idx = atoi(gc->lg_val);
6002118f387SNathan Whitehorn 	}
6012118f387SNathan Whitehorn 
6022118f387SNathan Whitehorn 	TAILQ_FOREACH(md, &part_metadata, metadata) {
6032118f387SNathan Whitehorn 		if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
6042118f387SNathan Whitehorn 			if (md->fstab != NULL)
6052118f387SNathan Whitehorn 				items[2].text = md->fstab->fs_file;
6062118f387SNathan Whitehorn 			break;
6072118f387SNathan Whitehorn 		}
6082118f387SNathan Whitehorn 	}
6092118f387SNathan Whitehorn 
6102118f387SNathan Whitehorn 	humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
6112118f387SNathan Whitehorn 	    HN_NOSPACE | HN_DECIMAL);
6122118f387SNathan Whitehorn 	items[1].text = sizestr;
6132118f387SNathan Whitehorn 
6142118f387SNathan Whitehorn editpart:
6152118f387SNathan Whitehorn 	choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
6162118f387SNathan Whitehorn 
6172118f387SNathan Whitehorn 	if (choice) /* Cancel pressed */
618987415b1SNathan Whitehorn 		goto endedit;
6192118f387SNathan Whitehorn 
6202118f387SNathan Whitehorn 	/* Check if the label has a / in it */
6212118f387SNathan Whitehorn 	if (strchr(items[3].text, '/') != NULL) {
6222118f387SNathan Whitehorn 		dialog_msgbox("Error", "Label contains a /, which is not an "
6232118f387SNathan Whitehorn 		    "allowed character.", 0, 0, TRUE);
6242118f387SNathan Whitehorn 		goto editpart;
6252118f387SNathan Whitehorn 	}
6262118f387SNathan Whitehorn 
6272118f387SNathan Whitehorn 	r = gctl_get_handle();
6282118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
6292118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, geom->lg_name);
6302118f387SNathan Whitehorn 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
6312118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "modify");
6322118f387SNathan Whitehorn 	gctl_ro_param(r, "index", sizeof(idx), &idx);
6332118f387SNathan Whitehorn 	if (hadlabel || items[3].text[0] != '\0')
6342118f387SNathan Whitehorn 		gctl_ro_param(r, "label", -1, items[3].text);
6352118f387SNathan Whitehorn 	gctl_ro_param(r, "type", -1, items[0].text);
6362118f387SNathan Whitehorn 	errstr = gctl_issue(r);
6372118f387SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0') {
6382118f387SNathan Whitehorn 		gpart_show_error("Error", NULL, errstr);
6392118f387SNathan Whitehorn 		gctl_free(r);
6402118f387SNathan Whitehorn 		goto editpart;
6412118f387SNathan Whitehorn 	}
6422118f387SNathan Whitehorn 	gctl_free(r);
6432118f387SNathan Whitehorn 
64450de5d07SNathan Whitehorn 	newfs_command(items[0].text, newfs, 1);
6452118f387SNathan Whitehorn 	set_default_part_metadata(pp->lg_name, scheme, items[0].text,
64650de5d07SNathan Whitehorn 	    items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
64750de5d07SNathan Whitehorn 	    newfs : NULL);
6482118f387SNathan Whitehorn 
649987415b1SNathan Whitehorn endedit:
650987415b1SNathan Whitehorn 	if (strcmp(oldtype, items[0].text) != 0 && cp != NULL)
651987415b1SNathan Whitehorn 		gpart_destroy(cp->lg_geom);
652987415b1SNathan Whitehorn 	if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text,
653987415b1SNathan Whitehorn 	    "freebsd") == 0)
654987415b1SNathan Whitehorn 		gpart_partition(pp->lg_name, "BSD");
655987415b1SNathan Whitehorn 
6562118f387SNathan Whitehorn 	for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
6572118f387SNathan Whitehorn 		if (items[i].text_free)
6582118f387SNathan Whitehorn 			free(items[i].text);
6592118f387SNathan Whitehorn }
6602118f387SNathan Whitehorn 
6612118f387SNathan Whitehorn void
6622118f387SNathan Whitehorn set_default_part_metadata(const char *name, const char *scheme,
66350de5d07SNathan Whitehorn     const char *type, const char *mountpoint, const char *newfs)
6642118f387SNathan Whitehorn {
6652118f387SNathan Whitehorn 	struct partition_metadata *md;
6666e15678aSNathan Whitehorn 	char *zpool_name = NULL;
6676e15678aSNathan Whitehorn 	int i;
6682118f387SNathan Whitehorn 
6692118f387SNathan Whitehorn 	/* Set part metadata */
6702118f387SNathan Whitehorn 	md = get_part_metadata(name, 1);
6712118f387SNathan Whitehorn 
6722118f387SNathan Whitehorn 	if (newfs) {
6732118f387SNathan Whitehorn 		if (md->newfs != NULL) {
6742118f387SNathan Whitehorn 			free(md->newfs);
6752118f387SNathan Whitehorn 			md->newfs = NULL;
6762118f387SNathan Whitehorn 		}
6772118f387SNathan Whitehorn 
67850de5d07SNathan Whitehorn 		if (newfs != NULL && newfs[0] != '\0') {
67950de5d07SNathan Whitehorn 			md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
6806e15678aSNathan Whitehorn 			    strlen(mountpoint) + 5 + strlen(name) + 1);
6816e15678aSNathan Whitehorn 			if (strcmp("freebsd-zfs", type) == 0) {
6826e15678aSNathan Whitehorn 				zpool_name = strdup((strlen(mountpoint) == 1) ?
6836e15678aSNathan Whitehorn 				    "root" : &mountpoint[1]);
6846e15678aSNathan Whitehorn 				for (i = 0; zpool_name[i] != 0; i++)
6856e15678aSNathan Whitehorn 					if (!isalnum(zpool_name[i]))
6866e15678aSNathan Whitehorn 						zpool_name[i] = '_';
6876e15678aSNathan Whitehorn 				sprintf(md->newfs, "%s %s /dev/%s", newfs,
6886e15678aSNathan Whitehorn 				    zpool_name, name);
6896e15678aSNathan Whitehorn 			} else {
69050de5d07SNathan Whitehorn 				sprintf(md->newfs, "%s /dev/%s", newfs, name);
6912118f387SNathan Whitehorn 			}
6922118f387SNathan Whitehorn 		}
6936e15678aSNathan Whitehorn 	}
6942118f387SNathan Whitehorn 
6952118f387SNathan Whitehorn 	if (strcmp(type, "freebsd-swap") == 0)
6962118f387SNathan Whitehorn 		mountpoint = "none";
6976b446ed5SNathan Whitehorn 	if (strcmp(type, bootpart_type(scheme)) == 0)
6982118f387SNathan Whitehorn 		md->bootcode = 1;
6992118f387SNathan Whitehorn 
7006e15678aSNathan Whitehorn 	/* VTOC8 needs partcode at the start of partitions */
7016e15678aSNathan Whitehorn 	if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
7026e15678aSNathan Whitehorn 	    || strcmp(type, "freebsd-zfs") == 0))
7032118f387SNathan Whitehorn 		md->bootcode = 1;
7042118f387SNathan Whitehorn 
7052118f387SNathan Whitehorn 	if (mountpoint == NULL || mountpoint[0] == '\0') {
7062118f387SNathan Whitehorn 		if (md->fstab != NULL) {
7072118f387SNathan Whitehorn 			free(md->fstab->fs_spec);
7082118f387SNathan Whitehorn 			free(md->fstab->fs_file);
7092118f387SNathan Whitehorn 			free(md->fstab->fs_vfstype);
7102118f387SNathan Whitehorn 			free(md->fstab->fs_mntops);
7112118f387SNathan Whitehorn 			free(md->fstab->fs_type);
7122118f387SNathan Whitehorn 			free(md->fstab);
7132118f387SNathan Whitehorn 			md->fstab = NULL;
7142118f387SNathan Whitehorn 		}
7152118f387SNathan Whitehorn 	} else {
7162118f387SNathan Whitehorn 		if (md->fstab == NULL) {
7172118f387SNathan Whitehorn 			md->fstab = malloc(sizeof(struct fstab));
7182118f387SNathan Whitehorn 		} else {
7192118f387SNathan Whitehorn 			free(md->fstab->fs_spec);
7202118f387SNathan Whitehorn 			free(md->fstab->fs_file);
7212118f387SNathan Whitehorn 			free(md->fstab->fs_vfstype);
7222118f387SNathan Whitehorn 			free(md->fstab->fs_mntops);
7232118f387SNathan Whitehorn 			free(md->fstab->fs_type);
7242118f387SNathan Whitehorn 		}
7256e15678aSNathan Whitehorn 		if (strcmp("freebsd-zfs", type) == 0) {
7266e15678aSNathan Whitehorn 			md->fstab->fs_spec = strdup(zpool_name);
7276e15678aSNathan Whitehorn 		} else {
7286e15678aSNathan Whitehorn 			md->fstab->fs_spec = malloc(strlen(name) +
7296e15678aSNathan Whitehorn 			    strlen("/dev/") + 1);
7302118f387SNathan Whitehorn 			sprintf(md->fstab->fs_spec, "/dev/%s", name);
7316e15678aSNathan Whitehorn 		}
7322118f387SNathan Whitehorn 		md->fstab->fs_file = strdup(mountpoint);
7332118f387SNathan Whitehorn 		/* Get VFS from text after freebsd-, if possible */
73450de5d07SNathan Whitehorn 		if (strncmp("freebsd-", type, 8) == 0)
7352118f387SNathan Whitehorn 			md->fstab->fs_vfstype = strdup(&type[8]);
73650de5d07SNathan Whitehorn 		else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0)
73750de5d07SNathan Whitehorn 			md->fstab->fs_vfstype = strdup("msdosfs");
7382118f387SNathan Whitehorn 		else
7392118f387SNathan Whitehorn 			md->fstab->fs_vfstype = strdup(type); /* Guess */
7402118f387SNathan Whitehorn 		if (strcmp(type, "freebsd-swap") == 0) {
7412118f387SNathan Whitehorn 			md->fstab->fs_type = strdup(FSTAB_SW);
7422118f387SNathan Whitehorn 			md->fstab->fs_freq = 0;
7432118f387SNathan Whitehorn 			md->fstab->fs_passno = 0;
7446e15678aSNathan Whitehorn 		} else if (strcmp(type, "freebsd-zfs") == 0) {
7456e15678aSNathan Whitehorn 			md->fstab->fs_type = strdup(FSTAB_RW);
7466e15678aSNathan Whitehorn 			md->fstab->fs_freq = 0;
7476e15678aSNathan Whitehorn 			md->fstab->fs_passno = 0;
7482118f387SNathan Whitehorn 		} else {
7492118f387SNathan Whitehorn 			md->fstab->fs_type = strdup(FSTAB_RW);
7502118f387SNathan Whitehorn 			if (strcmp(mountpoint, "/") == 0) {
7512118f387SNathan Whitehorn 				md->fstab->fs_freq = 1;
7522118f387SNathan Whitehorn 				md->fstab->fs_passno = 1;
7532118f387SNathan Whitehorn 			} else {
7542118f387SNathan Whitehorn 				md->fstab->fs_freq = 2;
7552118f387SNathan Whitehorn 				md->fstab->fs_passno = 2;
7562118f387SNathan Whitehorn 			}
7572118f387SNathan Whitehorn 		}
7582118f387SNathan Whitehorn 		md->fstab->fs_mntops = strdup(md->fstab->fs_type);
7592118f387SNathan Whitehorn 	}
7606e15678aSNathan Whitehorn 
7616e15678aSNathan Whitehorn 	if (zpool_name != NULL)
7626e15678aSNathan Whitehorn 		free(zpool_name);
7632118f387SNathan Whitehorn }
7642118f387SNathan Whitehorn 
7652118f387SNathan Whitehorn static
7662118f387SNathan Whitehorn int part_compare(const void *xa, const void *xb)
7672118f387SNathan Whitehorn {
7682118f387SNathan Whitehorn 	struct gprovider **a = (struct gprovider **)xa;
7692118f387SNathan Whitehorn 	struct gprovider **b = (struct gprovider **)xb;
7702118f387SNathan Whitehorn 	intmax_t astart, bstart;
7712118f387SNathan Whitehorn 	struct gconfig *gc;
7722118f387SNathan Whitehorn 
7732118f387SNathan Whitehorn 	astart = bstart = 0;
7742118f387SNathan Whitehorn 	LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
7752118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "start") == 0) {
7762118f387SNathan Whitehorn 			astart = strtoimax(gc->lg_val, NULL, 0);
7772118f387SNathan Whitehorn 			break;
7782118f387SNathan Whitehorn 		}
7792118f387SNathan Whitehorn 	LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
7802118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "start") == 0) {
7812118f387SNathan Whitehorn 			bstart = strtoimax(gc->lg_val, NULL, 0);
7822118f387SNathan Whitehorn 			break;
7832118f387SNathan Whitehorn 		}
7842118f387SNathan Whitehorn 
7852118f387SNathan Whitehorn 	if (astart < bstart)
7862118f387SNathan Whitehorn 		return -1;
7872118f387SNathan Whitehorn 	else if (astart > bstart)
7882118f387SNathan Whitehorn 		return 1;
7892118f387SNathan Whitehorn 	else
7902118f387SNathan Whitehorn 		return 0;
7912118f387SNathan Whitehorn }
7922118f387SNathan Whitehorn 
7932118f387SNathan Whitehorn intmax_t
7942118f387SNathan Whitehorn gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
7952118f387SNathan Whitehorn {
7962118f387SNathan Whitehorn 	struct gconfig *gc;
7972118f387SNathan Whitehorn 	struct gprovider *pp, **providers;
798*47ead00dSDag-Erling Smørgrav 	intmax_t sectorsize, stripesize, offset;
7992118f387SNathan Whitehorn 	intmax_t lastend;
8002118f387SNathan Whitehorn 	intmax_t start, end;
8012118f387SNathan Whitehorn 	intmax_t maxsize, maxstart;
8022118f387SNathan Whitehorn 	intmax_t partstart, partend;
8032118f387SNathan Whitehorn 	int i, nparts;
8042118f387SNathan Whitehorn 
8052118f387SNathan Whitehorn 	/* Now get the maximum free size and free start */
8062118f387SNathan Whitehorn 	start = end = 0;
8072118f387SNathan Whitehorn 	LIST_FOREACH(gc, &geom->lg_config, lg_config) {
8082118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "first") == 0)
8092118f387SNathan Whitehorn 			start = strtoimax(gc->lg_val, NULL, 0);
8102118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "last") == 0)
8112118f387SNathan Whitehorn 			end = strtoimax(gc->lg_val, NULL, 0);
8122118f387SNathan Whitehorn 	}
8132118f387SNathan Whitehorn 
8142118f387SNathan Whitehorn 	i = nparts = 0;
8152118f387SNathan Whitehorn 	LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
8162118f387SNathan Whitehorn 		nparts++;
8172118f387SNathan Whitehorn 	providers = calloc(nparts, sizeof(providers[0]));
8182118f387SNathan Whitehorn 	LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
8192118f387SNathan Whitehorn 		providers[i++] = pp;
8202118f387SNathan Whitehorn 	qsort(providers, nparts, sizeof(providers[0]), part_compare);
8212118f387SNathan Whitehorn 
8222118f387SNathan Whitehorn 	lastend = start - 1;
8232118f387SNathan Whitehorn 	maxsize = 0;
8242118f387SNathan Whitehorn 	for (i = 0; i < nparts; i++) {
8252118f387SNathan Whitehorn 		pp = providers[i];
8262118f387SNathan Whitehorn 
8272118f387SNathan Whitehorn 		LIST_FOREACH(gc, &pp->lg_config, lg_config) {
8282118f387SNathan Whitehorn 			if (strcmp(gc->lg_name, "start") == 0)
8292118f387SNathan Whitehorn 				partstart = strtoimax(gc->lg_val, NULL, 0);
8302118f387SNathan Whitehorn 			if (strcmp(gc->lg_name, "end") == 0)
8312118f387SNathan Whitehorn 				partend = strtoimax(gc->lg_val, NULL, 0);
8322118f387SNathan Whitehorn 		}
8332118f387SNathan Whitehorn 
8342118f387SNathan Whitehorn 		if (partstart - lastend > maxsize) {
8352118f387SNathan Whitehorn 			maxsize = partstart - lastend - 1;
8362118f387SNathan Whitehorn 			maxstart = lastend + 1;
8372118f387SNathan Whitehorn 		}
8382118f387SNathan Whitehorn 
8392118f387SNathan Whitehorn 		lastend = partend;
8402118f387SNathan Whitehorn 	}
8412118f387SNathan Whitehorn 
8422118f387SNathan Whitehorn 	if (end - lastend > maxsize) {
8432118f387SNathan Whitehorn 		maxsize = end - lastend - 1;
8442118f387SNathan Whitehorn 		maxstart = lastend + 1;
8452118f387SNathan Whitehorn 	}
8462118f387SNathan Whitehorn 
8472118f387SNathan Whitehorn 	pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
8482118f387SNathan Whitehorn 
849*47ead00dSDag-Erling Smørgrav 	/*
850*47ead00dSDag-Erling Smørgrav 	 * Round the start and size of the largest available space up to
851*47ead00dSDag-Erling Smørgrav 	 * the nearest multiple of the adjusted stripe size.
852*47ead00dSDag-Erling Smørgrav 	 *
853*47ead00dSDag-Erling Smørgrav 	 * The adjusted stripe size is the least common multiple of the
854*47ead00dSDag-Erling Smørgrav 	 * actual stripe size, or the sector size if no stripe size was
855*47ead00dSDag-Erling Smørgrav 	 * reported, and 4096.  The reason for this is that contemporary
856*47ead00dSDag-Erling Smørgrav 	 * disks often have 4096-byte physical sectors but report 512
857*47ead00dSDag-Erling Smørgrav 	 * bytes instead for compatibility with older / broken operating
858*47ead00dSDag-Erling Smørgrav 	 * systems and BIOSes.  For the same reasons, virtualized storage
859*47ead00dSDag-Erling Smørgrav 	 * may also report a 512-byte stripe size, or none at all.
860*47ead00dSDag-Erling Smørgrav 	 */
861*47ead00dSDag-Erling Smørgrav 	sectorsize = pp->lg_sectorsize;
862*47ead00dSDag-Erling Smørgrav 	if ((stripesize = pp->lg_stripesize) == 0)
863*47ead00dSDag-Erling Smørgrav 		stripesize = sectorsize;
864*47ead00dSDag-Erling Smørgrav 	while (stripesize % 4096 != 0)
865*47ead00dSDag-Erling Smørgrav 		stripesize *= 2;
866*47ead00dSDag-Erling Smørgrav 	if ((offset = maxstart * sectorsize % stripesize) != 0) {
867*47ead00dSDag-Erling Smørgrav 		offset = (stripesize - offset) / sectorsize;
8682118f387SNathan Whitehorn 		maxstart += offset;
8692118f387SNathan Whitehorn 		maxsize -= offset;
8702118f387SNathan Whitehorn 	}
8712118f387SNathan Whitehorn 
8722118f387SNathan Whitehorn 	if (npartstart != NULL)
8732118f387SNathan Whitehorn 		*npartstart = maxstart;
8742118f387SNathan Whitehorn 
8752118f387SNathan Whitehorn 	return (maxsize);
8762118f387SNathan Whitehorn }
8772118f387SNathan Whitehorn 
8782118f387SNathan Whitehorn void
8792118f387SNathan Whitehorn gpart_create(struct gprovider *pp, char *default_type, char *default_size,
8802118f387SNathan Whitehorn      char *default_mountpoint, char **partname, int interactive)
8812118f387SNathan Whitehorn {
8822118f387SNathan Whitehorn 	struct gctl_req *r;
8832118f387SNathan Whitehorn 	struct gconfig *gc;
8842118f387SNathan Whitehorn 	struct gconsumer *cp;
8852118f387SNathan Whitehorn 	struct ggeom *geom;
8862118f387SNathan Whitehorn 	const char *errstr, *scheme;
8877899c6c1SNathan Whitehorn 	char sizestr[32], startstr[32], output[64], *newpartname;
8886e15678aSNathan Whitehorn 	char newfs[255], options_fstype[64];
8892118f387SNathan Whitehorn 	intmax_t maxsize, size, sector, firstfree, stripe;
8902118f387SNathan Whitehorn 	uint64_t bytes;
8912118f387SNathan Whitehorn 	int nitems, choice, junk;
8922118f387SNathan Whitehorn 	unsigned i;
8932118f387SNathan Whitehorn 
8942118f387SNathan Whitehorn 	DIALOG_FORMITEM items[] = {
8952118f387SNathan Whitehorn 		{0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
8966e15678aSNathan Whitehorn 		    FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
8976e15678aSNathan Whitehorn 		    "freebsd-swap)", FALSE},
8982118f387SNathan Whitehorn 		{0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
8992118f387SNathan Whitehorn 		    FALSE, "Partition size. Append K, M, G for kilobytes, "
9002118f387SNathan Whitehorn 		    "megabytes or gigabytes.", FALSE},
9012118f387SNathan Whitehorn 		{0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
9022118f387SNathan Whitehorn 		    FALSE, "Path at which to mount partition (blank for "
9032118f387SNathan Whitehorn 		    "swap, / for root filesystem)", FALSE},
9042118f387SNathan Whitehorn 		{0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
9052118f387SNathan Whitehorn 		    "Partition name. Not all partition schemes support this.",
9062118f387SNathan Whitehorn 		    FALSE},
9072118f387SNathan Whitehorn 	};
9082118f387SNathan Whitehorn 
9092118f387SNathan Whitehorn 	if (partname != NULL)
9102118f387SNathan Whitehorn 		*partname = NULL;
9112118f387SNathan Whitehorn 
9122118f387SNathan Whitehorn 	/* Record sector and stripe sizes */
9132118f387SNathan Whitehorn 	sector = pp->lg_sectorsize;
9142118f387SNathan Whitehorn 	stripe = pp->lg_stripesize;
9152118f387SNathan Whitehorn 
9162118f387SNathan Whitehorn 	/*
9172118f387SNathan Whitehorn 	 * Find the PART geom we are manipulating. This may be a consumer of
9182118f387SNathan Whitehorn 	 * this provider, or its parent. Check the consumer case first.
9192118f387SNathan Whitehorn 	 */
9202118f387SNathan Whitehorn 	geom = NULL;
9212118f387SNathan Whitehorn 	LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
9222118f387SNathan Whitehorn 		if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
9232118f387SNathan Whitehorn 			geom = cp->lg_geom;
9242118f387SNathan Whitehorn 			break;
9252118f387SNathan Whitehorn 		}
9262118f387SNathan Whitehorn 
9272118f387SNathan Whitehorn 	if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
9282118f387SNathan Whitehorn 		geom = pp->lg_geom;
9292118f387SNathan Whitehorn 
9302118f387SNathan Whitehorn 	/* Now get the partition scheme */
9312118f387SNathan Whitehorn 	scheme = NULL;
9322118f387SNathan Whitehorn 	if (geom != NULL) {
9332118f387SNathan Whitehorn 		LIST_FOREACH(gc, &geom->lg_config, lg_config)
9342118f387SNathan Whitehorn 			if (strcmp(gc->lg_name, "scheme") == 0)
9352118f387SNathan Whitehorn 				scheme = gc->lg_val;
9362118f387SNathan Whitehorn 	}
9372118f387SNathan Whitehorn 
9382118f387SNathan Whitehorn 	if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
939bcc25b7eSNathan Whitehorn 		if (gpart_partition(pp->lg_name, NULL) == 0)
9402118f387SNathan Whitehorn 			dialog_msgbox("",
9412118f387SNathan Whitehorn 			    "The partition table has been successfully created."
9422118f387SNathan Whitehorn 			    " Please press Create again to create partitions.",
9432118f387SNathan Whitehorn 			    0, 0, TRUE);
9442118f387SNathan Whitehorn 
9452118f387SNathan Whitehorn 		return;
9462118f387SNathan Whitehorn 	}
9472118f387SNathan Whitehorn 
9482118f387SNathan Whitehorn 	/*
9492118f387SNathan Whitehorn 	 * If we still don't have a geom, either the user has
9502118f387SNathan Whitehorn 	 * canceled partitioning or there has been an error which has already
9512118f387SNathan Whitehorn 	 * been displayed, so bail.
9522118f387SNathan Whitehorn 	 */
9532118f387SNathan Whitehorn 	if (geom == NULL)
9542118f387SNathan Whitehorn 		return;
9552118f387SNathan Whitehorn 
9562118f387SNathan Whitehorn 	maxsize = size = gpart_max_free(geom, &firstfree);
9572118f387SNathan Whitehorn 	if (size <= 0) {
9582118f387SNathan Whitehorn 		dialog_msgbox("Error", "No free space left on device.", 0, 0,
9592118f387SNathan Whitehorn 		    TRUE);
9602118f387SNathan Whitehorn 		return;
9612118f387SNathan Whitehorn 	}
9622118f387SNathan Whitehorn 
9632118f387SNathan Whitehorn 	humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
9642118f387SNathan Whitehorn 	    HN_NOSPACE | HN_DECIMAL);
9652118f387SNathan Whitehorn 	items[1].text = sizestr;
9662118f387SNathan Whitehorn 
9672118f387SNathan Whitehorn 	/* Special-case the MBR default type for nested partitions */
968bcc25b7eSNathan Whitehorn 	if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "PC98") == 0) {
9692118f387SNathan Whitehorn 		items[0].text = "freebsd";
970bcc25b7eSNathan Whitehorn 		items[0].help = "Filesystem type (e.g. freebsd, fat32)";
971bcc25b7eSNathan Whitehorn 	}
9722118f387SNathan Whitehorn 
973c67f41d0SNathan Whitehorn 	nitems = scheme_supports_labels(scheme) ? 4 : 3;
9742118f387SNathan Whitehorn 
9752118f387SNathan Whitehorn 	if (default_type != NULL)
9762118f387SNathan Whitehorn 		items[0].text = default_type;
9772118f387SNathan Whitehorn 	if (default_size != NULL)
9782118f387SNathan Whitehorn 		items[1].text = default_size;
9792118f387SNathan Whitehorn 	if (default_mountpoint != NULL)
9802118f387SNathan Whitehorn 		items[2].text = default_mountpoint;
9812118f387SNathan Whitehorn 
98250de5d07SNathan Whitehorn 	/* Default options */
98350de5d07SNathan Whitehorn 	strncpy(options_fstype, items[0].text,
98450de5d07SNathan Whitehorn 	    sizeof(options_fstype));
98550de5d07SNathan Whitehorn 	newfs_command(options_fstype, newfs, 1);
9862118f387SNathan Whitehorn addpartform:
9872118f387SNathan Whitehorn 	if (interactive) {
98850de5d07SNathan Whitehorn 		dialog_vars.extra_label = "Options";
98950de5d07SNathan Whitehorn 		dialog_vars.extra_button = TRUE;
9902118f387SNathan Whitehorn 		choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
9912118f387SNathan Whitehorn 		    items, &junk);
99250de5d07SNathan Whitehorn 		dialog_vars.extra_button = FALSE;
99350de5d07SNathan Whitehorn 		switch (choice) {
99450de5d07SNathan Whitehorn 		case 0: /* OK */
99550de5d07SNathan Whitehorn 			break;
99650de5d07SNathan Whitehorn 		case 1: /* Cancel */
9972118f387SNathan Whitehorn 			return;
99850de5d07SNathan Whitehorn 		case 3: /* Options */
99950de5d07SNathan Whitehorn 			strncpy(options_fstype, items[0].text,
100050de5d07SNathan Whitehorn 			    sizeof(options_fstype));
100150de5d07SNathan Whitehorn 			newfs_command(options_fstype, newfs, 0);
100250de5d07SNathan Whitehorn 			goto addpartform;
100350de5d07SNathan Whitehorn 		}
100450de5d07SNathan Whitehorn 	}
100550de5d07SNathan Whitehorn 
100650de5d07SNathan Whitehorn 	/*
100750de5d07SNathan Whitehorn 	 * If the user changed the fs type after specifying options, undo
100850de5d07SNathan Whitehorn 	 * their choices in favor of the new filesystem's defaults.
100950de5d07SNathan Whitehorn 	 */
10100d49c6d5SNathan Whitehorn 	if (strcmp(options_fstype, items[0].text) != 0) {
101150de5d07SNathan Whitehorn 		strncpy(options_fstype, items[0].text, sizeof(options_fstype));
101250de5d07SNathan Whitehorn 		newfs_command(options_fstype, newfs, 1);
10132118f387SNathan Whitehorn 	}
10142118f387SNathan Whitehorn 
10152118f387SNathan Whitehorn 	size = maxsize;
10162118f387SNathan Whitehorn 	if (strlen(items[1].text) > 0) {
10172118f387SNathan Whitehorn 		if (expand_number(items[1].text, &bytes) != 0) {
10182118f387SNathan Whitehorn 			char error[512];
10192118f387SNathan Whitehorn 
10202118f387SNathan Whitehorn 			sprintf(error, "Invalid size: %s\n", strerror(errno));
10212118f387SNathan Whitehorn 			dialog_msgbox("Error", error, 0, 0, TRUE);
10222118f387SNathan Whitehorn 			goto addpartform;
10232118f387SNathan Whitehorn 		}
10242118f387SNathan Whitehorn 		size = MIN((intmax_t)(bytes/sector), maxsize);
10252118f387SNathan Whitehorn 	}
10262118f387SNathan Whitehorn 
10272118f387SNathan Whitehorn 	/* Check if the label has a / in it */
10282118f387SNathan Whitehorn 	if (strchr(items[3].text, '/') != NULL) {
10292118f387SNathan Whitehorn 		dialog_msgbox("Error", "Label contains a /, which is not an "
10302118f387SNathan Whitehorn 		    "allowed character.", 0, 0, TRUE);
10312118f387SNathan Whitehorn 		goto addpartform;
10322118f387SNathan Whitehorn 	}
10332118f387SNathan Whitehorn 
10342118f387SNathan Whitehorn 	/* Warn if no mountpoint set */
10352118f387SNathan Whitehorn 	if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
10362118f387SNathan Whitehorn 	    items[2].text[0] != '/') {
10372118f387SNathan Whitehorn 		dialog_vars.defaultno = TRUE;
10382118f387SNathan Whitehorn 		choice = dialog_yesno("Warning",
10392118f387SNathan Whitehorn 		    "This partition does not have a valid mountpoint "
10402118f387SNathan Whitehorn 		    "(for the partition from which you intend to boot the "
10412118f387SNathan Whitehorn 		    "operating system, the mountpoint should be /). Are you "
10422118f387SNathan Whitehorn 		    "sure you want to continue?"
10432118f387SNathan Whitehorn 		, 0, 0);
10442118f387SNathan Whitehorn 		dialog_vars.defaultno = FALSE;
10452118f387SNathan Whitehorn 		if (choice == 1) /* cancel */
10462118f387SNathan Whitehorn 			goto addpartform;
10472118f387SNathan Whitehorn 	}
10482118f387SNathan Whitehorn 
1049a780c996SNathan Whitehorn 	/*
1050a780c996SNathan Whitehorn 	 * Error if this scheme needs nested partitions, this is one, and
1051a780c996SNathan Whitehorn 	 * a mountpoint was set.
1052a780c996SNathan Whitehorn 	 */
1053a780c996SNathan Whitehorn 	if (strcmp(items[0].text, "freebsd") == 0 &&
1054a780c996SNathan Whitehorn 	    strlen(items[2].text) > 0) {
1055a780c996SNathan Whitehorn 		dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1056a780c996SNathan Whitehorn 		    "nested BSD-type partition schemes and cannot have "
1057a780c996SNathan Whitehorn 		    "mountpoints. After creating one, select it and press "
1058a780c996SNathan Whitehorn 		    "Create again to add the actual file systems.", 0, 0, TRUE);
1059a780c996SNathan Whitehorn 		goto addpartform;
1060a780c996SNathan Whitehorn 	}
1061a780c996SNathan Whitehorn 
10622118f387SNathan Whitehorn 	/* If this is the root partition, check that this scheme is bootable */
10632118f387SNathan Whitehorn 	if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
10642118f387SNathan Whitehorn 		char message[512];
10652118f387SNathan Whitehorn 		sprintf(message, "This partition scheme (%s) is not bootable "
10662118f387SNathan Whitehorn 		    "on this platform. Are you sure you want to proceed?",
10672118f387SNathan Whitehorn 		    scheme);
10682118f387SNathan Whitehorn 		dialog_vars.defaultno = TRUE;
10692118f387SNathan Whitehorn 		choice = dialog_yesno("Warning", message, 0, 0);
10702118f387SNathan Whitehorn 		dialog_vars.defaultno = FALSE;
10712118f387SNathan Whitehorn 		if (choice == 1) /* cancel */
10722118f387SNathan Whitehorn 			goto addpartform;
10732118f387SNathan Whitehorn 	}
10742118f387SNathan Whitehorn 
10756e15678aSNathan Whitehorn 	/* If this is the root partition, check that this fs is bootable */
10766e15678aSNathan Whitehorn 	if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
10776e15678aSNathan Whitehorn 	    items[0].text)) {
10786e15678aSNathan Whitehorn 		char message[512];
10796e15678aSNathan Whitehorn 		sprintf(message, "This file system (%s) is not bootable "
10806e15678aSNathan Whitehorn 		    "on this system. Are you sure you want to proceed?",
10816e15678aSNathan Whitehorn 		    items[0].text);
10826e15678aSNathan Whitehorn 		dialog_vars.defaultno = TRUE;
10836e15678aSNathan Whitehorn 		choice = dialog_yesno("Warning", message, 0, 0);
10846e15678aSNathan Whitehorn 		dialog_vars.defaultno = FALSE;
10856e15678aSNathan Whitehorn 		if (choice == 1) /* cancel */
10866e15678aSNathan Whitehorn 			goto addpartform;
10876e15678aSNathan Whitehorn 	}
10886e15678aSNathan Whitehorn 
10892118f387SNathan Whitehorn 	/*
10902118f387SNathan Whitehorn 	 * If this is the root partition, and we need a boot partition, ask
10912118f387SNathan Whitehorn 	 * the user to add one.
10922118f387SNathan Whitehorn 	 */
1093a780c996SNathan Whitehorn 
1094a780c996SNathan Whitehorn 	/* Check for existing freebsd-boot partition */
1095a780c996SNathan Whitehorn 	LIST_FOREACH(pp, &geom->lg_provider, lg_provider) {
1096a780c996SNathan Whitehorn 		struct partition_metadata *md;
1097a780c996SNathan Whitehorn 		md = get_part_metadata(pp->lg_name, 0);
1098a780c996SNathan Whitehorn 		if (md == NULL || !md->bootcode)
1099a780c996SNathan Whitehorn 			continue;
1100a780c996SNathan Whitehorn 		LIST_FOREACH(gc, &pp->lg_config, lg_config)
1101a780c996SNathan Whitehorn 			if (strcmp(gc->lg_name, "type") == 0)
1102a780c996SNathan Whitehorn 				break;
11036b446ed5SNathan Whitehorn 		if (gc != NULL && strcmp(gc->lg_val,
11046b446ed5SNathan Whitehorn 		    bootpart_type(scheme)) == 0)
1105a780c996SNathan Whitehorn 			break;
1106a780c996SNathan Whitehorn 	}
1107a780c996SNathan Whitehorn 
1108a780c996SNathan Whitehorn 	/* If there isn't one, and we need one, ask */
11098d795806SNathan Whitehorn 	if ((strcmp(items[0].text, "freebsd") == 0 ||
11108d795806SNathan Whitehorn 	    strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0 &&
1111a780c996SNathan Whitehorn 	    pp == NULL) {
11122118f387SNathan Whitehorn 		if (interactive)
11132118f387SNathan Whitehorn 			choice = dialog_yesno("Boot Partition",
11142118f387SNathan Whitehorn 			    "This partition scheme requires a boot partition "
11152118f387SNathan Whitehorn 			    "for the disk to be bootable. Would you like to "
11162118f387SNathan Whitehorn 			    "make one now?", 0, 0);
11172118f387SNathan Whitehorn 		else
11182118f387SNathan Whitehorn 			choice = 0;
11192118f387SNathan Whitehorn 
11202118f387SNathan Whitehorn 		if (choice == 0) { /* yes */
11212118f387SNathan Whitehorn 			r = gctl_get_handle();
11222118f387SNathan Whitehorn 			gctl_ro_param(r, "class", -1, "PART");
11232118f387SNathan Whitehorn 			gctl_ro_param(r, "arg0", -1, geom->lg_name);
11242118f387SNathan Whitehorn 			gctl_ro_param(r, "flags", -1, GPART_FLAGS);
11252118f387SNathan Whitehorn 			gctl_ro_param(r, "verb", -1, "add");
11266b446ed5SNathan Whitehorn 			gctl_ro_param(r, "type", -1, bootpart_type(scheme));
11272118f387SNathan Whitehorn 			snprintf(sizestr, sizeof(sizestr), "%jd",
11282118f387SNathan Whitehorn 			    bootpart_size(scheme) / sector);
11292118f387SNathan Whitehorn 			gctl_ro_param(r, "size", -1, sizestr);
11302118f387SNathan Whitehorn 			snprintf(startstr, sizeof(startstr), "%jd", firstfree);
11312118f387SNathan Whitehorn 			gctl_ro_param(r, "start", -1, startstr);
11322118f387SNathan Whitehorn 			gctl_rw_param(r, "output", sizeof(output), output);
11332118f387SNathan Whitehorn 			errstr = gctl_issue(r);
11342118f387SNathan Whitehorn 			if (errstr != NULL && errstr[0] != '\0')
11352118f387SNathan Whitehorn 				gpart_show_error("Error", NULL, errstr);
11362118f387SNathan Whitehorn 			gctl_free(r);
11372118f387SNathan Whitehorn 
11382118f387SNathan Whitehorn 			get_part_metadata(strtok(output, " "), 1)->bootcode = 1;
11392118f387SNathan Whitehorn 
11402118f387SNathan Whitehorn 			/* Now adjust the part we are really adding forward */
11412118f387SNathan Whitehorn 			firstfree += bootpart_size(scheme) / sector;
11422118f387SNathan Whitehorn 			size -= (bootpart_size(scheme) + stripe)/sector;
11432118f387SNathan Whitehorn 			if (stripe > 0 && (firstfree*sector % stripe) != 0)
11442118f387SNathan Whitehorn 				firstfree += (stripe - ((firstfree*sector) %
11452118f387SNathan Whitehorn 				    stripe)) / sector;
11462118f387SNathan Whitehorn 		}
11472118f387SNathan Whitehorn 	}
11482118f387SNathan Whitehorn 
11492118f387SNathan Whitehorn 	r = gctl_get_handle();
11502118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
11512118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, geom->lg_name);
11522118f387SNathan Whitehorn 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
11532118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "add");
11542118f387SNathan Whitehorn 
11552118f387SNathan Whitehorn 	gctl_ro_param(r, "type", -1, items[0].text);
11562118f387SNathan Whitehorn 	snprintf(sizestr, sizeof(sizestr), "%jd", size);
11572118f387SNathan Whitehorn 	gctl_ro_param(r, "size", -1, sizestr);
11582118f387SNathan Whitehorn 	snprintf(startstr, sizeof(startstr), "%jd", firstfree);
11592118f387SNathan Whitehorn 	gctl_ro_param(r, "start", -1, startstr);
11602118f387SNathan Whitehorn 	if (items[3].text[0] != '\0')
11612118f387SNathan Whitehorn 		gctl_ro_param(r, "label", -1, items[3].text);
11622118f387SNathan Whitehorn 	gctl_rw_param(r, "output", sizeof(output), output);
11632118f387SNathan Whitehorn 	errstr = gctl_issue(r);
11642118f387SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0') {
11652118f387SNathan Whitehorn 		gpart_show_error("Error", NULL, errstr);
11662118f387SNathan Whitehorn 		gctl_free(r);
11672118f387SNathan Whitehorn 		goto addpartform;
11682118f387SNathan Whitehorn 	}
11697899c6c1SNathan Whitehorn 	newpartname = strtok(output, " ");
11707899c6c1SNathan Whitehorn 	gctl_free(r);
11717899c6c1SNathan Whitehorn 
11727899c6c1SNathan Whitehorn 	/*
11737899c6c1SNathan Whitehorn 	 * Try to destroy any geom that gpart picked up already here from
11747899c6c1SNathan Whitehorn 	 * dirty blocks.
11757899c6c1SNathan Whitehorn 	 */
11767899c6c1SNathan Whitehorn 	r = gctl_get_handle();
11777899c6c1SNathan Whitehorn 	gctl_ro_param(r, "class", -1, "PART");
11787899c6c1SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, newpartname);
11797899c6c1SNathan Whitehorn 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
11807899c6c1SNathan Whitehorn 	junk = 1;
11817899c6c1SNathan Whitehorn 	gctl_ro_param(r, "force", sizeof(junk), &junk);
11827899c6c1SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "destroy");
11837899c6c1SNathan Whitehorn 	gctl_issue(r); /* Error usually expected and non-fatal */
11847899c6c1SNathan Whitehorn 	gctl_free(r);
11852118f387SNathan Whitehorn 
11866b446ed5SNathan Whitehorn 	if (strcmp(items[0].text, bootpart_type(scheme)) == 0)
11877899c6c1SNathan Whitehorn 		get_part_metadata(newpartname, 1)->bootcode = 1;
11882118f387SNathan Whitehorn 	else if (strcmp(items[0].text, "freebsd") == 0)
11897899c6c1SNathan Whitehorn 		gpart_partition(newpartname, "BSD");
11902118f387SNathan Whitehorn 	else
11917899c6c1SNathan Whitehorn 		set_default_part_metadata(newpartname, scheme,
119250de5d07SNathan Whitehorn 		    items[0].text, items[2].text, newfs);
11932118f387SNathan Whitehorn 
11942118f387SNathan Whitehorn 	for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
11952118f387SNathan Whitehorn 		if (items[i].text_free)
11962118f387SNathan Whitehorn 			free(items[i].text);
11972118f387SNathan Whitehorn 
11982118f387SNathan Whitehorn 	if (partname != NULL)
11997899c6c1SNathan Whitehorn 		*partname = strdup(newpartname);
12002118f387SNathan Whitehorn }
12012118f387SNathan Whitehorn 
12022118f387SNathan Whitehorn void
12032118f387SNathan Whitehorn gpart_delete(struct gprovider *pp)
12042118f387SNathan Whitehorn {
12052118f387SNathan Whitehorn 	struct gconfig *gc;
12062118f387SNathan Whitehorn 	struct ggeom *geom;
12072118f387SNathan Whitehorn 	struct gconsumer *cp;
12082118f387SNathan Whitehorn 	struct gctl_req *r;
12092118f387SNathan Whitehorn 	const char *errstr;
12102118f387SNathan Whitehorn 	intmax_t idx;
1211f36a5e0fSNathan Whitehorn 	int is_partition;
12122118f387SNathan Whitehorn 
12132118f387SNathan Whitehorn 	/* Is it a partition? */
12142118f387SNathan Whitehorn 	is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
12152118f387SNathan Whitehorn 
12162118f387SNathan Whitehorn 	/* Find out if this is the root of a gpart geom */
12172118f387SNathan Whitehorn 	geom = NULL;
12182118f387SNathan Whitehorn 	LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
12192118f387SNathan Whitehorn 		if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
12202118f387SNathan Whitehorn 			geom = cp->lg_geom;
12212118f387SNathan Whitehorn 			break;
12222118f387SNathan Whitehorn 		}
12232118f387SNathan Whitehorn 
1224f36a5e0fSNathan Whitehorn 	/* If so, destroy all children */
12252118f387SNathan Whitehorn 	if (geom != NULL) {
1226f36a5e0fSNathan Whitehorn 		gpart_destroy(geom);
1227f36a5e0fSNathan Whitehorn 
1228f36a5e0fSNathan Whitehorn 		/* If this is a partition, revert it, so it can be deleted */
12292118f387SNathan Whitehorn 		if (is_partition) {
1230f36a5e0fSNathan Whitehorn 			r = gctl_get_handle();
1231f36a5e0fSNathan Whitehorn 			gctl_ro_param(r, "class", -1, "PART");
1232f36a5e0fSNathan Whitehorn 			gctl_ro_param(r, "arg0", -1, geom->lg_name);
1233f36a5e0fSNathan Whitehorn 			gctl_ro_param(r, "verb", -1, "undo");
1234f36a5e0fSNathan Whitehorn 			gctl_issue(r); /* Ignore non-fatal errors */
1235f36a5e0fSNathan Whitehorn 			gctl_free(r);
12362118f387SNathan Whitehorn 		}
12372118f387SNathan Whitehorn 	}
12382118f387SNathan Whitehorn 
12392118f387SNathan Whitehorn 	/*
12402118f387SNathan Whitehorn 	 * If this is not a partition, see if that is a problem, complain if
12412118f387SNathan Whitehorn 	 * necessary, and return always, since we need not do anything further,
12422118f387SNathan Whitehorn 	 * error or no.
12432118f387SNathan Whitehorn 	 */
12442118f387SNathan Whitehorn 	if (!is_partition) {
12452118f387SNathan Whitehorn 		if (geom == NULL)
12462118f387SNathan Whitehorn 			dialog_msgbox("Error",
12472118f387SNathan Whitehorn 			    "Only partitions can be deleted.", 0, 0, TRUE);
12482118f387SNathan Whitehorn 		return;
12492118f387SNathan Whitehorn 	}
12502118f387SNathan Whitehorn 
12512118f387SNathan Whitehorn 	r = gctl_get_handle();
12522118f387SNathan Whitehorn 	gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
12532118f387SNathan Whitehorn 	gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
12542118f387SNathan Whitehorn 	gctl_ro_param(r, "flags", -1, GPART_FLAGS);
12552118f387SNathan Whitehorn 	gctl_ro_param(r, "verb", -1, "delete");
12562118f387SNathan Whitehorn 
12572118f387SNathan Whitehorn 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
12582118f387SNathan Whitehorn 		if (strcmp(gc->lg_name, "index") == 0) {
12592118f387SNathan Whitehorn 			idx = atoi(gc->lg_val);
12602118f387SNathan Whitehorn 			gctl_ro_param(r, "index", sizeof(idx), &idx);
12612118f387SNathan Whitehorn 			break;
12622118f387SNathan Whitehorn 		}
12632118f387SNathan Whitehorn 	}
12642118f387SNathan Whitehorn 
12652118f387SNathan Whitehorn 	errstr = gctl_issue(r);
12662118f387SNathan Whitehorn 	if (errstr != NULL && errstr[0] != '\0') {
12672118f387SNathan Whitehorn 		gpart_show_error("Error", NULL, errstr);
12682118f387SNathan Whitehorn 		gctl_free(r);
12692118f387SNathan Whitehorn 		return;
12702118f387SNathan Whitehorn 	}
12712118f387SNathan Whitehorn 
12722118f387SNathan Whitehorn 	gctl_free(r);
12732118f387SNathan Whitehorn 
12742118f387SNathan Whitehorn 	delete_part_metadata(pp->lg_name);
12752118f387SNathan Whitehorn }
12762118f387SNathan Whitehorn 
12772118f387SNathan Whitehorn void
12782118f387SNathan Whitehorn gpart_revert_all(struct gmesh *mesh)
12792118f387SNathan Whitehorn {
12802118f387SNathan Whitehorn 	struct gclass *classp;
12812118f387SNathan Whitehorn 	struct gconfig *gc;
12822118f387SNathan Whitehorn 	struct ggeom *gp;
12832118f387SNathan Whitehorn 	struct gctl_req *r;
12842118f387SNathan Whitehorn 	const char *modified;
12852118f387SNathan Whitehorn 
12862118f387SNathan Whitehorn 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
12872118f387SNathan Whitehorn 		if (strcmp(classp->lg_name, "PART") == 0)
12882118f387SNathan Whitehorn 			break;
12892118f387SNathan Whitehorn 	}
12902118f387SNathan Whitehorn 
12912118f387SNathan Whitehorn 	if (strcmp(classp->lg_name, "PART") != 0) {
12922118f387SNathan Whitehorn 		dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
12932118f387SNathan Whitehorn 		return;
12942118f387SNathan Whitehorn 	}
12952118f387SNathan Whitehorn 
12962118f387SNathan Whitehorn 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
12972118f387SNathan Whitehorn 		modified = "true"; /* XXX: If we don't know (kernel too old),
12982118f387SNathan Whitehorn 				    * assume there are modifications. */
12992118f387SNathan Whitehorn 		LIST_FOREACH(gc, &gp->lg_config, lg_config) {
13002118f387SNathan Whitehorn 			if (strcmp(gc->lg_name, "modified") == 0) {
13012118f387SNathan Whitehorn 				modified = gc->lg_val;
13022118f387SNathan Whitehorn 				break;
13032118f387SNathan Whitehorn 			}
13042118f387SNathan Whitehorn 		}
13052118f387SNathan Whitehorn 
13062118f387SNathan Whitehorn 		if (strcmp(modified, "false") == 0)
13072118f387SNathan Whitehorn 			continue;
13082118f387SNathan Whitehorn 
13092118f387SNathan Whitehorn 		r = gctl_get_handle();
13102118f387SNathan Whitehorn 		gctl_ro_param(r, "class", -1, "PART");
13112118f387SNathan Whitehorn 		gctl_ro_param(r, "arg0", -1, gp->lg_name);
13122118f387SNathan Whitehorn 		gctl_ro_param(r, "verb", -1, "undo");
13132118f387SNathan Whitehorn 
13147899c6c1SNathan Whitehorn 		gctl_issue(r);
13152118f387SNathan Whitehorn 		gctl_free(r);
13162118f387SNathan Whitehorn 	}
13172118f387SNathan Whitehorn }
13182118f387SNathan Whitehorn 
13192118f387SNathan Whitehorn void
13202118f387SNathan Whitehorn gpart_commit(struct gmesh *mesh)
13212118f387SNathan Whitehorn {
13222118f387SNathan Whitehorn 	struct partition_metadata *md;
13232118f387SNathan Whitehorn 	struct gclass *classp;
13242118f387SNathan Whitehorn 	struct ggeom *gp;
13252118f387SNathan Whitehorn 	struct gconfig *gc;
13262118f387SNathan Whitehorn 	struct gconsumer *cp;
13272118f387SNathan Whitehorn 	struct gprovider *pp;
13282118f387SNathan Whitehorn 	struct gctl_req *r;
13292118f387SNathan Whitehorn 	const char *errstr;
13302118f387SNathan Whitehorn 	const char *modified;
13316e15678aSNathan Whitehorn 	const char *rootfs;
13322118f387SNathan Whitehorn 
13332118f387SNathan Whitehorn 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
13342118f387SNathan Whitehorn 		if (strcmp(classp->lg_name, "PART") == 0)
13352118f387SNathan Whitehorn 			break;
13362118f387SNathan Whitehorn 	}
13372118f387SNathan Whitehorn 
13386e15678aSNathan Whitehorn 	/* Figure out what filesystem / uses */
13396e15678aSNathan Whitehorn 	rootfs = "ufs"; /* Assume ufs if nothing else present */
13406e15678aSNathan Whitehorn 	TAILQ_FOREACH(md, &part_metadata, metadata) {
13416e15678aSNathan Whitehorn 		if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
13426e15678aSNathan Whitehorn 			rootfs = md->fstab->fs_vfstype;
13436e15678aSNathan Whitehorn 			break;
13446e15678aSNathan Whitehorn 		}
13456e15678aSNathan Whitehorn 	}
13466e15678aSNathan Whitehorn 
13472118f387SNathan Whitehorn 	if (strcmp(classp->lg_name, "PART") != 0) {
13482118f387SNathan Whitehorn 		dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
13492118f387SNathan Whitehorn 		return;
13502118f387SNathan Whitehorn 	}
13512118f387SNathan Whitehorn 
13522118f387SNathan Whitehorn 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
13532118f387SNathan Whitehorn 		modified = "true"; /* XXX: If we don't know (kernel too old),
13542118f387SNathan Whitehorn 				    * assume there are modifications. */
13552118f387SNathan Whitehorn 		LIST_FOREACH(gc, &gp->lg_config, lg_config) {
13562118f387SNathan Whitehorn 			if (strcmp(gc->lg_name, "modified") == 0) {
13572118f387SNathan Whitehorn 				modified = gc->lg_val;
13582118f387SNathan Whitehorn 				break;
13592118f387SNathan Whitehorn 			}
13602118f387SNathan Whitehorn 		}
13612118f387SNathan Whitehorn 
13622118f387SNathan Whitehorn 		if (strcmp(modified, "false") == 0)
13632118f387SNathan Whitehorn 			continue;
13642118f387SNathan Whitehorn 
13652118f387SNathan Whitehorn 		/* Add bootcode if necessary, before the commit */
13662118f387SNathan Whitehorn 		md = get_part_metadata(gp->lg_name, 0);
13672118f387SNathan Whitehorn 		if (md != NULL && md->bootcode)
13682118f387SNathan Whitehorn 			gpart_bootcode(gp);
13692118f387SNathan Whitehorn 
13702118f387SNathan Whitehorn 		/* Now install partcode on its partitions, if necessary */
13712118f387SNathan Whitehorn 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
13722118f387SNathan Whitehorn 			md = get_part_metadata(pp->lg_name, 0);
13732118f387SNathan Whitehorn 			if (md == NULL || !md->bootcode)
13742118f387SNathan Whitehorn 				continue;
13752118f387SNathan Whitehorn 
13762118f387SNathan Whitehorn 			/* Mark this partition active if that's required */
13772118f387SNathan Whitehorn 			gpart_activate(pp);
13782118f387SNathan Whitehorn 
13792118f387SNathan Whitehorn 			/* Check if the partition has sub-partitions */
13802118f387SNathan Whitehorn 			LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
13812118f387SNathan Whitehorn 				if (strcmp(cp->lg_geom->lg_class->lg_name,
13822118f387SNathan Whitehorn 				    "PART") == 0)
13832118f387SNathan Whitehorn 					break;
13842118f387SNathan Whitehorn 
13852118f387SNathan Whitehorn 			if (cp == NULL) /* No sub-partitions */
13866e15678aSNathan Whitehorn 				gpart_partcode(pp, rootfs);
13872118f387SNathan Whitehorn 		}
13882118f387SNathan Whitehorn 
13892118f387SNathan Whitehorn 		r = gctl_get_handle();
13902118f387SNathan Whitehorn 		gctl_ro_param(r, "class", -1, "PART");
13912118f387SNathan Whitehorn 		gctl_ro_param(r, "arg0", -1, gp->lg_name);
13922118f387SNathan Whitehorn 		gctl_ro_param(r, "verb", -1, "commit");
13932118f387SNathan Whitehorn 
13942118f387SNathan Whitehorn 		errstr = gctl_issue(r);
13952118f387SNathan Whitehorn 		if (errstr != NULL && errstr[0] != '\0')
13962118f387SNathan Whitehorn 			gpart_show_error("Error", NULL, errstr);
13972118f387SNathan Whitehorn 		gctl_free(r);
13982118f387SNathan Whitehorn 	}
13992118f387SNathan Whitehorn }
14002118f387SNathan Whitehorn 
1401