part_wizard.c revision 273831
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: stable/10/usr.sbin/bsdinstall/partedit/part_wizard.c 273831 2014-10-29 16:48:18Z nwhitehorn $
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
46static char *boot_disk(struct gmesh *mesh);
47static char *wizard_partition(struct gmesh *mesh, const char *disk);
48
49int
50part_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
61startwizard:
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
92static char *
93boot_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
171static struct gprovider *
172provider_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
196static char *
197wizard_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
230query:
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		gpart_partition(disk, default_scheme());
261		scheme = default_scheme();
262	}
263
264	if (scheme == NULL || choice == 0) {
265		if (gpart != NULL && scheme != NULL) {
266			/* Erase partitioned disk */
267			choice = dialog_yesno("Confirmation", "This will erase "
268			   "the disk. Are you sure you want to proceed?", 0, 0);
269			if (choice != 0)
270				goto query;
271
272			gpart_destroy(gpart);
273		}
274
275		gpart_partition(disk, default_scheme());
276		scheme = default_scheme();
277	}
278
279	if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
280		struct gmesh submesh;
281		geom_gettree(&submesh);
282		gpart_create(provider_for_name(&submesh, disk),
283		    "freebsd", NULL, NULL, &retval,
284		    choice /* Non-interactive for "Entire Disk" */);
285		geom_deletetree(&submesh);
286	} else {
287		retval = strdup(disk);
288	}
289
290	return (retval);
291}
292
293int
294wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype, int interactive)
295{
296	struct gmesh submesh;
297	struct gclass *classp;
298	struct ggeom *gp;
299	struct gprovider *pp;
300	intmax_t swapsize, available;
301	char swapsizestr[10], rootsizestr[10], *fsname;
302	char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"};
303	int retval;
304
305	if (strcmp(fstype, "zfs") == 0) {
306		fsname = fsnames[1];
307	} else {
308		/* default to UFS */
309		fsname = fsnames[0];
310	}
311
312	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
313		if (strcmp(classp->lg_name, "PART") == 0)
314			break;
315
316	LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
317		if (strcmp(gp->lg_name, disk) == 0)
318			break;
319
320	pp = provider_for_name(mesh, disk);
321
322	available = gpart_max_free(gp, NULL)*pp->lg_sectorsize;
323	if (interactive && available < MIN_FREE_SPACE) {
324		char availablestr[10], neededstr[10], message[512];
325		humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE,
326		    HN_DECIMAL);
327		humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE,
328		    HN_DECIMAL);
329		sprintf(message, "There is not enough free space on %s to "
330		    "install FreeBSD (%s free, %s required). Would you like "
331		    "to choose another disk or to open the partition editor?",
332		    disk, availablestr, neededstr);
333
334		dialog_vars.yes_label = "Another Disk";
335		dialog_vars.no_label = "Editor";
336		retval = dialog_yesno("Warning", message, 0, 0);
337		dialog_vars.yes_label = NULL;
338		dialog_vars.no_label = NULL;
339
340		return (!retval); /* Editor -> return 0 */
341	}
342
343	swapsize = SWAP_SIZE(available);
344	humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE,
345	    HN_NOSPACE | HN_DECIMAL);
346	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
347	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
348
349	geom_gettree(&submesh);
350	pp = provider_for_name(&submesh, disk);
351	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
352	geom_deletetree(&submesh);
353
354	geom_gettree(&submesh);
355	pp = provider_for_name(&submesh, disk);
356	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
357	geom_deletetree(&submesh);
358
359	return (0);
360}
361
362