xref: /freebsd/usr.sbin/bsdinstall/partedit/part_wizard.c (revision 5dcd9c10612684d1c823670cbb5b4715028784e7)
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, "MD") != 0)
100 			continue;
101 
102 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
103 			if (LIST_EMPTY(&gp->lg_provider))
104 				continue;
105 
106 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
107 				desc = type = NULL;
108 				LIST_FOREACH(gc, &pp->lg_config, lg_config) {
109 					if (strcmp(gc->lg_name, "type") == 0)
110 						type = gc->lg_val;
111 					if (strcmp(gc->lg_name, "descr") == 0)
112 						desc = gc->lg_val;
113 				}
114 
115 				/* Skip swap-backed md and WORM devices */
116 				if (strcmp(classp->lg_name, "MD") == 0 &&
117 				    type != NULL && strcmp(type, "swap") == 0)
118 					continue;
119 				if (strncmp(pp->lg_name, "cd", 2) == 0)
120 					continue;
121 
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");
130 				else
131 					strcat(diskdesc, " Disk");
132 
133 				if (desc != NULL)
134 					snprintf(diskdesc, sizeof(diskdesc),
135 					    "%s <%s>", diskdesc, desc);
136 
137 				disks[n-1].text = strdup(diskdesc);
138 				disks[n-1].help = NULL;
139 				disks[n-1].state = 0;
140 			}
141 		}
142 	}
143 
144 	if (n > 1) {
145 		err = dlg_menu("Partitioning",
146 		    "Select the disk on which to install FreeBSD.", 0, 0, 0,
147 		    n, disks, &selected, NULL);
148 
149 		chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
150 	} else if (n == 1) {
151 		chosen = strdup(disks[0].name);
152 	} else {
153 		chosen = NULL;
154 	}
155 
156 	for (i = 0; i < n; i++)
157 		free(disks[i].text);
158 
159 	return (chosen);
160 }
161 
162 static struct gprovider *
163 provider_for_name(struct gmesh *mesh, const char *name)
164 {
165 	struct gclass *classp;
166 	struct gprovider *pp = NULL;
167 	struct ggeom *gp;
168 
169 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
170 		if (strcmp(classp->lg_name, "DISK") != 0 &&
171 		    strcmp(classp->lg_name, "PART") != 0 &&
172 		    strcmp(classp->lg_name, "MD") != 0)
173 			continue;
174 
175 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
176 			if (LIST_EMPTY(&gp->lg_provider))
177 				continue;
178 
179 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
180 				if (strcmp(pp->lg_name, name) == 0)
181 					break;
182 
183 			if (pp != NULL) break;
184 		}
185 
186 		if (pp != NULL) break;
187 	}
188 
189 	return (pp);
190 }
191 
192 static char *
193 wizard_partition(struct gmesh *mesh, const char *disk)
194 {
195 	struct gclass *classp;
196 	struct ggeom *gpart = NULL;
197 	struct gconfig *gc;
198 	char message[512];
199 	const char *scheme = NULL;
200 	char *retval = NULL;
201 	int choice;
202 
203 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
204 		if (strcmp(classp->lg_name, "PART") == 0)
205 			break;
206 
207 	if (classp != NULL) {
208 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
209 			if (strcmp(gpart->lg_name, disk) == 0)
210 				break;
211 	}
212 
213 	if (gpart != NULL) {
214 		LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
215 			if (strcmp(gc->lg_name, "scheme") == 0) {
216 				scheme = gc->lg_val;
217 				break;
218 			}
219 		}
220 	}
221 
222 	/* Treat uncommitted scheme deletions as no scheme */
223 	if (scheme != NULL && strcmp(scheme, "(none)") == 0)
224 		scheme = NULL;
225 
226 query:
227 	dialog_vars.yes_label = "Entire Disk";
228 	dialog_vars.no_label = "Partition";
229 	if (gpart != NULL)
230 		dialog_vars.defaultno = TRUE;
231 
232 	snprintf(message, sizeof(message), "Would you like to use this entire "
233 	    "disk (%s) for FreeBSD or partition it to share it with other "
234 	    "operating systems? Using the entire disk will erase any data "
235 	    "currently stored there.", disk);
236 	choice = dialog_yesno("Partition", message, 0, 0);
237 
238 	dialog_vars.yes_label = NULL;
239 	dialog_vars.no_label = NULL;
240 	dialog_vars.defaultno = FALSE;
241 
242 	if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
243 		char warning[512];
244 		int subchoice;
245 
246 		sprintf(warning, "The existing partition scheme on this "
247 		    "disk (%s) is not bootable on this platform. To install "
248 		    "FreeBSD, it must be repartitioned. This will destroy all "
249 		    "data on the disk. Are you sure you want to proceed?",
250 		    scheme);
251 		subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
252 		if (subchoice != 0)
253 			goto query;
254 
255 		gpart_destroy(gpart, 1);
256 		gpart_partition(disk, default_scheme());
257 		scheme = default_scheme();
258 	}
259 
260 	if (scheme == NULL || choice == 0) {
261 		if (gpart != NULL && scheme != NULL) {
262 			/* Erase partitioned disk */
263 			choice = dialog_yesno("Confirmation", "This will erase "
264 			   "the disk. Are you sure you want to proceed?", 0, 0);
265 			if (choice != 0)
266 				goto query;
267 
268 			gpart_destroy(gpart, 1);
269 		}
270 
271 		gpart_partition(disk, default_scheme());
272 		scheme = default_scheme();
273 	}
274 
275 	if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
276 		struct gmesh submesh;
277 		geom_gettree(&submesh);
278 		gpart_create(provider_for_name(&submesh, disk),
279 		    "freebsd", NULL, NULL, &retval,
280 		    choice /* Non-interactive for "Entire Disk" */);
281 		geom_deletetree(&submesh);
282 	} else {
283 		retval = strdup(disk);
284 	}
285 
286 	return (retval);
287 }
288 
289 static int
290 wizard_makeparts(struct gmesh *mesh, const char *disk)
291 {
292 	struct gmesh submesh;
293 	struct gclass *classp;
294 	struct ggeom *gp;
295 	struct gconfig *gc;
296 	const char *scheme;
297 	struct gprovider *pp;
298 	intmax_t swapsize, available;
299 	char swapsizestr[10], rootsizestr[10];
300 	int retval;
301 
302 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
303 		if (strcmp(classp->lg_name, "PART") == 0)
304 			break;
305 
306 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
307 		if (strcmp(gp->lg_name, disk) == 0)
308 			break;
309 
310 	LIST_FOREACH(gc, &gp->lg_config, lg_config)
311 		if (strcmp(gc->lg_name, "scheme") == 0)
312 			scheme = gc->lg_val;
313 
314 	pp = provider_for_name(mesh, disk);
315 
316 	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
317 	if (available < MIN_FREE_SPACE) {
318 		char availablestr[10], neededstr[10], message[512];
319 		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
320 		    HN_DECIMAL);
321 		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
322 		    HN_DECIMAL);
323 		sprintf(message, "There is not enough free space on %s to "
324 		    "install FreeBSD (%s free, %s required). Would you like "
325 		    "to choose another disk or to open the partition editor?",
326 		    disk, availablestr, neededstr);
327 
328 		dialog_vars.yes_label = "Another Disk";
329 		dialog_vars.no_label = "Editor";
330 		retval = dialog_yesno("Warning", message, 0, 0);
331 		dialog_vars.yes_label = NULL;
332 		dialog_vars.no_label = NULL;
333 
334 		return (!retval); /* Editor -> return 0 */
335 	}
336 
337 	swapsize = SWAP_SIZE(available);
338 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
339 	    HN_NOSPACE | HN_DECIMAL);
340 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
341 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
342 
343 	geom_gettree(&submesh);
344 	pp = provider_for_name(&submesh, disk);
345 	gpart_create(pp, "freebsd-ufs", rootsizestr, "/", NULL, 0);
346 	geom_deletetree(&submesh);
347 
348 	geom_gettree(&submesh);
349 	pp = provider_for_name(&submesh, disk);
350 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
351 	geom_deletetree(&submesh);
352 
353 	return (0);
354 }
355 
356