xref: /freebsd/usr.sbin/bsdinstall/partedit/part_wizard.c (revision 56cc89585d392d8c8f120b2e4b8faa7df236b4ea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <libutil.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include <libgeom.h>
41 #include <bsddialog.h>
42 
43 #include "partedit.h"
44 
45 #define MIN_FREE_SPACE		(1024*1024*1024) /* 1 GB */
46 #define SWAP_SIZE(available)	MIN(available/20, 4*1024*1024*1024LL)
47 
48 static char *wizard_partition(struct gmesh *mesh, const char *disk);
49 
50 int
51 part_wizard(const char *fsreq)
52 {
53 	char *disk, *schemeroot;
54 	const char *fstype;
55 	struct gmesh mesh;
56 	int error;
57 	struct bsddialog_conf conf;
58 
59 	bsddialog_initconf(&conf);
60 
61 	if (fsreq != NULL)
62 		fstype = fsreq;
63 	else
64 		fstype = "ufs";
65 
66 startwizard:
67 	error = geom_gettree(&mesh);
68 
69 	bsddialog_backtitle(&conf, "FreeBSD Installer");
70 	error = geom_gettree(&mesh);
71 	disk = boot_disk_select(&mesh);
72 	if (disk == NULL)
73 		return (1);
74 
75 	bsddialog_clearterminal();
76 	bsddialog_backtitle(&conf, "FreeBSD Installer");
77 	schemeroot = wizard_partition(&mesh, disk);
78 	free(disk);
79 	if (schemeroot == NULL)
80 		return (1);
81 
82 	geom_deletetree(&mesh);
83 	bsddialog_clearterminal();
84 	bsddialog_backtitle(&conf, "FreeBSD Installer");
85 	error = geom_gettree(&mesh);
86 
87 	error = wizard_makeparts(&mesh, schemeroot, fstype, 1);
88 	if (error)
89 		goto startwizard;
90 	free(schemeroot);
91 
92 	geom_deletetree(&mesh);
93 
94 	return (0);
95 }
96 
97 char *
98 boot_disk_select(struct gmesh *mesh)
99 {
100 	struct gclass *classp;
101 	struct gconfig *gc;
102 	struct ggeom *gp;
103 	struct gprovider *pp;
104 	struct bsddialog_menuitem *disks = NULL;
105 	const char *type, *desc;
106 	char diskdesc[512];
107 	char *chosen;
108 	int i, button, selected, n = 0;
109 	struct bsddialog_conf conf;
110 
111 	bsddialog_initconf(&conf);
112 
113 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
114 		if (strcmp(classp->lg_name, "DISK") != 0 &&
115 		    strcmp(classp->lg_name, "RAID") != 0 &&
116 		    strcmp(classp->lg_name, "MD") != 0)
117 			continue;
118 
119 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
120 			if (LIST_EMPTY(&gp->lg_provider))
121 				continue;
122 
123 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
124 				desc = type = NULL;
125 				LIST_FOREACH(gc, &pp->lg_config, lg_config) {
126 					if (strcmp(gc->lg_name, "type") == 0)
127 						type = gc->lg_val;
128 					if (strcmp(gc->lg_name, "descr") == 0)
129 						desc = gc->lg_val;
130 				}
131 
132 				/* Skip swap-backed md and WORM devices */
133 				if (strcmp(classp->lg_name, "MD") == 0 &&
134 				    type != NULL && strcmp(type, "swap") == 0)
135 					continue;
136 				if (strncmp(pp->lg_name, "cd", 2) == 0)
137 					continue;
138 
139 				disks = realloc(disks, (++n)*sizeof(disks[0]));
140 				disks[n-1].name = pp->lg_name;
141 				humanize_number(diskdesc, 7, pp->lg_mediasize,
142 				    "B", HN_AUTOSCALE, HN_DECIMAL);
143 				if (strncmp(pp->lg_name, "ad", 2) == 0)
144 					strcat(diskdesc, " ATA Hard Disk");
145 				else if (strncmp(pp->lg_name, "md", 2) == 0)
146 					strcat(diskdesc, " Memory Disk");
147 				else
148 					strcat(diskdesc, " Disk");
149 
150 				if (desc != NULL)
151 					snprintf(diskdesc, sizeof(diskdesc),
152 					    "%s <%s>", diskdesc, desc);
153 
154 				disks[n-1].prefix = "";
155 				disks[n-1].on = false;
156 				disks[n-1].depth = 0;
157 				disks[n-1].desc = strdup(diskdesc);
158 				disks[n-1].bottomdesc = "";
159 			}
160 		}
161 	}
162 
163 	if (n > 1) {
164 		conf.title = "Partitioning";
165 		button = bsddialog_menu(&conf,
166 		    "Select the disk on which to install FreeBSD.", 0, 0, 0,
167 		    n, disks, &selected);
168 
169 		chosen = (button == BSDDIALOG_OK) ?
170 		    strdup(disks[selected].name) : NULL;
171 	} else if (n == 1) {
172 		chosen = strdup(disks[0].name);
173 	} else {
174 		chosen = NULL;
175 	}
176 
177 	for (i = 0; i < n; i++)
178 		free((char*)disks[i].desc);
179 
180 	return (chosen);
181 }
182 
183 static struct gprovider *
184 provider_for_name(struct gmesh *mesh, const char *name)
185 {
186 	struct gclass *classp;
187 	struct gprovider *pp = NULL;
188 	struct ggeom *gp;
189 
190 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
191 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
192 			if (LIST_EMPTY(&gp->lg_provider))
193 				continue;
194 
195 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
196 				if (strcmp(pp->lg_name, name) == 0)
197 					break;
198 
199 			if (pp != NULL) break;
200 		}
201 
202 		if (pp != NULL) break;
203 	}
204 
205 	return (pp);
206 }
207 
208 static char *
209 wizard_partition(struct gmesh *mesh, const char *disk)
210 {
211 	struct gclass *classp;
212 	struct ggeom *gpart = NULL;
213 	struct gconfig *gc;
214 	char *retval = NULL;
215 	const char *scheme = NULL;
216 	char message[512];
217 	int choice;
218 	struct bsddialog_conf conf;
219 
220 	bsddialog_initconf(&conf);
221 
222 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
223 		if (strcmp(classp->lg_name, "PART") == 0)
224 			break;
225 
226 	if (classp != NULL) {
227 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
228 			if (strcmp(gpart->lg_name, disk) == 0)
229 				break;
230 	}
231 
232 	if (gpart != NULL) {
233 		LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
234 			if (strcmp(gc->lg_name, "scheme") == 0) {
235 				scheme = gc->lg_val;
236 				break;
237 			}
238 		}
239 	}
240 
241 	/* Treat uncommitted scheme deletions as no scheme */
242 	if (scheme != NULL && strcmp(scheme, "(none)") == 0)
243 		scheme = NULL;
244 
245 query:
246 	conf.button.ok_label = "Entire Disk";
247 	conf.button.cancel_label = "Partition";
248 	if (gpart != NULL)
249 		conf.button.default_cancel = true;
250 
251 	snprintf(message, sizeof(message), "Would you like to use this entire "
252 	    "disk (%s) for FreeBSD or partition it to share it with other "
253 	    "operating systems? Using the entire disk will erase any data "
254 	    "currently stored there.", disk);
255 	conf.title = "Partition";
256 	choice = bsddialog_yesno(&conf, message, 9, 45);
257 
258 	conf.button.ok_label = NULL;
259 	conf.button.cancel_label = NULL;
260 	conf.button.default_cancel = false;
261 
262 	if (choice == BSDDIALOG_NO && scheme != NULL && !is_scheme_bootable(scheme)) {
263 		char warning[512];
264 		int subchoice;
265 
266 		sprintf(warning, "The existing partition scheme on this "
267 		    "disk (%s) is not bootable on this platform. To install "
268 		    "FreeBSD, it must be repartitioned. This will destroy all "
269 		    "data on the disk. Are you sure you want to proceed?",
270 		    scheme);
271 		conf.title = "Non-bootable Disk";
272 		subchoice = bsddialog_yesno(&conf, warning, 0, 0);
273 		if (subchoice != BSDDIALOG_YES)
274 			goto query;
275 
276 		gpart_destroy(gpart);
277 		scheme = choose_part_type(default_scheme());
278 		if (scheme == NULL)
279 			return NULL;
280 		gpart_partition(disk, scheme);
281 	}
282 
283 	if (scheme == NULL || choice == 0) {
284 		if (gpart != NULL && scheme != NULL) {
285 			/* Erase partitioned disk */
286 			conf.title = "Confirmation";
287 			choice = bsddialog_yesno(&conf, "This will erase "
288 			   "the disk. Are you sure you want to proceed?", 0, 0);
289 			if (choice != BSDDIALOG_YES)
290 				goto query;
291 
292 			gpart_destroy(gpart);
293 		}
294 
295 		scheme = choose_part_type(default_scheme());
296 		if (scheme == NULL)
297 			return NULL;
298 		gpart_partition(disk, scheme);
299 	}
300 
301 	if (strcmp(scheme, "MBR") == 0) {
302 		struct gmesh submesh;
303 		geom_gettree(&submesh);
304 		gpart_create(provider_for_name(&submesh, disk),
305 		    "freebsd", NULL, NULL, &retval,
306 		    choice /* Non-interactive for "Entire Disk" */);
307 		geom_deletetree(&submesh);
308 	} else {
309 		retval = strdup(disk);
310 	}
311 
312 	return (retval);
313 }
314 
315 int
316 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
317     int interactive)
318 {
319 	struct gclass *classp;
320 	struct ggeom *gp;
321 	struct gprovider *pp;
322 	char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
323 	char *fsname;
324 	struct gmesh submesh;
325 	char swapsizestr[10], rootsizestr[10];
326 	intmax_t swapsize, available;
327 	int retval;
328 	struct bsddialog_conf conf;
329 
330 	if (strcmp(fstype, "zfs") == 0) {
331 		fsname = fsnames[1];
332 	} else {
333 		/* default to UFS */
334 		fsname = fsnames[0];
335 	}
336 
337 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
338 		if (strcmp(classp->lg_name, "PART") == 0)
339 			break;
340 
341 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
342 		if (strcmp(gp->lg_name, disk) == 0)
343 			break;
344 
345 	pp = provider_for_name(mesh, disk);
346 
347 	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
348 	if (interactive && available < MIN_FREE_SPACE) {
349 		char availablestr[10], neededstr[10], message[512];
350 		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
351 		    HN_DECIMAL);
352 		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
353 		    HN_DECIMAL);
354 		sprintf(message, "There is not enough free space on %s to "
355 		    "install FreeBSD (%s free, %s required). Would you like "
356 		    "to choose another disk or to open the partition editor?",
357 		    disk, availablestr, neededstr);
358 
359 		bsddialog_initconf(&conf);
360 		conf.button.ok_label = "Another Disk";
361 		conf.button.cancel_label = "Editor";
362 		conf.title = "Warning";
363 		retval = bsddialog_yesno(&conf, message, 0, 0);
364 
365 		return (!retval); /* Editor -> return 0 */
366 	}
367 
368 	swapsize = SWAP_SIZE(available);
369 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
370 	    HN_NOSPACE | HN_DECIMAL);
371 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
372 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
373 
374 	geom_gettree(&submesh);
375 	pp = provider_for_name(&submesh, disk);
376 	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
377 	geom_deletetree(&submesh);
378 
379 	geom_gettree(&submesh);
380 	pp = provider_for_name(&submesh, disk);
381 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
382 	geom_deletetree(&submesh);
383 
384 	return (0);
385 }
386