xref: /freebsd/usr.sbin/bsdinstall/partedit/part_wizard.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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 <sys/sysctl.h>
35 #include <string.h>
36 
37 #include <libgeom.h>
38 #include <dialog.h>
39 #include <dlg_keys.h>
40 
41 #include "partedit.h"
42 
43 #define MIN_FREE_SPACE		(1024*1024*1024) /* 1 GB */
44 #define SWAP_SIZE(available)	MIN(available/20, 4*1024*1024*1024LL)
45 
46 static char *boot_disk(struct gmesh *mesh);
47 static char *wizard_partition(struct gmesh *mesh, const char *disk);
48 
49 int
50 part_wizard(const char *fsreq) {
51 	int error;
52 	struct gmesh mesh;
53 	char *disk, *schemeroot;
54 	const char *fstype;
55 
56 	if (fsreq != NULL)
57 		fstype = fsreq;
58 	else
59 		fstype = "ufs";
60 
61 startwizard:
62 	error = geom_gettree(&mesh);
63 
64 	dlg_put_backtitle();
65 	error = geom_gettree(&mesh);
66 	disk = boot_disk(&mesh);
67 	if (disk == NULL)
68 		return (1);
69 
70 	dlg_clear();
71 	dlg_put_backtitle();
72 	schemeroot = wizard_partition(&mesh, disk);
73 	free(disk);
74 	if (schemeroot == NULL)
75 		return (1);
76 
77 	geom_deletetree(&mesh);
78 	dlg_clear();
79 	dlg_put_backtitle();
80 	error = geom_gettree(&mesh);
81 
82 	error = wizard_makeparts(&mesh, schemeroot, fstype, 1);
83 	if (error)
84 		goto startwizard;
85 	free(schemeroot);
86 
87 	geom_deletetree(&mesh);
88 
89 	return (0);
90 }
91 
92 static char *
93 boot_disk(struct gmesh *mesh)
94 {
95 	struct gclass *classp;
96 	struct gconfig *gc;
97 	struct ggeom *gp;
98 	struct gprovider *pp;
99 	DIALOG_LISTITEM *disks = NULL;
100 	const char *type, *desc;
101 	char diskdesc[512];
102 	char *chosen;
103 	int i, err, selected, n = 0;
104 
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)
109 			continue;
110 
111 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
112 			if (LIST_EMPTY(&gp->lg_provider))
113 				continue;
114 
115 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
116 				desc = type = NULL;
117 				LIST_FOREACH(gc, &pp->lg_config, lg_config) {
118 					if (strcmp(gc->lg_name, "type") == 0)
119 						type = gc->lg_val;
120 					if (strcmp(gc->lg_name, "descr") == 0)
121 						desc = gc->lg_val;
122 				}
123 
124 				/* Skip swap-backed md and WORM devices */
125 				if (strcmp(classp->lg_name, "MD") == 0 &&
126 				    type != NULL && strcmp(type, "swap") == 0)
127 					continue;
128 				if (strncmp(pp->lg_name, "cd", 2) == 0)
129 					continue;
130 
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");
139 				else
140 					strcat(diskdesc, " Disk");
141 
142 				if (desc != NULL)
143 					snprintf(diskdesc, sizeof(diskdesc),
144 					    "%s <%s>", diskdesc, desc);
145 
146 				disks[n-1].text = strdup(diskdesc);
147 				disks[n-1].help = NULL;
148 				disks[n-1].state = 0;
149 			}
150 		}
151 	}
152 
153 	if (n > 1) {
154 		err = dlg_menu("Partitioning",
155 		    "Select the disk on which to install FreeBSD.", 0, 0, 0,
156 		    n, disks, &selected, NULL);
157 
158 		chosen = (err == 0) ? strdup(disks[selected].name) : NULL;
159 	} else if (n == 1) {
160 		chosen = strdup(disks[0].name);
161 	} else {
162 		chosen = NULL;
163 	}
164 
165 	for (i = 0; i < n; i++)
166 		free(disks[i].text);
167 
168 	return (chosen);
169 }
170 
171 static struct gprovider *
172 provider_for_name(struct gmesh *mesh, const char *name)
173 {
174 	struct gclass *classp;
175 	struct gprovider *pp = NULL;
176 	struct ggeom *gp;
177 
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))
181 				continue;
182 
183 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
184 				if (strcmp(pp->lg_name, name) == 0)
185 					break;
186 
187 			if (pp != NULL) break;
188 		}
189 
190 		if (pp != NULL) break;
191 	}
192 
193 	return (pp);
194 }
195 
196 static char *
197 wizard_partition(struct gmesh *mesh, const char *disk)
198 {
199 	struct gclass *classp;
200 	struct ggeom *gpart = NULL;
201 	struct gconfig *gc;
202 	char message[512];
203 	const char *scheme = NULL;
204 	char *retval = NULL;
205 	int choice;
206 
207 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
208 		if (strcmp(classp->lg_name, "PART") == 0)
209 			break;
210 
211 	if (classp != NULL) {
212 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
213 			if (strcmp(gpart->lg_name, disk) == 0)
214 				break;
215 	}
216 
217 	if (gpart != NULL) {
218 		LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
219 			if (strcmp(gc->lg_name, "scheme") == 0) {
220 				scheme = gc->lg_val;
221 				break;
222 			}
223 		}
224 	}
225 
226 	/* Treat uncommitted scheme deletions as no scheme */
227 	if (scheme != NULL && strcmp(scheme, "(none)") == 0)
228 		scheme = NULL;
229 
230 query:
231 	dialog_vars.yes_label = "Entire Disk";
232 	dialog_vars.no_label = "Partition";
233 	if (gpart != NULL)
234 		dialog_vars.defaultno = TRUE;
235 
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);
241 
242 	dialog_vars.yes_label = NULL;
243 	dialog_vars.no_label = NULL;
244 	dialog_vars.defaultno = FALSE;
245 
246 	if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) {
247 		char warning[512];
248 		int subchoice;
249 
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?",
254 		    scheme);
255 		subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0);
256 		if (subchoice != 0)
257 			goto query;
258 
259 		gpart_destroy(gpart);
260 		scheme = choose_part_type(default_scheme());
261 		if (scheme == NULL)
262 			return NULL;
263 		gpart_partition(disk, scheme);
264 	}
265 
266 	if (scheme == NULL || choice == 0) {
267 		if (gpart != NULL && scheme != NULL) {
268 			/* Erase partitioned disk */
269 			choice = dialog_yesno("Confirmation", "This will erase "
270 			   "the disk. Are you sure you want to proceed?", 0, 0);
271 			if (choice != 0)
272 				goto query;
273 
274 			gpart_destroy(gpart);
275 		}
276 
277 		scheme = choose_part_type(default_scheme());
278 		if (scheme == NULL)
279 			return NULL;
280 		gpart_partition(disk, scheme);
281 	}
282 
283 	if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
284 		struct gmesh submesh;
285 		geom_gettree(&submesh);
286 		gpart_create(provider_for_name(&submesh, disk),
287 		    "freebsd", NULL, NULL, &retval,
288 		    choice /* Non-interactive for "Entire Disk" */);
289 		geom_deletetree(&submesh);
290 	} else {
291 		retval = strdup(disk);
292 	}
293 
294 	return (retval);
295 }
296 
297 int
298 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype, int interactive)
299 {
300 	struct gmesh submesh;
301 	struct gclass *classp;
302 	struct ggeom *gp;
303 	struct gprovider *pp;
304 	intmax_t swapsize, available;
305 	char swapsizestr[10], rootsizestr[10], *fsname;
306 	char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
307 	int retval;
308 
309 	if (strcmp(fstype, "zfs") == 0) {
310 		fsname = fsnames[1];
311 	} else {
312 		/* default to UFS */
313 		fsname = fsnames[0];
314 	}
315 
316 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
317 		if (strcmp(classp->lg_name, "PART") == 0)
318 			break;
319 
320 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
321 		if (strcmp(gp->lg_name, disk) == 0)
322 			break;
323 
324 	pp = provider_for_name(mesh, disk);
325 
326 	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
327 	if (interactive && available < MIN_FREE_SPACE) {
328 		char availablestr[10], neededstr[10], message[512];
329 		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
330 		    HN_DECIMAL);
331 		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
332 		    HN_DECIMAL);
333 		sprintf(message, "There is not enough free space on %s to "
334 		    "install FreeBSD (%s free, %s required). Would you like "
335 		    "to choose another disk or to open the partition editor?",
336 		    disk, availablestr, neededstr);
337 
338 		dialog_vars.yes_label = "Another Disk";
339 		dialog_vars.no_label = "Editor";
340 		retval = dialog_yesno("Warning", message, 0, 0);
341 		dialog_vars.yes_label = NULL;
342 		dialog_vars.no_label = NULL;
343 
344 		return (!retval); /* Editor -> return 0 */
345 	}
346 
347 	swapsize = SWAP_SIZE(available);
348 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
349 	    HN_NOSPACE | HN_DECIMAL);
350 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
351 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
352 
353 	geom_gettree(&submesh);
354 	pp = provider_for_name(&submesh, disk);
355 	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
356 	geom_deletetree(&submesh);
357 
358 	geom_gettree(&submesh);
359 	pp = provider_for_name(&submesh, disk);
360 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
361 	geom_deletetree(&submesh);
362 
363 	return (0);
364 }
365 
366