2 * Copyright (c) 2011 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
34 #include <sys/sysctl.h>
43 #define MIN_FREE_SPACE (1024*1024*1024) /* 1 GB */
44 #define SWAP_SIZE(available) MIN(available/20, 4*1024*1024*1024LL)
46 static char *boot_disk(struct gmesh *mesh);
47 static char *wizard_partition(struct gmesh *mesh, const char *disk);
50 part_wizard(const char *fsreq) {
53 char *disk, *schemeroot;
62 error = geom_gettree(&mesh);
65 error = geom_gettree(&mesh);
66 disk = boot_disk(&mesh);
72 schemeroot = wizard_partition(&mesh, disk);
74 if (schemeroot == NULL)
77 geom_deletetree(&mesh);
80 error = geom_gettree(&mesh);
82 error = wizard_makeparts(&mesh, schemeroot, fstype, 1);
87 geom_deletetree(&mesh);
93 boot_disk(struct gmesh *mesh)
95 struct gclass *classp;
99 DIALOG_LISTITEM *disks = NULL;
100 const char *type, *desc;
103 int i, err, selected, n = 0;
105 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
106 if (strcmp(classp->lg_name, "DISK") != 0 &&
107 strcmp(classp->lg_name, "RAID") != 0 &&
108 strcmp(classp->lg_name, "MD") != 0)
111 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
112 if (LIST_EMPTY(&gp->lg_provider))
115 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
117 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
118 if (strcmp(gc->lg_name, "type") == 0)
120 if (strcmp(gc->lg_name, "descr") == 0)
124 /* Skip swap-backed md and WORM devices */
125 if (strcmp(classp->lg_name, "MD") == 0 &&
126 type != NULL && strcmp(type, "swap") == 0)
128 if (strncmp(pp->lg_name, "cd", 2) == 0)
131 disks = realloc(disks, (++n)*sizeof(disks[0]));
132 disks[n-1].name = pp->lg_name;
133 humanize_number(diskdesc, 7, pp->lg_mediasize,
134 "B", HN_AUTOSCALE, HN_DECIMAL);
135 if (strncmp(pp->lg_name, "ad", 2) == 0)
136 strcat(diskdesc, " ATA Hard Disk");
137 else if (strncmp(pp->lg_name, "md", 2) == 0)
138 strcat(diskdesc, " Memory Disk");
140 strcat(diskdesc, " Disk");
143 snprintf(diskdesc, sizeof(diskdesc),
144 "%s <%s>", diskdesc, desc);
146 disks[n-1].text = strdup(diskdesc);
147 disks[n-1].help = NULL;
148 disks[n-1].state = 0;
154 err = dlg_menu("Partitioning",
155 "Select the disk on which to install FreeBSD.", 0, 0, 0,
156 n, disks, &selected, NULL);
158 chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
160 chosen = strdup(disks[0].name);
165 for (i = 0; i < n; i++)
171 static struct gprovider *
172 provider_for_name(struct gmesh *mesh, const char *name)
174 struct gclass *classp;
175 struct gprovider *pp = NULL;
178 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
179 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
180 if (LIST_EMPTY(&gp->lg_provider))
183 LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
184 if (strcmp(pp->lg_name, name) == 0)
187 if (pp != NULL) break;
190 if (pp != NULL) break;
197 wizard_partition(struct gmesh *mesh, const char *disk)
199 struct gclass *classp;
200 struct ggeom *gpart = NULL;
203 const char *scheme = NULL;
207 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
208 if (strcmp(classp->lg_name, "PART") == 0)
211 if (classp != NULL) {
212 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
213 if (strcmp(gpart->lg_name, disk) == 0)
218 LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
219 if (strcmp(gc->lg_name, "scheme") == 0) {
226 /* Treat uncommitted scheme deletions as no scheme */
227 if (scheme != NULL && strcmp(scheme, "(none)") == 0)
231 dialog_vars.yes_label = "Entire Disk";
232 dialog_vars.no_label = "Partition";
234 dialog_vars.defaultno = TRUE;
236 snprintf(message, sizeof(message), "Would you like to use this entire "
237 "disk (%s) for FreeBSD or partition it to share it with other "
238 "operating systems? Using the entire disk will erase any data "
239 "currently stored there.", disk);
240 choice = dialog_yesno("Partition", message, 0, 0);
242 dialog_vars.yes_label = NULL;
243 dialog_vars.no_label = NULL;
244 dialog_vars.defaultno = FALSE;
246 if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
250 sprintf(warning, "The existing partition scheme on this "
251 "disk (%s) is not bootable on this platform. To install "
252 "FreeBSD, it must be repartitioned. This will destroy all "
253 "data on the disk. Are you sure you want to proceed?",
255 subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
259 gpart_destroy(gpart);
260 gpart_partition(disk, default_scheme());
261 scheme = default_scheme();
264 if (scheme == NULL || choice == 0) {
265 if (gpart != NULL && scheme != NULL) {
266 /* Erase partitioned disk */
267 choice = dialog_yesno("Confirmation", "This will erase "
268 "the disk. Are you sure you want to proceed?", 0, 0);
272 gpart_destroy(gpart);
275 gpart_partition(disk, default_scheme());
276 scheme = default_scheme();
279 if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
280 struct gmesh submesh;
281 geom_gettree(&submesh);
282 gpart_create(provider_for_name(&submesh, disk),
283 "freebsd", NULL, NULL, &retval,
284 choice /* Non-interactive for "Entire Disk" */);
285 geom_deletetree(&submesh);
287 retval = strdup(disk);
294 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype, int interactive)
296 struct gmesh submesh;
297 struct gclass *classp;
299 struct gprovider *pp;
300 intmax_t swapsize, available;
301 char swapsizestr[10], rootsizestr[10], *fsname;
302 char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
305 if (strcmp(fstype, "zfs") == 0) {
312 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
313 if (strcmp(classp->lg_name, "PART") == 0)
316 LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
317 if (strcmp(gp->lg_name, disk) == 0)
320 pp = provider_for_name(mesh, disk);
322 available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
323 if (interactive && available < MIN_FREE_SPACE) {
324 char availablestr[10], neededstr[10], message[512];
325 humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
327 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
329 sprintf(message, "There is not enough free space on %s to "
330 "install FreeBSD (%s free, %s required). Would you like "
331 "to choose another disk or to open the partition editor?",
332 disk, availablestr, neededstr);
334 dialog_vars.yes_label = "Another Disk";
335 dialog_vars.no_label = "Editor";
336 retval = dialog_yesno("Warning", message, 0, 0);
337 dialog_vars.yes_label = NULL;
338 dialog_vars.no_label = NULL;
340 return (!retval); /* Editor -> return 0 */
343 swapsize = SWAP_SIZE(available);
344 humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
345 HN_NOSPACE | HN_DECIMAL);
346 humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
347 "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
349 geom_gettree(&submesh);
350 pp = provider_for_name(&submesh, disk);
351 gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
352 geom_deletetree(&submesh);
354 geom_gettree(&submesh);
355 pp = provider_for_name(&submesh, disk);
356 gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
357 geom_deletetree(&submesh);