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);
50 char *disk, *schemeroot;
53 error = geom_gettree(&mesh);
56 error = geom_gettree(&mesh);
57 disk = boot_disk(&mesh);
63 schemeroot = wizard_partition(&mesh, disk);
65 if (schemeroot == NULL)
68 geom_deletetree(&mesh);
71 error = geom_gettree(&mesh);
73 error = wizard_makeparts(&mesh, schemeroot, 1);
78 geom_deletetree(&mesh);
84 boot_disk(struct gmesh *mesh)
86 struct gclass *classp;
90 DIALOG_LISTITEM *disks = NULL;
91 const char *type, *desc;
94 int i, err, selected, n = 0;
96 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
97 if (strcmp(classp->lg_name, "DISK") != 0 &&
98 strcmp(classp->lg_name, "RAID") != 0 &&
99 strcmp(classp->lg_name, "MD") != 0)
102 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
103 if (LIST_EMPTY(&gp->lg_provider))
106 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
108 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
109 if (strcmp(gc->lg_name, "type") == 0)
111 if (strcmp(gc->lg_name, "descr") == 0)
115 /* Skip swap-backed md and WORM devices */
116 if (strcmp(classp->lg_name, "MD") == 0 &&
117 type != NULL && strcmp(type, "swap") == 0)
119 if (strncmp(pp->lg_name, "cd", 2) == 0)
122 disks = realloc(disks, (++n)*sizeof(disks[0]));
123 disks[n-1].name = pp->lg_name;
124 humanize_number(diskdesc, 7, pp->lg_mediasize,
125 "B", HN_AUTOSCALE, HN_DECIMAL);
126 if (strncmp(pp->lg_name, "ad", 2) == 0)
127 strcat(diskdesc, " ATA Hard Disk");
128 else if (strncmp(pp->lg_name, "md", 2) == 0)
129 strcat(diskdesc, " Memory Disk");
131 strcat(diskdesc, " Disk");
134 snprintf(diskdesc, sizeof(diskdesc),
135 "%s <%s>", diskdesc, desc);
137 disks[n-1].text = strdup(diskdesc);
138 disks[n-1].help = NULL;
139 disks[n-1].state = 0;
145 err = dlg_menu("Partitioning",
146 "Select the disk on which to install FreeBSD.", 0, 0, 0,
147 n, disks, &selected, NULL);
149 chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
151 chosen = strdup(disks[0].name);
156 for (i = 0; i < n; i++)
162 static struct gprovider *
163 provider_for_name(struct gmesh *mesh, const char *name)
165 struct gclass *classp;
166 struct gprovider *pp = NULL;
169 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
170 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
171 if (LIST_EMPTY(&gp->lg_provider))
174 LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
175 if (strcmp(pp->lg_name, name) == 0)
178 if (pp != NULL) break;
181 if (pp != NULL) break;
188 wizard_partition(struct gmesh *mesh, const char *disk)
190 struct gclass *classp;
191 struct ggeom *gpart = NULL;
194 const char *scheme = NULL;
198 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
199 if (strcmp(classp->lg_name, "PART") == 0)
202 if (classp != NULL) {
203 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
204 if (strcmp(gpart->lg_name, disk) == 0)
209 LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
210 if (strcmp(gc->lg_name, "scheme") == 0) {
217 /* Treat uncommitted scheme deletions as no scheme */
218 if (scheme != NULL && strcmp(scheme, "(none)") == 0)
222 dialog_vars.yes_label = "Entire Disk";
223 dialog_vars.no_label = "Partition";
225 dialog_vars.defaultno = TRUE;
227 snprintf(message, sizeof(message), "Would you like to use this entire "
228 "disk (%s) for FreeBSD or partition it to share it with other "
229 "operating systems? Using the entire disk will erase any data "
230 "currently stored there.", disk);
231 choice = dialog_yesno("Partition", message, 0, 0);
233 dialog_vars.yes_label = NULL;
234 dialog_vars.no_label = NULL;
235 dialog_vars.defaultno = FALSE;
237 if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
241 sprintf(warning, "The existing partition scheme on this "
242 "disk (%s) is not bootable on this platform. To install "
243 "FreeBSD, it must be repartitioned. This will destroy all "
244 "data on the disk. Are you sure you want to proceed?",
246 subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
250 gpart_destroy(gpart);
251 gpart_partition(disk, default_scheme());
252 scheme = default_scheme();
255 if (scheme == NULL || choice == 0) {
256 if (gpart != NULL && scheme != NULL) {
257 /* Erase partitioned disk */
258 choice = dialog_yesno("Confirmation", "This will erase "
259 "the disk. Are you sure you want to proceed?", 0, 0);
263 gpart_destroy(gpart);
266 gpart_partition(disk, default_scheme());
267 scheme = default_scheme();
270 if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
271 struct gmesh submesh;
272 geom_gettree(&submesh);
273 gpart_create(provider_for_name(&submesh, disk),
274 "freebsd", NULL, NULL, &retval,
275 choice /* Non-interactive for "Entire Disk" */);
276 geom_deletetree(&submesh);
278 retval = strdup(disk);
285 wizard_makeparts(struct gmesh *mesh, const char *disk, int interactive)
287 struct gmesh submesh;
288 struct gclass *classp;
290 struct gprovider *pp;
291 intmax_t swapsize, available;
292 char swapsizestr[10], rootsizestr[10];
295 LIST_FOREACH(classp, &mesh->lg_class, lg_class)
296 if (strcmp(classp->lg_name, "PART") == 0)
299 LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
300 if (strcmp(gp->lg_name, disk) == 0)
303 pp = provider_for_name(mesh, disk);
305 available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
306 if (interactive && available < MIN_FREE_SPACE) {
307 char availablestr[10], neededstr[10], message[512];
308 humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
310 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
312 sprintf(message, "There is not enough free space on %s to "
313 "install FreeBSD (%s free, %s required). Would you like "
314 "to choose another disk or to open the partition editor?",
315 disk, availablestr, neededstr);
317 dialog_vars.yes_label = "Another Disk";
318 dialog_vars.no_label = "Editor";
319 retval = dialog_yesno("Warning", message, 0, 0);
320 dialog_vars.yes_label = NULL;
321 dialog_vars.no_label = NULL;
323 return (!retval); /* Editor -> return 0 */
326 swapsize = SWAP_SIZE(available);
327 humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
328 HN_NOSPACE | HN_DECIMAL);
329 humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
330 "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
332 geom_gettree(&submesh);
333 pp = provider_for_name(&submesh, disk);
334 gpart_create(pp, "freebsd-ufs", rootsizestr, "/", NULL, 0);
335 geom_deletetree(&submesh);
337 geom_gettree(&submesh);
338 pp = provider_for_name(&submesh, disk);
339 gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
340 geom_deletetree(&submesh);