installer: Check the user-entered swap size against our actual limits, too.
[dragonfly.git] / usr.sbin / installer / dfuibe_installer / fn_subpart_ufs.c
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 %sdev/%s",
212                                     a->os_root, cmd_name(a, "CRYPTSETUP"),
213                                     a->os_root,
214                                     subpartition_get_device_name(sp));
215                                 command_add(cmds,
216                                     "%s%s -d /tmp/t1 luksOpen %sdev/%s swap",
217                                     a->os_root, cmd_name(a, "CRYPTSETUP"),
218                                     a->os_root,
219                                     subpartition_get_device_name(sp));
220                         }
221                         continue;
222                 }
223
224                 if (subpartition_is_encrypted(sp) &&
225                     strcmp(subpartition_get_mountpoint(sp), "/") != 0) {
226                         command_add(cmds,
227                             "%s%s -d /tmp/t1 luksFormat %sdev/%s",
228                             a->os_root, cmd_name(a, "CRYPTSETUP"),
229                             a->os_root,
230                             subpartition_get_device_name(sp));
231                         command_add(cmds,
232                             "%s%s -d /tmp/t1 luksOpen %sdev/%s %s",
233                             a->os_root, cmd_name(a, "CRYPTSETUP"),
234                             a->os_root,
235                             subpartition_get_device_name(sp),
236                             subpartition_get_mountpoint(sp) + 1);
237                         command_add(cmds, "%s%s%s -b %ld -f %ld %sdev/mapper/%s",
238                             a->os_root, cmd_name(a, "NEWFS"),
239                             subpartition_is_softupdated(sp) ? " -U" : "",
240                             subpartition_get_bsize(sp),
241                             subpartition_get_fsize(sp),
242                             a->os_root,
243                             subpartition_get_mountpoint(sp) + 1);
244                 } else {
245                         command_add(cmds, "%s%s%s -b %ld -f %ld %sdev/%s",
246                             a->os_root, cmd_name(a, "NEWFS"),
247                             subpartition_is_softupdated(sp) ? " -U" : "",
248                             subpartition_get_bsize(sp),
249                             subpartition_get_fsize(sp),
250                             a->os_root,
251                             subpartition_get_device_name(sp));
252                 }
253         }
254
255         result = commands_execute(a, cmds);
256         commands_free(cmds);
257         return(result);
258 }
259
260 static long
261 default_capacity(struct storage *s, int mtpt)
262 {
263         unsigned long swap;
264         unsigned long capacity;
265         unsigned long mem;
266
267         if (mtpt == MTPT_HOME)
268                 return(-1);
269
270         capacity = slice_get_capacity(storage_get_selected_slice(s));
271         mem = storage_get_memsize(s);
272         swap = 2 * mem;
273         if (mem > (capacity / 2) || capacity < 4096)
274                 swap = mem;
275         if (mem > capacity)
276                 swap = capacity / 2;
277         if (swap > SWAP_MAX)
278                 swap = SWAP_MAX;
279
280         if (capacity < DISK_MIN) {
281                 /*
282                  * For the purposes of this installer:
283                  * can't be done.  Sorry.
284                  */
285                 return(-1);
286         } else if (capacity < 4096) {
287                 switch (mtpt) {
288                 case MTPT_ROOT: return(320);
289                 case MTPT_SWAP: return(swap);
290                 case MTPT_VAR:  return(128);
291                 case MTPT_TMP:  return(128);
292                 case MTPT_USR:  return(1472);
293                 }
294         } else if (capacity < 10240) {
295                 switch (mtpt) {
296                 case MTPT_ROOT: return(640);
297                 case MTPT_SWAP: return(swap);
298                 case MTPT_VAR:  return(256);
299                 case MTPT_TMP:  return(256);
300                 case MTPT_USR:  return(2688);
301                 }
302         } else {
303                 switch (mtpt) {
304                 case MTPT_ROOT: return(768);
305                 case MTPT_SWAP: return(swap);
306                 case MTPT_VAR:  return(256);
307                 case MTPT_TMP:  return(256);
308                 case MTPT_USR:  return(7680);
309                 }
310         }
311         /* shouldn't ever happen */
312         return(-1);
313 }
314
315 static int
316 check_capacity(struct i_fn_args *a)
317 {
318         struct subpartition *sp;
319         long min_capacity[] = {320, 0, 16, 0, 1472, 0, 0};
320         unsigned long total_capacity = 0;
321         int mtpt;
322
323         if (subpartition_find(storage_get_selected_slice(a->s), "/usr") == NULL)
324                 min_capacity[MTPT_ROOT] += min_capacity[MTPT_USR];
325
326         for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
327              sp != NULL; sp = subpartition_next(sp)) {
328                 long subpart_capacity = subpartition_get_capacity(sp);
329                 const char *mountpt = subpartition_get_mountpoint(sp);
330
331                 if (subpart_capacity == -1)
332                         total_capacity++;
333                 else
334                         total_capacity += subpart_capacity;
335                 for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
336                         if (strcmp(mountpt, def_mountpt[mtpt]) == 0 &&
337                             subpart_capacity < min_capacity[mtpt] &&
338                             subpart_capacity != -1) {
339                                 inform(a->c, _("WARNING: The size (%ldM) specified for "
340                                     "the %s subpartition is too small. It "
341                                     "should be at least %ldM or you will "
342                                     "risk running out of space during "
343                                     "the installation."),
344                                     subpart_capacity, mountpt,
345                                     min_capacity[mtpt]);
346                         }
347                 }
348         }
349
350         if (total_capacity > slice_get_capacity(storage_get_selected_slice(a->s))) {
351                 inform(a->c, _("The space allocated to all of your selected "
352                     "subpartitions (%luM) exceeds the total "
353                     "capacity of the selected primary partition "
354                     "(%luM). Remove some subpartitions or choose "
355                     "a smaller size for them and try again."),
356                     total_capacity, slice_get_capacity(storage_get_selected_slice(a->s)));
357                 return(0);
358         }
359
360         return(1);
361 }
362
363 static int
364 check_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
365 {
366         struct dfui_dataset *ds;
367         struct dfui_dataset *star_ds = NULL;
368         struct aura_dict *d;
369         const char *mountpoint, *capstring;
370         long capacity = 0;
371         long bsize, fsize;
372         int found_root = 0;
373         int softupdates, tmpfsbacked;
374         int valid = 1;
375
376         d = aura_dict_new(1, AURA_DICT_LIST);
377
378         if ((ds = dfui_response_dataset_get_first(r)) == NULL) {
379                 inform(a->c, _("Please set up at least one subpartition."));
380                 valid = 0;
381         }
382
383         for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
384             ds = dfui_dataset_get_next(ds)) {
385 #ifdef DEBUG
386                 dfui_dataset_dump(ds);
387 #endif
388                 mountpoint = dfui_dataset_get_value(ds, "mountpoint");
389                 capstring = dfui_dataset_get_value(ds, "capacity");
390
391                 if (expert) {
392                         softupdates =
393                             (strcmp(dfui_dataset_get_value(ds, "softupdates"), "Y") == 0);
394                         fsize = atol(dfui_dataset_get_value(ds, "fsize"));
395                         bsize = atol(dfui_dataset_get_value(ds, "bsize"));
396                         tmpfsbacked = (strcmp(dfui_dataset_get_value(ds, "tmpfsbacked"), "Y") == 0);
397                 } else {
398                         softupdates = (strcmp(mountpoint, "/") == 0 ? 0 : 1);
399                         tmpfsbacked = (strcmp(mountpoint, "/tmp") == 0 ? 0 : 1);
400                         fsize = -1;
401                         bsize = -1;
402                 }
403
404                 if (aura_dict_exists(d, mountpoint, strlen(mountpoint) + 1)) {
405                         inform(a->c, _("The same mount point cannot be specified "
406                             "for two different subpartitions."));
407                         valid = 0;
408                 }
409
410                 if (strcmp(mountpoint, "/") == 0)
411                         found_root = 1;
412
413                 if (strcmp(capstring, "*") == 0) {
414                         if (star_ds != NULL) {
415                                 inform(a->c, _("You cannot have more than one subpartition "
416                                     "with a '*' capacity (meaning 'use the remainder "
417                                     "of the primary partition'.)"));
418                                 valid = 0;
419                         } else {
420                                 star_ds = ds;
421                         }
422                 }
423
424                 if (!(!strcasecmp(mountpoint, "swap") || mountpoint[0] == '/')) {
425                         inform(a->c, _("Mount point must be either 'swap', or it must "
426                             "start with a '/'."));
427                         valid = 0;
428                 }
429
430                 if (strpbrk(mountpoint, " \\\"'`") != NULL) {
431                         inform(a->c, _("Mount point may not contain the following "
432                             "characters: blank space, backslash, or "
433                             "single, double, or back quotes."));
434                         valid = 0;
435                 }
436
437                 if (strlen(capstring) == 0) {
438                         inform(a->c, _("A capacity must be specified."));
439                         valid = 0;
440                 }
441
442                 if (!string_to_capacity(capstring, &capacity)) {
443                         inform(a->c, _("Capacity must be either a '*' symbol to indicate "
444                             "'use the rest of the primary partition', or it "
445                             "must be a series of decimal digits ending with a "
446                             "'M' (indicating megabytes) or a 'G' (indicating "
447                             "gigabytes.)"));
448                         valid = 0;
449                 }
450
451                 /*
452                  * Maybe remove this limit entirely?
453                  */
454                 if ((strcasecmp(mountpoint, "swap") == 0) &&
455                     (capacity > SWAP_MAX)) {
456                         inform(a->c, _("Swap capacity is limited to %dG."),
457                             SWAP_MAX / 1024);
458                         valid = 0;
459                 }
460
461                 /*
462                  * If we made it through that obstacle course, all is well.
463                  */
464
465                 if (valid)
466                         aura_dict_store(d, mountpoint, strlen(mountpoint) + 1, "", 1);
467         }
468
469         if (!found_root) {
470                 inform(a->c, _("You must include a / (root) subpartition."));
471                 valid = 0;
472         }
473
474         if (aura_dict_size(d) > 16) {
475                 inform(a->c, _("You cannot have more than 16 subpartitions "
476                     "on a single primary partition.  Remove some "
477                     "and try again."));
478                 valid = 0;
479         }
480
481         aura_dict_free(d);
482
483         return(valid);
484 }
485
486 static void
487 save_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
488 {
489         struct dfui_dataset *ds;
490         char tmpfsbacked;
491         const char *mountpoint, *capstring;
492         long capacity;
493         long bsize, fsize;
494         int softupdates;
495         int valid = 1;
496
497         subpartitions_free(storage_get_selected_slice(a->s));
498
499         for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
500             ds = dfui_dataset_get_next(ds)) {
501                 mountpoint = dfui_dataset_get_value(ds, "mountpoint");
502                 capstring = dfui_dataset_get_value(ds, "capacity");
503
504                 if (expert) {
505                         softupdates =
506                             (strcmp(dfui_dataset_get_value(ds, "softupdates"), "Y") == 0);
507                         fsize = atol(dfui_dataset_get_value(ds, "fsize"));
508                         bsize = atol(dfui_dataset_get_value(ds, "bsize"));
509                         tmpfsbacked = (strcmp(dfui_dataset_get_value(ds, "tmpfsbacked"), "Y") == 0);
510                 } else {
511                         softupdates = (strcmp(mountpoint, "/") == 0 ? 0 : 1);
512                         tmpfsbacked = 0;
513                         fsize = -1;
514                         bsize = -1;
515                 }
516
517                 if (string_to_capacity(capstring, &capacity)) {
518                         subpartition_new_ufs(storage_get_selected_slice(a->s),
519                             mountpoint, capacity,
520                             strcasecmp(dfui_dataset_get_value(ds, "encrypted"), "Y") == 0,
521                             softupdates, fsize, bsize, tmpfsbacked);
522                 }
523         }
524 }
525
526 static void
527 populate_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
528 {
529         struct subpartition *sp;
530         struct dfui_dataset *ds;
531         char temp[32];
532         int mtpt;
533         long capacity;
534
535         if (slice_subpartition_first(storage_get_selected_slice(a->s)) != NULL) {
536                 /*
537                  * The user has already given us their subpartition
538                  * preferences, so use them here.
539                  */
540                 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
541                      sp != NULL; sp = subpartition_next(sp)) {
542                         ds = dfui_dataset_new();
543                         dfui_dataset_celldata_add(ds, "mountpoint",
544                             subpartition_get_mountpoint(sp));
545                         dfui_dataset_celldata_add(ds, "capacity",
546                             capacity_to_string(subpartition_get_capacity(sp)));
547                         dfui_dataset_celldata_add(ds, "encrypted",
548                             subpartition_is_encrypted(sp) ? "Y" : "N");
549                         if (expert) {
550                                 dfui_dataset_celldata_add(ds, "softupdates",
551                                     subpartition_is_softupdated(sp) ? "Y" : "N");
552                                 dfui_dataset_celldata_add(ds, "tmpfsbacked",
553                                     subpartition_is_tmpfsbacked(sp) ? "Y" : "N");
554                                 snprintf(temp, 32, "%ld", subpartition_get_fsize(sp));
555                                 dfui_dataset_celldata_add(ds, "fsize",
556                                     temp);
557                                 snprintf(temp, 32, "%ld", subpartition_get_bsize(sp));
558                                 dfui_dataset_celldata_add(ds, "bsize",
559                                     temp);
560                         }
561                         dfui_form_dataset_add(f, ds);
562                 }
563         } else {
564                 /*
565                  * Otherwise, populate the form with datasets representing
566                  * reasonably-calculated defaults.  The defaults are chosen
567                  * based on the slice's total capacity and the machine's
568                  * total physical memory (for swap.)
569                  */
570                 for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
571                         capacity = default_capacity(a->s, mtpt);
572                         ds = dfui_dataset_new();
573                         dfui_dataset_celldata_add(ds, "mountpoint",
574                             def_mountpt[mtpt]);
575                         dfui_dataset_celldata_add(ds, "capacity",
576                             capacity_to_string(capacity));
577                         dfui_dataset_celldata_add(ds, "encrypted", "N");
578                         if (expert) {
579                                 dfui_dataset_celldata_add(ds, "softupdates",
580                                     strcmp(def_mountpt[mtpt], "/") != 0 ? "Y" : "N");
581                                 dfui_dataset_celldata_add(ds, "tmpfsbacked",
582                                     "N");
583                                 dfui_dataset_celldata_add(ds, "fsize",
584                                     capacity < 1024 ? "1024" : "2048");
585                                 dfui_dataset_celldata_add(ds, "bsize",
586                                     capacity < 1024 ? "8192" : "16384");
587                         }
588                         dfui_form_dataset_add(f, ds);
589                 }
590         }
591 }
592
593 static int
594 warn_subpartition_selections(struct i_fn_args *a)
595 {
596         int valid = 0;
597         struct aura_buffer *omit, *consequences;
598
599         omit = aura_buffer_new(2048);
600         consequences = aura_buffer_new(2048);
601
602         valid = check_capacity(a);
603         if (subpartition_find(storage_get_selected_slice(a->s), "/var") == NULL) {
604                 aura_buffer_cat(omit, "/var ");
605                 aura_buffer_cat(consequences, _("/var will be a plain dir in /\n"));
606         }
607         if (subpartition_find(storage_get_selected_slice(a->s), "/usr") == NULL) {
608                 aura_buffer_cat(omit, "/usr ");
609                 aura_buffer_cat(consequences, _("/usr will be a plain dir in /\n"));
610         }
611         if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL) {
612                 aura_buffer_cat(omit, "/tmp ");
613                 aura_buffer_cat(consequences, _("/tmp will be symlinked to /var/tmp\n"));
614         }
615         if (subpartition_find(storage_get_selected_slice(a->s), "/home") == NULL) {
616                 aura_buffer_cat(omit, "/home ");
617                 aura_buffer_cat(consequences, _("/home will be symlinked to /usr/home\n"));
618         }
619
620         if (valid && aura_buffer_len(omit) > 0) {
621                 switch (dfui_be_present_dialog(a->c, _("Really omit?"),
622                     _("Omit Subpartition(s)|Return to Create Subpartitions"),
623                     _("You have elected to not have the following "
624                     "subpartition(s):\n\n%s\n\n"
625                     "The ramifications of these subpartition(s) being "
626                     "missing will be:\n\n%s\n"
627                     "Is this really what you want to do?"),
628                     aura_buffer_buf(omit), aura_buffer_buf(consequences))) {
629                 case 1:
630                         valid = 1;
631                         break;
632                 case 2:
633                         valid = 0;
634                         break;
635                 default:
636                         abort_backend();
637                 }
638         }
639
640         aura_buffer_free(omit);
641         aura_buffer_free(consequences);
642
643         return(!valid);
644 }
645
646 static int
647 warn_encrypted_root(struct i_fn_args *a)
648 {
649         int valid = 1;
650         struct subpartition *sp;
651
652         sp = subpartition_find(storage_get_selected_slice(a->s), "/");
653         if (sp == NULL)
654                 return(!valid);
655
656         if (subpartition_is_encrypted(sp)) {
657                 switch (dfui_be_present_dialog(a->c, _("root cannot be encrypted"),
658                     _("Leave root unencrypted|Return to Create Subpartitions"),
659                     _("You have selected encryption for the root partition which "
660                     "is not supported."))) {
661                 case 1:
662                         subpartition_clr_encrypted(sp);
663                         valid = 1;
664                         break;
665                 case 2:
666                         valid = 0;
667                         break;
668                 default:
669                         abort_backend();
670                 }
671         }
672
673         return(!valid);
674 }
675
676 static struct dfui_form *
677 make_create_subpartitions_form(struct i_fn_args *a)
678 {
679         struct dfui_field *fi;
680         struct dfui_form *f;
681         char msg_buf[1][1024];
682
683         snprintf(msg_buf[0], sizeof(msg_buf[0]),
684             _("Subpartitions further divide a primary partition for "
685             "use with %s.  Some reasons you may want "
686             "a set of subpartitions are:\n\n"
687             "- you want to restrict how much data can be written "
688             "to certain parts of the primary partition, to quell "
689             "denial-of-service attacks; and\n"
690             "- you want to speed up access to data on the disk."
691             ""), OPERATING_SYSTEM_NAME);
692
693         f = dfui_form_create(
694             "create_subpartitions",
695             _("Create Subpartitions"),
696             _("Set up the subpartitions (also known as just `partitions' "
697             "in BSD tradition) you want to have on this primary "
698             "partition.\n\n"
699             "For Capacity, use 'M' to indicate megabytes, 'G' to "
700             "indicate gigabytes, or a single '*' to indicate "
701             "'use the remaining space on the primary partition'."),
702
703             msg_buf[0],
704
705             "p", "special", "dfinstaller_create_subpartitions",
706             "p", "minimum_width","64",
707
708             "f", "mountpoint", _("Mountpoint"), "", "",
709             "f", "capacity", _("Capacity"), "", "",
710
711             "f", "encrypted", _("Encrypted"), "", "",
712             "p", "control", "checkbox",
713
714             "a", "ok", _("Accept and Create"), "", "",
715             "a", "cancel",
716             (disk_get_formatted(storage_get_selected_disk(a->s)) ?
717             _("Return to Select Disk") :
718             _("Return to Select Primary Partition")), "", "",
719             "p", "accelerator", "ESC",
720
721             NULL
722         );
723
724         dfui_form_set_multiple(f, 1);
725         dfui_form_set_extensible(f, 1);
726
727         if (expert) {
728                 fi = dfui_form_field_add(f, "softupdates",
729                     dfui_info_new(_("Softupdates"), "", ""));
730                 dfui_field_property_set(fi, "control", "checkbox");
731
732                 fi = dfui_form_field_add(f, "tmpfsbacked",
733                     dfui_info_new(_("TMPFS"), "", ""));
734                 dfui_field_property_set(fi, "control", "checkbox");
735
736                 fi = dfui_form_field_add(f, "fsize",
737                     dfui_info_new(_("Frag Sz"), "", ""));
738
739                 fi = dfui_form_field_add(f, "bsize",
740                     dfui_info_new(_("Block Sz"), "", ""));
741
742                 dfui_form_action_add(f, "switch",
743                     dfui_info_new(_("Switch to Normal Mode"), "", ""));
744         } else {
745                 dfui_form_action_add(f, "switch",
746                     dfui_info_new(_("Switch to Expert Mode"), "", ""));
747         }
748
749         return(f);
750 }
751
752 /*
753  * Returns:
754  *      -1 = the form should be redisplayed
755  *       0 = failure, function is over
756  *       1 = success, function is over
757  */
758 static int
759 show_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
760 {
761         struct dfui_dataset *ds;
762         struct dfui_response *r;
763
764         for (;;) {
765                 if (dfui_form_dataset_get_first(f) == NULL)
766                         populate_create_subpartitions_form(f, a);
767
768                 if (!dfui_be_present(a->c, f, &r))
769                         abort_backend();
770
771                 if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
772                         dfui_response_free(r);
773                         return(0);
774                 } else if (strcmp(dfui_response_get_action_id(r), "switch") == 0) {
775                         if (check_subpartition_selections(r, a)) {
776                                 save_subpartition_selections(r, a);
777                                 expert = expert ? 0 : 1;
778                                 dfui_response_free(r);
779                                 return(-1);
780                         }
781                 } else {
782                         if (check_subpartition_selections(r, a)) {
783                                 save_subpartition_selections(r, a);
784                                 if (!warn_subpartition_selections(a) &&
785                                     !warn_encrypted_root(a)) {
786                                         if (!create_subpartitions(a)) {
787                                                 inform(a->c, _("The subpartitions you chose were "
788                                                         "not correctly created, and the "
789                                                         "primary partition may "
790                                                         "now be in an inconsistent state. "
791                                                         "We recommend re-formatting it "
792                                                         "before proceeding."));
793                                                 dfui_response_free(r);
794                                                 return(0);
795                                         } else {
796                                                 dfui_response_free(r);
797                                                 return(1);
798                                         }
799                                 }
800                         }
801                 }
802
803                 dfui_form_datasets_free(f);
804                 /* dfui_form_datasets_add_from_response(f, r); */
805                 for (ds = dfui_response_dataset_get_first(r); ds != NULL;
806                     ds = dfui_dataset_get_next(ds)) {
807                         dfui_form_dataset_add(f, dfui_dataset_dup(ds));
808                 }
809         }
810 }
811
812 void
813 fn_create_subpartitions_ufs(struct i_fn_args *a)
814 {
815         struct dfui_form *f;
816         int done = 0;
817
818         a->result = 0;
819         while (!done) {
820                 f = make_create_subpartitions_form(a);
821                 switch (show_create_subpartitions_form(f, a)) {
822                 case -1:
823                         done = 0;
824                         break;
825                 case 0:
826                         done = 1;
827                         a->result = 0;
828                         break;
829                 case 1:
830                         done = 1;
831                         a->result = 1;
832                         break;
833                 }
834                 dfui_form_free(f);
835         }
836 }