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 #define GPART_FLAGS "x" /* Do not commit changes by default */
43 gpart_show_error(const char *title, const char *explanation, const char *errstr)
49 if (explanation == NULL)
52 error = strtol(errstr, &errmsg, 0);
53 if (errmsg != errstr) {
54 while (errmsg[0] == ' ')
56 if (errmsg[0] != '\0')
57 sprintf(message, "%s%s. %s", explanation,
58 strerror(error), errmsg);
60 sprintf(message, "%s%s", explanation, strerror(error));
62 sprintf(message, "%s%s", explanation, errmsg);
65 dialog_msgbox(title, message, 0, 0, TRUE);
69 scheme_supports_labels(const char *scheme)
71 if (strcmp(scheme, "APM") == 0)
73 if (strcmp(scheme, "GPT") == 0)
75 if (strcmp(scheme, "PC98") == 0)
82 newfs_command(const char *fstype, char *command, int use_default)
84 if (strcmp(fstype, "freebsd-ufs") == 0) {
86 DIALOG_LISTITEM items[] = {
87 {"UFS1", "UFS Version 1",
88 "Use version 1 of the UFS file system instead "
89 "of version 2 (not recommended)", 0 },
91 "Enable softupdates (default)", 1 },
92 {"SUJ", "Softupdates journaling",
93 "Enable file system journaling (default - "
94 "turn off for SSDs)", 1 },
95 {"TRIM", "Enable SSD TRIM support",
96 "Enable TRIM support, useful on solid-state drives",
102 choice = dlg_checklist("UFS Options", "", 0, 0, 0,
103 sizeof(items)/sizeof(items[0]), items, NULL,
105 if (choice == 1) /* Cancel */
109 strcpy(command, "newfs ");
110 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
111 if (items[i].state == 0)
113 if (strcmp(items[i].name, "UFS1") == 0)
114 strcat(command, "-O1 ");
115 else if (strcmp(items[i].name, "SU") == 0)
116 strcat(command, "-U ");
117 else if (strcmp(items[i].name, "SUJ") == 0)
118 strcat(command, "-j ");
119 else if (strcmp(items[i].name, "TRIM") == 0)
120 strcat(command, "-t ");
122 } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) {
124 DIALOG_LISTITEM items[] = {
125 {"FAT32", "FAT Type 32",
126 "Create a FAT32 filesystem (default)", 1 },
127 {"FAT16", "FAT Type 16",
128 "Create a FAT16 filesystem", 0 },
129 {"FAT12", "FAT Type 12",
130 "Create a FAT12 filesystem", 0 },
135 choice = dlg_checklist("FAT Options", "", 0, 0, 0,
136 sizeof(items)/sizeof(items[0]), items, NULL,
138 if (choice == 1) /* Cancel */
142 strcpy(command, "newfs_msdos ");
143 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) {
144 if (items[i].state == 0)
146 if (strcmp(items[i].name, "FAT32") == 0)
147 strcat(command, "-F 32 ");
148 else if (strcmp(items[i].name, "FAT16") == 0)
149 strcat(command, "-F 16 ");
150 else if (strcmp(items[i].name, "SUJ") == 0)
151 strcat(command, "-F 12 ");
155 dialog_msgbox("Error", "No configurable options exist "
156 "for this filesystem.", 0, 0, TRUE);
162 gpart_partition(const char *lg_name, const char *scheme)
168 DIALOG_LISTITEM items[] = {
169 {"APM", "Apple Partition Map",
170 "Bootable on PowerPC Apple Hardware", 0 },
171 {"BSD", "BSD Labels",
172 "Bootable on most x86 systems", 0 },
173 {"GPT", "GUID Partition Table",
174 "Bootable on most x86 systems", 0 },
175 {"MBR", "DOS Partitions",
176 "Bootable on most x86 systems", 0 },
177 {"PC98", "NEC PC9801 Partition Table",
178 "Bootable on NEC PC9801 systems", 0 },
179 {"VTOC8", "Sun VTOC8 Partition Table",
180 "Bootable on Sun SPARC systems", 0 },
184 if (scheme == NULL) {
185 dialog_vars.default_item = __DECONST(char *, default_scheme());
186 cancel = dlg_menu("Partition Scheme",
187 "Select a partition scheme for this volume:", 0, 0, 0,
188 sizeof(items) / sizeof(items[0]), items, &choice, NULL);
189 dialog_vars.default_item = NULL;
194 if (!is_scheme_bootable(items[choice].name)) {
196 sprintf(message, "This partition scheme (%s) is not "
197 "bootable on this platform. Are you sure you want "
198 "to proceed?", items[choice].name);
199 dialog_vars.defaultno = TRUE;
200 cancel = dialog_yesno("Warning", message, 0, 0);
201 dialog_vars.defaultno = FALSE;
202 if (cancel) /* cancel */
206 scheme = items[choice].name;
209 r = gctl_get_handle();
210 gctl_ro_param(r, "class", -1, "PART");
211 gctl_ro_param(r, "arg0", -1, lg_name);
212 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
213 gctl_ro_param(r, "scheme", -1, scheme);
214 gctl_ro_param(r, "verb", -1, "create");
216 errstr = gctl_issue(r);
217 if (errstr != NULL && errstr[0] != '\0') {
218 gpart_show_error("Error", NULL, errstr);
225 if (bootcode_path(scheme) != NULL)
226 get_part_metadata(lg_name, 1)->bootcode = 1;
231 gpart_activate(struct gprovider *pp)
235 const char *errstr, *scheme;
236 const char *attribute = NULL;
240 * Some partition schemes need this partition to be marked 'active'
241 * for it to be bootable.
243 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
244 if (strcmp(gc->lg_name, "scheme") == 0) {
250 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0 ||
251 strcmp(scheme, "PC98") == 0)
252 attribute = "active";
256 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
257 if (strcmp(gc->lg_name, "index") == 0) {
258 idx = atoi(gc->lg_val);
263 r = gctl_get_handle();
264 gctl_ro_param(r, "class", -1, "PART");
265 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
266 gctl_ro_param(r, "verb", -1, "set");
267 gctl_ro_param(r, "attrib", -1, attribute);
268 gctl_ro_param(r, "index", sizeof(idx), &idx);
270 errstr = gctl_issue(r);
271 if (errstr != NULL && errstr[0] != '\0')
272 gpart_show_error("Error", "Error marking partition active:",
278 gpart_bootcode(struct ggeom *gp)
280 const char *bootcode;
283 const char *errstr, *scheme;
285 size_t bootsize, bytes;
289 * Write default bootcode to the newly partitioned disk, if that
290 * applies on this platform.
292 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
293 if (strcmp(gc->lg_name, "scheme") == 0) {
299 bootcode = bootcode_path(scheme);
300 if (bootcode == NULL)
303 bootfd = open(bootcode, O_RDONLY);
305 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
310 bootsize = lseek(bootfd, 0, SEEK_END);
311 boot = malloc(bootsize);
312 lseek(bootfd, 0, SEEK_SET);
314 while (bytes < bootsize)
315 bytes += read(bootfd, boot + bytes, bootsize - bytes);
318 r = gctl_get_handle();
319 gctl_ro_param(r, "class", -1, "PART");
320 gctl_ro_param(r, "arg0", -1, gp->lg_name);
321 gctl_ro_param(r, "verb", -1, "bootcode");
322 gctl_ro_param(r, "bootcode", bootsize, boot);
324 errstr = gctl_issue(r);
325 if (errstr != NULL && errstr[0] != '\0')
326 gpart_show_error("Bootcode Error", NULL, errstr);
332 gpart_partcode(struct gprovider *pp)
336 const char *indexstr;
337 char message[255], command[255];
339 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
340 if (strcmp(gc->lg_name, "scheme") == 0) {
346 /* Make sure this partition scheme needs partcode on this platform */
347 if (partcode_path(scheme) == NULL)
350 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
351 if (strcmp(gc->lg_name, "index") == 0) {
352 indexstr = gc->lg_val;
357 /* Shell out to gpart for partcode for now */
358 sprintf(command, "gpart bootcode -p %s -i %s %s",
359 partcode_path(scheme), indexstr, pp->lg_geom->lg_name);
360 if (system(command) != 0) {
361 sprintf(message, "Error installing partcode on partition %s",
363 dialog_msgbox("Error", message, 0, 0, TRUE);
368 gpart_destroy(struct ggeom *lg_geom)
371 struct gprovider *pp;
375 /* Delete all child metadata */
376 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
379 /* Revert any local changes to get this geom into a pristine state */
380 r = gctl_get_handle();
381 gctl_ro_param(r, "class", -1, "PART");
382 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
383 gctl_ro_param(r, "verb", -1, "undo");
384 gctl_issue(r); /* Ignore errors -- these are non-fatal */
387 /* Now destroy the geom itself */
388 r = gctl_get_handle();
389 gctl_ro_param(r, "class", -1, "PART");
390 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
391 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
392 gctl_ro_param(r, "force", sizeof(force), &force);
393 gctl_ro_param(r, "verb", -1, "destroy");
394 errstr = gctl_issue(r);
395 if (errstr != NULL && errstr[0] != '\0')
396 gpart_show_error("Error", NULL, errstr);
399 /* And any metadata associated with the partition scheme itself */
400 delete_part_metadata(lg_geom->lg_name);
404 gpart_edit(struct gprovider *pp)
408 struct gconsumer *cp;
410 const char *errstr, *oldtype, *scheme;
411 struct partition_metadata *md;
415 int hadlabel, choice, junk, nitems;
418 DIALOG_FORMITEM items[] = {
419 {0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
420 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-swap)",
422 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
423 FALSE, "Partition size. Append K, M, G for kilobytes, "
424 "megabytes or gigabytes.", FALSE},
425 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
426 FALSE, "Path at which to mount this partition (leave blank "
427 "for swap, set to / for root filesystem)", FALSE},
428 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
429 "Partition name. Not all partition schemes support this.",
434 * Find the PART geom we are manipulating. This may be a consumer of
435 * this provider, or its parent. Check the consumer case first.
438 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
439 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
440 /* Check for zombie geoms, treating them as blank */
442 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
443 if (strcmp(gc->lg_name, "scheme") == 0) {
448 if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
449 gpart_partition(cp->lg_geom->lg_name, NULL);
453 /* Destroy the geom and all sub-partitions */
454 gpart_destroy(cp->lg_geom);
456 /* Now re-partition and return */
457 gpart_partition(cp->lg_geom->lg_name, NULL);
461 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
465 /* Disk not partitioned, so partition it */
466 gpart_partition(pp->lg_name, NULL);
470 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
471 if (strcmp(gc->lg_name, "scheme") == 0) {
477 nitems = scheme_supports_labels(scheme) ? 4 : 3;
479 /* Edit editable parameters of a partition */
481 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
482 if (strcmp(gc->lg_name, "type") == 0) {
483 oldtype = gc->lg_val;
484 items[0].text = gc->lg_val;
486 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
488 items[3].text = gc->lg_val;
490 if (strcmp(gc->lg_name, "index") == 0)
491 idx = atoi(gc->lg_val);
494 TAILQ_FOREACH(md, &part_metadata, metadata) {
495 if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
496 if (md->fstab != NULL)
497 items[2].text = md->fstab->fs_file;
502 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
503 HN_NOSPACE | HN_DECIMAL);
504 items[1].text = sizestr;
507 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
509 if (choice) /* Cancel pressed */
512 /* Check if the label has a / in it */
513 if (strchr(items[3].text, '/') != NULL) {
514 dialog_msgbox("Error", "Label contains a /, which is not an "
515 "allowed character.", 0, 0, TRUE);
519 r = gctl_get_handle();
520 gctl_ro_param(r, "class", -1, "PART");
521 gctl_ro_param(r, "arg0", -1, geom->lg_name);
522 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
523 gctl_ro_param(r, "verb", -1, "modify");
524 gctl_ro_param(r, "index", sizeof(idx), &idx);
525 if (hadlabel || items[3].text[0] != '\0')
526 gctl_ro_param(r, "label", -1, items[3].text);
527 gctl_ro_param(r, "type", -1, items[0].text);
528 errstr = gctl_issue(r);
529 if (errstr != NULL && errstr[0] != '\0') {
530 gpart_show_error("Error", NULL, errstr);
536 newfs_command(items[0].text, newfs, 1);
537 set_default_part_metadata(pp->lg_name, scheme, items[0].text,
538 items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
541 for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
542 if (items[i].text_free)
547 set_default_part_metadata(const char *name, const char *scheme,
548 const char *type, const char *mountpoint, const char *newfs)
550 struct partition_metadata *md;
552 /* Set part metadata */
553 md = get_part_metadata(name, 1);
556 if (md->newfs != NULL) {
561 if (newfs != NULL && newfs[0] != '\0') {
562 md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
564 sprintf(md->newfs, "%s /dev/%s", newfs, name);
568 if (strcmp(type, "freebsd-swap") == 0)
570 if (strcmp(type, "freebsd-boot") == 0)
573 /* VTOC8 needs partcode in UFS partitions */
574 if (strcmp(scheme, "VTOC8") == 0 && strcmp(type, "freebsd-ufs") == 0)
577 if (mountpoint == NULL || mountpoint[0] == '\0') {
578 if (md->fstab != NULL) {
579 free(md->fstab->fs_spec);
580 free(md->fstab->fs_file);
581 free(md->fstab->fs_vfstype);
582 free(md->fstab->fs_mntops);
583 free(md->fstab->fs_type);
588 if (md->fstab == NULL) {
589 md->fstab = malloc(sizeof(struct fstab));
591 free(md->fstab->fs_spec);
592 free(md->fstab->fs_file);
593 free(md->fstab->fs_vfstype);
594 free(md->fstab->fs_mntops);
595 free(md->fstab->fs_type);
597 md->fstab->fs_spec = malloc(strlen(name) + 6);
598 sprintf(md->fstab->fs_spec, "/dev/%s", name);
599 md->fstab->fs_file = strdup(mountpoint);
600 /* Get VFS from text after freebsd-, if possible */
601 if (strncmp("freebsd-", type, 8) == 0)
602 md->fstab->fs_vfstype = strdup(&type[8]);
603 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0)
604 md->fstab->fs_vfstype = strdup("msdosfs");
606 md->fstab->fs_vfstype = strdup(type); /* Guess */
607 if (strcmp(type, "freebsd-swap") == 0) {
608 md->fstab->fs_type = strdup(FSTAB_SW);
609 md->fstab->fs_freq = 0;
610 md->fstab->fs_passno = 0;
612 md->fstab->fs_type = strdup(FSTAB_RW);
613 if (strcmp(mountpoint, "/") == 0) {
614 md->fstab->fs_freq = 1;
615 md->fstab->fs_passno = 1;
617 md->fstab->fs_freq = 2;
618 md->fstab->fs_passno = 2;
621 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
626 int part_compare(const void *xa, const void *xb)
628 struct gprovider **a = (struct gprovider **)xa;
629 struct gprovider **b = (struct gprovider **)xb;
630 intmax_t astart, bstart;
634 LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
635 if (strcmp(gc->lg_name, "start") == 0) {
636 astart = strtoimax(gc->lg_val, NULL, 0);
639 LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
640 if (strcmp(gc->lg_name, "start") == 0) {
641 bstart = strtoimax(gc->lg_val, NULL, 0);
647 else if (astart > bstart)
654 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
657 struct gprovider *pp, **providers;
660 intmax_t maxsize, maxstart;
661 intmax_t partstart, partend;
664 /* Now get the maximum free size and free start */
666 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
667 if (strcmp(gc->lg_name, "first") == 0)
668 start = strtoimax(gc->lg_val, NULL, 0);
669 if (strcmp(gc->lg_name, "last") == 0)
670 end = strtoimax(gc->lg_val, NULL, 0);
674 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
676 providers = calloc(nparts, sizeof(providers[0]));
677 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
679 qsort(providers, nparts, sizeof(providers[0]), part_compare);
683 for (i = 0; i < nparts; i++) {
686 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
687 if (strcmp(gc->lg_name, "start") == 0)
688 partstart = strtoimax(gc->lg_val, NULL, 0);
689 if (strcmp(gc->lg_name, "end") == 0)
690 partend = strtoimax(gc->lg_val, NULL, 0);
693 if (partstart - lastend > maxsize) {
694 maxsize = partstart - lastend - 1;
695 maxstart = lastend + 1;
701 if (end - lastend > maxsize) {
702 maxsize = end - lastend - 1;
703 maxstart = lastend + 1;
706 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
708 /* Compute beginning of new partition and maximum available space */
709 if (pp->lg_stripesize > 0 &&
710 (maxstart*pp->lg_sectorsize % pp->lg_stripesize) != 0) {
711 intmax_t offset = (pp->lg_stripesize -
712 ((maxstart*pp->lg_sectorsize) % pp->lg_stripesize)) /
718 if (npartstart != NULL)
719 *npartstart = maxstart;
725 gpart_create(struct gprovider *pp, char *default_type, char *default_size,
726 char *default_mountpoint, char **partname, int interactive)
730 struct gconsumer *cp;
732 const char *errstr, *scheme;
733 char sizestr[32], startstr[32], output[64], *newpartname;
734 char newfs[64], options_fstype[64];
735 intmax_t maxsize, size, sector, firstfree, stripe;
737 int nitems, choice, junk;
740 DIALOG_FORMITEM items[] = {
741 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
742 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-swap)",
744 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
745 FALSE, "Partition size. Append K, M, G for kilobytes, "
746 "megabytes or gigabytes.", FALSE},
747 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
748 FALSE, "Path at which to mount partition (blank for "
749 "swap, / for root filesystem)", FALSE},
750 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
751 "Partition name. Not all partition schemes support this.",
755 if (partname != NULL)
758 /* Record sector and stripe sizes */
759 sector = pp->lg_sectorsize;
760 stripe = pp->lg_stripesize;
763 * Find the PART geom we are manipulating. This may be a consumer of
764 * this provider, or its parent. Check the consumer case first.
767 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
768 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
773 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
776 /* Now get the partition scheme */
779 LIST_FOREACH(gc, &geom->lg_config, lg_config)
780 if (strcmp(gc->lg_name, "scheme") == 0)
784 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
785 if (gpart_partition(pp->lg_name, NULL) == 0)
787 "The partition table has been successfully created."
788 " Please press Create again to create partitions.",
795 * If we still don't have a geom, either the user has
796 * canceled partitioning or there has been an error which has already
797 * been displayed, so bail.
802 maxsize = size = gpart_max_free(geom, &firstfree);
804 dialog_msgbox("Error", "No free space left on device.", 0, 0,
809 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
810 HN_NOSPACE | HN_DECIMAL);
811 items[1].text = sizestr;
813 /* Special-case the MBR default type for nested partitions */
814 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "PC98") == 0) {
815 items[0].text = "freebsd";
816 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
819 nitems = scheme_supports_labels(scheme) ? 4 : 3;
821 if (default_type != NULL)
822 items[0].text = default_type;
823 if (default_size != NULL)
824 items[1].text = default_size;
825 if (default_mountpoint != NULL)
826 items[2].text = default_mountpoint;
828 /* Default options */
829 strncpy(options_fstype, items[0].text,
830 sizeof(options_fstype));
831 newfs_command(options_fstype, newfs, 1);
834 dialog_vars.extra_label = "Options";
835 dialog_vars.extra_button = TRUE;
836 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
838 dialog_vars.extra_button = FALSE;
844 case 3: /* Options */
845 strncpy(options_fstype, items[0].text,
846 sizeof(options_fstype));
847 newfs_command(options_fstype, newfs, 0);
853 * If the user changed the fs type after specifying options, undo
854 * their choices in favor of the new filesystem's defaults.
856 if (strcmp(options_fstype, items[0].text) != 0) {
857 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
858 newfs_command(options_fstype, newfs, 1);
862 if (strlen(items[1].text) > 0) {
863 if (expand_number(items[1].text, &bytes) != 0) {
866 sprintf(error, "Invalid size: %s\n", strerror(errno));
867 dialog_msgbox("Error", error, 0, 0, TRUE);
870 size = MIN((intmax_t)(bytes/sector), maxsize);
873 /* Check if the label has a / in it */
874 if (strchr(items[3].text, '/') != NULL) {
875 dialog_msgbox("Error", "Label contains a /, which is not an "
876 "allowed character.", 0, 0, TRUE);
880 /* Warn if no mountpoint set */
881 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
882 items[2].text[0] != '/') {
883 dialog_vars.defaultno = TRUE;
884 choice = dialog_yesno("Warning",
885 "This partition does not have a valid mountpoint "
886 "(for the partition from which you intend to boot the "
887 "operating system, the mountpoint should be /). Are you "
888 "sure you want to continue?"
890 dialog_vars.defaultno = FALSE;
891 if (choice == 1) /* cancel */
896 * Error if this scheme needs nested partitions, this is one, and
897 * a mountpoint was set.
899 if (strcmp(items[0].text, "freebsd") == 0 &&
900 strlen(items[2].text) > 0) {
901 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
902 "nested BSD-type partition schemes and cannot have "
903 "mountpoints. After creating one, select it and press "
904 "Create again to add the actual file systems.", 0, 0, TRUE);
908 /* If this is the root partition, check that this scheme is bootable */
909 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
911 sprintf(message, "This partition scheme (%s) is not bootable "
912 "on this platform. Are you sure you want to proceed?",
914 dialog_vars.defaultno = TRUE;
915 choice = dialog_yesno("Warning", message, 0, 0);
916 dialog_vars.defaultno = FALSE;
917 if (choice == 1) /* cancel */
922 * If this is the root partition, and we need a boot partition, ask
923 * the user to add one.
926 /* Check for existing freebsd-boot partition */
927 LIST_FOREACH(pp, &geom->lg_provider, lg_provider) {
928 struct partition_metadata *md;
929 md = get_part_metadata(pp->lg_name, 0);
930 if (md == NULL || !md->bootcode)
932 LIST_FOREACH(gc, &pp->lg_config, lg_config)
933 if (strcmp(gc->lg_name, "type") == 0)
935 if (gc != NULL && strcmp(gc->lg_val, "freebsd-boot") == 0)
939 /* If there isn't one, and we need one, ask */
940 if (strcmp(items[2].text, "/") == 0 && bootpart_size(scheme) > 0 &&
943 choice = dialog_yesno("Boot Partition",
944 "This partition scheme requires a boot partition "
945 "for the disk to be bootable. Would you like to "
946 "make one now?", 0, 0);
950 if (choice == 0) { /* yes */
951 r = gctl_get_handle();
952 gctl_ro_param(r, "class", -1, "PART");
953 gctl_ro_param(r, "arg0", -1, geom->lg_name);
954 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
955 gctl_ro_param(r, "verb", -1, "add");
956 gctl_ro_param(r, "type", -1, "freebsd-boot");
957 snprintf(sizestr, sizeof(sizestr), "%jd",
958 bootpart_size(scheme) / sector);
959 gctl_ro_param(r, "size", -1, sizestr);
960 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
961 gctl_ro_param(r, "start", -1, startstr);
962 gctl_rw_param(r, "output", sizeof(output), output);
963 errstr = gctl_issue(r);
964 if (errstr != NULL && errstr[0] != '\0')
965 gpart_show_error("Error", NULL, errstr);
968 get_part_metadata(strtok(output, " "), 1)->bootcode = 1;
970 /* Now adjust the part we are really adding forward */
971 firstfree += bootpart_size(scheme) / sector;
972 size -= (bootpart_size(scheme) + stripe)/sector;
973 if (stripe > 0 && (firstfree*sector % stripe) != 0)
974 firstfree += (stripe - ((firstfree*sector) %
979 r = gctl_get_handle();
980 gctl_ro_param(r, "class", -1, "PART");
981 gctl_ro_param(r, "arg0", -1, geom->lg_name);
982 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
983 gctl_ro_param(r, "verb", -1, "add");
985 gctl_ro_param(r, "type", -1, items[0].text);
986 snprintf(sizestr, sizeof(sizestr), "%jd", size);
987 gctl_ro_param(r, "size", -1, sizestr);
988 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
989 gctl_ro_param(r, "start", -1, startstr);
990 if (items[3].text[0] != '\0')
991 gctl_ro_param(r, "label", -1, items[3].text);
992 gctl_rw_param(r, "output", sizeof(output), output);
993 errstr = gctl_issue(r);
994 if (errstr != NULL && errstr[0] != '\0') {
995 gpart_show_error("Error", NULL, errstr);
999 newpartname = strtok(output, " ");
1003 * Try to destroy any geom that gpart picked up already here from
1006 r = gctl_get_handle();
1007 gctl_ro_param(r, "class", -1, "PART");
1008 gctl_ro_param(r, "arg0", -1, newpartname);
1009 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1011 gctl_ro_param(r, "force", sizeof(junk), &junk);
1012 gctl_ro_param(r, "verb", -1, "destroy");
1013 gctl_issue(r); /* Error usually expected and non-fatal */
1016 if (strcmp(items[0].text, "freebsd-boot") == 0)
1017 get_part_metadata(newpartname, 1)->bootcode = 1;
1018 else if (strcmp(items[0].text, "freebsd") == 0)
1019 gpart_partition(newpartname, "BSD");
1021 set_default_part_metadata(newpartname, scheme,
1022 items[0].text, items[2].text, newfs);
1024 for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
1025 if (items[i].text_free)
1026 free(items[i].text);
1028 if (partname != NULL)
1029 *partname = strdup(newpartname);
1033 gpart_delete(struct gprovider *pp)
1037 struct gconsumer *cp;
1043 /* Is it a partition? */
1044 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1046 /* Find out if this is the root of a gpart geom */
1048 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1049 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1054 /* If so, destroy all children */
1056 gpart_destroy(geom);
1058 /* If this is a partition, revert it, so it can be deleted */
1060 r = gctl_get_handle();
1061 gctl_ro_param(r, "class", -1, "PART");
1062 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1063 gctl_ro_param(r, "verb", -1, "undo");
1064 gctl_issue(r); /* Ignore non-fatal errors */
1070 * If this is not a partition, see if that is a problem, complain if
1071 * necessary, and return always, since we need not do anything further,
1074 if (!is_partition) {
1076 dialog_msgbox("Error",
1077 "Only partitions can be deleted.", 0, 0, TRUE);
1081 r = gctl_get_handle();
1082 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1083 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1084 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1085 gctl_ro_param(r, "verb", -1, "delete");
1087 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1088 if (strcmp(gc->lg_name, "index") == 0) {
1089 idx = atoi(gc->lg_val);
1090 gctl_ro_param(r, "index", sizeof(idx), &idx);
1095 errstr = gctl_issue(r);
1096 if (errstr != NULL && errstr[0] != '\0') {
1097 gpart_show_error("Error", NULL, errstr);
1104 delete_part_metadata(pp->lg_name);
1108 gpart_revert_all(struct gmesh *mesh)
1110 struct gclass *classp;
1114 const char *modified;
1116 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1117 if (strcmp(classp->lg_name, "PART") == 0)
1121 if (strcmp(classp->lg_name, "PART") != 0) {
1122 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1126 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1127 modified = "true"; /* XXX: If we don't know (kernel too old),
1128 * assume there are modifications. */
1129 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1130 if (strcmp(gc->lg_name, "modified") == 0) {
1131 modified = gc->lg_val;
1136 if (strcmp(modified, "false") == 0)
1139 r = gctl_get_handle();
1140 gctl_ro_param(r, "class", -1, "PART");
1141 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1142 gctl_ro_param(r, "verb", -1, "undo");
1150 gpart_commit(struct gmesh *mesh)
1152 struct partition_metadata *md;
1153 struct gclass *classp;
1156 struct gconsumer *cp;
1157 struct gprovider *pp;
1160 const char *modified;
1162 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1163 if (strcmp(classp->lg_name, "PART") == 0)
1167 if (strcmp(classp->lg_name, "PART") != 0) {
1168 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1172 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1173 modified = "true"; /* XXX: If we don't know (kernel too old),
1174 * assume there are modifications. */
1175 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1176 if (strcmp(gc->lg_name, "modified") == 0) {
1177 modified = gc->lg_val;
1182 if (strcmp(modified, "false") == 0)
1185 /* Add bootcode if necessary, before the commit */
1186 md = get_part_metadata(gp->lg_name, 0);
1187 if (md != NULL && md->bootcode)
1190 /* Now install partcode on its partitions, if necessary */
1191 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1192 md = get_part_metadata(pp->lg_name, 0);
1193 if (md == NULL || !md->bootcode)
1196 /* Mark this partition active if that's required */
1199 /* Check if the partition has sub-partitions */
1200 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1201 if (strcmp(cp->lg_geom->lg_class->lg_name,
1205 if (cp == NULL) /* No sub-partitions */
1209 r = gctl_get_handle();
1210 gctl_ro_param(r, "class", -1, "PART");
1211 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1212 gctl_ro_param(r, "verb", -1, "commit");
1214 errstr = gctl_issue(r);
1215 if (errstr != NULL && errstr[0] != '\0')
1216 gpart_show_error("Error", NULL, errstr);