xref: /freebsd/usr.sbin/bsdinstall/partedit/part_wizard.c (revision 2c4499dcd72a335a72341492e37f859eed422a6f)
12118f387SNathan Whitehorn /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
31de7b4b8SPedro F. Giffuni  *
42118f387SNathan Whitehorn  * Copyright (c) 2011 Nathan Whitehorn
52118f387SNathan Whitehorn  * All rights reserved.
62118f387SNathan Whitehorn  *
72118f387SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
82118f387SNathan Whitehorn  * modification, are permitted provided that the following conditions
92118f387SNathan Whitehorn  * are met:
102118f387SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
112118f387SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
122118f387SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
132118f387SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
142118f387SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
152118f387SNathan Whitehorn  *
162118f387SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172118f387SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182118f387SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192118f387SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202118f387SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212118f387SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222118f387SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232118f387SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242118f387SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252118f387SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262118f387SNathan Whitehorn  * SUCH DAMAGE.
272118f387SNathan Whitehorn  *
282118f387SNathan Whitehorn  * $FreeBSD$
292118f387SNathan Whitehorn  */
302118f387SNathan Whitehorn 
312118f387SNathan Whitehorn #include <sys/param.h>
3250e24496SAlfonso S. Siciliano 
33d6f6b201SEnji Cooper #include <errno.h>
34d6f6b201SEnji Cooper #include <inttypes.h>
35d6f6b201SEnji Cooper #include <libutil.h>
3650e24496SAlfonso S. Siciliano #include <stdio.h>
3750e24496SAlfonso S. Siciliano #include <stdlib.h>
386e15678aSNathan Whitehorn #include <string.h>
396e15678aSNathan Whitehorn 
402118f387SNathan Whitehorn #include <libgeom.h>
4150e24496SAlfonso S. Siciliano #include <bsddialog.h>
422118f387SNathan Whitehorn 
432118f387SNathan Whitehorn #include "partedit.h"
442118f387SNathan Whitehorn 
452118f387SNathan Whitehorn #define MIN_FREE_SPACE		(1024*1024*1024) /* 1 GB */
462118f387SNathan Whitehorn #define SWAP_SIZE(available)	MIN(available/20, 4*1024*1024*1024LL)
472118f387SNathan Whitehorn 
482118f387SNathan Whitehorn static char *wizard_partition(struct gmesh *mesh, const char *disk);
492118f387SNathan Whitehorn 
502118f387SNathan Whitehorn int
51d547c56fSEnji Cooper part_wizard(const char *fsreq)
52d547c56fSEnji Cooper {
532118f387SNathan Whitehorn 	char *disk, *schemeroot;
546e15678aSNathan Whitehorn 	const char *fstype;
55d547c56fSEnji Cooper 	struct gmesh mesh;
56d547c56fSEnji Cooper 	int error;
5750e24496SAlfonso S. Siciliano 	struct bsddialog_conf conf;
5850e24496SAlfonso S. Siciliano 
5950e24496SAlfonso S. Siciliano 	bsddialog_initconf(&conf);
606e15678aSNathan Whitehorn 
616e15678aSNathan Whitehorn 	if (fsreq != NULL)
626e15678aSNathan Whitehorn 		fstype = fsreq;
636e15678aSNathan Whitehorn 	else
646e15678aSNathan Whitehorn 		fstype = "ufs";
652118f387SNathan Whitehorn 
662118f387SNathan Whitehorn startwizard:
672118f387SNathan Whitehorn 	error = geom_gettree(&mesh);
682118f387SNathan Whitehorn 
6950e24496SAlfonso S. Siciliano 	bsddialog_backtitle(&conf, "FreeBSD Installer");
702118f387SNathan Whitehorn 	error = geom_gettree(&mesh);
715140034cSNathan Whitehorn 	disk = boot_disk_select(&mesh);
722118f387SNathan Whitehorn 	if (disk == NULL)
732118f387SNathan Whitehorn 		return (1);
742118f387SNathan Whitehorn 
7550e24496SAlfonso S. Siciliano 	bsddialog_clearterminal();
7650e24496SAlfonso S. Siciliano 	bsddialog_backtitle(&conf, "FreeBSD Installer");
772118f387SNathan Whitehorn 	schemeroot = wizard_partition(&mesh, disk);
782118f387SNathan Whitehorn 	free(disk);
792118f387SNathan Whitehorn 	if (schemeroot == NULL)
802118f387SNathan Whitehorn 		return (1);
812118f387SNathan Whitehorn 
822118f387SNathan Whitehorn 	geom_deletetree(&mesh);
8350e24496SAlfonso S. Siciliano 	bsddialog_clearterminal();
8450e24496SAlfonso S. Siciliano 	bsddialog_backtitle(&conf, "FreeBSD Installer");
852118f387SNathan Whitehorn 	error = geom_gettree(&mesh);
862118f387SNathan Whitehorn 
876e15678aSNathan Whitehorn 	error = wizard_makeparts(&mesh, schemeroot, fstype, 1);
882118f387SNathan Whitehorn 	if (error)
892118f387SNathan Whitehorn 		goto startwizard;
902118f387SNathan Whitehorn 	free(schemeroot);
912118f387SNathan Whitehorn 
922118f387SNathan Whitehorn 	geom_deletetree(&mesh);
932118f387SNathan Whitehorn 
942118f387SNathan Whitehorn 	return (0);
952118f387SNathan Whitehorn }
962118f387SNathan Whitehorn 
975140034cSNathan Whitehorn char *
985140034cSNathan Whitehorn boot_disk_select(struct gmesh *mesh)
992118f387SNathan Whitehorn {
1002118f387SNathan Whitehorn 	struct gclass *classp;
1012118f387SNathan Whitehorn 	struct gconfig *gc;
1022118f387SNathan Whitehorn 	struct ggeom *gp;
1032118f387SNathan Whitehorn 	struct gprovider *pp;
10450e24496SAlfonso S. Siciliano 	struct bsddialog_menuitem *disks = NULL;
1059636f84bSNathan Whitehorn 	const char *type, *desc;
1062118f387SNathan Whitehorn 	char diskdesc[512];
1072118f387SNathan Whitehorn 	char *chosen;
108*2c4499dcSBrad Davis 	int i, button, fd, selected, n = 0;
10950e24496SAlfonso S. Siciliano 	struct bsddialog_conf conf;
11050e24496SAlfonso S. Siciliano 
11150e24496SAlfonso S. Siciliano 	bsddialog_initconf(&conf);
1122118f387SNathan Whitehorn 
1132118f387SNathan Whitehorn 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1142118f387SNathan Whitehorn 		if (strcmp(classp->lg_name, "DISK") != 0 &&
115bcc25b7eSNathan Whitehorn 		    strcmp(classp->lg_name, "RAID") != 0 &&
1162118f387SNathan Whitehorn 		    strcmp(classp->lg_name, "MD") != 0)
1172118f387SNathan Whitehorn 			continue;
1182118f387SNathan Whitehorn 
1192118f387SNathan Whitehorn 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1202118f387SNathan Whitehorn 			if (LIST_EMPTY(&gp->lg_provider))
1212118f387SNathan Whitehorn 				continue;
1222118f387SNathan Whitehorn 
1232118f387SNathan Whitehorn 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1249636f84bSNathan Whitehorn 				desc = type = NULL;
1259636f84bSNathan Whitehorn 				LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1262118f387SNathan Whitehorn 					if (strcmp(gc->lg_name, "type") == 0)
1272118f387SNathan Whitehorn 						type = gc->lg_val;
1289636f84bSNathan Whitehorn 					if (strcmp(gc->lg_name, "descr") == 0)
1299636f84bSNathan Whitehorn 						desc = gc->lg_val;
1309636f84bSNathan Whitehorn 				}
1312118f387SNathan Whitehorn 
1329636f84bSNathan Whitehorn 				/* Skip swap-backed md and WORM devices */
1332118f387SNathan Whitehorn 				if (strcmp(classp->lg_name, "MD") == 0 &&
1342118f387SNathan Whitehorn 				    type != NULL && strcmp(type, "swap") == 0)
1352118f387SNathan Whitehorn 					continue;
1369636f84bSNathan Whitehorn 				if (strncmp(pp->lg_name, "cd", 2) == 0)
1379636f84bSNathan Whitehorn 					continue;
138964ad27fSBrad Davis 				/*
139964ad27fSBrad Davis 				 * Check if the disk is available to be opened for
140964ad27fSBrad Davis 				 * write operations, it helps prevent the USB
141964ad27fSBrad Davis 				 * stick used to boot from being listed as an option
142964ad27fSBrad Davis 				 */
143964ad27fSBrad Davis 				fd = g_open(pp->lg_name, 1);
144964ad27fSBrad Davis 				if (fd == -1) {
145964ad27fSBrad Davis 					continue;
146964ad27fSBrad Davis 				}
147964ad27fSBrad Davis 				g_close(fd);
1482118f387SNathan Whitehorn 
1492118f387SNathan Whitehorn 				disks = realloc(disks, (++n)*sizeof(disks[0]));
1502118f387SNathan Whitehorn 				disks[n-1].name = pp->lg_name;
1512118f387SNathan Whitehorn 				humanize_number(diskdesc, 7, pp->lg_mediasize,
1522118f387SNathan Whitehorn 				    "B", HN_AUTOSCALE, HN_DECIMAL);
1532118f387SNathan Whitehorn 				if (strncmp(pp->lg_name, "ad", 2) == 0)
1542118f387SNathan Whitehorn 					strcat(diskdesc, " ATA Hard Disk");
1552118f387SNathan Whitehorn 				else if (strncmp(pp->lg_name, "md", 2) == 0)
1562118f387SNathan Whitehorn 					strcat(diskdesc, " Memory Disk");
1579636f84bSNathan Whitehorn 				else
1589636f84bSNathan Whitehorn 					strcat(diskdesc, " Disk");
1599636f84bSNathan Whitehorn 
1609636f84bSNathan Whitehorn 				if (desc != NULL)
1619636f84bSNathan Whitehorn 					snprintf(diskdesc, sizeof(diskdesc),
1629636f84bSNathan Whitehorn 					    "%s <%s>", diskdesc, desc);
1639636f84bSNathan Whitehorn 
16450e24496SAlfonso S. Siciliano 				disks[n-1].prefix = "";
16550e24496SAlfonso S. Siciliano 				disks[n-1].on = false;
16650e24496SAlfonso S. Siciliano 				disks[n-1].depth = 0;
16750e24496SAlfonso S. Siciliano 				disks[n-1].desc = strdup(diskdesc);
16850e24496SAlfonso S. Siciliano 				disks[n-1].bottomdesc = "";
1692118f387SNathan Whitehorn 			}
1702118f387SNathan Whitehorn 		}
1712118f387SNathan Whitehorn 	}
1722118f387SNathan Whitehorn 
1732118f387SNathan Whitehorn 	if (n > 1) {
17450e24496SAlfonso S. Siciliano 		conf.title = "Partitioning";
17550e24496SAlfonso S. Siciliano 		button = bsddialog_menu(&conf,
1762118f387SNathan Whitehorn 		    "Select the disk on which to install FreeBSD.", 0, 0, 0,
17750e24496SAlfonso S. Siciliano 		    n, disks, &selected);
1782118f387SNathan Whitehorn 
17950e24496SAlfonso S. Siciliano 		chosen = (button == BSDDIALOG_OK) ?
18050e24496SAlfonso S. Siciliano 		    strdup(disks[selected].name) : NULL;
1812118f387SNathan Whitehorn 	} else if (n == 1) {
1822118f387SNathan Whitehorn 		chosen = strdup(disks[0].name);
1832118f387SNathan Whitehorn 	} else {
1842118f387SNathan Whitehorn 		chosen = NULL;
1852118f387SNathan Whitehorn 	}
1862118f387SNathan Whitehorn 
1872118f387SNathan Whitehorn 	for (i = 0; i < n; i++)
18850e24496SAlfonso S. Siciliano 		free((char*)disks[i].desc);
1892118f387SNathan Whitehorn 
1902118f387SNathan Whitehorn 	return (chosen);
1912118f387SNathan Whitehorn }
1922118f387SNathan Whitehorn 
1932118f387SNathan Whitehorn static struct gprovider *
1942118f387SNathan Whitehorn provider_for_name(struct gmesh *mesh, const char *name)
1952118f387SNathan Whitehorn {
1962118f387SNathan Whitehorn 	struct gclass *classp;
1972118f387SNathan Whitehorn 	struct gprovider *pp = NULL;
1982118f387SNathan Whitehorn 	struct ggeom *gp;
1992118f387SNathan Whitehorn 
2002118f387SNathan Whitehorn 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
2012118f387SNathan Whitehorn 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
2022118f387SNathan Whitehorn 			if (LIST_EMPTY(&gp->lg_provider))
2032118f387SNathan Whitehorn 				continue;
2042118f387SNathan Whitehorn 
2052118f387SNathan Whitehorn 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
2062118f387SNathan Whitehorn 				if (strcmp(pp->lg_name, name) == 0)
2072118f387SNathan Whitehorn 					break;
2082118f387SNathan Whitehorn 
2092118f387SNathan Whitehorn 			if (pp != NULL) break;
2102118f387SNathan Whitehorn 		}
2112118f387SNathan Whitehorn 
2122118f387SNathan Whitehorn 		if (pp != NULL) break;
2132118f387SNathan Whitehorn 	}
2142118f387SNathan Whitehorn 
2152118f387SNathan Whitehorn 	return (pp);
2162118f387SNathan Whitehorn }
2172118f387SNathan Whitehorn 
2182118f387SNathan Whitehorn static char *
2192118f387SNathan Whitehorn wizard_partition(struct gmesh *mesh, const char *disk)
2202118f387SNathan Whitehorn {
2212118f387SNathan Whitehorn 	struct gclass *classp;
2222118f387SNathan Whitehorn 	struct ggeom *gpart = NULL;
2232118f387SNathan Whitehorn 	struct gconfig *gc;
2242118f387SNathan Whitehorn 	char *retval = NULL;
225d547c56fSEnji Cooper 	const char *scheme = NULL;
226d547c56fSEnji Cooper 	char message[512];
2272118f387SNathan Whitehorn 	int choice;
22850e24496SAlfonso S. Siciliano 	struct bsddialog_conf conf;
22950e24496SAlfonso S. Siciliano 
23050e24496SAlfonso S. Siciliano 	bsddialog_initconf(&conf);
2312118f387SNathan Whitehorn 
2322118f387SNathan Whitehorn 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
2332118f387SNathan Whitehorn 		if (strcmp(classp->lg_name, "PART") == 0)
2342118f387SNathan Whitehorn 			break;
2352118f387SNathan Whitehorn 
2362118f387SNathan Whitehorn 	if (classp != NULL) {
2372118f387SNathan Whitehorn 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
2382118f387SNathan Whitehorn 			if (strcmp(gpart->lg_name, disk) == 0)
2392118f387SNathan Whitehorn 				break;
2402118f387SNathan Whitehorn 	}
2412118f387SNathan Whitehorn 
2422118f387SNathan Whitehorn 	if (gpart != NULL) {
2432118f387SNathan Whitehorn 		LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
2442118f387SNathan Whitehorn 			if (strcmp(gc->lg_name, "scheme") == 0) {
2452118f387SNathan Whitehorn 				scheme = gc->lg_val;
2462118f387SNathan Whitehorn 				break;
2472118f387SNathan Whitehorn 			}
2482118f387SNathan Whitehorn 		}
2492118f387SNathan Whitehorn 	}
2502118f387SNathan Whitehorn 
251da42677dSNathan Whitehorn 	/* Treat uncommitted scheme deletions as no scheme */
252da42677dSNathan Whitehorn 	if (scheme != NULL && strcmp(scheme, "(none)") == 0)
253da42677dSNathan Whitehorn 		scheme = NULL;
254da42677dSNathan Whitehorn 
2552118f387SNathan Whitehorn query:
25650e24496SAlfonso S. Siciliano 	conf.button.ok_label = "Entire Disk";
25750e24496SAlfonso S. Siciliano 	conf.button.cancel_label = "Partition";
2582118f387SNathan Whitehorn 	if (gpart != NULL)
25950e24496SAlfonso S. Siciliano 		conf.button.default_cancel = true;
2602118f387SNathan Whitehorn 
2612118f387SNathan Whitehorn 	snprintf(message, sizeof(message), "Would you like to use this entire "
2622118f387SNathan Whitehorn 	    "disk (%s) for FreeBSD or partition it to share it with other "
2632118f387SNathan Whitehorn 	    "operating systems? Using the entire disk will erase any data "
2642118f387SNathan Whitehorn 	    "currently stored there.", disk);
26550e24496SAlfonso S. Siciliano 	conf.title = "Partition";
26650e24496SAlfonso S. Siciliano 	choice = bsddialog_yesno(&conf, message, 9, 45);
2672118f387SNathan Whitehorn 
26850e24496SAlfonso S. Siciliano 	conf.button.ok_label = NULL;
26950e24496SAlfonso S. Siciliano 	conf.button.cancel_label = NULL;
27050e24496SAlfonso S. Siciliano 	conf.button.default_cancel = false;
2712118f387SNathan Whitehorn 
27250e24496SAlfonso S. Siciliano 	if (choice == BSDDIALOG_NO && scheme != NULL && !is_scheme_bootable(scheme)) {
2732118f387SNathan Whitehorn 		char warning[512];
2742118f387SNathan Whitehorn 		int subchoice;
2752118f387SNathan Whitehorn 
2762118f387SNathan Whitehorn 		sprintf(warning, "The existing partition scheme on this "
2772118f387SNathan Whitehorn 		    "disk (%s) is not bootable on this platform. To install "
2782118f387SNathan Whitehorn 		    "FreeBSD, it must be repartitioned. This will destroy all "
2792118f387SNathan Whitehorn 		    "data on the disk. Are you sure you want to proceed?",
2802118f387SNathan Whitehorn 		    scheme);
28150e24496SAlfonso S. Siciliano 		conf.title = "Non-bootable Disk";
28250e24496SAlfonso S. Siciliano 		subchoice = bsddialog_yesno(&conf, warning, 0, 0);
28350e24496SAlfonso S. Siciliano 		if (subchoice != BSDDIALOG_YES)
2842118f387SNathan Whitehorn 			goto query;
2852118f387SNathan Whitehorn 
286f36a5e0fSNathan Whitehorn 		gpart_destroy(gpart);
2877059fa6fSAllan Jude 		scheme = choose_part_type(default_scheme());
2887059fa6fSAllan Jude 		if (scheme == NULL)
2897059fa6fSAllan Jude 			return NULL;
2907059fa6fSAllan Jude 		gpart_partition(disk, scheme);
2912118f387SNathan Whitehorn 	}
2922118f387SNathan Whitehorn 
293da42677dSNathan Whitehorn 	if (scheme == NULL || choice == 0) {
294da42677dSNathan Whitehorn 		if (gpart != NULL && scheme != NULL) {
295da42677dSNathan Whitehorn 			/* Erase partitioned disk */
29650e24496SAlfonso S. Siciliano 			conf.title = "Confirmation";
29750e24496SAlfonso S. Siciliano 			choice = bsddialog_yesno(&conf, "This will erase "
2982118f387SNathan Whitehorn 			   "the disk. Are you sure you want to proceed?", 0, 0);
29950e24496SAlfonso S. Siciliano 			if (choice != BSDDIALOG_YES)
3002118f387SNathan Whitehorn 				goto query;
3012118f387SNathan Whitehorn 
302f36a5e0fSNathan Whitehorn 			gpart_destroy(gpart);
3032118f387SNathan Whitehorn 		}
3042118f387SNathan Whitehorn 
3057059fa6fSAllan Jude 		scheme = choose_part_type(default_scheme());
3067059fa6fSAllan Jude 		if (scheme == NULL)
3077059fa6fSAllan Jude 			return NULL;
3087059fa6fSAllan Jude 		gpart_partition(disk, scheme);
3092118f387SNathan Whitehorn 	}
3102118f387SNathan Whitehorn 
3112b375b4eSYoshihiro Takahashi 	if (strcmp(scheme, "MBR") == 0) {
3122118f387SNathan Whitehorn 		struct gmesh submesh;
3132118f387SNathan Whitehorn 		geom_gettree(&submesh);
3142118f387SNathan Whitehorn 		gpart_create(provider_for_name(&submesh, disk),
3152118f387SNathan Whitehorn 		    "freebsd", NULL, NULL, &retval,
3162118f387SNathan Whitehorn 		    choice /* Non-interactive for "Entire Disk" */);
3172118f387SNathan Whitehorn 		geom_deletetree(&submesh);
3182118f387SNathan Whitehorn 	} else {
3192118f387SNathan Whitehorn 		retval = strdup(disk);
3202118f387SNathan Whitehorn 	}
3212118f387SNathan Whitehorn 
3222118f387SNathan Whitehorn 	return (retval);
3232118f387SNathan Whitehorn }
3242118f387SNathan Whitehorn 
325a6b612e9SNathan Whitehorn int
326d547c56fSEnji Cooper wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
327d547c56fSEnji Cooper     int interactive)
3282118f387SNathan Whitehorn {
3292118f387SNathan Whitehorn 	struct gclass *classp;
3302118f387SNathan Whitehorn 	struct ggeom *gp;
3312118f387SNathan Whitehorn 	struct gprovider *pp;
3326e15678aSNathan Whitehorn 	char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
333d547c56fSEnji Cooper 	char *fsname;
334d547c56fSEnji Cooper 	struct gmesh submesh;
335d547c56fSEnji Cooper 	char swapsizestr[10], rootsizestr[10];
336d547c56fSEnji Cooper 	intmax_t swapsize, available;
3372118f387SNathan Whitehorn 	int retval;
33850e24496SAlfonso S. Siciliano 	struct bsddialog_conf conf;
3392118f387SNathan Whitehorn 
3406e15678aSNathan Whitehorn 	if (strcmp(fstype, "zfs") == 0) {
3416e15678aSNathan Whitehorn 		fsname = fsnames[1];
3426e15678aSNathan Whitehorn 	} else {
3436e15678aSNathan Whitehorn 		/* default to UFS */
3446e15678aSNathan Whitehorn 		fsname = fsnames[0];
3456e15678aSNathan Whitehorn 	}
3466e15678aSNathan Whitehorn 
3472118f387SNathan Whitehorn 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
3482118f387SNathan Whitehorn 		if (strcmp(classp->lg_name, "PART") == 0)
3492118f387SNathan Whitehorn 			break;
3502118f387SNathan Whitehorn 
3512118f387SNathan Whitehorn 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
3522118f387SNathan Whitehorn 		if (strcmp(gp->lg_name, disk) == 0)
3532118f387SNathan Whitehorn 			break;
3542118f387SNathan Whitehorn 
3552118f387SNathan Whitehorn 	pp = provider_for_name(mesh, disk);
3562118f387SNathan Whitehorn 
3572118f387SNathan Whitehorn 	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
358a6b612e9SNathan Whitehorn 	if (interactive && available < MIN_FREE_SPACE) {
3592118f387SNathan Whitehorn 		char availablestr[10], neededstr[10], message[512];
3602118f387SNathan Whitehorn 		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
3612118f387SNathan Whitehorn 		    HN_DECIMAL);
3622118f387SNathan Whitehorn 		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
3632118f387SNathan Whitehorn 		    HN_DECIMAL);
3642118f387SNathan Whitehorn 		sprintf(message, "There is not enough free space on %s to "
3652118f387SNathan Whitehorn 		    "install FreeBSD (%s free, %s required). Would you like "
3662118f387SNathan Whitehorn 		    "to choose another disk or to open the partition editor?",
3672118f387SNathan Whitehorn 		    disk, availablestr, neededstr);
3682118f387SNathan Whitehorn 
36950e24496SAlfonso S. Siciliano 		bsddialog_initconf(&conf);
37050e24496SAlfonso S. Siciliano 		conf.button.ok_label = "Another Disk";
37150e24496SAlfonso S. Siciliano 		conf.button.cancel_label = "Editor";
37250e24496SAlfonso S. Siciliano 		conf.title = "Warning";
37350e24496SAlfonso S. Siciliano 		retval = bsddialog_yesno(&conf, message, 0, 0);
3742118f387SNathan Whitehorn 
3752118f387SNathan Whitehorn 		return (!retval); /* Editor -> return 0 */
3762118f387SNathan Whitehorn 	}
3772118f387SNathan Whitehorn 
3782118f387SNathan Whitehorn 	swapsize = SWAP_SIZE(available);
3792118f387SNathan Whitehorn 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
3802118f387SNathan Whitehorn 	    HN_NOSPACE | HN_DECIMAL);
3812118f387SNathan Whitehorn 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
3822118f387SNathan Whitehorn 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
3832118f387SNathan Whitehorn 
3842118f387SNathan Whitehorn 	geom_gettree(&submesh);
3852118f387SNathan Whitehorn 	pp = provider_for_name(&submesh, disk);
3866e15678aSNathan Whitehorn 	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
3872118f387SNathan Whitehorn 	geom_deletetree(&submesh);
3882118f387SNathan Whitehorn 
3892118f387SNathan Whitehorn 	geom_gettree(&submesh);
3902118f387SNathan Whitehorn 	pp = provider_for_name(&submesh, disk);
3912118f387SNathan Whitehorn 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
3922118f387SNathan Whitehorn 	geom_deletetree(&submesh);
3932118f387SNathan Whitehorn 
3942118f387SNathan Whitehorn 	return (0);
3952118f387SNathan Whitehorn }
396