2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
44 #include "diskeditor.h"
47 struct pmetadata_head part_metadata;
49 static int sade_mode = 0;
51 static int apply_changes(struct gmesh *mesh);
52 static void apply_workaround(struct gmesh *mesh);
53 static struct partedit_item *read_geom_mesh(struct gmesh *mesh, int *nitems);
54 static void add_geom_children(struct ggeom *gp, int recurse,
55 struct partedit_item **items, int *nitems);
56 static void init_fstab_metadata(void);
57 static void get_mount_points(struct partedit_item *items, int nitems);
58 static int validate_setup(void);
61 sigint_handler(int sig)
65 /* Revert all changes and exit dialog-mode cleanly on SIGINT */
67 gpart_revert_all(&mesh);
68 geom_deletetree(&mesh);
78 main(int argc, const char **argv)
80 struct partition_metadata *md;
81 const char *progname, *prompt, *tmpdir;
82 struct partedit_item *items = NULL;
84 int i, op, nitems, nscroll;
87 progname = getprogname();
88 if (strcmp(progname, "sade") == 0)
91 TAILQ_INIT(&part_metadata);
93 tmpdir = getenv("TMPDIR");
96 tmpdfd = open(tmpdir, O_DIRECTORY);
98 err(EX_OSERR, "%s", tmpdir);
99 unlinkat(tmpdfd, "bsdinstall-esps", 0);
101 init_fstab_metadata();
103 init_dialog(stdin, stdout);
105 dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
106 dialog_vars.item_help = TRUE;
109 /* Revert changes on SIGINT */
110 signal(SIGINT, sigint_handler);
112 if (strcmp(progname, "autopart") == 0) { /* Guided */
113 prompt = "Please review the disk setup. When complete, press "
114 "the Finish button.";
115 /* Experimental ZFS autopartition support */
116 if (argc > 1 && strcmp(argv[1], "zfs") == 0) {
121 } else if (strcmp(progname, "scriptedpart") == 0) {
122 error = scripted_editor(argc, argv);
129 prompt = "Create partitions for FreeBSD. No changes will be "
130 "made until you select Finish.";
133 /* Show the part editor either immediately, or to confirm wizard */
134 while (prompt != NULL) {
138 error = geom_gettree(&mesh);
140 items = read_geom_mesh(&mesh, &nitems);
141 if (error || items == NULL) {
142 dialog_msgbox("Error", "No disks found. If you need to "
143 "install a kernel driver, choose Shell at the "
144 "installation menu.", 0, 0, TRUE);
148 get_mount_points(items, nitems);
152 op = diskeditor_show("Partition Editor", prompt,
153 items, nitems, &i, &nscroll);
157 gpart_create((struct gprovider *)(items[i].cookie),
158 NULL, NULL, NULL, NULL, 1);
161 gpart_delete((struct gprovider *)(items[i].cookie));
164 gpart_edit((struct gprovider *)(items[i].cookie));
167 gpart_revert_all(&mesh);
168 while ((md = TAILQ_FIRST(&part_metadata)) != NULL) {
169 if (md->fstab != NULL) {
170 free(md->fstab->fs_spec);
171 free(md->fstab->fs_file);
172 free(md->fstab->fs_vfstype);
173 free(md->fstab->fs_mntops);
174 free(md->fstab->fs_type);
177 if (md->newfs != NULL)
181 TAILQ_REMOVE(&part_metadata, md, metadata);
184 init_fstab_metadata();
192 if (op == 5) { /* Finished */
193 dialog_vars.ok_label = __DECONST(char *, "Commit");
194 dialog_vars.extra_label =
195 __DECONST(char *, "Revert & Exit");
196 dialog_vars.extra_button = TRUE;
197 dialog_vars.cancel_label = __DECONST(char *, "Back");
198 op = dialog_yesno("Confirmation", "Your changes will "
199 "now be written to disk. If you have chosen to "
200 "overwrite existing data, it will be PERMANENTLY "
201 "ERASED. Are you sure you want to commit your "
203 dialog_vars.ok_label = NULL;
204 dialog_vars.extra_button = FALSE;
205 dialog_vars.cancel_label = NULL;
207 if (op == 0 && validate_setup()) { /* Save */
208 error = apply_changes(&mesh);
210 apply_workaround(&mesh);
212 } else if (op == 3) { /* Quit */
213 gpart_revert_all(&mesh);
219 geom_deletetree(&mesh);
223 if (prompt == NULL) {
224 error = geom_gettree(&mesh);
225 if (validate_setup()) {
226 error = apply_changes(&mesh);
228 gpart_revert_all(&mesh);
233 geom_deletetree(&mesh);
241 struct partition_metadata *
242 get_part_metadata(const char *name, int create)
244 struct partition_metadata *md;
246 TAILQ_FOREACH(md, &part_metadata, metadata)
247 if (md->name != NULL && strcmp(md->name, name) == 0)
250 if (md == NULL && create) {
251 md = calloc(1, sizeof(*md));
252 md->name = strdup(name);
253 TAILQ_INSERT_TAIL(&part_metadata, md, metadata);
260 delete_part_metadata(const char *name)
262 struct partition_metadata *md;
264 TAILQ_FOREACH(md, &part_metadata, metadata) {
265 if (md->name != NULL && strcmp(md->name, name) == 0) {
266 if (md->fstab != NULL) {
267 free(md->fstab->fs_spec);
268 free(md->fstab->fs_file);
269 free(md->fstab->fs_vfstype);
270 free(md->fstab->fs_mntops);
271 free(md->fstab->fs_type);
274 if (md->newfs != NULL)
278 TAILQ_REMOVE(&part_metadata, md, metadata);
288 struct partition_metadata *md, *root = NULL;
291 TAILQ_FOREACH(md, &part_metadata, metadata) {
292 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0)
295 /* XXX: Check for duplicate mountpoints */
299 dialog_msgbox("Error", "No root partition was found. "
300 "The root FreeBSD partition must have a mountpoint of '/'.",
306 * Check for root partitions that we aren't formatting, which is
309 if (root->newfs == NULL && !sade_mode) {
310 dialog_vars.defaultno = TRUE;
311 cancel = dialog_yesno("Warning", "The chosen root partition "
312 "has a preexisting filesystem. If it contains an existing "
313 "FreeBSD system, please update it with freebsd-update "
314 "instead of installing a new system on it. The partition "
315 "can also be erased by pressing \"No\" and then deleting "
316 "and recreating it. Are you sure you want to proceed?",
318 dialog_vars.defaultno = FALSE;
327 apply_changes(struct gmesh *mesh)
329 struct partition_metadata *md;
331 int i, nitems, error;
333 const char *fstab_path;
336 nitems = 1; /* Partition table changes */
337 TAILQ_FOREACH(md, &part_metadata, metadata) {
338 if (md->newfs != NULL)
341 items = calloc(nitems * 2, sizeof(const char *));
342 items[0] = "Writing partition tables";
343 items[1] = "7"; /* In progress */
345 TAILQ_FOREACH(md, &part_metadata, metadata) {
346 if (md->newfs != NULL) {
349 sprintf(item, "Initializing %s", md->name);
351 items[i*2 + 1] = "Pending";
357 dialog_mixedgauge("Initializing",
358 "Initializing file systems. Please wait.", 0, 0, i*100/nitems,
359 nitems, __DECONST(char **, items));
361 items[i*2 + 1] = "3";
364 if (getenv("BSDINSTALL_LOG") == NULL)
365 setenv("BSDINSTALL_LOG", "/dev/null", 1);
367 TAILQ_FOREACH(md, &part_metadata, metadata) {
368 if (md->newfs != NULL) {
369 items[i*2 + 1] = "7"; /* In progress */
370 dialog_mixedgauge("Initializing",
371 "Initializing file systems. Please wait.", 0, 0,
372 i*100/nitems, nitems, __DECONST(char **, items));
373 sprintf(message, "(echo %s; %s) >>%s 2>>%s",
374 md->newfs, md->newfs, getenv("BSDINSTALL_LOG"),
375 getenv("BSDINSTALL_LOG"));
376 error = system(message);
377 items[i*2 + 1] = (error == 0) ? "3" : "1";
381 dialog_mixedgauge("Initializing",
382 "Initializing file systems. Please wait.", 0, 0,
383 i*100/nitems, nitems, __DECONST(char **, items));
385 for (i = 1; i < nitems; i++)
386 free(__DECONST(char *, items[i*2]));
389 if (getenv("PATH_FSTAB") != NULL)
390 fstab_path = getenv("PATH_FSTAB");
392 fstab_path = "/etc/fstab";
393 fstab = fopen(fstab_path, "w+");
395 sprintf(message, "Cannot open fstab file %s for writing (%s)\n",
396 getenv("PATH_FSTAB"), strerror(errno));
397 dialog_msgbox("Error", message, 0, 0, TRUE);
400 fprintf(fstab, "# Device\tMountpoint\tFStype\tOptions\tDump\tPass#\n");
401 TAILQ_FOREACH(md, &part_metadata, metadata) {
402 if (md->fstab != NULL)
403 fprintf(fstab, "%s\t%s\t\t%s\t%s\t%d\t%d\n",
404 md->fstab->fs_spec, md->fstab->fs_file,
405 md->fstab->fs_vfstype, md->fstab->fs_mntops,
406 md->fstab->fs_freq, md->fstab->fs_passno);
414 apply_workaround(struct gmesh *mesh)
416 struct gclass *classp;
419 const char *scheme = NULL, *modified = NULL;
421 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
422 if (strcmp(classp->lg_name, "PART") == 0)
426 if (strcmp(classp->lg_name, "PART") != 0) {
427 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
431 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
432 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
433 if (strcmp(gc->lg_name, "scheme") == 0) {
435 } else if (strcmp(gc->lg_name, "modified") == 0) {
436 modified = gc->lg_val;
440 if (scheme && strcmp(scheme, "GPT") == 0 &&
441 modified && strcmp(modified, "true") == 0) {
442 if (getenv("WORKAROUND_LENOVO"))
443 gpart_set_root(gp->lg_name, "lenovofix");
444 if (getenv("WORKAROUND_GPTACTIVE"))
445 gpart_set_root(gp->lg_name, "active");
450 static struct partedit_item *
451 read_geom_mesh(struct gmesh *mesh, int *nitems)
453 struct gclass *classp;
455 struct partedit_item *items;
461 * Build the device table. First add all disks (and CDs).
464 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
465 if (strcmp(classp->lg_name, "DISK") != 0 &&
466 strcmp(classp->lg_name, "MD") != 0)
469 /* Now recurse into all children */
470 LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
471 add_geom_children(gp, 0, &items, nitems);
478 add_geom_children(struct ggeom *gp, int recurse, struct partedit_item **items,
481 struct gconsumer *cp;
482 struct gprovider *pp;
485 if (strcmp(gp->lg_class->lg_name, "PART") == 0 &&
486 !LIST_EMPTY(&gp->lg_config)) {
487 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
488 if (strcmp(gc->lg_name, "scheme") == 0)
489 (*items)[*nitems-1].type = gc->lg_val;
493 if (LIST_EMPTY(&gp->lg_provider))
496 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
497 if (strcmp(gp->lg_class->lg_name, "LABEL") == 0)
500 /* Skip WORM media */
501 if (strncmp(pp->lg_name, "cd", 2) == 0)
504 *items = realloc(*items,
505 (*nitems+1)*sizeof(struct partedit_item));
506 (*items)[*nitems].indentation = recurse;
507 (*items)[*nitems].name = pp->lg_name;
508 (*items)[*nitems].size = pp->lg_mediasize;
509 (*items)[*nitems].mountpoint = NULL;
510 (*items)[*nitems].type = "";
511 (*items)[*nitems].cookie = pp;
513 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
514 if (strcmp(gc->lg_name, "type") == 0)
515 (*items)[*nitems].type = gc->lg_val;
518 /* Skip swap-backed MD devices */
519 if (strcmp(gp->lg_class->lg_name, "MD") == 0 &&
520 strcmp((*items)[*nitems].type, "swap") == 0)
525 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
526 add_geom_children(cp->lg_geom, recurse+1, items,
529 /* Only use first provider for acd */
530 if (strcmp(gp->lg_class->lg_name, "ACD") == 0)
536 init_fstab_metadata(void)
539 struct partition_metadata *md;
542 while ((fstab = getfsent()) != NULL) {
543 md = calloc(1, sizeof(struct partition_metadata));
546 if (strncmp(fstab->fs_spec, "/dev/", 5) == 0)
547 md->name = strdup(&fstab->fs_spec[5]);
549 md->fstab = malloc(sizeof(struct fstab));
550 md->fstab->fs_spec = strdup(fstab->fs_spec);
551 md->fstab->fs_file = strdup(fstab->fs_file);
552 md->fstab->fs_vfstype = strdup(fstab->fs_vfstype);
553 md->fstab->fs_mntops = strdup(fstab->fs_mntops);
554 md->fstab->fs_type = strdup(fstab->fs_type);
555 md->fstab->fs_freq = fstab->fs_freq;
556 md->fstab->fs_passno = fstab->fs_passno;
560 TAILQ_INSERT_TAIL(&part_metadata, md, metadata);
565 get_mount_points(struct partedit_item *items, int nitems)
567 struct partition_metadata *md;
570 for (i = 0; i < nitems; i++) {
571 TAILQ_FOREACH(md, &part_metadata, metadata) {
572 if (md->name != NULL && md->fstab != NULL &&
573 strcmp(md->name, items[i].name) == 0) {
574 items[i].mountpoint = md->fstab->fs_file;