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