xref: /freebsd/usr.sbin/bsdinstall/partedit/part_wizard.c (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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, fd, 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 				 * Check if the disk is available to be opened for
140 				 * write operations, it helps prevent the USB
141 				 * stick used to boot from being listed as an option
142 				 */
143 				fd = g_open(pp->lg_name, 1);
144 				if (fd == -1) {
145 					continue;
146 				}
147 				g_close(fd);
148 
149 				disks = realloc(disks, (++n)*sizeof(disks[0]));
150 				disks[n-1].name = pp->lg_name;
151 				humanize_number(diskdesc, 7, pp->lg_mediasize,
152 				    "B", HN_AUTOSCALE, HN_DECIMAL);
153 				if (strncmp(pp->lg_name, "ad", 2) == 0)
154 					strcat(diskdesc, " ATA Hard Disk");
155 				else if (strncmp(pp->lg_name, "md", 2) == 0)
156 					strcat(diskdesc, " Memory Disk");
157 				else
158 					strcat(diskdesc, " Disk");
159 
160 				if (desc != NULL)
161 					snprintf(diskdesc, sizeof(diskdesc),
162 					    "%s <%s>", diskdesc, desc);
163 
164 				disks[n-1].prefix = "";
165 				disks[n-1].on = false;
166 				disks[n-1].depth = 0;
167 				disks[n-1].desc = strdup(diskdesc);
168 				disks[n-1].bottomdesc = "";
169 			}
170 		}
171 	}
172 
173 	if (n > 1) {
174 		conf.title = "Partitioning";
175 		button = bsddialog_menu(&conf,
176 		    "Select the disk on which to install " OSNAME ".", 0, 0, 0,
177 		    n, disks, &selected);
178 
179 		chosen = (button == BSDDIALOG_OK) ?
180 		    strdup(disks[selected].name) : NULL;
181 	} else if (n == 1) {
182 		chosen = strdup(disks[0].name);
183 	} else {
184 		chosen = NULL;
185 	}
186 
187 	for (i = 0; i < n; i++)
188 		free((char*)disks[i].desc);
189 
190 	return (chosen);
191 }
192 
193 static struct gprovider *
194 provider_for_name(struct gmesh *mesh, const char *name)
195 {
196 	struct gclass *classp;
197 	struct gprovider *pp = NULL;
198 	struct ggeom *gp;
199 
200 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
201 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
202 			if (LIST_EMPTY(&gp->lg_provider))
203 				continue;
204 
205 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
206 				if (strcmp(pp->lg_name, name) == 0)
207 					break;
208 
209 			if (pp != NULL) break;
210 		}
211 
212 		if (pp != NULL) break;
213 	}
214 
215 	return (pp);
216 }
217 
218 static char *
219 wizard_partition(struct gmesh *mesh, const char *disk)
220 {
221 	struct gclass *classp;
222 	struct ggeom *gpart = NULL;
223 	struct gconfig *gc;
224 	char *retval = NULL;
225 	const char *scheme = NULL;
226 	char message[512];
227 	int choice;
228 	struct bsddialog_conf conf;
229 
230 	bsddialog_initconf(&conf);
231 
232 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
233 		if (strcmp(classp->lg_name, "PART") == 0)
234 			break;
235 
236 	if (classp != NULL) {
237 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
238 			if (strcmp(gpart->lg_name, disk) == 0)
239 				break;
240 	}
241 
242 	if (gpart != NULL) {
243 		LIST_FOREACH(gc, &gpart->lg_config, lg_config) {
244 			if (strcmp(gc->lg_name, "scheme") == 0) {
245 				scheme = gc->lg_val;
246 				break;
247 			}
248 		}
249 	}
250 
251 	/* Treat uncommitted scheme deletions as no scheme */
252 	if (scheme != NULL && strcmp(scheme, "(none)") == 0)
253 		scheme = NULL;
254 
255 query:
256 	conf.button.ok_label = "Entire Disk";
257 	conf.button.cancel_label = "Partition";
258 	if (gpart != NULL)
259 		conf.button.default_cancel = true;
260 
261 	snprintf(message, sizeof(message), "Would you like to use this entire "
262 	    "disk (%s) for " OSNAME " or partition it to share it with other "
263 	    "operating systems? Using the entire disk will erase any data "
264 	    "currently stored there.", disk);
265 	conf.title = "Partition";
266 	choice = bsddialog_yesno(&conf, message, 9, 45);
267 
268 	conf.button.ok_label = NULL;
269 	conf.button.cancel_label = NULL;
270 	conf.button.default_cancel = false;
271 
272 	if (choice == BSDDIALOG_NO && scheme != NULL && !is_scheme_bootable(scheme)) {
273 		char warning[512];
274 		int subchoice;
275 
276 		sprintf(warning, "The existing partition scheme on this "
277 		    "disk (%s) is not bootable on this platform. To install "
278 		    OSNAME ", it must be repartitioned. This will destroy all "
279 		    "data on the disk. Are you sure you want to proceed?",
280 		    scheme);
281 		conf.title = "Non-bootable Disk";
282 		subchoice = bsddialog_yesno(&conf, warning, 0, 0);
283 		if (subchoice != BSDDIALOG_YES)
284 			goto query;
285 
286 		gpart_destroy(gpart);
287 		scheme = choose_part_type(default_scheme());
288 		if (scheme == NULL)
289 			return NULL;
290 		gpart_partition(disk, scheme);
291 	}
292 
293 	if (scheme == NULL || choice == 0) {
294 		if (gpart != NULL && scheme != NULL) {
295 			/* Erase partitioned disk */
296 			conf.title = "Confirmation";
297 			choice = bsddialog_yesno(&conf, "This will erase "
298 			   "the disk. Are you sure you want to proceed?", 0, 0);
299 			if (choice != BSDDIALOG_YES)
300 				goto query;
301 
302 			gpart_destroy(gpart);
303 		}
304 
305 		scheme = choose_part_type(default_scheme());
306 		if (scheme == NULL)
307 			return NULL;
308 		gpart_partition(disk, scheme);
309 	}
310 
311 	if (strcmp(scheme, "MBR") == 0) {
312 		struct gmesh submesh;
313 		geom_gettree(&submesh);
314 		gpart_create(provider_for_name(&submesh, disk),
315 		    "freebsd", NULL, NULL, &retval,
316 		    choice /* Non-interactive for "Entire Disk" */);
317 		geom_deletetree(&submesh);
318 	} else {
319 		retval = strdup(disk);
320 	}
321 
322 	return (retval);
323 }
324 
325 int
326 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
327     int interactive)
328 {
329 	struct gclass *classp;
330 	struct ggeom *gp;
331 	struct gprovider *pp;
332 	char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
333 	char *fsname;
334 	struct gmesh submesh;
335 	char swapsizestr[10], rootsizestr[10];
336 	intmax_t swapsize, available;
337 	int retval;
338 	struct bsddialog_conf conf;
339 
340 	if (strcmp(fstype, "zfs") == 0) {
341 		fsname = fsnames[1];
342 	} else {
343 		/* default to UFS */
344 		fsname = fsnames[0];
345 	}
346 
347 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
348 		if (strcmp(classp->lg_name, "PART") == 0)
349 			break;
350 
351 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
352 		if (strcmp(gp->lg_name, disk) == 0)
353 			break;
354 
355 	pp = provider_for_name(mesh, disk);
356 
357 	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
358 	if (interactive && available < MIN_FREE_SPACE) {
359 		char availablestr[10], neededstr[10], message[512];
360 		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
361 		    HN_DECIMAL);
362 		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
363 		    HN_DECIMAL);
364 		sprintf(message, "There is not enough free space on %s to "
365 		    "install " OSNAME " (%s free, %s required). Would you like "
366 		    "to choose another disk or to open the partition editor?",
367 		    disk, availablestr, neededstr);
368 
369 		bsddialog_initconf(&conf);
370 		conf.button.ok_label = "Another Disk";
371 		conf.button.cancel_label = "Editor";
372 		conf.title = "Warning";
373 		retval = bsddialog_yesno(&conf, message, 0, 0);
374 
375 		return (!retval); /* Editor -> return 0 */
376 	}
377 
378 	swapsize = SWAP_SIZE(available);
379 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
380 	    HN_NOSPACE | HN_DECIMAL);
381 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
382 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
383 
384 	geom_gettree(&submesh);
385 	pp = provider_for_name(&submesh, disk);
386 	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
387 	geom_deletetree(&submesh);
388 
389 	geom_gettree(&submesh);
390 	pp = provider_for_name(&submesh, disk);
391 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
392 	geom_deletetree(&submesh);
393 
394 	return (0);
395 }
396