1 /*
2  * Copyright (c) 2008 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_hammer.c
36  * Installer Function : Create HAMMER or HAMMER2 Subpartitions.
37  */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <assert.h>
44 
45 #ifdef ENABLE_NLS
46 #include <libintl.h>
47 #define _(String) gettext (String)
48 #else
49 #define _(String) (String)
50 #endif
51 
52 #include "libaura/mem.h"
53 #include "libaura/buffer.h"
54 #include "libaura/dict.h"
55 #include "libaura/fspred.h"
56 
57 #include "libdfui/dfui.h"
58 #include "libdfui/dump.h"
59 #include "libdfui/system.h"
60 
61 #include "libinstaller/commands.h"
62 #include "libinstaller/diskutil.h"
63 #include "libinstaller/functions.h"
64 #include "libinstaller/uiutil.h"
65 
66 #include "fn.h"
67 #include "flow.h"
68 #include "pathnames.h"
69 
70 static int	create_subpartitions(int which, struct i_fn_args *);
71 static long	default_capacity(struct storage *, const char *);
72 static int	check_capacity(struct i_fn_args *);
73 static int	check_subpartition_selections(struct dfui_response *,
74 			struct i_fn_args *);
75 static void	save_subpartition_selections(struct dfui_response *,
76 			struct i_fn_args *);
77 static void	populate_create_subpartitions_form(struct dfui_form *,
78 			struct i_fn_args *);
79 static int	warn_subpartition_selections(struct i_fn_args *);
80 static int	warn_encrypted_boot(struct i_fn_args *);
81 static struct dfui_form *make_create_subpartitions_form(struct i_fn_args *);
82 static int	show_create_subpartitions_form(int which, struct dfui_form *,
83 			struct i_fn_args *);
84 static char	*construct_lname(const char *mtpt);
85 
86 static const char *def_mountpt[]  = {"/boot", "swap", "/", "/build", NULL};
87 static long min_capacity[] = { 128, 0, DISK_MIN - 128, BUILD_MIN };
88 static int expert = 0;
89 
90 /*
91  * Given a set of subpartitions-to-be in the selected slice,
92  * create them.
93  */
94 static int
95 create_subpartitions(int which, struct i_fn_args *a)
96 {
97 	struct subpartition *sp;
98 	struct commands *cmds;
99 	int result = 0;
100 	int num_partitions;
101 	const char *whichfs;
102 
103 	switch(which) {
104 	case FS_HAMMER:
105 		whichfs = "HAMMER";
106 		break;
107 	case FS_HAMMER2:
108 		whichfs = "HAMMER2";
109 		break;
110 	default:
111 		whichfs = NULL;
112 		assert(0);
113 	}
114 
115 	cmds = commands_new();
116 	if (!is_file("%sinstall.disklabel.%s",
117 	    a->tmp,
118 	    slice_get_device_name(storage_get_selected_slice(a->s)))) {
119 		/*
120 		 * Get a copy of the 'virgin' disklabel.
121 		 * XXX It might make more sense for this to
122 		 * happen right after format_slice() instead.
123 		 */
124 		command_add(cmds, "%s%s -r %s >%sinstall.disklabel.%s",
125 		    a->os_root, cmd_name(a, "DISKLABEL64"),
126 		    slice_get_device_name(storage_get_selected_slice(a->s)),
127 		    a->tmp,
128 		    slice_get_device_name(storage_get_selected_slice(a->s)));
129 	}
130 
131 	/*
132 	 * Weave together a new disklabel out the of the 'virgin'
133 	 * disklabel, and the user's subpartition choices.
134 	 */
135 
136 	/*
137 	 * Take everything from the 'virgin' disklabel up until the
138 	 * '16 partitions' line.
139 	 */
140 	num_partitions = 16;
141 	command_add(cmds,
142 	    "%s%s '$2==\"partitions:\" || "
143 	    "cut { cut = 1 } !cut { print $0 }' "
144 	    "<%sinstall.disklabel.%s >%sinstall.disklabel",
145 	    a->os_root, cmd_name(a, "AWK"),
146 	    a->tmp,
147 	    slice_get_device_name(storage_get_selected_slice(a->s)),
148 	    a->tmp);
149 
150 	/*
151 	 * 16 partitions:
152 	 * #          size     offset    fstype
153 	 *   c:   16383969          0    unused	#    7999.985MB
154 	 */
155 
156 	command_add(cmds, "%s%s '%d partitions:' >>%sinstall.disklabel",
157 	    a->os_root, cmd_name(a, "ECHO"), num_partitions ,a->tmp);
158 	command_add(cmds, "%s%s '%s' >>%sinstall.disklabel",
159 	    a->os_root, cmd_name(a, "ECHO"),
160 	    "#          size     offset    fstype",
161 	    a->tmp);
162 
163 #ifdef DEBUG
164 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
165 	     sp != NULL; sp = subpartition_next(sp)) {
166 		command_add(cmds, "%s%s 'mountpoint: %s device: %s'",
167 		     a->os_root, cmd_name(a, "ECHO"),
168 		     subpartition_get_mountpoint(sp),
169 		     subpartition_get_device_name(sp));
170 	}
171 #endif
172 
173 	/*
174 	 * Write a line for each subpartition the user wants.
175 	 */
176 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
177 	     sp != NULL; sp = subpartition_next(sp)) {
178 		if (subpartition_is_tmpfsbacked(sp)) {
179 			continue;
180 		}
181 		if (subpartition_is_swap(sp)) {
182 			command_add(cmds,
183 			    "%s%s '  %c:\t%s\t*\tswap' "
184 			    ">>%sinstall.disklabel",
185 			    a->os_root, cmd_name(a, "ECHO"),
186 			    subpartition_get_letter(sp),
187 			    capacity_to_string(subpartition_get_capacity(sp)),
188 			    a->tmp);
189 		} else if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0) {
190 			command_add(cmds,
191 			    "%s%s '  %c:\t%s\t0\t4.2BSD' "
192 			    ">>%sinstall.disklabel",
193 			    a->os_root, cmd_name(a, "ECHO"),
194 			    subpartition_get_letter(sp),
195 			    capacity_to_string(subpartition_get_capacity(sp)),
196 			    a->tmp);
197 		} else {
198 			command_add(cmds,
199 			    "%s%s '  %c:\t%s\t*\t%s' "
200 			    ">>%sinstall.disklabel",
201 			    a->os_root, cmd_name(a, "ECHO"),
202 			    subpartition_get_letter(sp),
203 			    capacity_to_string(subpartition_get_capacity(sp)),
204 			    whichfs, a->tmp);
205 		}
206 	}
207 	temp_file_add(a, "install.disklabel");
208 
209 	/*
210 	 * Label the slice from the disklabel we just wove together.
211 	 */
212 	command_add(cmds, "%s%s -R -B -r %s %sinstall.disklabel",
213 	    a->os_root, cmd_name(a, "DISKLABEL64"),
214 	    slice_get_device_name(storage_get_selected_slice(a->s)),
215 	    a->tmp);
216 
217 	/*
218 	 * Create a snapshot of the disklabel we just created
219 	 * for debugging inspection in the log.
220 	 */
221 	command_add(cmds, "%s%s %s",
222 	    a->os_root, cmd_name(a, "DISKLABEL64"),
223 	    slice_get_device_name(storage_get_selected_slice(a->s)));
224 
225 	/*
226 	 * If encryption was specified, load dm(4).
227 	 */
228 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
229 	     sp != NULL; sp = subpartition_next(sp)) {
230 		if (subpartition_is_encrypted(sp)) {
231 			fn_get_passphrase(a);
232 			break;
233 		}
234 	}
235 
236 	/*
237 	 * Create filesystems on the newly-created subpartitions.
238 	 */
239 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
240 	     sp != NULL; sp = subpartition_next(sp)) {
241 		if (subpartition_is_swap(sp) ||
242 		    subpartition_is_tmpfsbacked(sp)) {
243 			if (subpartition_is_swap(sp) &&
244 			    subpartition_is_encrypted(sp)) {
245 				command_add(cmds,
246 				    "%s%s -d /tmp/t1 luksFormat /dev/%s",
247 				    a->os_root, cmd_name(a, "CRYPTSETUP"),
248 				    subpartition_get_device_name(sp));
249 				command_add(cmds,
250 				    "%s%s -d /tmp/t1 luksOpen /dev/%s swap",
251 				    a->os_root, cmd_name(a, "CRYPTSETUP"),
252 				    subpartition_get_device_name(sp));
253 			}
254 			continue;
255 		}
256 
257 		if (strcmp(subpartition_get_mountpoint(sp), "/boot") == 0) {
258 			command_add(cmds, "%s%s -i 65536 /dev/%s",
259 			    a->os_root, cmd_name(a, "NEWFS"),
260 			    subpartition_get_device_name(sp));
261 		} else {
262 			char *ham_name;
263 			if (subpartition_is_encrypted(sp)) {
264 				command_add(cmds,
265 				    "%s%s -d /tmp/t1 luksFormat /dev/%s",
266 				    a->os_root, cmd_name(a, "CRYPTSETUP"),
267 				    subpartition_get_device_name(sp));
268 				command_add(cmds,
269 				    "%s%s -d /tmp/t1 luksOpen /dev/%s %s",
270 				    a->os_root, cmd_name(a, "CRYPTSETUP"),
271 				    subpartition_get_device_name(sp),
272 				    subpartition_get_mapper_name(sp, -1));
273 			}
274 
275 			if (which == FS_HAMMER) {
276 				ham_name = construct_lname(
277 					      subpartition_get_mountpoint(sp));
278 				command_add(cmds, "%s%s -f -L %s /dev/%s",
279 				    a->os_root, cmd_name(a, "NEWFS_HAMMER"),
280 				    ham_name,
281 				    (subpartition_is_encrypted(sp) ?
282 					subpartition_get_mapper_name(sp, 0) :
283 					subpartition_get_device_name(sp)));
284 				free(ham_name);
285 			} else {
286 				command_add(cmds, "%s%s -f /dev/%s",
287 				    a->os_root, cmd_name(a, "NEWFS_HAMMER2"),
288 				    (subpartition_is_encrypted(sp) ?
289 					subpartition_get_mapper_name(sp, 0) :
290 					subpartition_get_device_name(sp)));
291 			}
292 		}
293 	}
294 
295 	result = commands_execute(a, cmds);
296 	commands_free(cmds);
297 	return(result);
298 }
299 
300 /*
301  * Return default capacity field filler.  Return 0 for /build if drive
302  * space minus swap is < 40GB (causes installer to use PFS's on the root
303  * partition instead).
304  */
305 static long
306 default_capacity(struct storage *s, const char *mtpt)
307 {
308 	unsigned long boot, root, swap, build;
309 	unsigned long capacity;
310 	unsigned long mem;
311 
312 	capacity = slice_get_capacity(storage_get_selected_slice(s)); /* MB */
313 	mem = storage_get_memsize(s);
314 
315 	/*
316 	 * Slice capacity is at least 10G at this point.  Calculate basic
317 	 * defaults.
318 	 */
319 	swap = 2 * mem;
320 	if (swap > capacity / 10)	/* max 1/10 capacity */
321 		swap = capacity / 10;
322 	if (swap < SWAP_MIN)		/* having a little is nice */
323 		swap = SWAP_MIN;
324 	if (swap > SWAP_MAX)		/* installer cap */
325 		swap = SWAP_MAX;
326 
327 	boot = 1024;
328 
329 	build = (capacity - swap - boot) / 3;
330 	if (build > BUILD_MAX)
331 		build = BUILD_MAX;
332 
333 	for (;;) {
334 		root = (capacity - swap - boot - build);
335 
336 		/*
337 		 * Adjust until the defaults look sane
338 		 *
339 		 * root should be at least twice as large as build
340 		 */
341 		if (build && root < build * 2) {
342 			--build;
343 			continue;
344 		}
345 
346 		/*
347 		 * root should be at least 1/2 capacity
348 		 */
349 		if (build && root < capacity / 2) {
350 			--build;
351 			continue;
352 		}
353 		break;
354 	}
355 
356 	/*
357 	 * Finalize.  If build is too small do not supply a /build,
358 	 * and if swap is too small do not supply swap.
359 	 */
360 	if (build < BUILD_MIN)
361 		build = 0;
362 	if (swap < SWAP_MIN)
363 		swap = 0;
364 	if (build == 0)
365 		root = -1;	/* root is the last part */
366 	else
367 		build = -1;	/* last partition just use remaining space */
368 
369 	if (strcmp(mtpt, "/boot") == 0)
370 		return(boot);
371 	else if (strcmp(mtpt, "/build") == 0)
372 		return(build);
373 	else if (strcmp(mtpt, "swap") == 0)
374 		return(swap);
375 	else if (strcmp(mtpt, "/") == 0)
376 		return(root);
377 
378 	/* shouldn't ever happen */
379 	return(-1);
380 }
381 
382 static int
383 check_capacity(struct i_fn_args *a)
384 {
385 	struct subpartition *sp;
386 	unsigned long total_capacity = 0;
387 	unsigned long remaining_capacity;
388 	int mtpt, warn_smallpart = 0;
389 	int good;
390 
391 	remaining_capacity = slice_get_capacity(
392 					storage_get_selected_slice(a->s));
393 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
394 	     sp != NULL; sp = subpartition_next(sp)) {
395 		if (subpartition_get_capacity(sp) != -1)
396 			remaining_capacity -= subpartition_get_capacity(sp);
397 	}
398 
399 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
400 	     sp != NULL; sp = subpartition_next(sp)) {
401 		long subpart_capacity = subpartition_get_capacity(sp);
402 		const char *mountpt = subpartition_get_mountpoint(sp);
403 
404 		if (subpart_capacity == -1)
405 			total_capacity++;
406 		else
407 			total_capacity += subpart_capacity;
408 		for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
409 			if (strcmp(mountpt, def_mountpt[mtpt]) == 0 &&
410 			    subpart_capacity < min_capacity[mtpt] &&
411 			    subpart_capacity != -1) {
412 				inform(a->c,
413 				  _("WARNING: The size (%ldM) specified for "
414 				    "the %s subpartition is too small. It "
415 				    "should be at least %ldM or you will "
416 				    "risk running out of space during "
417 				    "installation or operation."),
418 				    subpart_capacity, mountpt,
419 				    min_capacity[mtpt]);
420 			}
421 		}
422 		if (strcmp(mountpt, "/boot") != 0 &&
423 		    strcmp(mountpt, "swap") != 0) {
424 			if ((subpart_capacity == -1 &&
425 			     remaining_capacity < HAMMER_WARN) ||
426 			    (subpart_capacity != -1 &&
427 			     subpart_capacity < HAMMER_WARN)) {
428 				warn_smallpart++;
429 			}
430 		}
431 	}
432 
433 	if (total_capacity > slice_get_capacity(storage_get_selected_slice(a->s))) {
434 		inform(a->c, _("The space allocated to all of your selected "
435 		    "subpartitions (%luM) exceeds the total "
436 		    "capacity of the selected primary partition "
437 		    "(%luM). Remove some subpartitions or choose "
438 		    "a smaller size for them and try again."),
439 		    total_capacity,
440 		    slice_get_capacity(storage_get_selected_slice(a->s)));
441 		return(0);
442 	}
443 
444 	if (warn_smallpart) {
445 		good = confirm_dangerous_action(a->c,
446 			_("WARNING: Small HAMMER filesystems can fill up "
447 			  "very quickly!\n"
448 			  "You may have to run 'hammer prune-everything' and "
449 			  "'hammer reblock'\n"
450 			  "manually or often via a cron job, even if using a "
451 			  "nohistory mount.\n"
452 			  "For HAMMER2 you may have to run 'hammer2 bulkfree' "
453 			  "manually or often via a cron job.\n"));
454 	} else {
455 		good = 1;
456 	}
457 
458 	return (good);
459 }
460 
461 static int
462 check_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
463 {
464 	struct dfui_dataset *ds;
465 	struct dfui_dataset *star_ds = NULL;
466 	struct aura_dict *d;
467 	const char *mountpoint, *capstring;
468 	long capacity = 0;
469 	int found_root = 0;
470 	int valid = 1;
471 
472 	d = aura_dict_new(1, AURA_DICT_LIST);
473 
474 	if ((ds = dfui_response_dataset_get_first(r)) == NULL) {
475 		inform(a->c, _("Please set up at least one subpartition."));
476 		valid = 0;
477 	}
478 
479 	for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
480 	    ds = dfui_dataset_get_next(ds)) {
481 #ifdef DEBUG
482 		dfui_dataset_dump(ds);
483 #endif
484 		mountpoint = dfui_dataset_get_value(ds, "mountpoint");
485 		capstring = dfui_dataset_get_value(ds, "capacity");
486 
487 		if (aura_dict_exists(d, mountpoint, strlen(mountpoint) + 1)) {
488 			inform(a->c, _("The same mount point cannot be specified "
489 			    "for two different subpartitions."));
490 			valid = 0;
491 		}
492 
493 		if (strcmp(mountpoint, "/") == 0)
494 			found_root = 1;
495 
496 		if (strcmp(capstring, "*") == 0) {
497 			if (star_ds != NULL) {
498 				inform(a->c, _("You cannot have more than one subpartition "
499 				    "with a '*' capacity (meaning 'use the remainder "
500 				    "of the primary partition'.)"));
501 				valid = 0;
502 			} else {
503 				star_ds = ds;
504 			}
505 		}
506 
507 		if (!(!strcasecmp(mountpoint, "swap") || mountpoint[0] == '/')) {
508 			inform(a->c, _("Mount point must be either 'swap', or it must "
509 			    "start with a '/'."));
510 			valid = 0;
511 		}
512 
513 		if (strpbrk(mountpoint, " \\\"'`") != NULL) {
514 			inform(a->c, _("Mount point may not contain the following "
515 			    "characters: blank space, backslash, or "
516 			    "single, double, or back quotes."));
517 			valid = 0;
518 		}
519 
520 		if (strlen(capstring) == 0) {
521 			inform(a->c, _("A capacity must be specified."));
522 			valid = 0;
523 		}
524 
525 		if (!string_to_capacity(capstring, &capacity)) {
526 			inform(a->c, _("Capacity must be either a '*' symbol "
527 			    "to indicate 'use the rest of the primary "
528 			    "partition', or it must be a series of decimal "
529 			    "digits ending with an 'M' (indicating "
530 			    "megabytes), a 'G' (indicating gigabytes) and "
531 			    "so on (up to 'E'.)"));
532 			valid = 0;
533 		}
534 
535 		/*
536 		 * Maybe remove this limit entirely?
537 		 */
538 		if ((strcasecmp(mountpoint, "swap") == 0) &&
539 		    (capacity > SWAP_MAX)) {
540 			inform(a->c, _("Swap capacity is limited to %dG."),
541 			    SWAP_MAX / 1024);
542 			valid = 0;
543 		}
544 
545 		/*
546 		 * If we made it through that obstacle course, all is well.
547 		 */
548 
549 		if (valid)
550 			aura_dict_store(d, mountpoint, strlen(mountpoint) + 1, "", 1);
551 	}
552 
553 	if (!found_root) {
554 		inform(a->c, _("You must include a / (root) subpartition."));
555 		valid = 0;
556 	}
557 
558 	if (aura_dict_size(d) > 16) {
559 		inform(a->c, _("You cannot have more than 16 subpartitions "
560 		    "on a single primary partition.  Remove some "
561 		    "and try again."));
562 		valid = 0;
563 	}
564 
565 	aura_dict_free(d);
566 
567 	return(valid);
568 }
569 
570 static void
571 save_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
572 {
573 	struct dfui_dataset *ds;
574 	const char *mountpoint, *capstring;
575 	long capacity;
576 	int valid = 1;
577 
578 	subpartitions_free(storage_get_selected_slice(a->s));
579 
580 	for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
581 	    ds = dfui_dataset_get_next(ds)) {
582 		mountpoint = dfui_dataset_get_value(ds, "mountpoint");
583 		capstring = dfui_dataset_get_value(ds, "capacity");
584 
585 		if (string_to_capacity(capstring, &capacity)) {
586 			subpartition_new_hammer(storage_get_selected_slice(a->s),
587 			    mountpoint, capacity,
588 			    strcasecmp(dfui_dataset_get_value(ds, "encrypted"), "Y") == 0);
589 		}
590 	}
591 }
592 
593 static void
594 populate_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
595 {
596 	struct subpartition *sp;
597 	struct dfui_dataset *ds;
598 	int i;
599 	long capacity;
600 
601 	if (slice_subpartition_first(storage_get_selected_slice(a->s)) != NULL) {
602 		/*
603 		 * The user has already given us their subpartition
604 		 * preferences, so use them here.
605 		 */
606 		for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
607 		     sp != NULL; sp = subpartition_next(sp)) {
608 			ds = dfui_dataset_new();
609 			dfui_dataset_celldata_add(ds, "mountpoint",
610 			    subpartition_get_mountpoint(sp));
611 			dfui_dataset_celldata_add(ds, "capacity",
612 			    capacity_to_string(subpartition_get_capacity(sp)));
613 			dfui_dataset_celldata_add(ds, "encrypted",
614 			    subpartition_is_encrypted(sp) ? "Y" : "N");
615 			dfui_form_dataset_add(f, ds);
616 		}
617 	} else {
618 		/*
619 		 * Otherwise, populate the form with datasets representing
620 		 * reasonably-calculated defaults.  The defaults are chosen
621 		 * based on the slice's total capacity and the machine's
622 		 * total physical memory (for swap.)
623 		 */
624 		for (i = 0; def_mountpt[i] != NULL; i++) {
625 			capacity = default_capacity(a->s, def_mountpt[i]);
626 			if (capacity == 0)	/* used to disable /build */
627 				continue;
628 			ds = dfui_dataset_new();
629 			dfui_dataset_celldata_add(ds, "mountpoint",
630 			    def_mountpt[i]);
631 			dfui_dataset_celldata_add(ds, "capacity",
632 			    capacity_to_string(capacity));
633 			dfui_dataset_celldata_add(ds, "encrypted", "N");
634 			dfui_form_dataset_add(f, ds);
635 		}
636 	}
637 }
638 
639 static int
640 warn_subpartition_selections(struct i_fn_args *a)
641 {
642 	int valid = 0;
643 
644 	if (subpartition_find(storage_get_selected_slice(a->s), "/boot") == NULL) {
645 		inform(a->c, _("The /boot partition must not be omitted."));
646 	} else if (subpartition_find(storage_get_selected_slice(a->s), "/build") == NULL) {
647 		inform(a->c, _("Without a /build, things like /usr/obj and "
648 			       "/var/crash will just be on the root mount."));
649 		valid = check_capacity(a);
650 	} else {
651 		valid = check_capacity(a);
652 	}
653 
654 	return(!valid);
655 }
656 
657 static int
658 warn_encrypted_boot(struct i_fn_args *a)
659 {
660 	int valid = 1;
661 
662 	struct subpartition *sp;
663 
664 	sp = subpartition_find(storage_get_selected_slice(a->s), "/boot");
665 	if (sp == NULL)
666 		return(!valid);
667 
668 	if (subpartition_is_encrypted(sp)) {
669 		switch (dfui_be_present_dialog(a->c, _("/boot cannot be encrypted"),
670 		    _("Leave /boot unencrypted|Return to Create Subpartitions"),
671 		    _("You have selected encryption for the /boot partition which "
672 		    "is not supported."))) {
673 		case 1:
674 			subpartition_clr_encrypted(sp);
675 			valid = 1;
676 			break;
677 		case 2:
678 			valid = 0;
679 			break;
680 		default:
681 			abort_backend();
682 		}
683 	}
684 
685 	return(!valid);
686 }
687 
688 static struct dfui_form *
689 make_create_subpartitions_form(struct i_fn_args *a)
690 {
691 	struct dfui_form *f;
692 	char msg_buf[1][1024];
693 
694 	snprintf(msg_buf[0], sizeof(msg_buf[0]),
695 	    _("Subpartitions further divide a primary partition for "
696 	    "use with %s.  Some reasons you may want "
697 	    "a set of subpartitions are:\n\n"
698 	    "- you want to restrict how much data can be written "
699 	    "to certain parts of the primary partition, to quell "
700 	    "denial-of-service attacks; and\n"
701 	    "- you want to speed up access to data on the disk."
702 	    ""), OPERATING_SYSTEM_NAME);
703 
704 	f = dfui_form_create(
705 	    "create_subpartitions",
706 	    _("Create Subpartitions"),
707 	    _("Set up the subpartitions you want to have on this primary "
708 	    "partition. In most cases you should be fine with "
709 	    "the default settings."
710 	    " Note that /build will hold /usr/obj, /var/crash, and other"
711 	    " elements of the topology that do not need to be backed up."
712 	    " If no /build is specified, these dirs will be on the root."
713 	    "\n\n"
714 	    "For Capacity, use 'M' to indicate megabytes, 'G' to "
715 	    "indicate gigabytes, and so on (up to 'E'.) A single '*' "
716 	    "indicates 'use the remaining space on the primary partition'."),
717 
718 	    msg_buf[0],
719 
720 	    "p", "special", "dfinstaller_create_subpartitions",
721 	    "p", "minimum_width","64",
722 
723 	    "f", "mountpoint", _("Mountpoint"), "", "",
724 	    "f", "capacity", _("Capacity"), "", "",
725 
726 	    "f", "encrypted", _("Encrypted"), "", "",
727 	    "p", "control", "checkbox",
728 
729 	    "a", "ok", _("Accept and Create"), "", "",
730 	    "a", "cancel",
731 	    (disk_get_formatted(storage_get_selected_disk(a->s)) ?
732 	    _("Return to Select Disk") :
733 	    _("Return to Select Primary Partition")), "", "",
734 	    "p", "accelerator", "ESC",
735 
736 	    NULL
737 	);
738 
739 	dfui_form_set_multiple(f, 1);
740 	dfui_form_set_extensible(f, 1);
741 	/*
742 	 * Remove ATM until HAMMER installer support is better
743 	 * dfui_form_set_extensible(f, 1);
744 	 */
745 #if 0
746 	if (expert) {
747 		fi = dfui_form_field_add(f, "softupdates",
748 		    dfui_info_new(_("Softupdates"), "", ""));
749 		dfui_field_property_set(fi, "control", "checkbox");
750 
751 		fi = dfui_form_field_add(f, "tmpfsbacked",
752 		    dfui_info_new(_("TMPFS"), "", ""));
753 		dfui_field_property_set(fi, "control", "checkbox");
754 
755 		fi = dfui_form_field_add(f, "fsize",
756 		    dfui_info_new(_("Frag Sz"), "", ""));
757 
758 		fi = dfui_form_field_add(f, "bsize",
759 		    dfui_info_new(_("Block Sz"), "", ""));
760 
761 		dfui_form_action_add(f, "switch",
762 		    dfui_info_new(_("Switch to Normal Mode"), "", ""));
763 	} else {
764 		dfui_form_action_add(f, "switch",
765 		    dfui_info_new(_("Switch to Expert Mode"), "", ""));
766 	}
767 #endif
768 	return(f);
769 }
770 
771 /*
772  * Returns:
773  *	-1 = the form should be redisplayed
774  *	 0 = failure, function is over
775  *	 1 = success, function is over
776  */
777 static int
778 show_create_subpartitions_form(int which, struct dfui_form *f,
779 			       struct i_fn_args *a)
780 {
781 	struct dfui_dataset *ds;
782 	struct dfui_response *r;
783 
784 	for (;;) {
785 		if (dfui_form_dataset_get_first(f) == NULL)
786 			populate_create_subpartitions_form(f, a);
787 
788 		if (!dfui_be_present(a->c, f, &r))
789 			abort_backend();
790 
791 		if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
792 			dfui_response_free(r);
793 			return(0);
794 		} else if (strcmp(dfui_response_get_action_id(r), "switch") == 0) {
795 			if (check_subpartition_selections(r, a)) {
796 				save_subpartition_selections(r, a);
797 				expert = expert ? 0 : 1;
798 				dfui_response_free(r);
799 				return(-1);
800 			}
801 		} else {
802 			if (check_subpartition_selections(r, a)) {
803 				save_subpartition_selections(r, a);
804 				if (!warn_subpartition_selections(a) &&
805 				    !warn_encrypted_boot(a)) {
806 					if (!create_subpartitions(which, a)) {
807 						inform(a->c, _("The subpartitions you chose were "
808 							"not correctly created, and the "
809 							"primary partition may "
810 							"now be in an inconsistent state. "
811 							"We recommend re-formatting it "
812 							"before proceeding."));
813 						dfui_response_free(r);
814 						return(0);
815 					} else {
816 						dfui_response_free(r);
817 						return(1);
818 					}
819 				}
820 			}
821 		}
822 
823 		dfui_form_datasets_free(f);
824 		/* dfui_form_datasets_add_from_response(f, r); */
825 		for (ds = dfui_response_dataset_get_first(r); ds != NULL;
826 		    ds = dfui_dataset_get_next(ds)) {
827 			dfui_form_dataset_add(f, dfui_dataset_dup(ds));
828 		}
829 	}
830 }
831 
832 /*
833  * fn_create_subpartitions_hammer: let the user specify what subpartitions they
834  * want on the disk, how large each should be, and where it should be mounted.
835  */
836 void
837 fn_create_subpartitions_hammer(int which, struct i_fn_args *a)
838 {
839 	struct dfui_form *f;
840 	unsigned long capacity;
841 	int done = 0;
842 
843 	a->result = 0;
844 	capacity = disk_get_capacity(storage_get_selected_disk(a->s));
845 	if (which == FS_HAMMER && capacity < HAMMER_MIN) {
846 		inform(a->c, _("The selected %dM disk is smaller than the "
847 		    "required %dM for the HAMMER filesystem."),
848 		    (int)capacity,
849 		    (int)HAMMER_MIN);
850 		return;
851 	}
852 	while (!done) {
853 		f = make_create_subpartitions_form(a);
854 		switch (show_create_subpartitions_form(which, f, a)) {
855 		case -1:
856 			done = 0;
857 			break;
858 		case 0:
859 			done = 1;
860 			a->result = 0;
861 			break;
862 		case 1:
863 			done = 1;
864 			a->result = 1;
865 			break;
866 		}
867 		dfui_form_free(f);
868 	}
869 }
870 
871 static
872 char *
873 construct_lname(const char *mtpt)
874 {
875 	char *res;
876 	int i;
877 
878 	if (strcmp(mtpt, "/") == 0) {
879 		res = strdup("ROOT");
880 	} else {
881 		if (strrchr(mtpt, '/'))
882 			mtpt = strrchr(mtpt, '/') + 1;
883 		if (*mtpt == 0)
884 			mtpt = "unknown";
885 		res = malloc(strlen(mtpt) + 1);
886 		for (i = 0; mtpt[i]; ++i)
887 			res[i] = toupper(mtpt[i]);
888 		res[i] = 0;
889 	}
890 	return res;
891 }
892