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 /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) / 4;
330 
331 #if 0
332 	/*
333 	 * No longer cap the size of /build, the assumption didn't hold
334 	 * well particularly with /var/crash being placed on /build now.
335 	 */
336 	if (build > BUILD_MAX)
337 		build = BUILD_MAX;
338 #endif
339 
340 	for (;;) {
341 		root = (capacity - swap - boot - build);
342 
343 		/*
344 		 * Adjust until the defaults look sane
345 		 *
346 		 * root should be at least twice as large as build
347 		 */
348 		if (build && root < build * 2) {
349 			--build;
350 			continue;
351 		}
352 
353 		/*
354 		 * root should be at least 1/2 capacity
355 		 */
356 		if (build && root < capacity / 2) {
357 			--build;
358 			continue;
359 		}
360 		break;
361 	}
362 
363 	/*
364 	 * Finalize.  If build is too small do not supply a /build,
365 	 * and if swap is too small do not supply swap.  Cascade the
366 	 * released space to swap and root.
367 	 */
368 	if (build < BUILD_MIN) {
369 		if (swap < SWAP_MIN && build >= SWAP_MIN - swap) {
370 			build -= SWAP_MIN - swap;
371 			swap = SWAP_MIN;
372 		}
373 		if (swap < 2 * mem && build >= 2 * mem - swap) {
374 			build -= 2 * mem - swap;
375 			swap = 2 * mem;
376 		}
377 		root += build;
378 		build = 0;
379 	}
380 	if (swap < SWAP_MIN) {
381 		root += swap;
382 		swap = 0;
383 	}
384 
385 	if (build == 0)
386 		root = -1;	/* root is the last part */
387 	else
388 		build = -1;	/* last partition just use remaining space */
389 
390 	if (strcmp(mtpt, "/boot") == 0)
391 		return(boot);
392 	else if (strcmp(mtpt, "/build") == 0)
393 		return(build);
394 	else if (strcmp(mtpt, "swap") == 0)
395 		return(swap);
396 	else if (strcmp(mtpt, "/") == 0)
397 		return(root);
398 
399 	/* shouldn't ever happen */
400 	return(-1);
401 }
402 
403 static int
404 check_capacity(struct i_fn_args *a)
405 {
406 	struct subpartition *sp;
407 	unsigned long total_capacity = 0;
408 	unsigned long remaining_capacity;
409 	int mtpt, warn_smallpart = 0;
410 	int good;
411 
412 	remaining_capacity = slice_get_capacity(
413 					storage_get_selected_slice(a->s));
414 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
415 	     sp != NULL; sp = subpartition_next(sp)) {
416 		if (subpartition_get_capacity(sp) != -1)
417 			remaining_capacity -= subpartition_get_capacity(sp);
418 	}
419 
420 	for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
421 	     sp != NULL; sp = subpartition_next(sp)) {
422 		long subpart_capacity = subpartition_get_capacity(sp);
423 		const char *mountpt = subpartition_get_mountpoint(sp);
424 
425 		if (subpart_capacity == -1)
426 			total_capacity++;
427 		else
428 			total_capacity += subpart_capacity;
429 		for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
430 			if (strcmp(mountpt, def_mountpt[mtpt]) == 0 &&
431 			    subpart_capacity < min_capacity[mtpt] &&
432 			    subpart_capacity != -1) {
433 				inform(a->c,
434 				  _("WARNING: The size (%ldM) specified for "
435 				    "the %s subpartition is too small. It "
436 				    "should be at least %ldM or you will "
437 				    "risk running out of space during "
438 				    "installation or operation."),
439 				    subpart_capacity, mountpt,
440 				    min_capacity[mtpt]);
441 			}
442 		}
443 		if (strcmp(mountpt, "/boot") != 0 &&
444 		    strcmp(mountpt, "swap") != 0) {
445 			if ((subpart_capacity == -1 &&
446 			     remaining_capacity < HAMMER_WARN) ||
447 			    (subpart_capacity != -1 &&
448 			     subpart_capacity < HAMMER_WARN)) {
449 				warn_smallpart++;
450 			}
451 		}
452 	}
453 
454 	if (total_capacity > slice_get_capacity(storage_get_selected_slice(a->s))) {
455 		inform(a->c, _("The space allocated to all of your selected "
456 		    "subpartitions (%luM) exceeds the total "
457 		    "capacity of the selected primary partition "
458 		    "(%luM). Remove some subpartitions or choose "
459 		    "a smaller size for them and try again."),
460 		    total_capacity,
461 		    slice_get_capacity(storage_get_selected_slice(a->s)));
462 		return(0);
463 	}
464 
465 	if (warn_smallpart) {
466 		good = confirm_dangerous_action(a->c,
467 			_("WARNING: Small HAMMER filesystems can fill up "
468 			  "very quickly!\n"
469 			  "You may have to run 'hammer prune-everything' and "
470 			  "'hammer reblock'\n"
471 			  "manually or often via a cron job, even if using a "
472 			  "nohistory mount.\n"
473 			  "For HAMMER2 you may have to run 'hammer2 bulkfree' "
474 			  "manually or often via a cron job.\n"));
475 	} else {
476 		good = 1;
477 	}
478 
479 	return (good);
480 }
481 
482 static int
483 check_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
484 {
485 	struct dfui_dataset *ds;
486 	struct dfui_dataset *star_ds = NULL;
487 	struct aura_dict *d;
488 	const char *mountpoint, *capstring;
489 	long capacity = 0;
490 	int found_root = 0;
491 	int valid = 1;
492 
493 	d = aura_dict_new(1, AURA_DICT_LIST);
494 
495 	if ((ds = dfui_response_dataset_get_first(r)) == NULL) {
496 		inform(a->c, _("Please set up at least one subpartition."));
497 		valid = 0;
498 	}
499 
500 	for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
501 	    ds = dfui_dataset_get_next(ds)) {
502 #ifdef DEBUG
503 		dfui_dataset_dump(ds);
504 #endif
505 		mountpoint = dfui_dataset_get_value(ds, "mountpoint");
506 		capstring = dfui_dataset_get_value(ds, "capacity");
507 
508 		if (aura_dict_exists(d, mountpoint, strlen(mountpoint) + 1)) {
509 			inform(a->c, _("The same mount point cannot be specified "
510 			    "for two different subpartitions."));
511 			valid = 0;
512 		}
513 
514 		if (strcmp(mountpoint, "/") == 0)
515 			found_root = 1;
516 
517 		if (strcmp(capstring, "*") == 0) {
518 			if (star_ds != NULL) {
519 				inform(a->c, _("You cannot have more than one subpartition "
520 				    "with a '*' capacity (meaning 'use the remainder "
521 				    "of the primary partition'.)"));
522 				valid = 0;
523 			} else {
524 				star_ds = ds;
525 			}
526 		}
527 
528 		if (!(!strcasecmp(mountpoint, "swap") || mountpoint[0] == '/')) {
529 			inform(a->c, _("Mount point must be either 'swap', or it must "
530 			    "start with a '/'."));
531 			valid = 0;
532 		}
533 
534 		if (strpbrk(mountpoint, " \\\"'`") != NULL) {
535 			inform(a->c, _("Mount point may not contain the following "
536 			    "characters: blank space, backslash, or "
537 			    "single, double, or back quotes."));
538 			valid = 0;
539 		}
540 
541 		if (strlen(capstring) == 0) {
542 			inform(a->c, _("A capacity must be specified."));
543 			valid = 0;
544 		}
545 
546 		if (!string_to_capacity(capstring, &capacity)) {
547 			inform(a->c, _("Capacity must be either a '*' symbol "
548 			    "to indicate 'use the rest of the primary "
549 			    "partition', or it must be a series of decimal "
550 			    "digits ending with an 'M' (indicating "
551 			    "megabytes), a 'G' (indicating gigabytes) and "
552 			    "so on (up to 'E'.)"));
553 			valid = 0;
554 		}
555 
556 		/*
557 		 * Maybe remove this limit entirely?
558 		 */
559 		if ((strcasecmp(mountpoint, "swap") == 0) &&
560 		    (capacity > SWAP_MAX)) {
561 			inform(a->c, _("Swap capacity is limited to %dG."),
562 			    SWAP_MAX / 1024);
563 			valid = 0;
564 		}
565 
566 		/*
567 		 * If we made it through that obstacle course, all is well.
568 		 */
569 
570 		if (valid)
571 			aura_dict_store(d, mountpoint, strlen(mountpoint) + 1, "", 1);
572 	}
573 
574 	if (!found_root) {
575 		inform(a->c, _("You must include a / (root) subpartition."));
576 		valid = 0;
577 	}
578 
579 	if (aura_dict_size(d) > 16) {
580 		inform(a->c, _("You cannot have more than 16 subpartitions "
581 		    "on a single primary partition.  Remove some "
582 		    "and try again."));
583 		valid = 0;
584 	}
585 
586 	aura_dict_free(d);
587 
588 	return(valid);
589 }
590 
591 static void
592 save_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
593 {
594 	struct dfui_dataset *ds;
595 	const char *mountpoint, *capstring;
596 	long capacity;
597 	int valid = 1;
598 
599 	subpartitions_free(storage_get_selected_slice(a->s));
600 
601 	for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
602 	    ds = dfui_dataset_get_next(ds)) {
603 		mountpoint = dfui_dataset_get_value(ds, "mountpoint");
604 		capstring = dfui_dataset_get_value(ds, "capacity");
605 
606 		if (string_to_capacity(capstring, &capacity)) {
607 			subpartition_new_hammer(storage_get_selected_slice(a->s),
608 			    mountpoint, capacity,
609 			    strcasecmp(dfui_dataset_get_value(ds, "encrypted"), "Y") == 0);
610 		}
611 	}
612 }
613 
614 static void
615 populate_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
616 {
617 	struct subpartition *sp;
618 	struct dfui_dataset *ds;
619 	int i;
620 	long capacity;
621 
622 	if (slice_subpartition_first(storage_get_selected_slice(a->s)) != NULL) {
623 		/*
624 		 * The user has already given us their subpartition
625 		 * preferences, so use them here.
626 		 */
627 		for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
628 		     sp != NULL; sp = subpartition_next(sp)) {
629 			ds = dfui_dataset_new();
630 			dfui_dataset_celldata_add(ds, "mountpoint",
631 			    subpartition_get_mountpoint(sp));
632 			dfui_dataset_celldata_add(ds, "capacity",
633 			    capacity_to_string(subpartition_get_capacity(sp)));
634 			dfui_dataset_celldata_add(ds, "encrypted",
635 			    subpartition_is_encrypted(sp) ? "Y" : "N");
636 			dfui_form_dataset_add(f, ds);
637 		}
638 	} else {
639 		/*
640 		 * Otherwise, populate the form with datasets representing
641 		 * reasonably-calculated defaults.  The defaults are chosen
642 		 * based on the slice's total capacity and the machine's
643 		 * total physical memory (for swap.)
644 		 */
645 		for (i = 0; def_mountpt[i] != NULL; i++) {
646 			capacity = default_capacity(a->s, def_mountpt[i]);
647 			if (capacity == 0)	/* used to disable /build */
648 				continue;
649 			ds = dfui_dataset_new();
650 			dfui_dataset_celldata_add(ds, "mountpoint",
651 			    def_mountpt[i]);
652 			dfui_dataset_celldata_add(ds, "capacity",
653 			    capacity_to_string(capacity));
654 			dfui_dataset_celldata_add(ds, "encrypted", "N");
655 			dfui_form_dataset_add(f, ds);
656 		}
657 	}
658 }
659 
660 static int
661 warn_subpartition_selections(struct i_fn_args *a)
662 {
663 	int valid = 0;
664 
665 	if (subpartition_find(storage_get_selected_slice(a->s), "/boot") == NULL) {
666 		inform(a->c, _("The /boot partition must not be omitted."));
667 	} else if (subpartition_find(storage_get_selected_slice(a->s), "/build") == NULL) {
668 		inform(a->c, _("Without a /build, things like /usr/obj and "
669 			       "/var/crash will just be on the root mount."));
670 		valid = check_capacity(a);
671 	} else {
672 		valid = check_capacity(a);
673 	}
674 
675 	return(!valid);
676 }
677 
678 static int
679 warn_encrypted_boot(struct i_fn_args *a)
680 {
681 	int valid = 1;
682 
683 	struct subpartition *sp;
684 
685 	sp = subpartition_find(storage_get_selected_slice(a->s), "/boot");
686 	if (sp == NULL)
687 		return(!valid);
688 
689 	if (subpartition_is_encrypted(sp)) {
690 		switch (dfui_be_present_dialog(a->c, _("/boot cannot be encrypted"),
691 		    _("Leave /boot unencrypted|Return to Create Subpartitions"),
692 		    _("You have selected encryption for the /boot partition which "
693 		    "is not supported."))) {
694 		case 1:
695 			subpartition_clr_encrypted(sp);
696 			valid = 1;
697 			break;
698 		case 2:
699 			valid = 0;
700 			break;
701 		default:
702 			abort_backend();
703 		}
704 	}
705 
706 	return(!valid);
707 }
708 
709 static struct dfui_form *
710 make_create_subpartitions_form(struct i_fn_args *a)
711 {
712 	struct dfui_form *f;
713 	char msg_buf[1][1024];
714 
715 	snprintf(msg_buf[0], sizeof(msg_buf[0]),
716 	    _("Subpartitions further divide a primary partition for "
717 	    "use with %s.  Some reasons you may want "
718 	    "a set of subpartitions are:\n\n"
719 	    "- you want to restrict how much data can be written "
720 	    "to certain parts of the primary partition, to quell "
721 	    "denial-of-service attacks; and\n"
722 	    "- you want to speed up access to data on the disk."
723 	    ""), OPERATING_SYSTEM_NAME);
724 
725 	f = dfui_form_create(
726 	    "create_subpartitions",
727 	    _("Create Subpartitions"),
728 	    _("Set up the subpartitions you want to have on this primary "
729 	    "partition. In most cases you should be fine with "
730 	    "the default settings."
731 	    " Note that /build will hold /usr/obj, /var/crash, and other"
732 	    " elements of the topology that do not need to be backed up."
733 	    " If no /build is specified, these dirs will be on the root."
734 	    "\n\n"
735 	    "For Capacity, use 'M' to indicate megabytes, 'G' to "
736 	    "indicate gigabytes, and so on (up to 'E'.) A single '*' "
737 	    "indicates 'use the remaining space on the primary partition'."),
738 
739 	    msg_buf[0],
740 
741 	    "p", "special", "dfinstaller_create_subpartitions",
742 	    "p", "minimum_width","64",
743 
744 	    "f", "mountpoint", _("Mountpoint"), "", "",
745 	    "f", "capacity", _("Capacity"), "", "",
746 
747 	    "f", "encrypted", _("Encrypted"), "", "",
748 	    "p", "control", "checkbox",
749 
750 	    "a", "ok", _("Accept and Create"), "", "",
751 	    "a", "cancel",
752 	    (disk_get_formatted(storage_get_selected_disk(a->s)) ?
753 	    _("Return to Select Disk") :
754 	    _("Return to Select Primary Partition")), "", "",
755 	    "p", "accelerator", "ESC",
756 
757 	    NULL
758 	);
759 
760 	dfui_form_set_multiple(f, 1);
761 	dfui_form_set_extensible(f, 1);
762 	/*
763 	 * Remove ATM until HAMMER installer support is better
764 	 * dfui_form_set_extensible(f, 1);
765 	 */
766 #if 0
767 	if (expert) {
768 		fi = dfui_form_field_add(f, "softupdates",
769 		    dfui_info_new(_("Softupdates"), "", ""));
770 		dfui_field_property_set(fi, "control", "checkbox");
771 
772 		fi = dfui_form_field_add(f, "tmpfsbacked",
773 		    dfui_info_new(_("TMPFS"), "", ""));
774 		dfui_field_property_set(fi, "control", "checkbox");
775 
776 		fi = dfui_form_field_add(f, "fsize",
777 		    dfui_info_new(_("Frag Sz"), "", ""));
778 
779 		fi = dfui_form_field_add(f, "bsize",
780 		    dfui_info_new(_("Block Sz"), "", ""));
781 
782 		dfui_form_action_add(f, "switch",
783 		    dfui_info_new(_("Switch to Normal Mode"), "", ""));
784 	} else {
785 		dfui_form_action_add(f, "switch",
786 		    dfui_info_new(_("Switch to Expert Mode"), "", ""));
787 	}
788 #endif
789 	return(f);
790 }
791 
792 /*
793  * Returns:
794  *	-1 = the form should be redisplayed
795  *	 0 = failure, function is over
796  *	 1 = success, function is over
797  */
798 static int
799 show_create_subpartitions_form(int which, struct dfui_form *f,
800 			       struct i_fn_args *a)
801 {
802 	struct dfui_dataset *ds;
803 	struct dfui_response *r;
804 
805 	for (;;) {
806 		if (dfui_form_dataset_get_first(f) == NULL)
807 			populate_create_subpartitions_form(f, a);
808 
809 		if (!dfui_be_present(a->c, f, &r))
810 			abort_backend();
811 
812 		if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
813 			dfui_response_free(r);
814 			return(0);
815 		} else if (strcmp(dfui_response_get_action_id(r), "switch") == 0) {
816 			if (check_subpartition_selections(r, a)) {
817 				save_subpartition_selections(r, a);
818 				expert = expert ? 0 : 1;
819 				dfui_response_free(r);
820 				return(-1);
821 			}
822 		} else {
823 			if (check_subpartition_selections(r, a)) {
824 				save_subpartition_selections(r, a);
825 				if (!warn_subpartition_selections(a) &&
826 				    !warn_encrypted_boot(a)) {
827 					if (!create_subpartitions(which, a)) {
828 						inform(a->c, _("The subpartitions you chose were "
829 							"not correctly created, and the "
830 							"primary partition may "
831 							"now be in an inconsistent state. "
832 							"We recommend re-formatting it "
833 							"before proceeding."));
834 						dfui_response_free(r);
835 						return(0);
836 					} else {
837 						dfui_response_free(r);
838 						return(1);
839 					}
840 				}
841 			}
842 		}
843 
844 		dfui_form_datasets_free(f);
845 		/* dfui_form_datasets_add_from_response(f, r); */
846 		for (ds = dfui_response_dataset_get_first(r); ds != NULL;
847 		    ds = dfui_dataset_get_next(ds)) {
848 			dfui_form_dataset_add(f, dfui_dataset_dup(ds));
849 		}
850 	}
851 }
852 
853 /*
854  * fn_create_subpartitions_hammer: let the user specify what subpartitions they
855  * want on the disk, how large each should be, and where it should be mounted.
856  */
857 void
858 fn_create_subpartitions_hammer(int which, struct i_fn_args *a)
859 {
860 	struct dfui_form *f;
861 	unsigned long capacity;
862 	int done = 0;
863 
864 	a->result = 0;
865 	capacity = disk_get_capacity(storage_get_selected_disk(a->s));
866 	if (which == FS_HAMMER && capacity < HAMMER_MIN) {
867 		inform(a->c, _("The selected %dM disk is smaller than the "
868 		    "required %dM for the HAMMER filesystem."),
869 		    (int)capacity,
870 		    (int)HAMMER_MIN);
871 		return;
872 	}
873 	while (!done) {
874 		f = make_create_subpartitions_form(a);
875 		switch (show_create_subpartitions_form(which, f, a)) {
876 		case -1:
877 			done = 0;
878 			break;
879 		case 0:
880 			done = 1;
881 			a->result = 0;
882 			break;
883 		case 1:
884 			done = 1;
885 			a->result = 1;
886 			break;
887 		}
888 		dfui_form_free(f);
889 	}
890 }
891 
892 static
893 char *
894 construct_lname(const char *mtpt)
895 {
896 	char *res;
897 	int i;
898 
899 	if (strcmp(mtpt, "/") == 0) {
900 		res = strdup("ROOT");
901 	} else {
902 		if (strrchr(mtpt, '/'))
903 			mtpt = strrchr(mtpt, '/') + 1;
904 		if (*mtpt == 0)
905 			mtpt = "unknown";
906 		res = malloc(strlen(mtpt) + 1);
907 		for (i = 0; mtpt[i]; ++i)
908 			res[i] = toupper(mtpt[i]);
909 		res[i] = 0;
910 	}
911 	return res;
912 }
913