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 <errno.h>
33 #include <inttypes.h>
34 #include <libutil.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 *wizard_partition(struct gmesh *mesh, const char *disk);
47 
48 int
49 part_wizard(const char *fsreq)
50 {
51 	char *disk, *schemeroot;
52 	const char *fstype;
53 	struct gmesh mesh;
54 	int error;
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_select(&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 char *
93 boot_disk_select(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 *retval = NULL;
203 	const char *scheme = NULL;
204 	char message[512];
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, "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,
299     int interactive)
300 {
301 	struct gclass *classp;
302 	struct ggeom *gp;
303 	struct gprovider *pp;
304 	char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
305 	char *fsname;
306 	struct gmesh submesh;
307 	char swapsizestr[10], rootsizestr[10];
308 	intmax_t swapsize, available;
309 	int retval;
310 
311 	if (strcmp(fstype, "zfs") == 0) {
312 		fsname = fsnames[1];
313 	} else {
314 		/* default to UFS */
315 		fsname = fsnames[0];
316 	}
317 
318 	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
319 		if (strcmp(classp->lg_name, "PART") == 0)
320 			break;
321 
322 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
323 		if (strcmp(gp->lg_name, disk) == 0)
324 			break;
325 
326 	pp = provider_for_name(mesh, disk);
327 
328 	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
329 	if (interactive && available < MIN_FREE_SPACE) {
330 		char availablestr[10], neededstr[10], message[512];
331 		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
332 		    HN_DECIMAL);
333 		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
334 		    HN_DECIMAL);
335 		sprintf(message, "There is not enough free space on %s to "
336 		    "install FreeBSD (%s free, %s required). Would you like "
337 		    "to choose another disk or to open the partition editor?",
338 		    disk, availablestr, neededstr);
339 
340 		dialog_vars.yes_label = "Another Disk";
341 		dialog_vars.no_label = "Editor";
342 		retval = dialog_yesno("Warning", message, 0, 0);
343 		dialog_vars.yes_label = NULL;
344 		dialog_vars.no_label = NULL;
345 
346 		return (!retval); /* Editor -> return 0 */
347 	}
348 
349 	swapsize = SWAP_SIZE(available);
350 	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
351 	    HN_NOSPACE | HN_DECIMAL);
352 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
353 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
354 
355 	geom_gettree(&submesh);
356 	pp = provider_for_name(&submesh, disk);
357 	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
358 	geom_deletetree(&submesh);
359 
360 	geom_gettree(&submesh);
361 	pp = provider_for_name(&submesh, disk);
362 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
363 	geom_deletetree(&submesh);
364 
365 	return (0);
366 }
367