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>
41 #define GPART_FLAGS "x" /* Do not commit changes by default */
44 gpart_show_error(const char *title, const char *explanation, const char *errstr)
50 if (explanation == NULL)
53 error = strtol(errstr, &errmsg, 0);
54 if (errmsg != errstr) {
55 while (errmsg[0] == ' ')
57 if (errmsg[0] != '\0')
58 sprintf(message, "%s%s. %s", explanation,
59 strerror(error), errmsg);
61 sprintf(message, "%s%s", explanation, strerror(error));
63 sprintf(message, "%s%s", explanation, errmsg);
66 dialog_msgbox(title, message, 0, 0, TRUE);
70 scheme_supports_labels(const char *scheme)
72 if (strcmp(scheme, "APM") == 0)
74 if (strcmp(scheme, "GPT") == 0)
76 if (strcmp(scheme, "PC98") == 0)
83 newfs_command(const char *fstype, char *command, int use_default)
85 if (strcmp(fstype, "freebsd-ufs") == 0) {
87 DIALOG_LISTITEM items[] = {
88 {"UFS1", "UFS Version 1",
89 "Use version 1 of the UFS file system instead "
90 "of version 2 (not recommended)", 0 },
92 "Enable softupdates (default)", 1 },
93 {"SUJ", "Softupdates journaling",
94 "Enable file system journaling (default - "
95 "turn off for SSDs)", 1 },
96 {"TRIM", "Enable SSD TRIM support",
97 "Enable TRIM support, useful on solid-state drives",
103 choice = dlg_checklist("UFS Options", "", 0, 0, 0,
104 sizeof(items)/sizeof(items[0]), items, NULL,
106 if (choice == 1) /* Cancel */
110 strcpy(command, "newfs ");
111 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
112 if (items[i].state == 0)
114 if (strcmp(items[i].name, "UFS1") == 0)
115 strcat(command, "-O1 ");
116 else if (strcmp(items[i].name, "SU") == 0)
117 strcat(command, "-U ");
118 else if (strcmp(items[i].name, "SUJ") == 0)
119 strcat(command, "-j ");
120 else if (strcmp(items[i].name, "TRIM") == 0)
121 strcat(command, "-t ");
123 } else if (strcmp(fstype, "freebsd-zfs") == 0) {
125 DIALOG_LISTITEM items[] = {
126 {"fletcher4", "checksum algorithm: fletcher4",
127 "Use fletcher4 for data integrity checking. "
129 {"fletcher2", "checksum algorithm: fletcher2",
130 "Use fletcher2 for data integrity checking. "
131 "(not recommended)", 0 },
132 {"sha256", "checksum algorithm: sha256",
133 "Use sha256 for data integrity checking. "
134 "(not recommended)", 0 },
135 {"atime", "Update atimes for files",
136 "Disable atime update", 0 },
141 choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
142 sizeof(items)/sizeof(items[0]), items, NULL,
144 if (choice == 1) /* Cancel */
148 strcpy(command, "zpool create -f -m none ");
149 if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
150 char zfsboot_path[MAXPATHLEN];
151 sprintf(zfsboot_path, "%s/zfs",
152 getenv("BSDINSTALL_TMPBOOT"));
153 mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
155 sprintf(command, "%s -o cachefile=%s/zpool.cache ",
156 command, zfsboot_path);
158 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
159 if (items[i].state == 0)
161 if (strcmp(items[i].name, "fletcher4") == 0)
162 strcat(command, "-O checksum=fletcher4 ");
163 else if (strcmp(items[i].name, "fletcher2") == 0)
164 strcat(command, "-O checksum=fletcher2 ");
165 else if (strcmp(items[i].name, "sha256") == 0)
166 strcat(command, "-O checksum=sha256 ");
167 else if (strcmp(items[i].name, "atime") == 0)
168 strcat(command, "-O atime=off ");
170 } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) {
172 DIALOG_LISTITEM items[] = {
173 {"FAT32", "FAT Type 32",
174 "Create a FAT32 filesystem (default)", 1 },
175 {"FAT16", "FAT Type 16",
176 "Create a FAT16 filesystem", 0 },
177 {"FAT12", "FAT Type 12",
178 "Create a FAT12 filesystem", 0 },
183 choice = dlg_checklist("FAT Options", "", 0, 0, 0,
184 sizeof(items)/sizeof(items[0]), items, NULL,
186 if (choice == 1) /* Cancel */
190 strcpy(command, "newfs_msdos ");
191 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
192 if (items[i].state == 0)
194 if (strcmp(items[i].name, "FAT32") == 0)
195 strcat(command, "-F 32 ");
196 else if (strcmp(items[i].name, "FAT16") == 0)
197 strcat(command, "-F 16 ");
198 else if (strcmp(items[i].name, "FAT12") == 0)
199 strcat(command, "-F 12 ");
203 dialog_msgbox("Error", "No configurable options exist "
204 "for this filesystem.", 0, 0, TRUE);
210 choose_part_type(const char *def_scheme)
213 const char *scheme = NULL;
215 DIALOG_LISTITEM items[] = {
216 {"APM", "Apple Partition Map",
217 "Bootable on PowerPC Apple Hardware", 0 },
218 {"BSD", "BSD Labels",
219 "Bootable on most x86 systems", 0 },
220 {"GPT", "GUID Partition Table",
221 "Bootable on most x86 systems", 0 },
222 {"MBR", "DOS Partitions",
223 "Bootable on most x86 systems", 0 },
224 {"PC98", "NEC PC9801 Partition Table",
225 "Bootable on NEC PC9801 systems", 0 },
226 {"VTOC8", "Sun VTOC8 Partition Table",
227 "Bootable on Sun SPARC systems", 0 },
231 dialog_vars.default_item = __DECONST(char *, def_scheme);
232 cancel = dlg_menu("Partition Scheme",
233 "Select a partition scheme for this volume:", 0, 0, 0,
234 sizeof(items) / sizeof(items[0]), items, &choice, NULL);
235 dialog_vars.default_item = NULL;
240 if (!is_scheme_bootable(items[choice].name)) {
242 sprintf(message, "This partition scheme (%s) is not "
243 "bootable on this platform. Are you sure you want "
244 "to proceed?", items[choice].name);
245 dialog_vars.defaultno = TRUE;
246 cancel = dialog_yesno("Warning", message, 0, 0);
247 dialog_vars.defaultno = FALSE;
248 if (cancel) /* cancel */
252 scheme = items[choice].name;
258 gpart_partition(const char *lg_name, const char *scheme)
265 if (scheme == NULL) {
266 scheme = choose_part_type(default_scheme());
271 if (!is_scheme_bootable(scheme)) {
273 sprintf(message, "This partition scheme (%s) is not "
274 "bootable on this platform. Are you sure you want "
275 "to proceed?", scheme);
276 dialog_vars.defaultno = TRUE;
277 cancel = dialog_yesno("Warning", message, 0, 0);
278 dialog_vars.defaultno = FALSE;
279 if (cancel) { /* cancel */
280 /* Reset scheme so user can choose another */
287 r = gctl_get_handle();
288 gctl_ro_param(r, "class", -1, "PART");
289 gctl_ro_param(r, "arg0", -1, lg_name);
290 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
291 gctl_ro_param(r, "scheme", -1, scheme);
292 gctl_ro_param(r, "verb", -1, "create");
294 errstr = gctl_issue(r);
295 if (errstr != NULL && errstr[0] != '\0') {
296 gpart_show_error("Error", NULL, errstr);
303 if (bootcode_path(scheme) != NULL)
304 get_part_metadata(lg_name, 1)->bootcode = 1;
309 gpart_activate(struct gprovider *pp)
313 const char *errstr, *scheme;
314 const char *attribute = NULL;
318 * Some partition schemes need this partition to be marked 'active'
319 * for it to be bootable.
321 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
322 if (strcmp(gc->lg_name, "scheme") == 0) {
328 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0 ||
329 strcmp(scheme, "PC98") == 0)
330 attribute = "active";
334 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
335 if (strcmp(gc->lg_name, "index") == 0) {
336 idx = atoi(gc->lg_val);
341 r = gctl_get_handle();
342 gctl_ro_param(r, "class", -1, "PART");
343 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
344 gctl_ro_param(r, "verb", -1, "set");
345 gctl_ro_param(r, "attrib", -1, attribute);
346 gctl_ro_param(r, "index", sizeof(idx), &idx);
348 errstr = gctl_issue(r);
349 if (errstr != NULL && errstr[0] != '\0')
350 gpart_show_error("Error", "Error marking partition active:",
356 gpart_set_root(const char *lg_name, const char *attribute)
361 r = gctl_get_handle();
362 gctl_ro_param(r, "class", -1, "PART");
363 gctl_ro_param(r, "arg0", -1, lg_name);
364 gctl_ro_param(r, "flags", -1, "C");
365 gctl_ro_param(r, "verb", -1, "set");
366 gctl_ro_param(r, "attrib", -1, attribute);
368 errstr = gctl_issue(r);
369 if (errstr != NULL && errstr[0] != '\0')
370 gpart_show_error("Error", "Error setting parameter on disk:",
376 gpart_bootcode(struct ggeom *gp)
378 const char *bootcode;
381 const char *errstr, *scheme;
383 size_t bootsize, bytes;
387 * Write default bootcode to the newly partitioned disk, if that
388 * applies on this platform.
390 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
391 if (strcmp(gc->lg_name, "scheme") == 0) {
397 bootcode = bootcode_path(scheme);
398 if (bootcode == NULL)
401 bootfd = open(bootcode, O_RDONLY);
403 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
408 bootsize = lseek(bootfd, 0, SEEK_END);
409 boot = malloc(bootsize);
410 lseek(bootfd, 0, SEEK_SET);
412 while (bytes < bootsize)
413 bytes += read(bootfd, boot + bytes, bootsize - bytes);
416 r = gctl_get_handle();
417 gctl_ro_param(r, "class", -1, "PART");
418 gctl_ro_param(r, "arg0", -1, gp->lg_name);
419 gctl_ro_param(r, "verb", -1, "bootcode");
420 gctl_ro_param(r, "bootcode", bootsize, boot);
422 errstr = gctl_issue(r);
423 if (errstr != NULL && errstr[0] != '\0')
424 gpart_show_error("Bootcode Error", NULL, errstr);
430 gpart_partcode(struct gprovider *pp, const char *fstype)
434 const char *indexstr;
435 char message[255], command[255];
437 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
438 if (strcmp(gc->lg_name, "scheme") == 0) {
444 /* Make sure this partition scheme needs partcode on this platform */
445 if (partcode_path(scheme, fstype) == NULL)
448 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
449 if (strcmp(gc->lg_name, "index") == 0) {
450 indexstr = gc->lg_val;
455 /* Shell out to gpart for partcode for now */
456 sprintf(command, "gpart bootcode -p %s -i %s %s",
457 partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
458 if (system(command) != 0) {
459 sprintf(message, "Error installing partcode on partition %s",
461 dialog_msgbox("Error", message, 0, 0, TRUE);
466 gpart_destroy(struct ggeom *lg_geom)
469 struct gprovider *pp;
473 /* Delete all child metadata */
474 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
477 /* Revert any local changes to get this geom into a pristine state */
478 r = gctl_get_handle();
479 gctl_ro_param(r, "class", -1, "PART");
480 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
481 gctl_ro_param(r, "verb", -1, "undo");
482 gctl_issue(r); /* Ignore errors -- these are non-fatal */
485 /* Now destroy the geom itself */
486 r = gctl_get_handle();
487 gctl_ro_param(r, "class", -1, "PART");
488 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
489 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
490 gctl_ro_param(r, "force", sizeof(force), &force);
491 gctl_ro_param(r, "verb", -1, "destroy");
492 errstr = gctl_issue(r);
493 if (errstr != NULL && errstr[0] != '\0') {
495 * Check if we reverted away the existence of the geom
496 * altogether. Show all other errors to the user.
498 if (strtol(errstr, NULL, 0) != EINVAL)
499 gpart_show_error("Error", NULL, errstr);
503 /* And any metadata associated with the partition scheme itself */
504 delete_part_metadata(lg_geom->lg_name);
508 gpart_edit(struct gprovider *pp)
512 struct gconsumer *cp;
514 const char *errstr, *oldtype, *scheme;
515 struct partition_metadata *md;
519 int hadlabel, choice, junk, nitems;
522 DIALOG_FORMITEM items[] = {
523 {0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
524 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
525 "freebsd-swap)", FALSE},
526 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
527 FALSE, "Partition size. Append K, M, G for kilobytes, "
528 "megabytes or gigabytes.", FALSE},
529 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
530 FALSE, "Path at which to mount this partition (leave blank "
531 "for swap, set to / for root filesystem)", FALSE},
532 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
533 "Partition name. Not all partition schemes support this.",
538 * Find the PART geom we are manipulating. This may be a consumer of
539 * this provider, or its parent. Check the consumer case first.
542 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
543 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
544 /* Check for zombie geoms, treating them as blank */
546 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
547 if (strcmp(gc->lg_name, "scheme") == 0) {
552 if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
553 gpart_partition(cp->lg_geom->lg_name, NULL);
557 /* If this is a nested partition, edit as usual */
558 if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
561 /* Destroy the geom and all sub-partitions */
562 gpart_destroy(cp->lg_geom);
564 /* Now re-partition and return */
565 gpart_partition(cp->lg_geom->lg_name, NULL);
569 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
573 /* Disk not partitioned, so partition it */
574 gpart_partition(pp->lg_name, NULL);
578 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
579 if (strcmp(gc->lg_name, "scheme") == 0) {
585 nitems = scheme_supports_labels(scheme) ? 4 : 3;
587 /* Edit editable parameters of a partition */
589 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
590 if (strcmp(gc->lg_name, "type") == 0) {
591 oldtype = gc->lg_val;
592 items[0].text = gc->lg_val;
594 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
596 items[3].text = gc->lg_val;
598 if (strcmp(gc->lg_name, "index") == 0)
599 idx = atoi(gc->lg_val);
602 TAILQ_FOREACH(md, &part_metadata, metadata) {
603 if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
604 if (md->fstab != NULL)
605 items[2].text = md->fstab->fs_file;
610 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
611 HN_NOSPACE | HN_DECIMAL);
612 items[1].text = sizestr;
615 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
617 if (choice) /* Cancel pressed */
620 /* Check if the label has a / in it */
621 if (strchr(items[3].text, '/') != NULL) {
622 dialog_msgbox("Error", "Label contains a /, which is not an "
623 "allowed character.", 0, 0, TRUE);
627 r = gctl_get_handle();
628 gctl_ro_param(r, "class", -1, "PART");
629 gctl_ro_param(r, "arg0", -1, geom->lg_name);
630 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
631 gctl_ro_param(r, "verb", -1, "modify");
632 gctl_ro_param(r, "index", sizeof(idx), &idx);
633 if (hadlabel || items[3].text[0] != '\0')
634 gctl_ro_param(r, "label", -1, items[3].text);
635 gctl_ro_param(r, "type", -1, items[0].text);
636 errstr = gctl_issue(r);
637 if (errstr != NULL && errstr[0] != '\0') {
638 gpart_show_error("Error", NULL, errstr);
644 newfs_command(items[0].text, newfs, 1);
645 set_default_part_metadata(pp->lg_name, scheme, items[0].text,
646 items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
650 if (strcmp(oldtype, items[0].text) != 0 && cp != NULL)
651 gpart_destroy(cp->lg_geom);
652 if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text,
654 gpart_partition(pp->lg_name, "BSD");
656 for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
657 if (items[i].text_free)
662 set_default_part_metadata(const char *name, const char *scheme,
663 const char *type, const char *mountpoint, const char *newfs)
665 struct partition_metadata *md;
666 char *zpool_name = NULL;
669 /* Set part metadata */
670 md = get_part_metadata(name, 1);
673 if (md->newfs != NULL) {
678 if (newfs != NULL && newfs[0] != '\0') {
679 md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
680 strlen(mountpoint) + 5 + strlen(name) + 1);
681 if (strcmp("freebsd-zfs", type) == 0) {
682 zpool_name = strdup((strlen(mountpoint) == 1) ?
683 "root" : &mountpoint[1]);
684 for (i = 0; zpool_name[i] != 0; i++)
685 if (!isalnum(zpool_name[i]))
687 sprintf(md->newfs, "%s %s /dev/%s", newfs,
690 sprintf(md->newfs, "%s /dev/%s", newfs, name);
695 if (strcmp(type, "freebsd-swap") == 0)
697 if (strcmp(type, bootpart_type(scheme)) == 0)
700 /* VTOC8 needs partcode at the start of partitions */
701 if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
702 || strcmp(type, "freebsd-zfs") == 0))
705 if (mountpoint == NULL || mountpoint[0] == '\0') {
706 if (md->fstab != NULL) {
707 free(md->fstab->fs_spec);
708 free(md->fstab->fs_file);
709 free(md->fstab->fs_vfstype);
710 free(md->fstab->fs_mntops);
711 free(md->fstab->fs_type);
716 if (md->fstab == NULL) {
717 md->fstab = malloc(sizeof(struct fstab));
719 free(md->fstab->fs_spec);
720 free(md->fstab->fs_file);
721 free(md->fstab->fs_vfstype);
722 free(md->fstab->fs_mntops);
723 free(md->fstab->fs_type);
725 if (strcmp("freebsd-zfs", type) == 0) {
726 md->fstab->fs_spec = strdup(zpool_name);
728 md->fstab->fs_spec = malloc(strlen(name) +
729 strlen("/dev/") + 1);
730 sprintf(md->fstab->fs_spec, "/dev/%s", name);
732 md->fstab->fs_file = strdup(mountpoint);
733 /* Get VFS from text after freebsd-, if possible */
734 if (strncmp("freebsd-", type, 8) == 0)
735 md->fstab->fs_vfstype = strdup(&type[8]);
736 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0)
737 md->fstab->fs_vfstype = strdup("msdosfs");
739 md->fstab->fs_vfstype = strdup(type); /* Guess */
740 if (strcmp(type, "freebsd-swap") == 0) {
741 md->fstab->fs_type = strdup(FSTAB_SW);
742 md->fstab->fs_freq = 0;
743 md->fstab->fs_passno = 0;
744 } else if (strcmp(type, "freebsd-zfs") == 0) {
745 md->fstab->fs_type = strdup(FSTAB_RW);
746 md->fstab->fs_freq = 0;
747 md->fstab->fs_passno = 0;
749 md->fstab->fs_type = strdup(FSTAB_RW);
750 if (strcmp(mountpoint, "/") == 0) {
751 md->fstab->fs_freq = 1;
752 md->fstab->fs_passno = 1;
754 md->fstab->fs_freq = 2;
755 md->fstab->fs_passno = 2;
758 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
761 if (zpool_name != NULL)
766 int part_compare(const void *xa, const void *xb)
768 struct gprovider **a = (struct gprovider **)xa;
769 struct gprovider **b = (struct gprovider **)xb;
770 intmax_t astart, bstart;
774 LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
775 if (strcmp(gc->lg_name, "start") == 0) {
776 astart = strtoimax(gc->lg_val, NULL, 0);
779 LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
780 if (strcmp(gc->lg_name, "start") == 0) {
781 bstart = strtoimax(gc->lg_val, NULL, 0);
787 else if (astart > bstart)
794 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
797 struct gprovider *pp, **providers;
798 intmax_t sectorsize, stripesize, offset;
801 intmax_t maxsize, maxstart;
802 intmax_t partstart, partend;
805 /* Now get the maximum free size and free start */
807 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
808 if (strcmp(gc->lg_name, "first") == 0)
809 start = strtoimax(gc->lg_val, NULL, 0);
810 if (strcmp(gc->lg_name, "last") == 0)
811 end = strtoimax(gc->lg_val, NULL, 0);
815 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
817 providers = calloc(nparts, sizeof(providers[0]));
818 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
820 qsort(providers, nparts, sizeof(providers[0]), part_compare);
824 for (i = 0; i < nparts; i++) {
827 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
828 if (strcmp(gc->lg_name, "start") == 0)
829 partstart = strtoimax(gc->lg_val, NULL, 0);
830 if (strcmp(gc->lg_name, "end") == 0)
831 partend = strtoimax(gc->lg_val, NULL, 0);
834 if (partstart - lastend > maxsize) {
835 maxsize = partstart - lastend - 1;
836 maxstart = lastend + 1;
842 if (end - lastend > maxsize) {
843 maxsize = end - lastend - 1;
844 maxstart = lastend + 1;
847 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
850 * Round the start and size of the largest available space up to
851 * the nearest multiple of the adjusted stripe size.
853 * The adjusted stripe size is the least common multiple of the
854 * actual stripe size, or the sector size if no stripe size was
855 * reported, and 4096. The reason for this is that contemporary
856 * disks often have 4096-byte physical sectors but report 512
857 * bytes instead for compatibility with older / broken operating
858 * systems and BIOSes. For the same reasons, virtualized storage
859 * may also report a 512-byte stripe size, or none at all.
861 sectorsize = pp->lg_sectorsize;
862 if ((stripesize = pp->lg_stripesize) == 0)
863 stripesize = sectorsize;
864 while (stripesize % 4096 != 0)
866 if ((offset = maxstart * sectorsize % stripesize) != 0) {
867 offset = (stripesize - offset) / sectorsize;
872 if (npartstart != NULL)
873 *npartstart = maxstart;
879 gpart_create(struct gprovider *pp, char *default_type, char *default_size,
880 char *default_mountpoint, char **partname, int interactive)
884 struct gconsumer *cp;
886 const char *errstr, *scheme;
887 char sizestr[32], startstr[32], output[64], *newpartname;
888 char newfs[255], options_fstype[64];
889 intmax_t maxsize, size, sector, firstfree, stripe;
891 int nitems, choice, junk;
894 DIALOG_FORMITEM items[] = {
895 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
896 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
897 "freebsd-swap)", FALSE},
898 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
899 FALSE, "Partition size. Append K, M, G for kilobytes, "
900 "megabytes or gigabytes.", FALSE},
901 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
902 FALSE, "Path at which to mount partition (blank for "
903 "swap, / for root filesystem)", FALSE},
904 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
905 "Partition name. Not all partition schemes support this.",
909 if (partname != NULL)
912 /* Record sector and stripe sizes */
913 sector = pp->lg_sectorsize;
914 stripe = pp->lg_stripesize;
917 * Find the PART geom we are manipulating. This may be a consumer of
918 * this provider, or its parent. Check the consumer case first.
921 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
922 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
927 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
930 /* Now get the partition scheme */
933 LIST_FOREACH(gc, &geom->lg_config, lg_config)
934 if (strcmp(gc->lg_name, "scheme") == 0)
938 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
939 if (gpart_partition(pp->lg_name, NULL) == 0)
941 "The partition table has been successfully created."
942 " Please press Create again to create partitions.",
949 * If we still don't have a geom, either the user has
950 * canceled partitioning or there has been an error which has already
951 * been displayed, so bail.
956 maxsize = size = gpart_max_free(geom, &firstfree);
958 dialog_msgbox("Error", "No free space left on device.", 0, 0,
963 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
964 HN_NOSPACE | HN_DECIMAL);
965 items[1].text = sizestr;
967 /* Special-case the MBR default type for nested partitions */
968 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "PC98") == 0) {
969 items[0].text = "freebsd";
970 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
973 nitems = scheme_supports_labels(scheme) ? 4 : 3;
975 if (default_type != NULL)
976 items[0].text = default_type;
977 if (default_size != NULL)
978 items[1].text = default_size;
979 if (default_mountpoint != NULL)
980 items[2].text = default_mountpoint;
982 /* Default options */
983 strncpy(options_fstype, items[0].text,
984 sizeof(options_fstype));
985 newfs_command(options_fstype, newfs, 1);
988 dialog_vars.extra_label = "Options";
989 dialog_vars.extra_button = TRUE;
990 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
992 dialog_vars.extra_button = FALSE;
998 case 3: /* Options */
999 strncpy(options_fstype, items[0].text,
1000 sizeof(options_fstype));
1001 newfs_command(options_fstype, newfs, 0);
1007 * If the user changed the fs type after specifying options, undo
1008 * their choices in favor of the new filesystem's defaults.
1010 if (strcmp(options_fstype, items[0].text) != 0) {
1011 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1012 newfs_command(options_fstype, newfs, 1);
1016 if (strlen(items[1].text) > 0) {
1017 if (expand_number(items[1].text, &bytes) != 0) {
1020 sprintf(error, "Invalid size: %s\n", strerror(errno));
1021 dialog_msgbox("Error", error, 0, 0, TRUE);
1024 size = MIN((intmax_t)(bytes/sector), maxsize);
1027 /* Check if the label has a / in it */
1028 if (strchr(items[3].text, '/') != NULL) {
1029 dialog_msgbox("Error", "Label contains a /, which is not an "
1030 "allowed character.", 0, 0, TRUE);
1034 /* Warn if no mountpoint set */
1035 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1036 items[2].text[0] != '/') {
1037 dialog_vars.defaultno = TRUE;
1038 choice = dialog_yesno("Warning",
1039 "This partition does not have a valid mountpoint "
1040 "(for the partition from which you intend to boot the "
1041 "operating system, the mountpoint should be /). Are you "
1042 "sure you want to continue?"
1044 dialog_vars.defaultno = FALSE;
1045 if (choice == 1) /* cancel */
1050 * Error if this scheme needs nested partitions, this is one, and
1051 * a mountpoint was set.
1053 if (strcmp(items[0].text, "freebsd") == 0 &&
1054 strlen(items[2].text) > 0) {
1055 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1056 "nested BSD-type partition schemes and cannot have "
1057 "mountpoints. After creating one, select it and press "
1058 "Create again to add the actual file systems.", 0, 0, TRUE);
1062 /* If this is the root partition, check that this scheme is bootable */
1063 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1065 sprintf(message, "This partition scheme (%s) is not bootable "
1066 "on this platform. Are you sure you want to proceed?",
1068 dialog_vars.defaultno = TRUE;
1069 choice = dialog_yesno("Warning", message, 0, 0);
1070 dialog_vars.defaultno = FALSE;
1071 if (choice == 1) /* cancel */
1075 /* If this is the root partition, check that this fs is bootable */
1076 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1079 sprintf(message, "This file system (%s) is not bootable "
1080 "on this system. Are you sure you want to proceed?",
1082 dialog_vars.defaultno = TRUE;
1083 choice = dialog_yesno("Warning", message, 0, 0);
1084 dialog_vars.defaultno = FALSE;
1085 if (choice == 1) /* cancel */
1090 * If this is the root partition, and we need a boot partition, ask
1091 * the user to add one.
1094 /* Check for existing freebsd-boot partition */
1095 LIST_FOREACH(pp, &geom->lg_provider, lg_provider) {
1096 struct partition_metadata *md;
1097 md = get_part_metadata(pp->lg_name, 0);
1098 if (md == NULL || !md->bootcode)
1100 LIST_FOREACH(gc, &pp->lg_config, lg_config)
1101 if (strcmp(gc->lg_name, "type") == 0)
1103 if (gc != NULL && strcmp(gc->lg_val,
1104 bootpart_type(scheme)) == 0)
1108 /* If there isn't one, and we need one, ask */
1109 if ((strcmp(items[0].text, "freebsd") == 0 ||
1110 strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0 &&
1113 choice = dialog_yesno("Boot Partition",
1114 "This partition scheme requires a boot partition "
1115 "for the disk to be bootable. Would you like to "
1116 "make one now?", 0, 0);
1120 if (choice == 0) { /* yes */
1121 r = gctl_get_handle();
1122 gctl_ro_param(r, "class", -1, "PART");
1123 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1124 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1125 gctl_ro_param(r, "verb", -1, "add");
1126 gctl_ro_param(r, "type", -1, bootpart_type(scheme));
1127 snprintf(sizestr, sizeof(sizestr), "%jd",
1128 bootpart_size(scheme) / sector);
1129 gctl_ro_param(r, "size", -1, sizestr);
1130 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1131 gctl_ro_param(r, "start", -1, startstr);
1132 gctl_rw_param(r, "output", sizeof(output), output);
1133 errstr = gctl_issue(r);
1134 if (errstr != NULL && errstr[0] != '\0')
1135 gpart_show_error("Error", NULL, errstr);
1138 get_part_metadata(strtok(output, " "), 1)->bootcode = 1;
1140 /* Now adjust the part we are really adding forward */
1141 firstfree += bootpart_size(scheme) / sector;
1142 size -= (bootpart_size(scheme) + stripe)/sector;
1143 if (stripe > 0 && (firstfree*sector % stripe) != 0)
1144 firstfree += (stripe - ((firstfree*sector) %
1149 r = gctl_get_handle();
1150 gctl_ro_param(r, "class", -1, "PART");
1151 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1152 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1153 gctl_ro_param(r, "verb", -1, "add");
1155 gctl_ro_param(r, "type", -1, items[0].text);
1156 snprintf(sizestr, sizeof(sizestr), "%jd", size);
1157 gctl_ro_param(r, "size", -1, sizestr);
1158 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1159 gctl_ro_param(r, "start", -1, startstr);
1160 if (items[3].text[0] != '\0')
1161 gctl_ro_param(r, "label", -1, items[3].text);
1162 gctl_rw_param(r, "output", sizeof(output), output);
1163 errstr = gctl_issue(r);
1164 if (errstr != NULL && errstr[0] != '\0') {
1165 gpart_show_error("Error", NULL, errstr);
1169 newpartname = strtok(output, " ");
1173 * Try to destroy any geom that gpart picked up already here from
1176 r = gctl_get_handle();
1177 gctl_ro_param(r, "class", -1, "PART");
1178 gctl_ro_param(r, "arg0", -1, newpartname);
1179 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1181 gctl_ro_param(r, "force", sizeof(junk), &junk);
1182 gctl_ro_param(r, "verb", -1, "destroy");
1183 gctl_issue(r); /* Error usually expected and non-fatal */
1186 if (strcmp(items[0].text, bootpart_type(scheme)) == 0)
1187 get_part_metadata(newpartname, 1)->bootcode = 1;
1188 else if (strcmp(items[0].text, "freebsd") == 0)
1189 gpart_partition(newpartname, "BSD");
1191 set_default_part_metadata(newpartname, scheme,
1192 items[0].text, items[2].text, newfs);
1194 for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
1195 if (items[i].text_free)
1196 free(items[i].text);
1198 if (partname != NULL)
1199 *partname = strdup(newpartname);
1203 gpart_delete(struct gprovider *pp)
1207 struct gconsumer *cp;
1213 /* Is it a partition? */
1214 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1216 /* Find out if this is the root of a gpart geom */
1218 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1219 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1224 /* If so, destroy all children */
1226 gpart_destroy(geom);
1228 /* If this is a partition, revert it, so it can be deleted */
1230 r = gctl_get_handle();
1231 gctl_ro_param(r, "class", -1, "PART");
1232 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1233 gctl_ro_param(r, "verb", -1, "undo");
1234 gctl_issue(r); /* Ignore non-fatal errors */
1240 * If this is not a partition, see if that is a problem, complain if
1241 * necessary, and return always, since we need not do anything further,
1244 if (!is_partition) {
1246 dialog_msgbox("Error",
1247 "Only partitions can be deleted.", 0, 0, TRUE);
1251 r = gctl_get_handle();
1252 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1253 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1254 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1255 gctl_ro_param(r, "verb", -1, "delete");
1257 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1258 if (strcmp(gc->lg_name, "index") == 0) {
1259 idx = atoi(gc->lg_val);
1260 gctl_ro_param(r, "index", sizeof(idx), &idx);
1265 errstr = gctl_issue(r);
1266 if (errstr != NULL && errstr[0] != '\0') {
1267 gpart_show_error("Error", NULL, errstr);
1274 delete_part_metadata(pp->lg_name);
1278 gpart_revert_all(struct gmesh *mesh)
1280 struct gclass *classp;
1284 const char *modified;
1286 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1287 if (strcmp(classp->lg_name, "PART") == 0)
1291 if (strcmp(classp->lg_name, "PART") != 0) {
1292 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1296 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1297 modified = "true"; /* XXX: If we don't know (kernel too old),
1298 * assume there are modifications. */
1299 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1300 if (strcmp(gc->lg_name, "modified") == 0) {
1301 modified = gc->lg_val;
1306 if (strcmp(modified, "false") == 0)
1309 r = gctl_get_handle();
1310 gctl_ro_param(r, "class", -1, "PART");
1311 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1312 gctl_ro_param(r, "verb", -1, "undo");
1320 gpart_commit(struct gmesh *mesh)
1322 struct partition_metadata *md;
1323 struct gclass *classp;
1326 struct gconsumer *cp;
1327 struct gprovider *pp;
1330 const char *modified;
1333 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1334 if (strcmp(classp->lg_name, "PART") == 0)
1338 /* Figure out what filesystem / uses */
1339 rootfs = "ufs"; /* Assume ufs if nothing else present */
1340 TAILQ_FOREACH(md, &part_metadata, metadata) {
1341 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1342 rootfs = md->fstab->fs_vfstype;
1347 if (strcmp(classp->lg_name, "PART") != 0) {
1348 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1352 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1353 modified = "true"; /* XXX: If we don't know (kernel too old),
1354 * assume there are modifications. */
1355 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1356 if (strcmp(gc->lg_name, "modified") == 0) {
1357 modified = gc->lg_val;
1362 if (strcmp(modified, "false") == 0)
1365 /* Add bootcode if necessary, before the commit */
1366 md = get_part_metadata(gp->lg_name, 0);
1367 if (md != NULL && md->bootcode)
1370 /* Now install partcode on its partitions, if necessary */
1371 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1372 md = get_part_metadata(pp->lg_name, 0);
1373 if (md == NULL || !md->bootcode)
1376 /* Mark this partition active if that's required */
1379 /* Check if the partition has sub-partitions */
1380 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1381 if (strcmp(cp->lg_geom->lg_class->lg_name,
1385 if (cp == NULL) /* No sub-partitions */
1386 gpart_partcode(pp, rootfs);
1389 r = gctl_get_handle();
1390 gctl_ro_param(r, "class", -1, "PART");
1391 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1392 gctl_ro_param(r, "verb", -1, "commit");
1394 errstr = gctl_issue(r);
1395 if (errstr != NULL && errstr[0] != '\0')
1396 gpart_show_error("Error", NULL, errstr);