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