]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.sbin/bsdinstall/partedit/part_wizard.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / usr.sbin / bsdinstall / partedit / part_wizard.c
1 /*-
2  * Copyright (c) 2011 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <errno.h>
31 #include <libutil.h>
32 #include <inttypes.h>
33
34 #include <libgeom.h>
35 #include <dialog.h>
36 #include <dlg_keys.h>
37
38 #include "partedit.h"
39
40 #define MIN_FREE_SPACE          (1024*1024*1024) /* 1 GB */
41 #define SWAP_SIZE(available)    MIN(available/20, 4*1024*1024*1024LL)
42
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);
46
47 int
48 part_wizard(void) {
49         int error;
50         struct gmesh mesh;
51         char *disk, *schemeroot;
52
53 startwizard:
54         error = geom_gettree(&mesh);
55
56         dlg_put_backtitle();
57         error = geom_gettree(&mesh);
58         disk = boot_disk(&mesh);
59         if (disk == NULL)
60                 return (1);
61
62         dlg_clear();
63         dlg_put_backtitle();
64         schemeroot = wizard_partition(&mesh, disk);
65         free(disk);
66         if (schemeroot == NULL)
67                 return (1);
68
69         geom_deletetree(&mesh);
70         dlg_clear();
71         dlg_put_backtitle();
72         error = geom_gettree(&mesh);
73
74         error = wizard_makeparts(&mesh, schemeroot);
75         if (error)
76                 goto startwizard;
77         free(schemeroot);
78         
79         geom_deletetree(&mesh);
80
81         return (0);
82 }
83
84 static char *
85 boot_disk(struct gmesh *mesh)
86 {
87         struct gclass *classp;
88         struct gconfig *gc;
89         struct ggeom *gp;
90         struct gprovider *pp;
91         DIALOG_LISTITEM *disks = NULL;
92         const char *type, *desc;
93         char diskdesc[512];
94         char *chosen;
95         int i, err, selected, n = 0;
96
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)
101                         continue;
102
103                 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
104                         if (LIST_EMPTY(&gp->lg_provider))
105                                 continue;
106
107                         LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
108                                 desc = type = NULL;
109                                 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
110                                         if (strcmp(gc->lg_name, "type") == 0) 
111                                                 type = gc->lg_val;
112                                         if (strcmp(gc->lg_name, "descr") == 0) 
113                                                 desc = gc->lg_val;
114                                 }
115
116                                 /* Skip swap-backed md and WORM devices */
117                                 if (strcmp(classp->lg_name, "MD") == 0 &&
118                                     type != NULL && strcmp(type, "swap") == 0)
119                                         continue;
120                                 if (strncmp(pp->lg_name, "cd", 2) == 0)
121                                         continue;
122
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");
131                                 else
132                                         strcat(diskdesc, " Disk");
133
134                                 if (desc != NULL)
135                                         snprintf(diskdesc, sizeof(diskdesc),
136                                             "%s <%s>", diskdesc, desc);
137
138                                 disks[n-1].text = strdup(diskdesc);
139                                 disks[n-1].help = NULL;
140                                 disks[n-1].state = 0;
141                         }
142                 }
143         }
144
145         if (n > 1) {
146                 err = dlg_menu("Partitioning",
147                     "Select the disk on which to install FreeBSD.", 0, 0, 0,
148                     n, disks, &selected, NULL);
149
150                 chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
151         } else if (n == 1) {
152                 chosen = strdup(disks[0].name);
153         } else {
154                 chosen = NULL;
155         }
156
157         for (i = 0; i < n; i++)
158                 free(disks[i].text);
159
160         return (chosen);
161 }
162
163 static struct gprovider *
164 provider_for_name(struct gmesh *mesh, const char *name)
165 {
166         struct gclass *classp;
167         struct gprovider *pp = NULL;
168         struct ggeom *gp;
169
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)
175                         continue;
176
177                 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
178                         if (LIST_EMPTY(&gp->lg_provider))
179                                 continue;
180
181                         LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
182                                 if (strcmp(pp->lg_name, name) == 0)
183                                         break;
184
185                         if (pp != NULL) break;
186                 }
187
188                 if (pp != NULL) break;
189         }
190
191         return (pp);
192 }
193
194 static char *
195 wizard_partition(struct gmesh *mesh, const char *disk)
196 {
197         struct gclass *classp;
198         struct ggeom *gpart = NULL;
199         struct gconfig *gc;
200         char message[512];
201         const char *scheme = NULL;
202         char *retval = NULL;
203         int choice;
204
205         LIST_FOREACH(classp, &mesh->lg_class, lg_class)
206                 if (strcmp(classp->lg_name, "PART") == 0)
207                         break;
208
209         if (classp != NULL) {
210                 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom) 
211                         if (strcmp(gpart->lg_name, disk) == 0)
212                                 break;
213         }
214
215         if (gpart != NULL) {
216                 LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
217                         if (strcmp(gc->lg_name, "scheme") == 0) {
218                                 scheme = gc->lg_val;
219                                 break;
220                         }
221                 }
222         }
223
224         /* Treat uncommitted scheme deletions as no scheme */
225         if (scheme != NULL && strcmp(scheme, "(none)") == 0)
226                 scheme = NULL;
227
228 query:
229         dialog_vars.yes_label = "Entire Disk";
230         dialog_vars.no_label = "Partition";
231         if (gpart != NULL)
232                 dialog_vars.defaultno = TRUE;
233
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);
239
240         dialog_vars.yes_label = NULL;
241         dialog_vars.no_label = NULL;
242         dialog_vars.defaultno = FALSE;
243
244         if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
245                 char warning[512];
246                 int subchoice;
247
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?",
252                     scheme);
253                 subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
254                 if (subchoice != 0)
255                         goto query;
256
257                 gpart_destroy(gpart);
258                 gpart_partition(disk, default_scheme());
259                 scheme = default_scheme();
260         }
261
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);
267                         if (choice != 0)
268                                 goto query;
269
270                         gpart_destroy(gpart);
271                 }
272
273                 gpart_partition(disk, default_scheme());
274                 scheme = default_scheme();
275         }
276
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);
284         } else {
285                 retval = strdup(disk);
286         }
287
288         return (retval);
289 }
290
291 static int
292 wizard_makeparts(struct gmesh *mesh, const char *disk)
293 {
294         struct gmesh submesh;
295         struct gclass *classp;
296         struct ggeom *gp;
297         struct gconfig *gc;
298         const char *scheme;
299         struct gprovider *pp;
300         intmax_t swapsize, available;
301         char swapsizestr[10], rootsizestr[10];
302         int retval;
303
304         LIST_FOREACH(classp, &mesh->lg_class, lg_class)
305                 if (strcmp(classp->lg_name, "PART") == 0)
306                         break;
307
308         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) 
309                 if (strcmp(gp->lg_name, disk) == 0)
310                         break;
311
312         LIST_FOREACH(gc, &gp->lg_config, lg_config) 
313                 if (strcmp(gc->lg_name, "scheme") == 0) 
314                         scheme = gc->lg_val;
315
316         pp = provider_for_name(mesh, disk);
317
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,
322                     HN_DECIMAL);
323                 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
324                     HN_DECIMAL);
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);
329
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;
335
336                 return (!retval); /* Editor -> return 0 */
337         }
338
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);
344
345         geom_gettree(&submesh);
346         pp = provider_for_name(&submesh, disk);
347         gpart_create(pp, "freebsd-ufs", rootsizestr, "/", NULL, 0);
348         geom_deletetree(&submesh);
349
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);
354
355         return (0);
356 }
357