1 /*
2  * Copyright (c)2004 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of the DragonFly Project nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * fn_subpart.c
36  * Installer Function : Create Subpartitions.
37  * $Id: fn_subpart.c,v 1.50 2005/04/07 20:22:40 cpressey Exp $
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #ifdef ENABLE_NLS
45 #include <libintl.h>
46 #define _(String) gettext (String)
47 #else
48 #define _(String) (String)
49 #endif
50 
51 #include "libaura/mem.h"
52 #include "libaura/buffer.h"
53 #include "libaura/dict.h"
54 #include "libaura/fspred.h"
55 
56 #include "libdfui/dfui.h"
57 #include "libdfui/dump.h"
58 #include "libdfui/system.h"
59 
60 #include "libinstaller/commands.h"
61 #include "libinstaller/diskutil.h"
62 #include "libinstaller/functions.h"
63 #include "libinstaller/uiutil.h"
64 
65 #include "fn.h"
66 #include "flow.h"
67 #include "pathnames.h"
68 
69 static int	create_subpartitions(struct i_fn_args *);
70 static long	default_capacity(struct storage *, int);
71 static int	check_capacity(struct i_fn_args *);
72 static int	check_subpartition_selections(struct dfui_response *, struct i_fn_args *);
73 static void	save_subpartition_selections(struct dfui_response *, struct i_fn_args *);
74 static void	populate_create_subpartitions_form(struct dfui_form *, struct i_fn_args *);
75 static int	warn_subpartition_selections(struct i_fn_args *);
76 static int	warn_encrypted_root(struct i_fn_args *);
77 static struct dfui_form *make_create_subpartitions_form(struct i_fn_args *);
78 static int	show_create_subpartitions_form(struct dfui_form *, struct i_fn_args *);
79 
80 static const char *def_mountpt[]  = {"/", "swap", "/var", "/tmp", "/usr", "/home", NULL};
81 static int expert = 0;
82 
83 /*
84  * Given a set of subpartitions-to-be in the selected slice,
85  * create them.
86  */
87 static int
88 create_subpartitions(struct i_fn_args *a)
89 {
90 	struct subpartition *sp;
91 	struct commands *cmds;
92 	int result = 0;
93 	int num_partitions;
94 
95 	cmds = commands_new();
96 	if (!is_file("%sinstall.disklabel.%s",
97 	    a->tmp,
98 	    slice_get_device_name(storage_get_selected_slice(a->s)))) {
99 		/*
100 		 * Get a copy of the 'virgin' disklabel.
101 		 * XXX It might make more sense for this to
102 		 * happen right after format_slice() instead.
103 		 */
104 		command_add(cmds, "%s%s -r %s >%sinstall.disklabel.%s",
105 		    a->os_root, cmd_name(a, "DISKLABEL64"),
106 		    slice_get_device_name(storage_get_selected_slice(a->s)),
107 		    a->tmp,
108 		    slice_get_device_name(storage_get_selected_slice(a->s)));
109 	}
110 
111 	/*
112 	 * Weave together a new disklabel out the of the 'virgin'
113 	 * disklabel, and the user's subpartition choices.
114 	 */
115 
116 	/*
117 	 * Take everything from the 'virgin' disklabel up until the
118 	 * '16 partitions' line.
119 	 */
120 	num_partitions = 16;
121 	command_add(cmds, "%s%s '$2==\"partitions:\" || cut { cut = 1 } !cut { print $0 }' <%sinstall.disklabel.%s >%sinstall.disklabel",
122 	    a->os_root, cmd_name(a, "AWK"),
123 	    a->tmp,
124 	    slice_get_device_name(storage_get_selected_slice(a->s)),
125 	    a->tmp);
126 
127 	/*
128 	 * 16 partitions:
129 	 * #          size     offset    fstype
130 	 *   c:   16383969          0    unused	#    7999.985MB
131 	 */
132 
133 	command_add(cmds, "%s%s '%d partitions:' >>%sinstall.disklabel",
134 	    a->os_root, cmd_name(a, "ECHO"), num_partitions ,a->tmp);
135 	command_add(cmds, "%s%s '%s' >>%sinstall.disklabel",
136 	    a->os_root, cmd_name(a, "ECHO"),
137 	    "#          size     offset    fstype",
138 	    a->tmp);
139 
140 #ifdef DEBUG
141 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
142 	     sp != NULL; sp = subpartition_next(sp)) {
143 		command_add(cmds, "%s%s 'mountpoint: %s device: %s'",
144 		     a->os_root, cmd_name(a, "ECHO"),
145 		     subpartition_get_mountpoint(sp),
146 		     subpartition_get_device_name(sp));
147 	}
148 #endif
149 
150 	/*
151 	 * Write a line for each subpartition the user wants.
152 	 */
153 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
154 	     sp != NULL; sp = subpartition_next(sp)) {
155 		if (subpartition_is_tmpfsbacked(sp)) {
156 			continue;
157 		}
158 		if (subpartition_is_swap(sp)) {
159 			command_add(cmds, "%s%s '  %c:\t%s\t*\tswap' >>%sinstall.disklabel",
160 			    a->os_root, cmd_name(a, "ECHO"),
161 			    subpartition_get_letter(sp),
162 			    capacity_to_string(subpartition_get_capacity(sp)),
163 			    a->tmp);
164 		} else {
165 			command_add(cmds, "%s%s '  %c:\t%s\t%s\t4.2BSD' >>%sinstall.disklabel",
166 			    a->os_root, cmd_name(a, "ECHO"),
167 			    subpartition_get_letter(sp),
168 			    capacity_to_string(subpartition_get_capacity(sp)),
169 			    subpartition_get_letter(sp) == 'a' ? "0" : "*",
170 			    a->tmp);
171 		}
172 	}
173 	temp_file_add(a, "install.disklabel");
174 
175 	/*
176 	 * Label the slice from the disklabel we just wove together.
177 	 */
178 	command_add(cmds, "%s%s -R -B -r %s %sinstall.disklabel",
179 	    a->os_root, cmd_name(a, "DISKLABEL64"),
180 	    slice_get_device_name(storage_get_selected_slice(a->s)),
181 	    a->tmp);
182 
183 	/*
184 	 * Create a snapshot of the disklabel we just created
185 	 * for debugging inspection in the log.
186 	 */
187 	command_add(cmds, "%s%s %s",
188 	    a->os_root, cmd_name(a, "DISKLABEL64"),
189 	    slice_get_device_name(storage_get_selected_slice(a->s)));
190 
191 	/*
192 	 * If encryption was specified, load dm(4).
193 	 */
194 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
195 	     sp != NULL; sp = subpartition_next(sp)) {
196 		if (subpartition_is_encrypted(sp)) {
197 			fn_get_passphrase(a);
198 			break;
199 		}
200 	}
201 
202 	/*
203 	 * Create filesystems on the newly-created subpartitions.
204 	 */
205 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
206 	     sp != NULL; sp = subpartition_next(sp)) {
207 		if (subpartition_is_swap(sp) || subpartition_is_tmpfsbacked(sp)) {
208 			if (subpartition_is_swap(sp) &&
209 			    subpartition_is_encrypted(sp)) {
210 				command_add(cmds,
211 				    "%s%s -d /tmp/t1 luksFormat /dev/%s",
212 				    a->os_root, cmd_name(a, "CRYPTSETUP"),
213 				    subpartition_get_device_name(sp));
214 				command_add(cmds,
215 				    "%s%s -d /tmp/t1 luksOpen /dev/%s swap",
216 				    a->os_root, cmd_name(a, "CRYPTSETUP"),
217 				    subpartition_get_device_name(sp));
218 			}
219 			continue;
220 		}
221 
222 		if (subpartition_is_encrypted(sp) &&
223 		    strcmp(subpartition_get_mountpoint(sp), "/") != 0) {
224 			command_add(cmds,
225 			    "%s%s -d /tmp/t1 luksFormat /dev/%s",
226 			    a->os_root, cmd_name(a, "CRYPTSETUP"),
227 			    subpartition_get_device_name(sp));
228 			command_add(cmds,
229 			    "%s%s -d /tmp/t1 luksOpen /dev/%s %s",
230 			    a->os_root, cmd_name(a, "CRYPTSETUP"),
231 			    subpartition_get_device_name(sp),
232 			    subpartition_get_mountpoint(sp) + 1);
233 			command_add(cmds, "%s%s%s -b %ld -f %ld /dev/mapper/%s",
234 			    a->os_root, cmd_name(a, "NEWFS"),
235 			    subpartition_is_softupdated(sp) ? " -U" : "",
236 			    subpartition_get_bsize(sp),
237 			    subpartition_get_fsize(sp),
238 			    subpartition_get_mountpoint(sp) + 1);
239 		} else {
240 			command_add(cmds, "%s%s%s -b %ld -f %ld /dev/%s",
241 			    a->os_root, cmd_name(a, "NEWFS"),
242 			    subpartition_is_softupdated(sp) ? " -U" : "",
243 			    subpartition_get_bsize(sp),
244 			    subpartition_get_fsize(sp),
245 			    subpartition_get_device_name(sp));
246 		}
247 	}
248 
249 	result = commands_execute(a, cmds);
250 	commands_free(cmds);
251 	return(result);
252 }
253 
254 static long
255 default_capacity(struct storage *s, int mtpt)
256 {
257 	unsigned long swap;
258 	unsigned long capacity;
259 	unsigned long mem;
260 
261 	if (mtpt == MTPT_HOME)
262 		return(-1);
263 
264 	capacity = slice_get_capacity(storage_get_selected_slice(s));
265 	mem = storage_get_memsize(s);
266 	swap = 2 * mem;
267 	if (mem > (capacity / 2) || capacity < 4096)
268 		swap = mem;
269 	if (mem > capacity)
270 		swap = capacity / 2;
271 	if (swap > SWAP_MAX)
272 		swap = SWAP_MAX;
273 
274 	if (capacity < DISK_MIN) {
275 		/*
276 		 * For the purposes of this installer:
277 		 * can't be done.  Sorry.
278 		 */
279 		return(-1);
280 	} else if (capacity < 4096) {
281 		switch (mtpt) {
282 		case MTPT_ROOT:	return(320);
283 		case MTPT_SWAP: return(swap);
284 		case MTPT_VAR:	return(128);
285 		case MTPT_TMP:	return(128);
286 		case MTPT_USR:	return(1472);
287 		}
288 	} else if (capacity < 10240) {
289 		switch (mtpt) {
290 		case MTPT_ROOT:	return(640);
291 		case MTPT_SWAP: return(swap);
292 		case MTPT_VAR:	return(256);
293 		case MTPT_TMP:	return(256);
294 		case MTPT_USR:	return(2688);
295 		}
296 	} else {
297 		switch (mtpt) {
298 		case MTPT_ROOT:	return(768);
299 		case MTPT_SWAP: return(swap);
300 		case MTPT_VAR:	return(256);
301 		case MTPT_TMP:	return(256);
302 		case MTPT_USR:	return(7680);
303 		}
304 	}
305 	/* shouldn't ever happen */
306 	return(-1);
307 }
308 
309 static int
310 check_capacity(struct i_fn_args *a)
311 {
312 	struct subpartition *sp;
313 	long min_capacity[] = {320, 0, 16, 0, 1472, 0, 0};
314 	unsigned long total_capacity = 0;
315 	int mtpt;
316 
317 	if (subpartition_find(storage_get_selected_slice(a->s), "/usr") == NULL)
318 		min_capacity[MTPT_ROOT] += min_capacity[MTPT_USR];
319 
320 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
321 	     sp != NULL; sp = subpartition_next(sp)) {
322 		long subpart_capacity = subpartition_get_capacity(sp);
323 		const char *mountpt = subpartition_get_mountpoint(sp);
324 
325 		if (subpart_capacity == -1)
326 			total_capacity++;
327 		else
328 			total_capacity += subpart_capacity;
329 		for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
330 			if (strcmp(mountpt, def_mountpt[mtpt]) == 0 &&
331 			    subpart_capacity < min_capacity[mtpt] &&
332 			    subpart_capacity != -1) {
333 				inform(a->c, _("WARNING: The size (%ldM) specified for "
334 				    "the %s subpartition is too small. It "
335 				    "should be at least %ldM or you will "
336 				    "risk running out of space during "
337 				    "the installation."),
338 				    subpart_capacity, mountpt,
339 				    min_capacity[mtpt]);
340 			}
341 		}
342 	}
343 
344 	if (total_capacity > slice_get_capacity(storage_get_selected_slice(a->s))) {
345 		inform(a->c, _("The space allocated to all of your selected "
346 		    "subpartitions (%luM) exceeds the total "
347 		    "capacity of the selected primary partition "
348 		    "(%luM). Remove some subpartitions or choose "
349 		    "a smaller size for them and try again."),
350 		    total_capacity, slice_get_capacity(storage_get_selected_slice(a->s)));
351 		return(0);
352 	}
353 
354 	return(1);
355 }
356 
357 static int
358 check_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
359 {
360 	struct dfui_dataset *ds;
361 	struct dfui_dataset *star_ds = NULL;
362 	struct aura_dict *d;
363 	const char *mountpoint, *capstring;
364 	long capacity = 0;
365 	int found_root = 0;
366 	int valid = 1;
367 
368 	d = aura_dict_new(1, AURA_DICT_LIST);
369 
370 	if ((ds = dfui_response_dataset_get_first(r)) == NULL) {
371 		inform(a->c, _("Please set up at least one subpartition."));
372 		valid = 0;
373 	}
374 
375 	for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
376 	    ds = dfui_dataset_get_next(ds)) {
377 #ifdef DEBUG
378 		dfui_dataset_dump(ds);
379 #endif
380 		mountpoint = dfui_dataset_get_value(ds, "mountpoint");
381 		capstring = dfui_dataset_get_value(ds, "capacity");
382 
383 		if (expert) {
384 			int tmpfsbacked;
385 
386 			tmpfsbacked = (strcmp(dfui_dataset_get_value(ds, "tmpfsbacked"), "Y") == 0);
387 			if (tmpfsbacked && strcmp(mountpoint, "/") == 0) {
388 				inform(a->c, _("/ cannot be TMPFS backed."));
389 				valid = 0;
390 			}
391 		}
392 
393 		if (aura_dict_exists(d, mountpoint, strlen(mountpoint) + 1)) {
394 			inform(a->c, _("The same mount point cannot be specified "
395 			    "for two different subpartitions."));
396 			valid = 0;
397 		}
398 
399 		if (strcmp(mountpoint, "/") == 0)
400 			found_root = 1;
401 
402 		if (strcmp(capstring, "*") == 0) {
403 			if (star_ds != NULL) {
404 				inform(a->c, _("You cannot have more than one subpartition "
405 				    "with a '*' capacity (meaning 'use the remainder "
406 				    "of the primary partition'.)"));
407 				valid = 0;
408 			} else {
409 				star_ds = ds;
410 			}
411 		}
412 
413 		if (!(!strcasecmp(mountpoint, "swap") || mountpoint[0] == '/')) {
414 			inform(a->c, _("Mount point must be either 'swap', or it must "
415 			    "start with a '/'."));
416 			valid = 0;
417 		}
418 
419 		if (strpbrk(mountpoint, " \\\"'`") != NULL) {
420 			inform(a->c, _("Mount point may not contain the following "
421 			    "characters: blank space, backslash, or "
422 			    "single, double, or back quotes."));
423 			valid = 0;
424 		}
425 
426 		if (strlen(capstring) == 0) {
427 			inform(a->c, _("A capacity must be specified."));
428 			valid = 0;
429 		}
430 
431 		if (!string_to_capacity(capstring, &capacity)) {
432 			inform(a->c, _("Capacity must be either a '*' symbol "
433 			    "to indicate 'use the rest of the primary "
434 			    "partition', or it must be a series of decimal "
435 			    "digits ending with an 'M' (indicating "
436 			    "megabytes), a 'G' (indicating gigabytes) and "
437 			    "so on (up to 'E'.)"));
438 			valid = 0;
439 		}
440 
441 		/*
442 		 * Maybe remove this limit entirely?
443 		 */
444 		if ((strcasecmp(mountpoint, "swap") == 0) &&
445 		    (capacity > SWAP_MAX)) {
446 			inform(a->c, _("Swap capacity is limited to %dG."),
447 			    SWAP_MAX / 1024);
448 			valid = 0;
449 		}
450 
451 		/*
452 		 * If we made it through that obstacle course, all is well.
453 		 */
454 
455 		if (valid)
456 			aura_dict_store(d, mountpoint, strlen(mountpoint) + 1, "", 1);
457 	}
458 
459 	if (!found_root) {
460 		inform(a->c, _("You must include a / (root) subpartition."));
461 		valid = 0;
462 	}
463 
464 	if (aura_dict_size(d) > 16) {
465 		inform(a->c, _("You cannot have more than 16 subpartitions "
466 		    "on a single primary partition.  Remove some "
467 		    "and try again."));
468 		valid = 0;
469 	}
470 
471 	aura_dict_free(d);
472 
473 	return(valid);
474 }
475 
476 static void
477 save_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
478 {
479 	struct dfui_dataset *ds;
480 	char tmpfsbacked;
481 	const char *mountpoint, *capstring;
482 	long capacity;
483 	long bsize, fsize;
484 	int softupdates;
485 	int valid = 1;
486 
487 	subpartitions_free(storage_get_selected_slice(a->s));
488 
489 	for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
490 	    ds = dfui_dataset_get_next(ds)) {
491 		mountpoint = dfui_dataset_get_value(ds, "mountpoint");
492 		capstring = dfui_dataset_get_value(ds, "capacity");
493 
494 		if (expert) {
495 			softupdates =
496 			    (strcmp(dfui_dataset_get_value(ds, "softupdates"), "Y") == 0);
497 			fsize = atol(dfui_dataset_get_value(ds, "fsize"));
498 			bsize = atol(dfui_dataset_get_value(ds, "bsize"));
499 			tmpfsbacked = (strcmp(dfui_dataset_get_value(ds, "tmpfsbacked"), "Y") == 0);
500 		} else {
501 			softupdates = (strcmp(mountpoint, "/") == 0 ? 0 : 1);
502 			tmpfsbacked = 0;
503 			fsize = -1;
504 			bsize = -1;
505 		}
506 
507 		if (string_to_capacity(capstring, &capacity)) {
508 			subpartition_new_ufs(storage_get_selected_slice(a->s),
509 			    mountpoint, capacity,
510 			    strcasecmp(dfui_dataset_get_value(ds, "encrypted"), "Y") == 0,
511 			    softupdates, fsize, bsize, tmpfsbacked);
512 		}
513 	}
514 }
515 
516 static void
517 populate_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
518 {
519 	struct subpartition *sp;
520 	struct dfui_dataset *ds;
521 	char temp[32];
522 	int mtpt;
523 	long capacity;
524 
525 	if (slice_subpartition_first(storage_get_selected_slice(a->s)) != NULL) {
526 		/*
527 		 * The user has already given us their subpartition
528 		 * preferences, so use them here.
529 		 */
530 		for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
531 		     sp != NULL; sp = subpartition_next(sp)) {
532 			ds = dfui_dataset_new();
533 			dfui_dataset_celldata_add(ds, "mountpoint",
534 			    subpartition_get_mountpoint(sp));
535 			dfui_dataset_celldata_add(ds, "capacity",
536 			    capacity_to_string(subpartition_get_capacity(sp)));
537 			dfui_dataset_celldata_add(ds, "encrypted",
538 			    subpartition_is_encrypted(sp) ? "Y" : "N");
539 			if (expert) {
540 				dfui_dataset_celldata_add(ds, "softupdates",
541 				    subpartition_is_softupdated(sp) ? "Y" : "N");
542 				dfui_dataset_celldata_add(ds, "tmpfsbacked",
543 				    subpartition_is_tmpfsbacked(sp) ? "Y" : "N");
544 				snprintf(temp, 32, "%ld", subpartition_get_fsize(sp));
545 				dfui_dataset_celldata_add(ds, "fsize",
546 				    temp);
547 				snprintf(temp, 32, "%ld", subpartition_get_bsize(sp));
548 				dfui_dataset_celldata_add(ds, "bsize",
549 				    temp);
550 			}
551 			dfui_form_dataset_add(f, ds);
552 		}
553 	} else {
554 		/*
555 		 * Otherwise, populate the form with datasets representing
556 		 * reasonably-calculated defaults.  The defaults are chosen
557 		 * based on the slice's total capacity and the machine's
558 		 * total physical memory (for swap.)
559 		 */
560 		for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
561 			capacity = default_capacity(a->s, mtpt);
562 			ds = dfui_dataset_new();
563 			dfui_dataset_celldata_add(ds, "mountpoint",
564 			    def_mountpt[mtpt]);
565 			dfui_dataset_celldata_add(ds, "capacity",
566 			    capacity_to_string(capacity));
567 			dfui_dataset_celldata_add(ds, "encrypted", "N");
568 			if (expert) {
569 				dfui_dataset_celldata_add(ds, "softupdates",
570 				    strcmp(def_mountpt[mtpt], "/") != 0 ? "Y" : "N");
571 				dfui_dataset_celldata_add(ds, "tmpfsbacked",
572 				    "N");
573 				dfui_dataset_celldata_add(ds, "fsize",
574 				    capacity < 1024 ? "1024" : "2048");
575 				dfui_dataset_celldata_add(ds, "bsize",
576 				    capacity < 1024 ? "8192" : "16384");
577 			}
578 			dfui_form_dataset_add(f, ds);
579 		}
580 	}
581 }
582 
583 static int
584 warn_subpartition_selections(struct i_fn_args *a)
585 {
586 	int valid = 0;
587 	struct aura_buffer *omit, *consequences;
588 
589 	omit = aura_buffer_new(2048);
590 	consequences = aura_buffer_new(2048);
591 
592 	valid = check_capacity(a);
593 	if (subpartition_find(storage_get_selected_slice(a->s), "/var") == NULL) {
594 		aura_buffer_cat(omit, "/var ");
595 		aura_buffer_cat(consequences, _("/var will be a plain dir in /\n"));
596 	}
597 	if (subpartition_find(storage_get_selected_slice(a->s), "/usr") == NULL) {
598 		aura_buffer_cat(omit, "/usr ");
599 		aura_buffer_cat(consequences, _("/usr will be a plain dir in /\n"));
600 	}
601         if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL) {
602                 aura_buffer_cat(omit, "/tmp ");
603 		aura_buffer_cat(consequences, _("/tmp will be symlinked to /var/tmp\n"));
604 	}
605         if (subpartition_find(storage_get_selected_slice(a->s), "/home") == NULL) {
606                 aura_buffer_cat(omit, "/home ");
607 		aura_buffer_cat(consequences, _("/home will be symlinked to /usr/home\n"));
608 	}
609 
610 	if (valid && aura_buffer_len(omit) > 0) {
611 		switch (dfui_be_present_dialog(a->c, _("Really omit?"),
612 		    _("Omit Subpartition(s)|Return to Create Subpartitions"),
613 		    _("You have elected to not have the following "
614 		    "subpartition(s):\n\n%s\n\n"
615 		    "The ramifications of these subpartition(s) being "
616 		    "missing will be:\n\n%s\n"
617 		    "Is this really what you want to do?"),
618 		    aura_buffer_buf(omit), aura_buffer_buf(consequences))) {
619 		case 1:
620 			valid = 1;
621 			break;
622 		case 2:
623 			valid = 0;
624 			break;
625 		default:
626 			abort_backend();
627 		}
628 	}
629 
630 	aura_buffer_free(omit);
631 	aura_buffer_free(consequences);
632 
633 	return(!valid);
634 }
635 
636 static int
637 warn_encrypted_root(struct i_fn_args *a)
638 {
639 	int valid = 1;
640 	struct subpartition *sp;
641 
642 	sp = subpartition_find(storage_get_selected_slice(a->s), "/");
643 	if (sp == NULL)
644 		return(!valid);
645 
646 	if (subpartition_is_encrypted(sp)) {
647 		switch (dfui_be_present_dialog(a->c, _("root cannot be encrypted"),
648 		    _("Leave root unencrypted|Return to Create Subpartitions"),
649 		    _("You have selected encryption for the root partition which "
650 		    "is not supported."))) {
651 		case 1:
652 			subpartition_clr_encrypted(sp);
653 			valid = 1;
654 			break;
655 		case 2:
656 			valid = 0;
657 			break;
658 		default:
659 			abort_backend();
660 		}
661 	}
662 
663 	return(!valid);
664 }
665 
666 static struct dfui_form *
667 make_create_subpartitions_form(struct i_fn_args *a)
668 {
669 	struct dfui_field *fi;
670 	struct dfui_form *f;
671 	char msg_buf[1][1024];
672 
673 	snprintf(msg_buf[0], sizeof(msg_buf[0]),
674 	    _("Subpartitions further divide a primary partition for "
675 	    "use with %s.  Some reasons you may want "
676 	    "a set of subpartitions are:\n\n"
677 	    "- you want to restrict how much data can be written "
678 	    "to certain parts of the primary partition, to quell "
679 	    "denial-of-service attacks; and\n"
680 	    "- you want to speed up access to data on the disk."
681 	    ""), OPERATING_SYSTEM_NAME);
682 
683 	f = dfui_form_create(
684 	    "create_subpartitions",
685 	    _("Create Subpartitions"),
686 	    _("Set up the subpartitions (also known as just `partitions' "
687 	    "in BSD tradition) you want to have on this primary "
688 	    "partition.\n\n"
689 	    "For Capacity, use 'M' to indicate megabytes, 'G' to "
690 	    "indicate gigabytes, and so on (up to 'E'.) A single '*' "
691 	    "indicates 'use the remaining space on the primary partition'."),
692 
693 	    msg_buf[0],
694 
695 	    "p", "special", "dfinstaller_create_subpartitions",
696 	    "p", "minimum_width","64",
697 
698 	    "f", "mountpoint", _("Mountpoint"), "", "",
699 	    "f", "capacity", _("Capacity"), "", "",
700 
701 	    "f", "encrypted", _("Encrypted"), "", "",
702 	    "p", "control", "checkbox",
703 
704 	    "a", "ok", _("Accept and Create"), "", "",
705 	    "a", "cancel",
706 	    (disk_get_formatted(storage_get_selected_disk(a->s)) ?
707 	    _("Return to Select Disk") :
708 	    _("Return to Select Primary Partition")), "", "",
709 	    "p", "accelerator", "ESC",
710 
711 	    NULL
712 	);
713 
714 	dfui_form_set_multiple(f, 1);
715 	dfui_form_set_extensible(f, 1);
716 
717 	if (expert) {
718 		fi = dfui_form_field_add(f, "softupdates",
719 		    dfui_info_new(_("Softupdates"), "", ""));
720 		dfui_field_property_set(fi, "control", "checkbox");
721 
722 		fi = dfui_form_field_add(f, "tmpfsbacked",
723 		    dfui_info_new(_("TMPFS"), "", ""));
724 		dfui_field_property_set(fi, "control", "checkbox");
725 
726 		fi = dfui_form_field_add(f, "fsize",
727 		    dfui_info_new(_("Frag Sz"), "", ""));
728 
729 		fi = dfui_form_field_add(f, "bsize",
730 		    dfui_info_new(_("Block Sz"), "", ""));
731 
732 		dfui_form_action_add(f, "switch",
733 		    dfui_info_new(_("Switch to Normal Mode"), "", ""));
734 	} else {
735 		dfui_form_action_add(f, "switch",
736 		    dfui_info_new(_("Switch to Expert Mode"), "", ""));
737 	}
738 
739 	return(f);
740 }
741 
742 /*
743  * Returns:
744  *	-1 = the form should be redisplayed
745  *	 0 = failure, function is over
746  *	 1 = success, function is over
747  */
748 static int
749 show_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
750 {
751 	struct dfui_dataset *ds;
752 	struct dfui_response *r;
753 
754 	for (;;) {
755 		if (dfui_form_dataset_get_first(f) == NULL)
756 			populate_create_subpartitions_form(f, a);
757 
758 		if (!dfui_be_present(a->c, f, &r))
759 			abort_backend();
760 
761 		if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
762 			dfui_response_free(r);
763 			return(0);
764 		} else if (strcmp(dfui_response_get_action_id(r), "switch") == 0) {
765 			if (check_subpartition_selections(r, a)) {
766 				save_subpartition_selections(r, a);
767 				expert = expert ? 0 : 1;
768 				dfui_response_free(r);
769 				return(-1);
770 			}
771 		} else {
772 			if (check_subpartition_selections(r, a)) {
773 				save_subpartition_selections(r, a);
774 				if (!warn_subpartition_selections(a) &&
775 				    !warn_encrypted_root(a)) {
776 					if (!create_subpartitions(a)) {
777 						inform(a->c, _("The subpartitions you chose were "
778 							"not correctly created, and the "
779 							"primary partition may "
780 							"now be in an inconsistent state. "
781 							"We recommend re-formatting it "
782 							"before proceeding."));
783 						dfui_response_free(r);
784 						return(0);
785 					} else {
786 						dfui_response_free(r);
787 						return(1);
788 					}
789 				}
790 			}
791 		}
792 
793 		dfui_form_datasets_free(f);
794 		/* dfui_form_datasets_add_from_response(f, r); */
795 		for (ds = dfui_response_dataset_get_first(r); ds != NULL;
796 		    ds = dfui_dataset_get_next(ds)) {
797 			dfui_form_dataset_add(f, dfui_dataset_dup(ds));
798 		}
799 	}
800 }
801 
802 void
803 fn_create_subpartitions_ufs(struct i_fn_args *a)
804 {
805 	struct dfui_form *f;
806 	int done = 0;
807 
808 	a->result = 0;
809 	while (!done) {
810 		f = make_create_subpartitions_form(a);
811 		switch (show_create_subpartitions_form(f, a)) {
812 		case -1:
813 			done = 0;
814 			break;
815 		case 0:
816 			done = 1;
817 			a->result = 0;
818 			break;
819 		case 1:
820 			done = 1;
821 			a->result = 1;
822 			break;
823 		}
824 		dfui_form_free(f);
825 	}
826 }
827