2 * Copyright (c) 2011 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
40 #include "diskeditor.h"
43 struct pmetadata_head part_metadata;
44 static int sade_mode = 0;
46 static int apply_changes(struct gmesh *mesh);
47 static struct partedit_item *read_geom_mesh(struct gmesh *mesh, int *nitems);
48 static void add_geom_children(struct ggeom *gp, int recurse,
49 struct partedit_item **items, int *nitems);
50 static void init_fstab_metadata(void);
51 static void get_mount_points(struct partedit_item *items, int nitems);
52 static int validate_setup(void);
55 sigint_handler(int sig)
59 /* Revert all changes and exit dialog-mode cleanly on SIGINT */
61 gpart_revert_all(&mesh);
62 geom_deletetree(&mesh);
70 main(int argc, const char **argv)
72 struct partition_metadata *md;
74 struct partedit_item *items = NULL;
76 int i, op, nitems, nscroll;
79 if (strcmp(basename(argv[0]), "sade") == 0)
82 TAILQ_INIT(&part_metadata);
84 init_fstab_metadata();
86 init_dialog(stdin, stdout);
88 dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
89 dialog_vars.item_help = TRUE;
92 /* Revert changes on SIGINT */
93 signal(SIGINT, sigint_handler);
95 if (strcmp(basename(argv[0]), "autopart") == 0) { /* Guided */
96 prompt = "Please review the disk setup. When complete, press "
98 /* Experimental ZFS autopartition support */
99 if (argc > 1 && strcmp(argv[1], "zfs") == 0) {
104 } else if (strcmp(basename(argv[0]), "scriptedpart") == 0) {
105 error = scripted_editor(argc, argv);
112 prompt = "Create partitions for FreeBSD. No changes will be "
113 "made until you select Finish.";
116 /* Show the part editor either immediately, or to confirm wizard */
117 while (prompt != NULL) {
121 error = geom_gettree(&mesh);
123 items = read_geom_mesh(&mesh, &nitems);
124 if (error || items == NULL) {
125 dialog_msgbox("Error", "No disks found. If you need to "
126 "install a kernel driver, choose Shell at the "
127 "installation menu.", 0, 0, TRUE);
131 get_mount_points(items, nitems);
135 op = diskeditor_show("Partition Editor", prompt,
136 items, nitems, &i, &nscroll);
140 gpart_create((struct gprovider *)(items[i].cookie),
141 NULL, NULL, NULL, NULL, 1);
144 gpart_delete((struct gprovider *)(items[i].cookie));
147 gpart_edit((struct gprovider *)(items[i].cookie));
150 gpart_revert_all(&mesh);
151 while ((md = TAILQ_FIRST(&part_metadata)) != NULL) {
152 if (md->fstab != NULL) {
153 free(md->fstab->fs_spec);
154 free(md->fstab->fs_file);
155 free(md->fstab->fs_vfstype);
156 free(md->fstab->fs_mntops);
157 free(md->fstab->fs_type);
160 if (md->newfs != NULL)
164 TAILQ_REMOVE(&part_metadata, md, metadata);
167 init_fstab_metadata();
175 if (op == 5) { /* Finished */
176 dialog_vars.ok_label = __DECONST(char *, "Commit");
177 dialog_vars.extra_label =
178 __DECONST(char *, "Revert & Exit");
179 dialog_vars.extra_button = TRUE;
180 dialog_vars.cancel_label = __DECONST(char *, "Back");
181 op = dialog_yesno("Confirmation", "Your changes will "
182 "now be written to disk. If you have chosen to "
183 "overwrite existing data, it will be PERMANENTLY "
184 "ERASED. Are you sure you want to commit your "
186 dialog_vars.ok_label = NULL;
187 dialog_vars.extra_button = FALSE;
188 dialog_vars.cancel_label = NULL;
190 if (op == 0 && validate_setup()) { /* Save */
191 error = apply_changes(&mesh);
193 } else if (op == 3) { /* Quit */
194 gpart_revert_all(&mesh);
200 geom_deletetree(&mesh);
204 if (prompt == NULL) {
205 error = geom_gettree(&mesh);
206 if (validate_setup()) {
207 error = apply_changes(&mesh);
209 gpart_revert_all(&mesh);
214 geom_deletetree(&mesh);
221 struct partition_metadata *
222 get_part_metadata(const char *name, int create)
224 struct partition_metadata *md;
226 TAILQ_FOREACH(md, &part_metadata, metadata)
227 if (md->name != NULL && strcmp(md->name, name) == 0)
230 if (md == NULL && create) {
231 md = calloc(1, sizeof(*md));
232 md->name = strdup(name);
233 TAILQ_INSERT_TAIL(&part_metadata, md, metadata);
240 delete_part_metadata(const char *name)
242 struct partition_metadata *md;
244 TAILQ_FOREACH(md, &part_metadata, metadata) {
245 if (md->name != NULL && strcmp(md->name, name) == 0) {
246 if (md->fstab != NULL) {
247 free(md->fstab->fs_spec);
248 free(md->fstab->fs_file);
249 free(md->fstab->fs_vfstype);
250 free(md->fstab->fs_mntops);
251 free(md->fstab->fs_type);
254 if (md->newfs != NULL)
258 TAILQ_REMOVE(&part_metadata, md, metadata);
268 struct partition_metadata *md, *root = NULL;
271 TAILQ_FOREACH(md, &part_metadata, metadata) {
272 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0)
275 /* XXX: Check for duplicate mountpoints */
279 dialog_msgbox("Error", "No root partition was found. "
280 "The root FreeBSD partition must have a mountpoint of '/'.",
286 * Check for root partitions that we aren't formatting, which is
289 if (root->newfs == NULL && !sade_mode) {
290 dialog_vars.defaultno = TRUE;
291 cancel = dialog_yesno("Warning", "The chosen root partition "
292 "has a preexisting filesystem. If it contains an existing "
293 "FreeBSD system, please update it with freebsd-update "
294 "instead of installing a new system on it. The partition "
295 "can also be erased by pressing \"No\" and then deleting "
296 "and recreating it. Are you sure you want to proceed?",
298 dialog_vars.defaultno = FALSE;
307 apply_changes(struct gmesh *mesh)
309 struct partition_metadata *md;
311 int i, nitems, error;
313 const char *fstab_path;
316 nitems = 1; /* Partition table changes */
317 TAILQ_FOREACH(md, &part_metadata, metadata) {
318 if (md->newfs != NULL)
321 items = calloc(nitems * 2, sizeof(const char *));
322 items[0] = "Writing partition tables";
323 items[1] = "7"; /* In progress */
325 TAILQ_FOREACH(md, &part_metadata, metadata) {
326 if (md->newfs != NULL) {
329 sprintf(item, "Initializing %s", md->name);
331 items[i*2 + 1] = "Pending";
337 dialog_mixedgauge("Initializing",
338 "Initializing file systems. Please wait.", 0, 0, i*100/nitems,
339 nitems, __DECONST(char **, items));
341 items[i*2 + 1] = "3";
344 if (getenv("BSDINSTALL_LOG") == NULL)
345 setenv("BSDINSTALL_LOG", "/dev/null", 1);
347 TAILQ_FOREACH(md, &part_metadata, metadata) {
348 if (md->newfs != NULL) {
349 items[i*2 + 1] = "7"; /* In progress */
350 dialog_mixedgauge("Initializing",
351 "Initializing file systems. Please wait.", 0, 0,
352 i*100/nitems, nitems, __DECONST(char **, items));
353 sprintf(message, "(echo %s; %s) >>%s 2>>%s",
354 md->newfs, md->newfs, getenv("BSDINSTALL_LOG"),
355 getenv("BSDINSTALL_LOG"));
356 error = system(message);
357 items[i*2 + 1] = (error == 0) ? "3" : "1";
361 dialog_mixedgauge("Initializing",
362 "Initializing file systems. Please wait.", 0, 0,
363 i*100/nitems, nitems, __DECONST(char **, items));
365 for (i = 1; i < nitems; i++)
366 free(__DECONST(char *, items[i*2]));
369 if (getenv("PATH_FSTAB") != NULL)
370 fstab_path = getenv("PATH_FSTAB");
372 fstab_path = "/etc/fstab";
373 fstab = fopen(fstab_path, "w+");
375 sprintf(message, "Cannot open fstab file %s for writing (%s)\n",
376 getenv("PATH_FSTAB"), strerror(errno));
377 dialog_msgbox("Error", message, 0, 0, TRUE);
380 fprintf(fstab, "# Device\tMountpoint\tFStype\tOptions\tDump\tPass#\n");
381 TAILQ_FOREACH(md, &part_metadata, metadata) {
382 if (md->fstab != NULL)
383 fprintf(fstab, "%s\t%s\t\t%s\t%s\t%d\t%d\n",
384 md->fstab->fs_spec, md->fstab->fs_file,
385 md->fstab->fs_vfstype, md->fstab->fs_mntops,
386 md->fstab->fs_freq, md->fstab->fs_passno);
393 static struct partedit_item *
394 read_geom_mesh(struct gmesh *mesh, int *nitems)
396 struct gclass *classp;
398 struct partedit_item *items;
404 * Build the device table. First add all disks (and CDs).
407 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
408 if (strcmp(classp->lg_name, "DISK") != 0 &&
409 strcmp(classp->lg_name, "MD") != 0)
412 /* Now recurse into all children */
413 LIST_FOREACH(gp, &classp->lg_geom, lg_geom)
414 add_geom_children(gp, 0, &items, nitems);
421 add_geom_children(struct ggeom *gp, int recurse, struct partedit_item **items,
424 struct gconsumer *cp;
425 struct gprovider *pp;
428 if (strcmp(gp->lg_class->lg_name, "PART") == 0 &&
429 !LIST_EMPTY(&gp->lg_config)) {
430 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
431 if (strcmp(gc->lg_name, "scheme") == 0)
432 (*items)[*nitems-1].type = gc->lg_val;
436 if (LIST_EMPTY(&gp->lg_provider))
439 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
440 if (strcmp(gp->lg_class->lg_name, "LABEL") == 0)
443 /* Skip WORM media */
444 if (strncmp(pp->lg_name, "cd", 2) == 0)
447 *items = realloc(*items,
448 (*nitems+1)*sizeof(struct partedit_item));
449 (*items)[*nitems].indentation = recurse;
450 (*items)[*nitems].name = pp->lg_name;
451 (*items)[*nitems].size = pp->lg_mediasize;
452 (*items)[*nitems].mountpoint = NULL;
453 (*items)[*nitems].type = "";
454 (*items)[*nitems].cookie = pp;
456 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
457 if (strcmp(gc->lg_name, "type") == 0)
458 (*items)[*nitems].type = gc->lg_val;
461 /* Skip swap-backed MD devices */
462 if (strcmp(gp->lg_class->lg_name, "MD") == 0 &&
463 strcmp((*items)[*nitems].type, "swap") == 0)
468 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
469 add_geom_children(cp->lg_geom, recurse+1, items,
472 /* Only use first provider for acd */
473 if (strcmp(gp->lg_class->lg_name, "ACD") == 0)
479 init_fstab_metadata(void)
482 struct partition_metadata *md;
485 while ((fstab = getfsent()) != NULL) {
486 md = calloc(1, sizeof(struct partition_metadata));
489 if (strncmp(fstab->fs_spec, "/dev/", 5) == 0)
490 md->name = strdup(&fstab->fs_spec[5]);
492 md->fstab = malloc(sizeof(struct fstab));
493 md->fstab->fs_spec = strdup(fstab->fs_spec);
494 md->fstab->fs_file = strdup(fstab->fs_file);
495 md->fstab->fs_vfstype = strdup(fstab->fs_vfstype);
496 md->fstab->fs_mntops = strdup(fstab->fs_mntops);
497 md->fstab->fs_type = strdup(fstab->fs_type);
498 md->fstab->fs_freq = fstab->fs_freq;
499 md->fstab->fs_passno = fstab->fs_passno;
503 TAILQ_INSERT_TAIL(&part_metadata, md, metadata);
508 get_mount_points(struct partedit_item *items, int nitems)
510 struct partition_metadata *md;
513 for (i = 0; i < nitems; i++) {
514 TAILQ_FOREACH(md, &part_metadata, metadata) {
515 if (md->name != NULL && md->fstab != NULL &&
516 strcmp(md->name, items[i].name) == 0) {
517 items[i].mountpoint = md->fstab->fs_file;