2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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.
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.
36 * Installer Function : Create HAMMER Subpartitions.
45 #define _(String) gettext (String)
47 #define _(String) (String)
50 #include "libaura/mem.h"
51 #include "libaura/buffer.h"
52 #include "libaura/dict.h"
53 #include "libaura/fspred.h"
55 #include "libdfui/dfui.h"
56 #include "libdfui/dump.h"
57 #include "libdfui/system.h"
59 #include "libinstaller/commands.h"
60 #include "libinstaller/diskutil.h"
61 #include "libinstaller/functions.h"
62 #include "libinstaller/uiutil.h"
66 #include "pathnames.h"
68 static int create_subpartitions(struct i_fn_args *);
69 static long default_capacity(struct storage *, int);
70 static int check_capacity(struct i_fn_args *);
71 static int check_subpartition_selections(struct dfui_response *, struct i_fn_args *);
72 static void save_subpartition_selections(struct dfui_response *, struct i_fn_args *);
73 static void populate_create_subpartitions_form(struct dfui_form *, struct i_fn_args *);
74 static int warn_subpartition_selections(struct i_fn_args *);
75 static struct dfui_form *make_create_subpartitions_form(struct i_fn_args *);
76 static int show_create_subpartitions_form(struct dfui_form *, struct i_fn_args *);
78 static const char *def_mountpt[7] = {"/", "swap", "/var", "/tmp", "/usr", "/home", NULL};
79 static int expert = 0;
82 * Given a set of subpartitions-to-be in the selected slice,
86 create_subpartitions(struct i_fn_args *a)
88 struct subpartition *sp;
89 struct commands *cmds;
91 int copied_original = 0;
94 cmds = commands_new();
95 if (!is_file("%sinstall.disklabel.%s",
97 slice_get_device_name(storage_get_selected_slice(a->s)))) {
99 * Get a copy of the 'virgin' disklabel.
100 * XXX It might make more sense for this to
101 * happen right after format_slice() instead.
103 command_add(cmds, "%s%s -r %s >%sinstall.disklabel.%s",
104 a->os_root, cmd_name(a, "DISKLABEL64"),
105 slice_get_device_name(storage_get_selected_slice(a->s)),
107 slice_get_device_name(storage_get_selected_slice(a->s)));
111 * Weave together a new disklabel out the of the 'virgin'
112 * disklabel, and the user's subpartition choices.
116 * Take everything from the 'virgin' disklabel up until the
117 * '16 partitions' line.
120 command_add(cmds, "%s%s '$2==\"partitions:\" || cut { cut = 1 } !cut { print $0 }' <%sinstall.disklabel.%s >%sinstall.disklabel",
121 a->os_root, cmd_name(a, "AWK"),
123 slice_get_device_name(storage_get_selected_slice(a->s)),
128 * # size offset fstype
129 * c: 16383969 0 unused # 7999.985MB
132 command_add(cmds, "%s%s '%d partitions:' >>%sinstall.disklabel",
133 a->os_root, cmd_name(a, "ECHO"), num_partitions ,a->tmp);
134 command_add(cmds, "%s%s '%s' >>%sinstall.disklabel",
135 a->os_root, cmd_name(a, "ECHO"),
136 "# size offset fstype",
140 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
141 sp != NULL; sp = subpartition_next(sp)) {
142 command_add(cmds, "%s%s 'mountpoint: %s device: %s'",
143 a->os_root, cmd_name(a, "ECHO"),
144 subpartition_get_mountpoint(sp),
145 subpartition_get_device_name(sp));
150 * Write a line for each subpartition the user wants.
152 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
153 sp != NULL; sp = subpartition_next(sp)) {
154 if (subpartition_is_mfsbacked(sp)) {
157 if (subpartition_is_swap(sp)) {
158 command_add(cmds, "%s%s ' %c:\t%s\t*\tswap' >>%sinstall.disklabel",
159 a->os_root, cmd_name(a, "ECHO"),
160 subpartition_get_letter(sp),
161 capacity_to_string(subpartition_get_capacity(sp)),
164 command_add(cmds, "%s%s ' %c:\t%s\t%s\tHAMMER' >>%sinstall.disklabel",
165 a->os_root, cmd_name(a, "ECHO"),
166 subpartition_get_letter(sp),
167 capacity_to_string(subpartition_get_capacity(sp)),
168 subpartition_get_letter(sp) == 'a' ? "0" : "*",
172 temp_file_add(a, "install.disklabel");
175 * Label the slice from the disklabel we just wove together.
177 command_add(cmds, "%s%s -R -B -r %s %sinstall.disklabel",
178 a->os_root, cmd_name(a, "DISKLABEL64"),
179 slice_get_device_name(storage_get_selected_slice(a->s)),
183 * Create a snapshot of the disklabel we just created
184 * for debugging inspection in the log.
186 command_add(cmds, "%s%s %s",
187 a->os_root, cmd_name(a, "DISKLABEL64"),
188 slice_get_device_name(storage_get_selected_slice(a->s)));
191 * Create filesystems on the newly-created subpartitions.
193 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
194 sp != NULL; sp = subpartition_next(sp)) {
195 if (subpartition_is_swap(sp) || subpartition_is_mfsbacked(sp))
199 * Ensure that all the needed device nodes exist.
201 command_add_ensure_dev(a, cmds,
202 disk_get_device_name(storage_get_selected_disk(a->s)));
203 command_add_ensure_dev(a, cmds,
204 slice_get_device_name(storage_get_selected_slice(a->s)));
205 command_add_ensure_dev(a, cmds,
206 subpartition_get_device_name(sp));
208 command_add(cmds, "%s%s -f -L ROOT %sdev/%s",
209 a->os_root, cmd_name(a, "NEWFS_HAMMER"),
211 subpartition_get_device_name(sp));
214 result = commands_execute(a, cmds);
220 default_capacity(struct storage *s, int mtpt)
223 unsigned long capacity;
225 if (mtpt == MTPT_HOME)
228 capacity = slice_get_capacity(storage_get_selected_slice(s));
229 swap = 2 * storage_get_memsize(s);
230 if (storage_get_memsize(s) > (capacity / 2) || capacity < 4096)
231 swap = storage_get_memsize(s);
232 if (storage_get_memsize(s) > capacity)
237 if (capacity < DISK_MIN) {
239 * For the purposes of this installer:
240 * can't be done. Sorry.
243 } else if (capacity < 1024) {
245 case MTPT_ROOT: return(-1);
246 case MTPT_SWAP: return(swap);
247 case MTPT_VAR: return(256);
248 case MTPT_TMP: return(256);
249 case MTPT_USR: return(3072);
253 case MTPT_ROOT: return(-1);
254 case MTPT_SWAP: return(swap);
255 case MTPT_VAR: return(256);
256 case MTPT_TMP: return(256);
257 case MTPT_USR: return(8192);
260 /* shouldn't ever happen */
265 check_capacity(struct i_fn_args *a)
267 struct subpartition *sp;
268 unsigned long min_capacity[7] = {128, 128,0, 8, 0, 174, 0};
269 unsigned long total_capacity = 0;
272 if (subpartition_find(storage_get_selected_slice(a->s), "/usr") == NULL)
273 min_capacity[MTPT_ROOT] += min_capacity[MTPT_USR];
275 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
276 sp != NULL; sp = subpartition_next(sp)) {
277 if (subpartition_get_capacity(sp) == -1)
280 total_capacity += subpartition_get_capacity(sp);
281 for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
282 if (strcmp(subpartition_get_mountpoint(sp), def_mountpt[mtpt]) == 0 &&
283 min_capacity[mtpt] > 0 &&
284 subpartition_get_capacity(sp) < min_capacity[mtpt]) {
285 inform(a->c, _("WARNING: the %s subpartition should "
286 "be at least %dM in size or you will "
287 "risk running out of space during "
288 "the installation."),
289 subpartition_get_mountpoint(sp), min_capacity[mtpt]);
292 if (strcmp(subpartition_get_mountpoint(sp), "/") == 0 &&
293 subpartition_get_capacity(sp) < HAMMER_MIN) {
294 inform(a->c, _("WARNING: HAMMER file systems"
295 "less than 50G are not recommended! You may"
296 "have to run 'hammer prune-everything' and 'hammer reblock'"
297 "quite often, even if using a nohistory mount."));
301 if (total_capacity > slice_get_capacity(storage_get_selected_slice(a->s))) {
302 inform(a->c, _("The space allocated to all of your selected "
303 "subpartitions (%dM) exceeds the total "
304 "capacity of the selected primary partition "
305 "(%dM). Remove some subpartitions or choose "
306 "a smaller size for them and try again."),
307 total_capacity, slice_get_capacity(storage_get_selected_slice(a->s)));
315 check_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
317 struct dfui_dataset *ds;
318 struct dfui_dataset *star_ds = NULL;
320 const char *mountpoint, *capstring;
326 d = aura_dict_new(1, AURA_DICT_LIST);
328 if ((ds = dfui_response_dataset_get_first(r)) == NULL) {
329 inform(a->c, _("Please set up at least one subpartition."));
333 for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
334 ds = dfui_dataset_get_next(ds)) {
336 dfui_dataset_dump(ds);
338 mountpoint = dfui_dataset_get_value(ds, "mountpoint");
339 capstring = dfui_dataset_get_value(ds, "capacity");
343 (strcmp(dfui_dataset_get_value(ds, "softupdates"), "Y") == 0);
344 fsize = atol(dfui_dataset_get_value(ds, "fsize"));
345 bsize = atol(dfui_dataset_get_value(ds, "bsize"));
346 mfsbacked = (strcmp(dfui_dataset_get_value(ds, "mfsbacked"), "Y") == 0);
348 softupdates = (strcmp(mountpoint, "/") == 0 ? 0 : 1);
349 mfsbacked = (strcmp(mountpoint, "/tmp") == 0 ? 0 : 1);
354 if (aura_dict_exists(d, mountpoint, strlen(mountpoint) + 1)) {
355 inform(a->c, _("The same mount point cannot be specified "
356 "for two different subpartitions."));
360 if (strcmp(mountpoint, "/") == 0)
363 if (strcmp(capstring, "*") == 0) {
364 if (star_ds != NULL) {
365 inform(a->c, _("You cannot have more than one subpartition "
366 "with a '*' capacity (meaning 'use the remainder "
367 "of the primary partition'.)"));
374 if (!(!strcasecmp(mountpoint, "swap") || mountpoint[0] == '/')) {
375 inform(a->c, _("Mount point must be either 'swap', or it must "
376 "start with a '/'."));
380 if (strpbrk(mountpoint, " \\\"'`") != NULL) {
381 inform(a->c, _("Mount point may not contain the following "
382 "characters: blank space, backslash, or "
383 "single, double, or back quotes."));
387 if (strlen(capstring) == 0) {
388 inform(a->c, _("A capacity must be specified."));
392 if (!string_to_capacity(capstring, &capacity)) {
393 inform(a->c, _("Capacity must be either a '*' symbol to indicate "
394 "'use the rest of the primary partition', or it "
395 "must be a series of decimal digits ending with a "
396 "'M' (indicating megabytes) or a 'G' (indicating "
401 if ((strcasecmp(mountpoint, "swap") == 0) && (capacity > 8192)) {
402 inform(a->c, _("Swap capacity is limited to 8G."));
407 * If we made it through that obstacle course, all is well.
411 aura_dict_store(d, mountpoint, strlen(mountpoint) + 1, "", 1);
415 inform(a->c, _("You must include a / (root) subpartition."));
419 if (aura_dict_size(d) > 16) {
420 inform(a->c, _("You cannot have more than 16 subpartitions "
421 "on a single primary partition. Remove some "
432 save_subpartition_selections(struct dfui_response *r, struct i_fn_args *a)
434 struct dfui_dataset *ds;
436 const char *mountpoint, *capstring;
441 subpartitions_free(storage_get_selected_slice(a->s));
443 for (ds = dfui_response_dataset_get_first(r); valid && ds != NULL;
444 ds = dfui_dataset_get_next(ds)) {
445 mountpoint = dfui_dataset_get_value(ds, "mountpoint");
446 capstring = dfui_dataset_get_value(ds, "capacity");
450 (strcmp(dfui_dataset_get_value(ds, "softupdates"), "Y") == 0);
451 fsize = atol(dfui_dataset_get_value(ds, "fsize"));
452 bsize = atol(dfui_dataset_get_value(ds, "bsize"));
453 mfsbacked = (strcmp(dfui_dataset_get_value(ds, "mfsbacked"), "Y") == 0);
455 softupdates = (strcmp(mountpoint, "/") == 0 ? 0 : 1);
461 if (string_to_capacity(capstring, &capacity)) {
462 subpartition_new_hammer(storage_get_selected_slice(a->s),
463 mountpoint, capacity);
469 populate_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
471 struct subpartition *sp;
472 struct dfui_dataset *ds;
477 if (slice_subpartition_first(storage_get_selected_slice(a->s)) != NULL) {
479 * The user has already given us their subpartition
480 * preferences, so use them here.
482 for (sp = slice_subpartition_first(storage_get_selected_slice(a->s));
483 sp != NULL; sp = subpartition_next(sp)) {
484 ds = dfui_dataset_new();
485 dfui_dataset_celldata_add(ds, "mountpoint",
486 subpartition_get_mountpoint(sp));
487 dfui_dataset_celldata_add(ds, "capacity",
488 capacity_to_string(subpartition_get_capacity(sp)));
489 dfui_form_dataset_add(f, ds);
493 * Otherwise, populate the form with datasets representing
494 * reasonably-calculated defaults. The defaults are chosen
495 * based on the slice's total capacity and the machine's
496 * total physical memory (for swap.)
498 for (mtpt = 0; def_mountpt[mtpt] != NULL; mtpt++) {
499 /* XXX matthias. skip them for now */
500 if (mtpt != MTPT_ROOT && mtpt != MTPT_SWAP)
502 capacity = default_capacity(a->s, mtpt);
503 ds = dfui_dataset_new();
504 dfui_dataset_celldata_add(ds, "mountpoint",
506 dfui_dataset_celldata_add(ds, "capacity",
507 capacity_to_string(capacity));
508 dfui_form_dataset_add(f, ds);
514 warn_subpartition_selections(struct i_fn_args *a)
517 struct aura_buffer *omit, *consequences;
519 omit = aura_buffer_new(2048);
520 consequences = aura_buffer_new(2048);
522 valid = check_capacity(a);
525 * Should we add an information here, that we created /usr, /var, /home
529 if (subpartition_find(storage_get_selected_slice(a->s), "/var") == NULL) {
530 aura_buffer_cat(omit, "/var ");
531 aura_buffer_cat(consequences, _("/var will be a plain dir in /\n"));
533 if (subpartition_find(storage_get_selected_slice(a->s), "/usr") == NULL) {
534 aura_buffer_cat(omit, "/usr ");
535 aura_buffer_cat(consequences, _("/usr will be a plain dir in /\n"));
537 if (subpartition_find(storage_get_selected_slice(a->s), "/tmp") == NULL) {
538 aura_buffer_cat(omit, "/tmp ");
539 aura_buffer_cat(consequences, _("/tmp will be symlinked to /var/tmp\n"));
541 if (subpartition_find(storage_get_selected_slice(a->s), "/home") == NULL) {
542 aura_buffer_cat(omit, "/home ");
543 aura_buffer_cat(consequences, _("/home will be symlinked to /usr/home\n"));
546 if (valid && aura_buffer_len(omit) > 0) {
547 switch (dfui_be_present_dialog(a->c, _("Really omit?"),
548 _("Omit Subpartition(s)|Return to Create Subpartitions"),
549 _("You have elected to not have the following "
550 "subpartition(s):\n\n%s\n\n"
551 "The ramifications of these subpartition(s) being "
552 "missing will be:\n\n%s\n"
553 "Is this really what you want to do?"),
554 aura_buffer_buf(omit), aura_buffer_buf(consequences))) {
566 aura_buffer_free(omit);
567 aura_buffer_free(consequences);
572 static struct dfui_form *
573 make_create_subpartitions_form(struct i_fn_args *a)
575 struct dfui_field *fi;
577 char msg_buf[1][1024];
579 snprintf(msg_buf[0], sizeof(msg_buf[0]),
580 _("Subpartitions further divide a primary partition for "
581 "use with %s. Some reasons you may want "
582 "a set of subpartitions are:\n\n"
583 "- you want to restrict how much data can be written "
584 "to certain parts of the primary partition, to quell "
585 "denial-of-service attacks; and\n"
586 "- you want to speed up access to data on the disk."
587 ""), OPERATING_SYSTEM_NAME);
589 f = dfui_form_create(
590 "create_subpartitions",
591 _("Create Subpartitions"),
592 _("Set up the subpartitions (also known as just `partitions' "
593 "in BSD tradition) you want to have on this primary "
594 "partition.\n\nIMPORTANT: "
595 "You have chosen HAMMER as your file system. This means you will "
596 "not need to create separate subpartitions for /home, /usr, /var and "
597 "/tmp. The installer will create them automatically as pseudo-"
598 "filesystems (PFS) for you. In most cases you should be fine with "
599 "the default settings.\n\n"
600 "For Capacity, use 'M' to indicate megabytes, 'G' to "
601 "indicate gigabytes, or a single '*' to indicate "
602 "'use the remaining space on the primary partition'."),
606 "p", "special", "dfinstaller_create_subpartitions",
607 "p", "minimum_width","64",
609 "f", "mountpoint", _("Mountpoint"), "", "",
610 "f", "capacity", _("Capacity"), "", "",
612 "a", "ok", _("Accept and Create"), "", "",
614 (disk_get_formatted(storage_get_selected_disk(a->s)) ?
615 _("Return to Select Disk") :
616 _("Return to Select Primary Partition")), "", "",
617 "p", "accelerator", "ESC",
622 dfui_form_set_multiple(f, 1);
623 dfui_form_set_extensible(f, 1);
625 * Remove ATM until HAMMER installer support is better
626 * dfui_form_set_extensible(f, 1);
630 fi = dfui_form_field_add(f, "softupdates",
631 dfui_info_new(_("Softupdates"), "", ""));
632 dfui_field_property_set(fi, "control", "checkbox");
634 fi = dfui_form_field_add(f, "mfsbacked",
635 dfui_info_new(_("MFS"), "", ""));
636 dfui_field_property_set(fi, "control", "checkbox");
638 fi = dfui_form_field_add(f, "fsize",
639 dfui_info_new(_("Frag Sz"), "", ""));
641 fi = dfui_form_field_add(f, "bsize",
642 dfui_info_new(_("Block Sz"), "", ""));
644 dfui_form_action_add(f, "switch",
645 dfui_info_new(_("Switch to Normal Mode"), "", ""));
647 dfui_form_action_add(f, "switch",
648 dfui_info_new(_("Switch to Expert Mode"), "", ""));
656 * -1 = the form should be redisplayed
657 * 0 = failure, function is over
658 * 1 = success, function is over
661 show_create_subpartitions_form(struct dfui_form *f, struct i_fn_args *a)
663 struct dfui_dataset *ds;
664 struct dfui_response *r;
667 if (dfui_form_dataset_get_first(f) == NULL)
668 populate_create_subpartitions_form(f, a);
670 if (!dfui_be_present(a->c, f, &r))
673 if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
674 dfui_response_free(r);
676 } else if (strcmp(dfui_response_get_action_id(r), "switch") == 0) {
677 if (check_subpartition_selections(r, a)) {
678 save_subpartition_selections(r, a);
679 expert = expert ? 0 : 1;
680 dfui_response_free(r);
684 if (check_subpartition_selections(r, a)) {
685 save_subpartition_selections(r, a);
686 if (!warn_subpartition_selections(a)) {
687 if (!create_subpartitions(a)) {
688 inform(a->c, _("The subpartitions you chose were "
689 "not correctly created, and the "
690 "primary partition may "
691 "now be in an inconsistent state. "
692 "We recommend re-formatting it "
693 "before proceeding."));
694 dfui_response_free(r);
697 dfui_response_free(r);
704 dfui_form_datasets_free(f);
705 /* dfui_form_datasets_add_from_response(f, r); */
706 for (ds = dfui_response_dataset_get_first(r); ds != NULL;
707 ds = dfui_dataset_get_next(ds)) {
708 dfui_form_dataset_add(f, dfui_dataset_dup(ds));
714 * fn_create_subpartitions_hammer: let the user specify what subpartitions they
715 * want on the disk, how large each should be, and where it should be mounted.
718 fn_create_subpartitions_hammer(struct i_fn_args *a)
725 f = make_create_subpartitions_form(a);
726 switch (show_create_subpartitions_form(f, a)) {