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>
40 #define MIN_FREE_SPACE (1024*1024*1024) /* 1 GB */
41 #define SWAP_SIZE(available) MIN(available/20, 4*1024*1024*1024LL)
43 static char *boot_disk(struct gmesh *mesh);
44 static char *wizard_partition(struct gmesh *mesh, const char *disk);
45 static int wizard_makeparts(struct gmesh *mesh, const char *disk);
51 char *disk, *schemeroot;
54 error = geom_gettree(&mesh);
57 error = geom_gettree(&mesh);
58 disk = boot_disk(&mesh);
64 schemeroot = wizard_partition(&mesh, disk);
66 if (schemeroot == NULL)
69 geom_deletetree(&mesh);
72 error = geom_gettree(&mesh);
74 error = wizard_makeparts(&mesh, schemeroot);
79 geom_deletetree(&mesh);
85 boot_disk(struct gmesh *mesh)
87 struct gclass *classp;
91 DIALOG_LISTITEM *disks = NULL;
92 const char *type, *desc;
95 int i, err, selected, n = 0;
97 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
98 if (strcmp(classp->lg_name, "DISK") != 0 &&
99 strcmp(classp->lg_name, "RAID") != 0 &&
100 strcmp(classp->lg_name, "MD") != 0)
103 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
104 if (LIST_EMPTY(&gp->lg_provider))
107 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
109 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
110 if (strcmp(gc->lg_name, "type") == 0)
112 if (strcmp(gc->lg_name, "descr") == 0)
116 /* Skip swap-backed md and WORM devices */
117 if (strcmp(classp->lg_name, "MD") == 0 &&
118 type != NULL && strcmp(type, "swap") == 0)
120 if (strncmp(pp->lg_name, "cd", 2) == 0)
123 disks = realloc(disks, (++n)*sizeof(disks[0]));
124 disks[n-1].name = pp->lg_name;
125 humanize_number(diskdesc, 7, pp->lg_mediasize,
126 "B", HN_AUTOSCALE, HN_DECIMAL);
127 if (strncmp(pp->lg_name, "ad", 2) == 0)
128 strcat(diskdesc, " ATA Hard Disk");
129 else if (strncmp(pp->lg_name, "md", 2) == 0)
130 strcat(diskdesc, " Memory Disk");
132 strcat(diskdesc, " Disk");
135 snprintf(diskdesc, sizeof(diskdesc),
136 "%s <%s>", diskdesc, desc);
138 disks[n-1].text = strdup(diskdesc);
139 disks[n-1].help = NULL;
140 disks[n-1].state = 0;
146 err = dlg_menu("Partitioning",
147 "Select the disk on which to install FreeBSD.", 0, 0, 0,
148 n, disks, &selected, NULL);
150 chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
152 chosen = strdup(disks[0].name);
157 for (i = 0; i < n; i++)
163 static struct gprovider *
164 provider_for_name(struct gmesh *mesh, const char *name)
166 struct gclass *classp;
167 struct gprovider *pp = NULL;
170 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
171 if (strcmp(classp->lg_name, "DISK") != 0 &&
172 strcmp(classp->lg_name, "PART") != 0 &&
173 strcmp(classp->lg_name, "RAID") != 0 &&
174 strcmp(classp->lg_name, "MD") != 0)
177 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
178 if (LIST_EMPTY(&gp->lg_provider))
181 LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
182 if (strcmp(pp->lg_name, name) == 0)
185 if (pp != NULL) break;
188 if (pp != NULL) break;
195 wizard_partition(struct gmesh *mesh, const char *disk)
197 struct gclass *classp;
198 struct ggeom *gpart = NULL;
201 const char *scheme = NULL;
205 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
206 if (strcmp(classp->lg_name, "PART") == 0)
209 if (classp != NULL) {
210 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
211 if (strcmp(gpart->lg_name, disk) == 0)
216 LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
217 if (strcmp(gc->lg_name, "scheme") == 0) {
224 /* Treat uncommitted scheme deletions as no scheme */
225 if (scheme != NULL && strcmp(scheme, "(none)") == 0)
229 dialog_vars.yes_label = "Entire Disk";
230 dialog_vars.no_label = "Partition";
232 dialog_vars.defaultno = TRUE;
234 snprintf(message, sizeof(message), "Would you like to use this entire "
235 "disk (%s) for FreeBSD or partition it to share it with other "
236 "operating systems? Using the entire disk will erase any data "
237 "currently stored there.", disk);
238 choice = dialog_yesno("Partition", message, 0, 0);
240 dialog_vars.yes_label = NULL;
241 dialog_vars.no_label = NULL;
242 dialog_vars.defaultno = FALSE;
244 if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
248 sprintf(warning, "The existing partition scheme on this "
249 "disk (%s) is not bootable on this platform. To install "
250 "FreeBSD, it must be repartitioned. This will destroy all "
251 "data on the disk. Are you sure you want to proceed?",
253 subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
257 gpart_destroy(gpart);
258 gpart_partition(disk, default_scheme());
259 scheme = default_scheme();
262 if (scheme == NULL || choice == 0) {
263 if (gpart != NULL && scheme != NULL) {
264 /* Erase partitioned disk */
265 choice = dialog_yesno("Confirmation", "This will erase "
266 "the disk. Are you sure you want to proceed?", 0, 0);
270 gpart_destroy(gpart);
273 gpart_partition(disk, default_scheme());
274 scheme = default_scheme();
277 if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
278 struct gmesh submesh;
279 geom_gettree(&submesh);
280 gpart_create(provider_for_name(&submesh, disk),
281 "freebsd", NULL, NULL, &retval,
282 choice /* Non-interactive for "Entire Disk" */);
283 geom_deletetree(&submesh);
285 retval = strdup(disk);
292 wizard_makeparts(struct gmesh *mesh, const char *disk)
294 struct gmesh submesh;
295 struct gclass *classp;
299 struct gprovider *pp;
300 intmax_t swapsize, available;
301 char swapsizestr[10], rootsizestr[10];
304 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
305 if (strcmp(classp->lg_name, "PART") == 0)
308 LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
309 if (strcmp(gp->lg_name, disk) == 0)
312 LIST_FOREACH(gc, &gp->lg_config, lg_config)
313 if (strcmp(gc->lg_name, "scheme") == 0)
316 pp = provider_for_name(mesh, disk);
318 available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
319 if (available < MIN_FREE_SPACE) {
320 char availablestr[10], neededstr[10], message[512];
321 humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
323 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
325 sprintf(message, "There is not enough free space on %s to "
326 "install FreeBSD (%s free, %s required). Would you like "
327 "to choose another disk or to open the partition editor?",
328 disk, availablestr, neededstr);
330 dialog_vars.yes_label = "Another Disk";
331 dialog_vars.no_label = "Editor";
332 retval = dialog_yesno("Warning", message, 0, 0);
333 dialog_vars.yes_label = NULL;
334 dialog_vars.no_label = NULL;
336 return (!retval); /* Editor -> return 0 */
339 swapsize = SWAP_SIZE(available);
340 humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
341 HN_NOSPACE | HN_DECIMAL);
342 humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
343 "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
345 geom_gettree(&submesh);
346 pp = provider_for_name(&submesh, disk);
347 gpart_create(pp, "freebsd-ufs", rootsizestr, "/", NULL, 0);
348 geom_deletetree(&submesh);
350 geom_gettree(&submesh);
351 pp = provider_for_name(&submesh, disk);
352 gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
353 geom_deletetree(&submesh);