2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
44 #define MIN_FREE_SPACE (1024*1024*1024) /* 1 GB */
45 #define SWAP_SIZE(available) MIN(available/20, 4*1024*1024*1024LL)
47 static char *boot_disk(struct gmesh *mesh);
48 static char *wizard_partition(struct gmesh *mesh, const char *disk);
51 part_wizard(const char *fsreq)
53 char *disk, *schemeroot;
64 error = geom_gettree(&mesh);
67 error = geom_gettree(&mesh);
68 disk = boot_disk(&mesh);
74 schemeroot = wizard_partition(&mesh, disk);
76 if (schemeroot == NULL)
79 geom_deletetree(&mesh);
82 error = geom_gettree(&mesh);
84 error = wizard_makeparts(&mesh, schemeroot, fstype, 1);
89 geom_deletetree(&mesh);
95 boot_disk(struct gmesh *mesh)
97 struct gclass *classp;
100 struct gprovider *pp;
101 DIALOG_LISTITEM *disks = NULL;
102 const char *type, *desc;
105 int i, err, selected, n = 0;
107 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
108 if (strcmp(classp->lg_name, "DISK") != 0 &&
109 strcmp(classp->lg_name, "RAID") != 0 &&
110 strcmp(classp->lg_name, "MD") != 0)
113 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
114 if (LIST_EMPTY(&gp->lg_provider))
117 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
119 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
120 if (strcmp(gc->lg_name, "type") == 0)
122 if (strcmp(gc->lg_name, "descr") == 0)
126 /* Skip swap-backed md and WORM devices */
127 if (strcmp(classp->lg_name, "MD") == 0 &&
128 type != NULL && strcmp(type, "swap") == 0)
130 if (strncmp(pp->lg_name, "cd", 2) == 0)
133 disks = realloc(disks, (++n)*sizeof(disks[0]));
134 disks[n-1].name = pp->lg_name;
135 humanize_number(diskdesc, 7, pp->lg_mediasize,
136 "B", HN_AUTOSCALE, HN_DECIMAL);
137 if (strncmp(pp->lg_name, "ad", 2) == 0)
138 strcat(diskdesc, " ATA Hard Disk");
139 else if (strncmp(pp->lg_name, "md", 2) == 0)
140 strcat(diskdesc, " Memory Disk");
142 strcat(diskdesc, " Disk");
145 snprintf(diskdesc, sizeof(diskdesc),
146 "%s <%s>", diskdesc, desc);
148 disks[n-1].text = strdup(diskdesc);
149 disks[n-1].help = NULL;
150 disks[n-1].state = 0;
156 err = dlg_menu("Partitioning",
157 "Select the disk on which to install FreeBSD.", 0, 0, 0,
158 n, disks, &selected, NULL);
160 chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
162 chosen = strdup(disks[0].name);
167 for (i = 0; i < n; i++)
173 static struct gprovider *
174 provider_for_name(struct gmesh *mesh, const char *name)
176 struct gclass *classp;
177 struct gprovider *pp = NULL;
180 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
181 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
182 if (LIST_EMPTY(&gp->lg_provider))
185 LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
186 if (strcmp(pp->lg_name, name) == 0)
189 if (pp != NULL) break;
192 if (pp != NULL) break;
199 wizard_partition(struct gmesh *mesh, const char *disk)
201 struct gclass *classp;
202 struct ggeom *gpart = NULL;
205 const char *scheme = NULL;
209 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
210 if (strcmp(classp->lg_name, "PART") == 0)
213 if (classp != NULL) {
214 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
215 if (strcmp(gpart->lg_name, disk) == 0)
220 LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
221 if (strcmp(gc->lg_name, "scheme") == 0) {
228 /* Treat uncommitted scheme deletions as no scheme */
229 if (scheme != NULL && strcmp(scheme, "(none)") == 0)
233 dialog_vars.yes_label = "Entire Disk";
234 dialog_vars.no_label = "Partition";
236 dialog_vars.defaultno = TRUE;
238 snprintf(message, sizeof(message), "Would you like to use this entire "
239 "disk (%s) for FreeBSD or partition it to share it with other "
240 "operating systems? Using the entire disk will erase any data "
241 "currently stored there.", disk);
242 choice = dialog_yesno("Partition", message, 0, 0);
244 dialog_vars.yes_label = NULL;
245 dialog_vars.no_label = NULL;
246 dialog_vars.defaultno = FALSE;
248 if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
252 sprintf(warning, "The existing partition scheme on this "
253 "disk (%s) is not bootable on this platform. To install "
254 "FreeBSD, it must be repartitioned. This will destroy all "
255 "data on the disk. Are you sure you want to proceed?",
257 subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
261 gpart_destroy(gpart);
262 scheme = choose_part_type(default_scheme());
265 gpart_partition(disk, scheme);
268 if (scheme == NULL || choice == 0) {
269 if (gpart != NULL && scheme != NULL) {
270 /* Erase partitioned disk */
271 choice = dialog_yesno("Confirmation", "This will erase "
272 "the disk. Are you sure you want to proceed?", 0, 0);
276 gpart_destroy(gpart);
279 scheme = choose_part_type(default_scheme());
282 gpart_partition(disk, scheme);
285 if (strcmp(scheme, "MBR") == 0) {
286 struct gmesh submesh;
287 geom_gettree(&submesh);
288 gpart_create(provider_for_name(&submesh, disk),
289 "freebsd", NULL, NULL, &retval,
290 choice /* Non-interactive for "Entire Disk" */);
291 geom_deletetree(&submesh);
293 retval = strdup(disk);
300 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
303 struct gclass *classp;
305 struct gprovider *pp;
306 char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
308 struct gmesh submesh;
309 char swapsizestr[10], rootsizestr[10];
310 intmax_t swapsize, available;
313 if (strcmp(fstype, "zfs") == 0) {
320 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
321 if (strcmp(classp->lg_name, "PART") == 0)
324 LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
325 if (strcmp(gp->lg_name, disk) == 0)
328 pp = provider_for_name(mesh, disk);
330 available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
331 if (interactive && available < MIN_FREE_SPACE) {
332 char availablestr[10], neededstr[10], message[512];
333 humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
335 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
337 sprintf(message, "There is not enough free space on %s to "
338 "install FreeBSD (%s free, %s required). Would you like "
339 "to choose another disk or to open the partition editor?",
340 disk, availablestr, neededstr);
342 dialog_vars.yes_label = "Another Disk";
343 dialog_vars.no_label = "Editor";
344 retval = dialog_yesno("Warning", message, 0, 0);
345 dialog_vars.yes_label = NULL;
346 dialog_vars.no_label = NULL;
348 return (!retval); /* Editor -> return 0 */
351 swapsize = SWAP_SIZE(available);
352 humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
353 HN_NOSPACE | HN_DECIMAL);
354 humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
355 "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
357 geom_gettree(&submesh);
358 pp = provider_for_name(&submesh, disk);
359 gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
360 geom_deletetree(&submesh);
362 geom_gettree(&submesh);
363 pp = provider_for_name(&submesh, disk);
364 gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
365 geom_deletetree(&submesh);