12118f387SNathan Whitehorn /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
42118f387SNathan Whitehorn * Copyright (c) 2011 Nathan Whitehorn
52118f387SNathan Whitehorn * All rights reserved.
62118f387SNathan Whitehorn *
72118f387SNathan Whitehorn * Redistribution and use in source and binary forms, with or without
82118f387SNathan Whitehorn * modification, are permitted provided that the following conditions
92118f387SNathan Whitehorn * are met:
102118f387SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
112118f387SNathan Whitehorn * notice, this list of conditions and the following disclaimer.
122118f387SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
132118f387SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the
142118f387SNathan Whitehorn * documentation and/or other materials provided with the distribution.
152118f387SNathan Whitehorn *
162118f387SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172118f387SNathan Whitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182118f387SNathan Whitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192118f387SNathan Whitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202118f387SNathan Whitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212118f387SNathan Whitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222118f387SNathan Whitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232118f387SNathan Whitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242118f387SNathan Whitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252118f387SNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262118f387SNathan Whitehorn * SUCH DAMAGE.
272118f387SNathan Whitehorn */
282118f387SNathan Whitehorn
292118f387SNathan Whitehorn #include <sys/param.h>
306e15678aSNathan Whitehorn #include <sys/stat.h>
3150e24496SAlfonso S. Siciliano
3250e24496SAlfonso S. Siciliano #include <bsddialog.h>
3350e24496SAlfonso S. Siciliano #include <ctype.h>
342118f387SNathan Whitehorn #include <errno.h>
35db8b5613SRebecca Cran #include <fcntl.h>
362118f387SNathan Whitehorn #include <libutil.h>
372118f387SNathan Whitehorn #include <inttypes.h>
3850e24496SAlfonso S. Siciliano #include <stdio.h>
3950e24496SAlfonso S. Siciliano #include <stdlib.h>
4050e24496SAlfonso S. Siciliano #include <string.h>
4150e24496SAlfonso S. Siciliano #include <unistd.h>
422118f387SNathan Whitehorn
432118f387SNathan Whitehorn #include <libgeom.h>
442118f387SNathan Whitehorn
452118f387SNathan Whitehorn #include "partedit.h"
462118f387SNathan Whitehorn
472118f387SNathan Whitehorn #define GPART_FLAGS "x" /* Do not commit changes by default */
482118f387SNathan Whitehorn
492118f387SNathan Whitehorn static void
gpart_show_error(const char * title,const char * explanation,const char * errstr)502118f387SNathan Whitehorn gpart_show_error(const char *title, const char *explanation, const char *errstr)
512118f387SNathan Whitehorn {
522118f387SNathan Whitehorn char *errmsg;
532118f387SNathan Whitehorn char message[512];
542118f387SNathan Whitehorn int error;
5550e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
562118f387SNathan Whitehorn
572118f387SNathan Whitehorn if (explanation == NULL)
582118f387SNathan Whitehorn explanation = "";
592118f387SNathan Whitehorn
602118f387SNathan Whitehorn error = strtol(errstr, &errmsg, 0);
612118f387SNathan Whitehorn if (errmsg != errstr) {
622118f387SNathan Whitehorn while (errmsg[0] == ' ')
632118f387SNathan Whitehorn errmsg++;
642118f387SNathan Whitehorn if (errmsg[0] != '\0')
65*6e8bf240SJohn Baldwin snprintf(message, sizeof(message), "%s%s. %s",
66*6e8bf240SJohn Baldwin explanation, strerror(error), errmsg);
672118f387SNathan Whitehorn else
68*6e8bf240SJohn Baldwin snprintf(message, sizeof(message), "%s%s", explanation,
69*6e8bf240SJohn Baldwin strerror(error));
702118f387SNathan Whitehorn } else {
71*6e8bf240SJohn Baldwin snprintf(message, sizeof(message), "%s%s", explanation, errmsg);
722118f387SNathan Whitehorn }
732118f387SNathan Whitehorn
7450e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
7550e24496SAlfonso S. Siciliano conf.title = title;
7650e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, message, 0, 0);
772118f387SNathan Whitehorn }
782118f387SNathan Whitehorn
79c67f41d0SNathan Whitehorn static int
scheme_supports_labels(const char * scheme)80c67f41d0SNathan Whitehorn scheme_supports_labels(const char *scheme)
81c67f41d0SNathan Whitehorn {
82c67f41d0SNathan Whitehorn if (strcmp(scheme, "APM") == 0)
83c67f41d0SNathan Whitehorn return (1);
84c67f41d0SNathan Whitehorn if (strcmp(scheme, "GPT") == 0)
85c67f41d0SNathan Whitehorn return (1);
86c67f41d0SNathan Whitehorn
87c67f41d0SNathan Whitehorn return (0);
88c67f41d0SNathan Whitehorn }
89c67f41d0SNathan Whitehorn
90ae2fc74fSJohn Baldwin static char *
newfs_command(const char * fstype,int use_default)91ae2fc74fSJohn Baldwin newfs_command(const char *fstype, int use_default)
9250de5d07SNathan Whitehorn {
9350e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
94ae2fc74fSJohn Baldwin FILE *fp;
95ae2fc74fSJohn Baldwin char *buf;
96ae2fc74fSJohn Baldwin size_t len;
9750e24496SAlfonso S. Siciliano
9850e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
99ae2fc74fSJohn Baldwin fp = open_memstream(&buf, &len);
10050e24496SAlfonso S. Siciliano
10150de5d07SNathan Whitehorn if (strcmp(fstype, "freebsd-ufs") == 0) {
10250de5d07SNathan Whitehorn int i;
10350e24496SAlfonso S. Siciliano struct bsddialog_menuitem items[] = {
10450e24496SAlfonso S. Siciliano {"", false, 0, "UFS1", "UFS Version 1",
10550de5d07SNathan Whitehorn "Use version 1 of the UFS file system instead "
10650e24496SAlfonso S. Siciliano "of version 2 (not recommended)"},
10750e24496SAlfonso S. Siciliano {"", true, 0, "SU", "Softupdates",
10850e24496SAlfonso S. Siciliano "Enable softupdates (default)"},
10950e24496SAlfonso S. Siciliano {"", true, 0, "SUJ", "Softupdates journaling",
1104a8b3e41SEd Maste "Enable file system journaling (default - "
11150e24496SAlfonso S. Siciliano "turn off for SSDs)"},
11250e24496SAlfonso S. Siciliano {"", false, 0, "TRIM", "Enable SSD TRIM support",
11350e24496SAlfonso S. Siciliano "Enable TRIM support, useful on solid-state "
11450e24496SAlfonso S. Siciliano "drives" },
11550de5d07SNathan Whitehorn };
11650de5d07SNathan Whitehorn
11750de5d07SNathan Whitehorn if (!use_default) {
11850de5d07SNathan Whitehorn int choice;
11950e24496SAlfonso S. Siciliano conf.title = "UFS Options";
12050e24496SAlfonso S. Siciliano choice = bsddialog_checklist(&conf, "", 0, 0, 0,
12150e24496SAlfonso S. Siciliano nitems(items), items, NULL);
12250e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
123ae2fc74fSJohn Baldwin goto out;
12450de5d07SNathan Whitehorn }
12550de5d07SNathan Whitehorn
126ae2fc74fSJohn Baldwin fputs("newfs ", fp);
12763128851SMarcelo Araujo for (i = 0; i < (int)nitems(items); i++) {
12850e24496SAlfonso S. Siciliano if (items[i].on == false)
12950de5d07SNathan Whitehorn continue;
13050de5d07SNathan Whitehorn if (strcmp(items[i].name, "UFS1") == 0)
131ae2fc74fSJohn Baldwin fputs("-O1 ", fp);
13250de5d07SNathan Whitehorn else if (strcmp(items[i].name, "SU") == 0)
133ae2fc74fSJohn Baldwin fputs("-U ", fp);
13450de5d07SNathan Whitehorn else if (strcmp(items[i].name, "SUJ") == 0)
135ae2fc74fSJohn Baldwin fputs("-j ", fp);
13650de5d07SNathan Whitehorn else if (strcmp(items[i].name, "TRIM") == 0)
137ae2fc74fSJohn Baldwin fputs("-t ", fp);
13850de5d07SNathan Whitehorn }
1396e15678aSNathan Whitehorn } else if (strcmp(fstype, "freebsd-zfs") == 0) {
1406e15678aSNathan Whitehorn int i;
14150e24496SAlfonso S. Siciliano struct bsddialog_menuitem items[] = {
14250e24496SAlfonso S. Siciliano {"", 0, true, "fletcher4", "checksum algorithm: fletcher4",
1436e15678aSNathan Whitehorn "Use fletcher4 for data integrity checking. "
14450e24496SAlfonso S. Siciliano "(default)"},
14550e24496SAlfonso S. Siciliano {"", 0, false, "fletcher2", "checksum algorithm: fletcher2",
1466e15678aSNathan Whitehorn "Use fletcher2 for data integrity checking. "
14750e24496SAlfonso S. Siciliano "(not recommended)"},
14850e24496SAlfonso S. Siciliano {"", 0, false, "sha256", "checksum algorithm: sha256",
1496e15678aSNathan Whitehorn "Use sha256 for data integrity checking. "
15050e24496SAlfonso S. Siciliano "(not recommended)"},
15150e24496SAlfonso S. Siciliano {"", 0, false, "atime", "Update atimes for files",
15250e24496SAlfonso S. Siciliano "Disable atime update"},
1536e15678aSNathan Whitehorn };
1546e15678aSNathan Whitehorn
1556e15678aSNathan Whitehorn if (!use_default) {
1566e15678aSNathan Whitehorn int choice;
15750e24496SAlfonso S. Siciliano conf.title = "ZFS Options";
15850e24496SAlfonso S. Siciliano choice = bsddialog_checklist(&conf, "", 0, 0, 0,
15950e24496SAlfonso S. Siciliano nitems(items), items, NULL);
16050e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
161ae2fc74fSJohn Baldwin goto out;
1626e15678aSNathan Whitehorn }
1636e15678aSNathan Whitehorn
164ae2fc74fSJohn Baldwin fputs("zpool create -f -m none ", fp);
1656e15678aSNathan Whitehorn if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
1666e15678aSNathan Whitehorn char zfsboot_path[MAXPATHLEN];
167ae2fc74fSJohn Baldwin
1686c546e77SConrad Meyer snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs",
1696e15678aSNathan Whitehorn getenv("BSDINSTALL_TMPBOOT"));
1706e15678aSNathan Whitehorn mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
1716e15678aSNathan Whitehorn S_IROTH | S_IXOTH);
172ae2fc74fSJohn Baldwin fprintf(fp, " -o cachefile=%s/zpool.cache ",
173ae2fc74fSJohn Baldwin zfsboot_path);
1746e15678aSNathan Whitehorn }
17563128851SMarcelo Araujo for (i = 0; i < (int)nitems(items); i++) {
17650e24496SAlfonso S. Siciliano if (items[i].on == false)
1776e15678aSNathan Whitehorn continue;
1786e15678aSNathan Whitehorn if (strcmp(items[i].name, "fletcher4") == 0)
179ae2fc74fSJohn Baldwin fputs("-O checksum=fletcher4 ", fp);
1806e15678aSNathan Whitehorn else if (strcmp(items[i].name, "fletcher2") == 0)
181ae2fc74fSJohn Baldwin fputs("-O checksum=fletcher2 ", fp);
1826e15678aSNathan Whitehorn else if (strcmp(items[i].name, "sha256") == 0)
183ae2fc74fSJohn Baldwin fputs("-O checksum=sha256 ", fp);
1846e15678aSNathan Whitehorn else if (strcmp(items[i].name, "atime") == 0)
185ae2fc74fSJohn Baldwin fputs("-O atime=off ", fp);
1866e15678aSNathan Whitehorn }
1879f7602f0SNathan Whitehorn } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0 ||
1889f7602f0SNathan Whitehorn strcmp(fstype, "ms-basic-data") == 0) {
18950de5d07SNathan Whitehorn int i;
19050e24496SAlfonso S. Siciliano struct bsddialog_menuitem items[] = {
19150e24496SAlfonso S. Siciliano {"", 0, true, "FAT32", "FAT Type 32",
19250e24496SAlfonso S. Siciliano "Create a FAT32 filesystem (default)"},
19350e24496SAlfonso S. Siciliano {"", 0, false, "FAT16", "FAT Type 16",
19450e24496SAlfonso S. Siciliano "Create a FAT16 filesystem"},
19550e24496SAlfonso S. Siciliano {"", 0, false, "FAT12", "FAT Type 12",
19650e24496SAlfonso S. Siciliano "Create a FAT12 filesystem"},
19750de5d07SNathan Whitehorn };
19850de5d07SNathan Whitehorn
19950de5d07SNathan Whitehorn if (!use_default) {
20050de5d07SNathan Whitehorn int choice;
20150e24496SAlfonso S. Siciliano conf.title = "FAT Options";
20250e24496SAlfonso S. Siciliano choice = bsddialog_radiolist(&conf, "", 0, 0, 0,
20350e24496SAlfonso S. Siciliano nitems(items), items, NULL);
20450e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
205ae2fc74fSJohn Baldwin goto out;
20650de5d07SNathan Whitehorn }
20750de5d07SNathan Whitehorn
208ae2fc74fSJohn Baldwin fputs("newfs_msdos ", fp);
20963128851SMarcelo Araujo for (i = 0; i < (int)nitems(items); i++) {
21050e24496SAlfonso S. Siciliano if (items[i].on == false)
21150de5d07SNathan Whitehorn continue;
212db8b5613SRebecca Cran if (strcmp(items[i].name, "FAT32") == 0)
213ae2fc74fSJohn Baldwin fputs("-F 32 -c 1", fp);
214db8b5613SRebecca Cran else if (strcmp(items[i].name, "FAT16") == 0)
215ae2fc74fSJohn Baldwin fputs("-F 16 ", fp);
216ae2e7abfSNathan Whitehorn else if (strcmp(items[i].name, "FAT12") == 0)
217ae2fc74fSJohn Baldwin fputs("-F 12 ", fp);
21850de5d07SNathan Whitehorn }
21950de5d07SNathan Whitehorn } else {
22050e24496SAlfonso S. Siciliano if (!use_default) {
22150e24496SAlfonso S. Siciliano conf.title = "Error";
22250e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "No configurable options exist "
22350e24496SAlfonso S. Siciliano "for this filesystem.", 0, 0);
22450e24496SAlfonso S. Siciliano }
22550de5d07SNathan Whitehorn }
226ae2fc74fSJohn Baldwin
227ae2fc74fSJohn Baldwin out:
228ae2fc74fSJohn Baldwin fclose(fp);
229ae2fc74fSJohn Baldwin return (buf);
23050de5d07SNathan Whitehorn }
23150de5d07SNathan Whitehorn
2327059fa6fSAllan Jude const char *
choose_part_type(const char * def_scheme)2337059fa6fSAllan Jude choose_part_type(const char *def_scheme)
2342118f387SNathan Whitehorn {
23550e24496SAlfonso S. Siciliano int button, choice, i;
2367059fa6fSAllan Jude const char *scheme = NULL;
23750e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
2382118f387SNathan Whitehorn
23950e24496SAlfonso S. Siciliano struct bsddialog_menuitem items[] = {
24050e24496SAlfonso S. Siciliano {"", false, 0, "APM", "Apple Partition Map",
24150e24496SAlfonso S. Siciliano "Bootable on PowerPC Apple Hardware" },
24250e24496SAlfonso S. Siciliano {"", false, 0, "BSD", "BSD Labels",
24350e24496SAlfonso S. Siciliano "Bootable on most x86 systems" },
24450e24496SAlfonso S. Siciliano {"", false, 0, "GPT", "GUID Partition Table",
24550e24496SAlfonso S. Siciliano "Bootable on most x86 systems and EFI aware ARM64" },
24650e24496SAlfonso S. Siciliano {"", false, 0, "MBR", "DOS Partitions",
24750e24496SAlfonso S. Siciliano "Bootable on most x86 systems" },
2482118f387SNathan Whitehorn };
2492118f387SNathan Whitehorn
25050e24496SAlfonso S. Siciliano for (i = 0; i < (int)nitems(items); i++)
25150e24496SAlfonso S. Siciliano if (strcmp(items[i].name, def_scheme) == 0)
25250e24496SAlfonso S. Siciliano choice = i;
2532118f387SNathan Whitehorn
25450e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
25550e24496SAlfonso S. Siciliano
25650e24496SAlfonso S. Siciliano parttypemenu:
25750e24496SAlfonso S. Siciliano conf.title = "Partition Scheme";
25850e24496SAlfonso S. Siciliano button = bsddialog_menu(&conf,
25950e24496SAlfonso S. Siciliano "Select a partition scheme for this volume:", 0, 0, 0,
26050e24496SAlfonso S. Siciliano nitems(items), items, &choice);
26150e24496SAlfonso S. Siciliano
26250e24496SAlfonso S. Siciliano if (button == BSDDIALOG_CANCEL)
2637059fa6fSAllan Jude return NULL;
2642118f387SNathan Whitehorn
2652118f387SNathan Whitehorn if (!is_scheme_bootable(items[choice].name)) {
2662118f387SNathan Whitehorn char message[512];
267*6e8bf240SJohn Baldwin
268*6e8bf240SJohn Baldwin snprintf(message, sizeof(message),
269*6e8bf240SJohn Baldwin "This partition scheme (%s) is not "
2702118f387SNathan Whitehorn "bootable on this platform. Are you sure you want "
2712118f387SNathan Whitehorn "to proceed?", items[choice].name);
27250e24496SAlfonso S. Siciliano conf.button.default_cancel = true;
27350e24496SAlfonso S. Siciliano conf.title = "Warning";
27450e24496SAlfonso S. Siciliano button = bsddialog_yesno(&conf, message, 0, 0);
27550e24496SAlfonso S. Siciliano conf.button.default_cancel = false;
27650e24496SAlfonso S. Siciliano if (button == BSDDIALOG_NO)
2777059fa6fSAllan Jude goto parttypemenu;
2782118f387SNathan Whitehorn }
2792118f387SNathan Whitehorn
2802118f387SNathan Whitehorn scheme = items[choice].name;
2817059fa6fSAllan Jude
2827059fa6fSAllan Jude return scheme;
2837059fa6fSAllan Jude }
2847059fa6fSAllan Jude
2857059fa6fSAllan Jude int
gpart_partition(const char * lg_name,const char * scheme)2867059fa6fSAllan Jude gpart_partition(const char *lg_name, const char *scheme)
2877059fa6fSAllan Jude {
28850e24496SAlfonso S. Siciliano int button;
2897059fa6fSAllan Jude struct gctl_req *r;
2907059fa6fSAllan Jude const char *errstr;
29150e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
29250e24496SAlfonso S. Siciliano
29350e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
2947059fa6fSAllan Jude
2957059fa6fSAllan Jude schememenu:
2967059fa6fSAllan Jude if (scheme == NULL) {
2977059fa6fSAllan Jude scheme = choose_part_type(default_scheme());
2987059fa6fSAllan Jude
2997059fa6fSAllan Jude if (scheme == NULL)
3007059fa6fSAllan Jude return (-1);
3017059fa6fSAllan Jude
3027059fa6fSAllan Jude if (!is_scheme_bootable(scheme)) {
3037059fa6fSAllan Jude char message[512];
304*6e8bf240SJohn Baldwin
305*6e8bf240SJohn Baldwin snprintf(message, sizeof(message),
306*6e8bf240SJohn Baldwin "This partition scheme (%s) is not "
3077059fa6fSAllan Jude "bootable on this platform. Are you sure you want "
3087059fa6fSAllan Jude "to proceed?", scheme);
30950e24496SAlfonso S. Siciliano conf.button.default_cancel = true;
31050e24496SAlfonso S. Siciliano conf.title = "Warning";
31150e24496SAlfonso S. Siciliano button = bsddialog_yesno(&conf, message, 0, 0);
31250e24496SAlfonso S. Siciliano conf.button.default_cancel = false;
31350e24496SAlfonso S. Siciliano if (button == BSDDIALOG_NO) {
3147059fa6fSAllan Jude /* Reset scheme so user can choose another */
3157059fa6fSAllan Jude scheme = NULL;
3167059fa6fSAllan Jude goto schememenu;
3177059fa6fSAllan Jude }
3187059fa6fSAllan Jude }
3192118f387SNathan Whitehorn }
3202118f387SNathan Whitehorn
3212118f387SNathan Whitehorn r = gctl_get_handle();
3222118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
3232118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, lg_name);
3242118f387SNathan Whitehorn gctl_ro_param(r, "flags", -1, GPART_FLAGS);
3252118f387SNathan Whitehorn gctl_ro_param(r, "scheme", -1, scheme);
3262118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "create");
3272118f387SNathan Whitehorn
3282118f387SNathan Whitehorn errstr = gctl_issue(r);
3292118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0') {
3302118f387SNathan Whitehorn gpart_show_error("Error", NULL, errstr);
3312118f387SNathan Whitehorn gctl_free(r);
3322118f387SNathan Whitehorn scheme = NULL;
3332118f387SNathan Whitehorn goto schememenu;
3342118f387SNathan Whitehorn }
3352118f387SNathan Whitehorn gctl_free(r);
3362118f387SNathan Whitehorn
3372118f387SNathan Whitehorn if (bootcode_path(scheme) != NULL)
3382118f387SNathan Whitehorn get_part_metadata(lg_name, 1)->bootcode = 1;
3392118f387SNathan Whitehorn return (0);
3402118f387SNathan Whitehorn }
3412118f387SNathan Whitehorn
3422118f387SNathan Whitehorn static void
gpart_activate(struct gprovider * pp)3432118f387SNathan Whitehorn gpart_activate(struct gprovider *pp)
3442118f387SNathan Whitehorn {
3452118f387SNathan Whitehorn struct gconfig *gc;
3462118f387SNathan Whitehorn struct gctl_req *r;
3472118f387SNathan Whitehorn const char *errstr, *scheme;
3482118f387SNathan Whitehorn const char *attribute = NULL;
3492118f387SNathan Whitehorn intmax_t idx;
3502118f387SNathan Whitehorn
3512118f387SNathan Whitehorn /*
3522118f387SNathan Whitehorn * Some partition schemes need this partition to be marked 'active'
3532118f387SNathan Whitehorn * for it to be bootable.
3542118f387SNathan Whitehorn */
3552118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
3562118f387SNathan Whitehorn if (strcmp(gc->lg_name, "scheme") == 0) {
3572118f387SNathan Whitehorn scheme = gc->lg_val;
3582118f387SNathan Whitehorn break;
3592118f387SNathan Whitehorn }
3602118f387SNathan Whitehorn }
3612118f387SNathan Whitehorn
3622b375b4eSYoshihiro Takahashi if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
3632118f387SNathan Whitehorn attribute = "active";
3642118f387SNathan Whitehorn else
3652118f387SNathan Whitehorn return;
3662118f387SNathan Whitehorn
3672118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_config, lg_config) {
3682118f387SNathan Whitehorn if (strcmp(gc->lg_name, "index") == 0) {
3692118f387SNathan Whitehorn idx = atoi(gc->lg_val);
3702118f387SNathan Whitehorn break;
3712118f387SNathan Whitehorn }
3722118f387SNathan Whitehorn }
3732118f387SNathan Whitehorn
3742118f387SNathan Whitehorn r = gctl_get_handle();
3752118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
3762118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
3772118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "set");
3782118f387SNathan Whitehorn gctl_ro_param(r, "attrib", -1, attribute);
3792118f387SNathan Whitehorn gctl_ro_param(r, "index", sizeof(idx), &idx);
3802118f387SNathan Whitehorn
3812118f387SNathan Whitehorn errstr = gctl_issue(r);
3822118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0')
3832118f387SNathan Whitehorn gpart_show_error("Error", "Error marking partition active:",
3842118f387SNathan Whitehorn errstr);
3852118f387SNathan Whitehorn gctl_free(r);
3862118f387SNathan Whitehorn }
3872118f387SNathan Whitehorn
3887059fa6fSAllan Jude void
gpart_set_root(const char * lg_name,const char * attribute)3897059fa6fSAllan Jude gpart_set_root(const char *lg_name, const char *attribute)
3907059fa6fSAllan Jude {
3917059fa6fSAllan Jude struct gctl_req *r;
3927059fa6fSAllan Jude const char *errstr;
3937059fa6fSAllan Jude
3947059fa6fSAllan Jude r = gctl_get_handle();
3957059fa6fSAllan Jude gctl_ro_param(r, "class", -1, "PART");
3967059fa6fSAllan Jude gctl_ro_param(r, "arg0", -1, lg_name);
3977059fa6fSAllan Jude gctl_ro_param(r, "flags", -1, "C");
3987059fa6fSAllan Jude gctl_ro_param(r, "verb", -1, "set");
3997059fa6fSAllan Jude gctl_ro_param(r, "attrib", -1, attribute);
4007059fa6fSAllan Jude
4017059fa6fSAllan Jude errstr = gctl_issue(r);
4027059fa6fSAllan Jude if (errstr != NULL && errstr[0] != '\0')
4037059fa6fSAllan Jude gpart_show_error("Error", "Error setting parameter on disk:",
4047059fa6fSAllan Jude errstr);
4057059fa6fSAllan Jude gctl_free(r);
4067059fa6fSAllan Jude }
4077059fa6fSAllan Jude
4082118f387SNathan Whitehorn static void
gpart_bootcode(struct ggeom * gp)4092118f387SNathan Whitehorn gpart_bootcode(struct ggeom *gp)
4102118f387SNathan Whitehorn {
4112118f387SNathan Whitehorn const char *bootcode;
4122118f387SNathan Whitehorn struct gconfig *gc;
4132118f387SNathan Whitehorn struct gctl_req *r;
4142118f387SNathan Whitehorn const char *errstr, *scheme;
4152118f387SNathan Whitehorn uint8_t *boot;
4162118f387SNathan Whitehorn size_t bootsize, bytes;
4172118f387SNathan Whitehorn int bootfd;
41850e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
4192118f387SNathan Whitehorn
4202118f387SNathan Whitehorn /*
4212118f387SNathan Whitehorn * Write default bootcode to the newly partitioned disk, if that
4222118f387SNathan Whitehorn * applies on this platform.
4232118f387SNathan Whitehorn */
4242118f387SNathan Whitehorn LIST_FOREACH(gc, &gp->lg_config, lg_config) {
4252118f387SNathan Whitehorn if (strcmp(gc->lg_name, "scheme") == 0) {
4262118f387SNathan Whitehorn scheme = gc->lg_val;
4272118f387SNathan Whitehorn break;
4282118f387SNathan Whitehorn }
4292118f387SNathan Whitehorn }
4302118f387SNathan Whitehorn
4312118f387SNathan Whitehorn bootcode = bootcode_path(scheme);
4322118f387SNathan Whitehorn if (bootcode == NULL)
4332118f387SNathan Whitehorn return;
4342118f387SNathan Whitehorn
4352118f387SNathan Whitehorn bootfd = open(bootcode, O_RDONLY);
436c725e3efSKevin Lo if (bootfd < 0) {
43750e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
43850e24496SAlfonso S. Siciliano conf.title = "Bootcode Error";
43950e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, strerror(errno), 0, 0);
4402118f387SNathan Whitehorn return;
4412118f387SNathan Whitehorn }
4422118f387SNathan Whitehorn
4432118f387SNathan Whitehorn bootsize = lseek(bootfd, 0, SEEK_END);
4442118f387SNathan Whitehorn boot = malloc(bootsize);
4452118f387SNathan Whitehorn lseek(bootfd, 0, SEEK_SET);
4462118f387SNathan Whitehorn bytes = 0;
4472118f387SNathan Whitehorn while (bytes < bootsize)
4482118f387SNathan Whitehorn bytes += read(bootfd, boot + bytes, bootsize - bytes);
4492118f387SNathan Whitehorn close(bootfd);
4502118f387SNathan Whitehorn
4512118f387SNathan Whitehorn r = gctl_get_handle();
4522118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
4532118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, gp->lg_name);
4542118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "bootcode");
4552118f387SNathan Whitehorn gctl_ro_param(r, "bootcode", bootsize, boot);
4562118f387SNathan Whitehorn
4572118f387SNathan Whitehorn errstr = gctl_issue(r);
4582118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0')
4592118f387SNathan Whitehorn gpart_show_error("Bootcode Error", NULL, errstr);
4602118f387SNathan Whitehorn gctl_free(r);
4612118f387SNathan Whitehorn free(boot);
4622118f387SNathan Whitehorn }
4632118f387SNathan Whitehorn
4642118f387SNathan Whitehorn static void
gpart_partcode(struct gprovider * pp,const char * fstype)4656e15678aSNathan Whitehorn gpart_partcode(struct gprovider *pp, const char *fstype)
4662118f387SNathan Whitehorn {
4672118f387SNathan Whitehorn struct gconfig *gc;
4682118f387SNathan Whitehorn const char *scheme;
4692118f387SNathan Whitehorn const char *indexstr;
4702118f387SNathan Whitehorn char message[255], command[255];
47150e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
4722118f387SNathan Whitehorn
4732118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
4742118f387SNathan Whitehorn if (strcmp(gc->lg_name, "scheme") == 0) {
4752118f387SNathan Whitehorn scheme = gc->lg_val;
4762118f387SNathan Whitehorn break;
4772118f387SNathan Whitehorn }
4782118f387SNathan Whitehorn }
4792118f387SNathan Whitehorn
4802118f387SNathan Whitehorn /* Make sure this partition scheme needs partcode on this platform */
4816e15678aSNathan Whitehorn if (partcode_path(scheme, fstype) == NULL)
4822118f387SNathan Whitehorn return;
4832118f387SNathan Whitehorn
4842118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_config, lg_config) {
4852118f387SNathan Whitehorn if (strcmp(gc->lg_name, "index") == 0) {
4862118f387SNathan Whitehorn indexstr = gc->lg_val;
4872118f387SNathan Whitehorn break;
4882118f387SNathan Whitehorn }
4892118f387SNathan Whitehorn }
4902118f387SNathan Whitehorn
4912118f387SNathan Whitehorn /* Shell out to gpart for partcode for now */
492*6e8bf240SJohn Baldwin snprintf(command, sizeof(command), "gpart bootcode -p %s -i %s %s",
4936e15678aSNathan Whitehorn partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
4942118f387SNathan Whitehorn if (system(command) != 0) {
495*6e8bf240SJohn Baldwin snprintf(message, sizeof(message),
496*6e8bf240SJohn Baldwin "Error installing partcode on partition %s",
4972118f387SNathan Whitehorn pp->lg_name);
49850e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
49950e24496SAlfonso S. Siciliano conf.title = "Error";
50050e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, message, 0, 0);
5012118f387SNathan Whitehorn }
5022118f387SNathan Whitehorn }
5032118f387SNathan Whitehorn
5042118f387SNathan Whitehorn void
gpart_destroy(struct ggeom * lg_geom)505f36a5e0fSNathan Whitehorn gpart_destroy(struct ggeom *lg_geom)
5062118f387SNathan Whitehorn {
5072118f387SNathan Whitehorn struct gctl_req *r;
508f36a5e0fSNathan Whitehorn struct gprovider *pp;
5092118f387SNathan Whitehorn const char *errstr;
510f36a5e0fSNathan Whitehorn int force = 1;
5112118f387SNathan Whitehorn
512f36a5e0fSNathan Whitehorn /* Delete all child metadata */
5132118f387SNathan Whitehorn LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
5142118f387SNathan Whitehorn gpart_delete(pp);
5152118f387SNathan Whitehorn
516f36a5e0fSNathan Whitehorn /* Revert any local changes to get this geom into a pristine state */
517f36a5e0fSNathan Whitehorn r = gctl_get_handle();
518f36a5e0fSNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
519f36a5e0fSNathan Whitehorn gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
520f36a5e0fSNathan Whitehorn gctl_ro_param(r, "verb", -1, "undo");
521f36a5e0fSNathan Whitehorn gctl_issue(r); /* Ignore errors -- these are non-fatal */
522f36a5e0fSNathan Whitehorn gctl_free(r);
523f36a5e0fSNathan Whitehorn
5242118f387SNathan Whitehorn /* Now destroy the geom itself */
5252118f387SNathan Whitehorn r = gctl_get_handle();
5262118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
5272118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
5282118f387SNathan Whitehorn gctl_ro_param(r, "flags", -1, GPART_FLAGS);
529f36a5e0fSNathan Whitehorn gctl_ro_param(r, "force", sizeof(force), &force);
5302118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "destroy");
5312118f387SNathan Whitehorn errstr = gctl_issue(r);
532987415b1SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0') {
533987415b1SNathan Whitehorn /*
534987415b1SNathan Whitehorn * Check if we reverted away the existence of the geom
535987415b1SNathan Whitehorn * altogether. Show all other errors to the user.
536987415b1SNathan Whitehorn */
537987415b1SNathan Whitehorn if (strtol(errstr, NULL, 0) != EINVAL)
5382118f387SNathan Whitehorn gpart_show_error("Error", NULL, errstr);
539987415b1SNathan Whitehorn }
5402118f387SNathan Whitehorn gctl_free(r);
5412118f387SNathan Whitehorn
5422118f387SNathan Whitehorn /* And any metadata associated with the partition scheme itself */
5432118f387SNathan Whitehorn delete_part_metadata(lg_geom->lg_name);
5442118f387SNathan Whitehorn }
5452118f387SNathan Whitehorn
5462118f387SNathan Whitehorn void
gpart_edit(struct gprovider * pp)5472118f387SNathan Whitehorn gpart_edit(struct gprovider *pp)
5482118f387SNathan Whitehorn {
5492118f387SNathan Whitehorn struct gctl_req *r;
5502118f387SNathan Whitehorn struct gconfig *gc;
5512118f387SNathan Whitehorn struct gconsumer *cp;
5522118f387SNathan Whitehorn struct ggeom *geom;
5532118f387SNathan Whitehorn const char *errstr, *oldtype, *scheme;
5542118f387SNathan Whitehorn struct partition_metadata *md;
5552118f387SNathan Whitehorn char sizestr[32];
556ae2fc74fSJohn Baldwin char *newfs;
5572118f387SNathan Whitehorn intmax_t idx;
55850e24496SAlfonso S. Siciliano int hadlabel, choice, nitems;
5592118f387SNathan Whitehorn unsigned i;
56050e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
5612118f387SNathan Whitehorn
56250e24496SAlfonso S. Siciliano struct bsddialog_formitem items[] = {
56350e24496SAlfonso S. Siciliano { "Type:", 1, 1, "", 1, 12, 12, 15, NULL, 0,
56450e24496SAlfonso S. Siciliano "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
56550e24496SAlfonso S. Siciliano "freebsd-swap)"},
56650e24496SAlfonso S. Siciliano { "Size:", 2, 1, "", 2, 12, 12, 15, NULL, 0,
56750e24496SAlfonso S. Siciliano "Partition size. Append K, M, G for kilobytes, "
56850e24496SAlfonso S. Siciliano "megabytes or gigabytes."},
56950e24496SAlfonso S. Siciliano { "Mountpoint:", 3, 1, "", 3, 12, 12, 15, NULL, 0,
57050e24496SAlfonso S. Siciliano "Path at which to mount this partition (leave blank "
57150e24496SAlfonso S. Siciliano "for swap, set to / for root filesystem)"},
57250e24496SAlfonso S. Siciliano { "Label:", 4, 1, "", 4, 12, 12, 15, NULL, 0,
57350e24496SAlfonso S. Siciliano "Partition name. Not all partition schemes support this."},
5742118f387SNathan Whitehorn };
5752118f387SNathan Whitehorn
57650e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
57750e24496SAlfonso S. Siciliano
5782118f387SNathan Whitehorn /*
5792118f387SNathan Whitehorn * Find the PART geom we are manipulating. This may be a consumer of
5802118f387SNathan Whitehorn * this provider, or its parent. Check the consumer case first.
5812118f387SNathan Whitehorn */
5822118f387SNathan Whitehorn geom = NULL;
5832118f387SNathan Whitehorn LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
5842118f387SNathan Whitehorn if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
585f36a5e0fSNathan Whitehorn /* Check for zombie geoms, treating them as blank */
586f36a5e0fSNathan Whitehorn scheme = NULL;
587f36a5e0fSNathan Whitehorn LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
588f36a5e0fSNathan Whitehorn if (strcmp(gc->lg_name, "scheme") == 0) {
589f36a5e0fSNathan Whitehorn scheme = gc->lg_val;
590f36a5e0fSNathan Whitehorn break;
591f36a5e0fSNathan Whitehorn }
592f36a5e0fSNathan Whitehorn }
593f36a5e0fSNathan Whitehorn if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
594f36a5e0fSNathan Whitehorn gpart_partition(cp->lg_geom->lg_name, NULL);
5952118f387SNathan Whitehorn return;
596f36a5e0fSNathan Whitehorn }
5972118f387SNathan Whitehorn
598987415b1SNathan Whitehorn /* If this is a nested partition, edit as usual */
599987415b1SNathan Whitehorn if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
600987415b1SNathan Whitehorn break;
601987415b1SNathan Whitehorn
6022118f387SNathan Whitehorn /* Destroy the geom and all sub-partitions */
603f36a5e0fSNathan Whitehorn gpart_destroy(cp->lg_geom);
6042118f387SNathan Whitehorn
6052118f387SNathan Whitehorn /* Now re-partition and return */
6062118f387SNathan Whitehorn gpart_partition(cp->lg_geom->lg_name, NULL);
6072118f387SNathan Whitehorn return;
6082118f387SNathan Whitehorn }
6092118f387SNathan Whitehorn
6102118f387SNathan Whitehorn if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
6112118f387SNathan Whitehorn geom = pp->lg_geom;
6122118f387SNathan Whitehorn
6132118f387SNathan Whitehorn if (geom == NULL) {
6142118f387SNathan Whitehorn /* Disk not partitioned, so partition it */
615bcc25b7eSNathan Whitehorn gpart_partition(pp->lg_name, NULL);
6162118f387SNathan Whitehorn return;
6172118f387SNathan Whitehorn }
6182118f387SNathan Whitehorn
6192118f387SNathan Whitehorn LIST_FOREACH(gc, &geom->lg_config, lg_config) {
6202118f387SNathan Whitehorn if (strcmp(gc->lg_name, "scheme") == 0) {
6212118f387SNathan Whitehorn scheme = gc->lg_val;
6222118f387SNathan Whitehorn break;
6232118f387SNathan Whitehorn }
6242118f387SNathan Whitehorn }
6252118f387SNathan Whitehorn
626c67f41d0SNathan Whitehorn nitems = scheme_supports_labels(scheme) ? 4 : 3;
6272118f387SNathan Whitehorn
6282118f387SNathan Whitehorn /* Edit editable parameters of a partition */
6292118f387SNathan Whitehorn hadlabel = 0;
6302118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_config, lg_config) {
6312118f387SNathan Whitehorn if (strcmp(gc->lg_name, "type") == 0) {
6322118f387SNathan Whitehorn oldtype = gc->lg_val;
63350e24496SAlfonso S. Siciliano items[0].init = gc->lg_val;
6342118f387SNathan Whitehorn }
6352118f387SNathan Whitehorn if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
6362118f387SNathan Whitehorn hadlabel = 1;
63750e24496SAlfonso S. Siciliano items[3].init = gc->lg_val;
6382118f387SNathan Whitehorn }
6392118f387SNathan Whitehorn if (strcmp(gc->lg_name, "index") == 0)
6402118f387SNathan Whitehorn idx = atoi(gc->lg_val);
6412118f387SNathan Whitehorn }
6422118f387SNathan Whitehorn
6432118f387SNathan Whitehorn TAILQ_FOREACH(md, &part_metadata, metadata) {
6442118f387SNathan Whitehorn if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
6452118f387SNathan Whitehorn if (md->fstab != NULL)
64650e24496SAlfonso S. Siciliano items[2].init = md->fstab->fs_file;
6472118f387SNathan Whitehorn break;
6482118f387SNathan Whitehorn }
6492118f387SNathan Whitehorn }
6502118f387SNathan Whitehorn
6512118f387SNathan Whitehorn humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
6522118f387SNathan Whitehorn HN_NOSPACE | HN_DECIMAL);
65350e24496SAlfonso S. Siciliano items[1].init = sizestr;
6542118f387SNathan Whitehorn
6552118f387SNathan Whitehorn editpart:
6567ef6e997SAlfonso S. Siciliano conf.button.always_active = true;
65750e24496SAlfonso S. Siciliano conf.title = "Edit Partition";
65861ba55bcSBaptiste Daroussin choice = bsddialog_form(&conf, "", 0, 0, 0, nitems, items, NULL);
6597ef6e997SAlfonso S. Siciliano conf.button.always_active = false;
6602118f387SNathan Whitehorn
66150e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
662987415b1SNathan Whitehorn goto endedit;
6632118f387SNathan Whitehorn
664e24e98d0SWojciech Macek /* If this is the root partition, check that this fs is bootable */
66550e24496SAlfonso S. Siciliano if (strcmp(items[2].value, "/") == 0 && !is_fs_bootable(scheme,
66650e24496SAlfonso S. Siciliano items[0].value)) {
667e24e98d0SWojciech Macek char message[512];
668*6e8bf240SJohn Baldwin
669*6e8bf240SJohn Baldwin snprintf(message, sizeof(message),
670*6e8bf240SJohn Baldwin "This file system (%s) is not bootable "
671e24e98d0SWojciech Macek "on this system. Are you sure you want to proceed?",
67250e24496SAlfonso S. Siciliano items[0].value);
67350e24496SAlfonso S. Siciliano conf.button.default_cancel = true;
67450e24496SAlfonso S. Siciliano conf.title = "Warning";
67550e24496SAlfonso S. Siciliano choice = bsddialog_yesno(&conf, message, 0, 0);
67650e24496SAlfonso S. Siciliano conf.button.default_cancel = false;
67750e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
678e24e98d0SWojciech Macek goto editpart;
679e24e98d0SWojciech Macek }
680e24e98d0SWojciech Macek
6812118f387SNathan Whitehorn /* Check if the label has a / in it */
68250e24496SAlfonso S. Siciliano if (items[3].value != NULL && strchr(items[3].value, '/') != NULL) {
68350e24496SAlfonso S. Siciliano conf.title = "Error";
68450e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "Label contains a /, which is not an "
68550e24496SAlfonso S. Siciliano "allowed character.", 0, 0);
6862118f387SNathan Whitehorn goto editpart;
6872118f387SNathan Whitehorn }
6882118f387SNathan Whitehorn
6892118f387SNathan Whitehorn r = gctl_get_handle();
6902118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
6912118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, geom->lg_name);
6922118f387SNathan Whitehorn gctl_ro_param(r, "flags", -1, GPART_FLAGS);
6932118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "modify");
6942118f387SNathan Whitehorn gctl_ro_param(r, "index", sizeof(idx), &idx);
69550e24496SAlfonso S. Siciliano if (items[3].value != NULL && (hadlabel || items[3].value[0] != '\0'))
69650e24496SAlfonso S. Siciliano gctl_ro_param(r, "label", -1, items[3].value);
69750e24496SAlfonso S. Siciliano gctl_ro_param(r, "type", -1, items[0].value);
6982118f387SNathan Whitehorn errstr = gctl_issue(r);
6992118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0') {
7002118f387SNathan Whitehorn gpart_show_error("Error", NULL, errstr);
7012118f387SNathan Whitehorn gctl_free(r);
7022118f387SNathan Whitehorn goto editpart;
7032118f387SNathan Whitehorn }
7042118f387SNathan Whitehorn gctl_free(r);
7052118f387SNathan Whitehorn
706ae2fc74fSJohn Baldwin newfs = newfs_command(items[0].value, 1);
70750e24496SAlfonso S. Siciliano set_default_part_metadata(pp->lg_name, scheme, items[0].value,
70850e24496SAlfonso S. Siciliano items[2].value, (strcmp(oldtype, items[0].value) != 0) ?
70950de5d07SNathan Whitehorn newfs : NULL);
710ae2fc74fSJohn Baldwin free(newfs);
7112118f387SNathan Whitehorn
712987415b1SNathan Whitehorn endedit:
71350e24496SAlfonso S. Siciliano if (strcmp(oldtype, items[0].value) != 0 && cp != NULL)
714987415b1SNathan Whitehorn gpart_destroy(cp->lg_geom);
71550e24496SAlfonso S. Siciliano if (strcmp(oldtype, items[0].value) != 0 && strcmp(items[0].value,
716987415b1SNathan Whitehorn "freebsd") == 0)
717987415b1SNathan Whitehorn gpart_partition(pp->lg_name, "BSD");
718987415b1SNathan Whitehorn
71963128851SMarcelo Araujo for (i = 0; i < nitems(items); i++)
72050e24496SAlfonso S. Siciliano if (items[i].value != NULL)
72150e24496SAlfonso S. Siciliano free(items[i].value);
7222118f387SNathan Whitehorn }
7232118f387SNathan Whitehorn
7242118f387SNathan Whitehorn void
set_default_part_metadata(const char * name,const char * scheme,const char * type,const char * mountpoint,const char * newfs)7252118f387SNathan Whitehorn set_default_part_metadata(const char *name, const char *scheme,
72650de5d07SNathan Whitehorn const char *type, const char *mountpoint, const char *newfs)
7272118f387SNathan Whitehorn {
7282118f387SNathan Whitehorn struct partition_metadata *md;
7296e15678aSNathan Whitehorn char *zpool_name = NULL;
73052f39da1SNathan Whitehorn const char *default_bootmount = NULL;
7316e15678aSNathan Whitehorn int i;
7322118f387SNathan Whitehorn
7332118f387SNathan Whitehorn /* Set part metadata */
7342118f387SNathan Whitehorn md = get_part_metadata(name, 1);
7352118f387SNathan Whitehorn
7362118f387SNathan Whitehorn if (newfs) {
7372118f387SNathan Whitehorn if (md->newfs != NULL) {
7382118f387SNathan Whitehorn free(md->newfs);
7392118f387SNathan Whitehorn md->newfs = NULL;
7402118f387SNathan Whitehorn }
7412118f387SNathan Whitehorn
74250de5d07SNathan Whitehorn if (newfs != NULL && newfs[0] != '\0') {
7436e15678aSNathan Whitehorn if (strcmp("freebsd-zfs", type) == 0) {
7446e15678aSNathan Whitehorn zpool_name = strdup((strlen(mountpoint) == 1) ?
7456e15678aSNathan Whitehorn "root" : &mountpoint[1]);
7466e15678aSNathan Whitehorn for (i = 0; zpool_name[i] != 0; i++)
7476e15678aSNathan Whitehorn if (!isalnum(zpool_name[i]))
7486e15678aSNathan Whitehorn zpool_name[i] = '_';
74951749e05SJohn Baldwin asprintf(&md->newfs, "%s %s /dev/%s", newfs,
7506e15678aSNathan Whitehorn zpool_name, name);
7516e15678aSNathan Whitehorn } else {
75251749e05SJohn Baldwin asprintf(&md->newfs, "%s /dev/%s", newfs, name);
7532118f387SNathan Whitehorn }
7542118f387SNathan Whitehorn }
7556e15678aSNathan Whitehorn }
7562118f387SNathan Whitehorn
7572118f387SNathan Whitehorn if (strcmp(type, "freebsd-swap") == 0)
7582118f387SNathan Whitehorn mountpoint = "none";
75952f39da1SNathan Whitehorn if (strcmp(type, bootpart_type(scheme, &default_bootmount)) == 0) {
7600b7472b3SNathan Whitehorn if (default_bootmount == NULL)
7612118f387SNathan Whitehorn md->bootcode = 1;
76252f39da1SNathan Whitehorn else if (mountpoint == NULL || strlen(mountpoint) == 0)
76352f39da1SNathan Whitehorn mountpoint = default_bootmount;
76452f39da1SNathan Whitehorn }
7652118f387SNathan Whitehorn
7662118f387SNathan Whitehorn if (mountpoint == NULL || mountpoint[0] == '\0') {
7672118f387SNathan Whitehorn if (md->fstab != NULL) {
7682118f387SNathan Whitehorn free(md->fstab->fs_spec);
7692118f387SNathan Whitehorn free(md->fstab->fs_file);
7702118f387SNathan Whitehorn free(md->fstab->fs_vfstype);
7712118f387SNathan Whitehorn free(md->fstab->fs_mntops);
7722118f387SNathan Whitehorn free(md->fstab->fs_type);
7732118f387SNathan Whitehorn free(md->fstab);
7742118f387SNathan Whitehorn md->fstab = NULL;
7752118f387SNathan Whitehorn }
7762118f387SNathan Whitehorn } else {
7772118f387SNathan Whitehorn if (md->fstab == NULL) {
7782118f387SNathan Whitehorn md->fstab = malloc(sizeof(struct fstab));
7792118f387SNathan Whitehorn } else {
7802118f387SNathan Whitehorn free(md->fstab->fs_spec);
7812118f387SNathan Whitehorn free(md->fstab->fs_file);
7822118f387SNathan Whitehorn free(md->fstab->fs_vfstype);
7832118f387SNathan Whitehorn free(md->fstab->fs_mntops);
7842118f387SNathan Whitehorn free(md->fstab->fs_type);
7852118f387SNathan Whitehorn }
7866e15678aSNathan Whitehorn if (strcmp("freebsd-zfs", type) == 0) {
7876e15678aSNathan Whitehorn md->fstab->fs_spec = strdup(zpool_name);
7886e15678aSNathan Whitehorn } else {
78951749e05SJohn Baldwin asprintf(&md->fstab->fs_spec, "/dev/%s", name);
7906e15678aSNathan Whitehorn }
7912118f387SNathan Whitehorn md->fstab->fs_file = strdup(mountpoint);
7922118f387SNathan Whitehorn /* Get VFS from text after freebsd-, if possible */
79350de5d07SNathan Whitehorn if (strncmp("freebsd-", type, 8) == 0)
7942118f387SNathan Whitehorn md->fstab->fs_vfstype = strdup(&type[8]);
7959f7602f0SNathan Whitehorn else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0
7969f7602f0SNathan Whitehorn || strcmp("ms-basic-data", type) == 0)
79750de5d07SNathan Whitehorn md->fstab->fs_vfstype = strdup("msdosfs");
7982118f387SNathan Whitehorn else
7992118f387SNathan Whitehorn md->fstab->fs_vfstype = strdup(type); /* Guess */
8002118f387SNathan Whitehorn if (strcmp(type, "freebsd-swap") == 0) {
8012118f387SNathan Whitehorn md->fstab->fs_type = strdup(FSTAB_SW);
8022118f387SNathan Whitehorn md->fstab->fs_freq = 0;
8032118f387SNathan Whitehorn md->fstab->fs_passno = 0;
8046e15678aSNathan Whitehorn } else if (strcmp(type, "freebsd-zfs") == 0) {
8056e15678aSNathan Whitehorn md->fstab->fs_type = strdup(FSTAB_RW);
8066e15678aSNathan Whitehorn md->fstab->fs_freq = 0;
8076e15678aSNathan Whitehorn md->fstab->fs_passno = 0;
8082118f387SNathan Whitehorn } else {
8092118f387SNathan Whitehorn md->fstab->fs_type = strdup(FSTAB_RW);
8102118f387SNathan Whitehorn if (strcmp(mountpoint, "/") == 0) {
8112118f387SNathan Whitehorn md->fstab->fs_freq = 1;
8122118f387SNathan Whitehorn md->fstab->fs_passno = 1;
8132118f387SNathan Whitehorn } else {
8142118f387SNathan Whitehorn md->fstab->fs_freq = 2;
8152118f387SNathan Whitehorn md->fstab->fs_passno = 2;
8162118f387SNathan Whitehorn }
8172118f387SNathan Whitehorn }
8182118f387SNathan Whitehorn md->fstab->fs_mntops = strdup(md->fstab->fs_type);
8192118f387SNathan Whitehorn }
8206e15678aSNathan Whitehorn
8216e15678aSNathan Whitehorn if (zpool_name != NULL)
8226e15678aSNathan Whitehorn free(zpool_name);
8232118f387SNathan Whitehorn }
8242118f387SNathan Whitehorn
8252118f387SNathan Whitehorn static
part_compare(const void * xa,const void * xb)8262118f387SNathan Whitehorn int part_compare(const void *xa, const void *xb)
8272118f387SNathan Whitehorn {
8282118f387SNathan Whitehorn struct gprovider **a = (struct gprovider **)xa;
8292118f387SNathan Whitehorn struct gprovider **b = (struct gprovider **)xb;
8302118f387SNathan Whitehorn intmax_t astart, bstart;
8312118f387SNathan Whitehorn struct gconfig *gc;
8322118f387SNathan Whitehorn
8332118f387SNathan Whitehorn astart = bstart = 0;
8342118f387SNathan Whitehorn LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
8352118f387SNathan Whitehorn if (strcmp(gc->lg_name, "start") == 0) {
8362118f387SNathan Whitehorn astart = strtoimax(gc->lg_val, NULL, 0);
8372118f387SNathan Whitehorn break;
8382118f387SNathan Whitehorn }
8392118f387SNathan Whitehorn LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
8402118f387SNathan Whitehorn if (strcmp(gc->lg_name, "start") == 0) {
8412118f387SNathan Whitehorn bstart = strtoimax(gc->lg_val, NULL, 0);
8422118f387SNathan Whitehorn break;
8432118f387SNathan Whitehorn }
8442118f387SNathan Whitehorn
8452118f387SNathan Whitehorn if (astart < bstart)
8462118f387SNathan Whitehorn return -1;
8472118f387SNathan Whitehorn else if (astart > bstart)
8482118f387SNathan Whitehorn return 1;
8492118f387SNathan Whitehorn else
8502118f387SNathan Whitehorn return 0;
8512118f387SNathan Whitehorn }
8522118f387SNathan Whitehorn
8532118f387SNathan Whitehorn intmax_t
gpart_max_free(struct ggeom * geom,intmax_t * npartstart)8542118f387SNathan Whitehorn gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
8552118f387SNathan Whitehorn {
8562118f387SNathan Whitehorn struct gconfig *gc;
8572118f387SNathan Whitehorn struct gprovider *pp, **providers;
85847ead00dSDag-Erling Smørgrav intmax_t sectorsize, stripesize, offset;
8592118f387SNathan Whitehorn intmax_t lastend;
8602118f387SNathan Whitehorn intmax_t start, end;
8612118f387SNathan Whitehorn intmax_t maxsize, maxstart;
8622118f387SNathan Whitehorn intmax_t partstart, partend;
8632118f387SNathan Whitehorn int i, nparts;
8642118f387SNathan Whitehorn
8652118f387SNathan Whitehorn /* Now get the maximum free size and free start */
8662118f387SNathan Whitehorn start = end = 0;
8672118f387SNathan Whitehorn LIST_FOREACH(gc, &geom->lg_config, lg_config) {
8682118f387SNathan Whitehorn if (strcmp(gc->lg_name, "first") == 0)
8692118f387SNathan Whitehorn start = strtoimax(gc->lg_val, NULL, 0);
8702118f387SNathan Whitehorn if (strcmp(gc->lg_name, "last") == 0)
8712118f387SNathan Whitehorn end = strtoimax(gc->lg_val, NULL, 0);
8722118f387SNathan Whitehorn }
8732118f387SNathan Whitehorn
8742118f387SNathan Whitehorn i = nparts = 0;
8752118f387SNathan Whitehorn LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
8762118f387SNathan Whitehorn nparts++;
8772118f387SNathan Whitehorn providers = calloc(nparts, sizeof(providers[0]));
8782118f387SNathan Whitehorn LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
8792118f387SNathan Whitehorn providers[i++] = pp;
8802118f387SNathan Whitehorn qsort(providers, nparts, sizeof(providers[0]), part_compare);
8812118f387SNathan Whitehorn
8822118f387SNathan Whitehorn lastend = start - 1;
8832118f387SNathan Whitehorn maxsize = 0;
8842118f387SNathan Whitehorn for (i = 0; i < nparts; i++) {
8852118f387SNathan Whitehorn pp = providers[i];
8862118f387SNathan Whitehorn
8872118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_config, lg_config) {
8882118f387SNathan Whitehorn if (strcmp(gc->lg_name, "start") == 0)
8892118f387SNathan Whitehorn partstart = strtoimax(gc->lg_val, NULL, 0);
8902118f387SNathan Whitehorn if (strcmp(gc->lg_name, "end") == 0)
8912118f387SNathan Whitehorn partend = strtoimax(gc->lg_val, NULL, 0);
8922118f387SNathan Whitehorn }
8932118f387SNathan Whitehorn
8942118f387SNathan Whitehorn if (partstart - lastend > maxsize) {
8952118f387SNathan Whitehorn maxsize = partstart - lastend - 1;
8962118f387SNathan Whitehorn maxstart = lastend + 1;
8972118f387SNathan Whitehorn }
8982118f387SNathan Whitehorn
8992118f387SNathan Whitehorn lastend = partend;
9002118f387SNathan Whitehorn }
9012118f387SNathan Whitehorn
9022118f387SNathan Whitehorn if (end - lastend > maxsize) {
9038c7de243SNathan Whitehorn maxsize = end - lastend;
9042118f387SNathan Whitehorn maxstart = lastend + 1;
9052118f387SNathan Whitehorn }
9062118f387SNathan Whitehorn
9072118f387SNathan Whitehorn pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
9082118f387SNathan Whitehorn
90947ead00dSDag-Erling Smørgrav /*
91047ead00dSDag-Erling Smørgrav * Round the start and size of the largest available space up to
91147ead00dSDag-Erling Smørgrav * the nearest multiple of the adjusted stripe size.
91247ead00dSDag-Erling Smørgrav *
91347ead00dSDag-Erling Smørgrav * The adjusted stripe size is the least common multiple of the
91447ead00dSDag-Erling Smørgrav * actual stripe size, or the sector size if no stripe size was
91547ead00dSDag-Erling Smørgrav * reported, and 4096. The reason for this is that contemporary
91647ead00dSDag-Erling Smørgrav * disks often have 4096-byte physical sectors but report 512
91747ead00dSDag-Erling Smørgrav * bytes instead for compatibility with older / broken operating
91847ead00dSDag-Erling Smørgrav * systems and BIOSes. For the same reasons, virtualized storage
91947ead00dSDag-Erling Smørgrav * may also report a 512-byte stripe size, or none at all.
92047ead00dSDag-Erling Smørgrav */
92147ead00dSDag-Erling Smørgrav sectorsize = pp->lg_sectorsize;
92247ead00dSDag-Erling Smørgrav if ((stripesize = pp->lg_stripesize) == 0)
92347ead00dSDag-Erling Smørgrav stripesize = sectorsize;
92447ead00dSDag-Erling Smørgrav while (stripesize % 4096 != 0)
92547ead00dSDag-Erling Smørgrav stripesize *= 2;
92647ead00dSDag-Erling Smørgrav if ((offset = maxstart * sectorsize % stripesize) != 0) {
92747ead00dSDag-Erling Smørgrav offset = (stripesize - offset) / sectorsize;
9282118f387SNathan Whitehorn maxstart += offset;
9292118f387SNathan Whitehorn maxsize -= offset;
9302118f387SNathan Whitehorn }
9312118f387SNathan Whitehorn
9322118f387SNathan Whitehorn if (npartstart != NULL)
9332118f387SNathan Whitehorn *npartstart = maxstart;
9342118f387SNathan Whitehorn
9352118f387SNathan Whitehorn return (maxsize);
9362118f387SNathan Whitehorn }
9372118f387SNathan Whitehorn
93852f39da1SNathan Whitehorn static size_t
add_boot_partition(struct ggeom * geom,struct gprovider * pp,const char * scheme,int interactive)93952f39da1SNathan Whitehorn add_boot_partition(struct ggeom *geom, struct gprovider *pp,
94052f39da1SNathan Whitehorn const char *scheme, int interactive)
94152f39da1SNathan Whitehorn {
94252f39da1SNathan Whitehorn struct gconfig *gc;
94352f39da1SNathan Whitehorn struct gprovider *ppi;
94452f39da1SNathan Whitehorn int choice;
94550e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
94652f39da1SNathan Whitehorn
94752f39da1SNathan Whitehorn /* Check for existing freebsd-boot partition */
94852f39da1SNathan Whitehorn LIST_FOREACH(ppi, &geom->lg_provider, lg_provider) {
94952f39da1SNathan Whitehorn struct partition_metadata *md;
95052f39da1SNathan Whitehorn const char *bootmount = NULL;
95152f39da1SNathan Whitehorn
95252f39da1SNathan Whitehorn LIST_FOREACH(gc, &ppi->lg_config, lg_config)
95352f39da1SNathan Whitehorn if (strcmp(gc->lg_name, "type") == 0)
95452f39da1SNathan Whitehorn break;
95552f39da1SNathan Whitehorn if (gc == NULL)
95652f39da1SNathan Whitehorn continue;
95752f39da1SNathan Whitehorn if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)
95852f39da1SNathan Whitehorn continue;
95952f39da1SNathan Whitehorn
96052f39da1SNathan Whitehorn /*
96152f39da1SNathan Whitehorn * If the boot partition is not mountable and needs partcode,
96252f39da1SNathan Whitehorn * but doesn't have it, it doesn't satisfy our requirements.
96352f39da1SNathan Whitehorn */
96452f39da1SNathan Whitehorn md = get_part_metadata(ppi->lg_name, 0);
96552f39da1SNathan Whitehorn if (bootmount == NULL && (md == NULL || !md->bootcode))
96652f39da1SNathan Whitehorn continue;
96752f39da1SNathan Whitehorn
96852f39da1SNathan Whitehorn /* If it is mountable, but mounted somewhere else, remount */
96952f39da1SNathan Whitehorn if (bootmount != NULL && md != NULL && md->fstab != NULL
97052f39da1SNathan Whitehorn && strlen(md->fstab->fs_file) > 0
97152f39da1SNathan Whitehorn && strcmp(md->fstab->fs_file, bootmount) != 0)
97252f39da1SNathan Whitehorn continue;
97352f39da1SNathan Whitehorn
97452f39da1SNathan Whitehorn /* If it is mountable, but mountpoint is not set, mount it */
97552f39da1SNathan Whitehorn if (bootmount != NULL && md == NULL)
97652f39da1SNathan Whitehorn set_default_part_metadata(ppi->lg_name, scheme,
97752f39da1SNathan Whitehorn gc->lg_val, bootmount, NULL);
97852f39da1SNathan Whitehorn
97952f39da1SNathan Whitehorn /* Looks good at this point, no added data needed */
98052f39da1SNathan Whitehorn return (0);
98152f39da1SNathan Whitehorn }
98252f39da1SNathan Whitehorn
98350e24496SAlfonso S. Siciliano if (interactive) {
98450e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
98550e24496SAlfonso S. Siciliano conf.title = "Boot Partition";
98650e24496SAlfonso S. Siciliano choice = bsddialog_yesno(&conf,
98752f39da1SNathan Whitehorn "This partition scheme requires a boot partition "
98852f39da1SNathan Whitehorn "for the disk to be bootable. Would you like to "
98952f39da1SNathan Whitehorn "make one now?", 0, 0);
99050e24496SAlfonso S. Siciliano } else {
99150e24496SAlfonso S. Siciliano choice = BSDDIALOG_YES;
99250e24496SAlfonso S. Siciliano }
99352f39da1SNathan Whitehorn
99450e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_YES) {
9956134b186SNathan Whitehorn struct partition_metadata *md;
99652f39da1SNathan Whitehorn const char *bootmount = NULL;
9976134b186SNathan Whitehorn char *bootpartname = NULL;
99852f39da1SNathan Whitehorn char sizestr[7];
99952f39da1SNathan Whitehorn
100052f39da1SNathan Whitehorn humanize_number(sizestr, 7,
100152f39da1SNathan Whitehorn bootpart_size(scheme), "B", HN_AUTOSCALE,
100252f39da1SNathan Whitehorn HN_NOSPACE | HN_DECIMAL);
100352f39da1SNathan Whitehorn
100452f39da1SNathan Whitehorn gpart_create(pp, bootpart_type(scheme, &bootmount),
10056134b186SNathan Whitehorn sizestr, bootmount, &bootpartname, 0);
10066134b186SNathan Whitehorn
10076134b186SNathan Whitehorn if (bootpartname == NULL) /* Error reported to user already */
10086134b186SNathan Whitehorn return 0;
10096134b186SNathan Whitehorn
10106134b186SNathan Whitehorn /* If the part is not mountable, make sure newfs isn't set */
10116134b186SNathan Whitehorn if (bootmount == NULL) {
10126134b186SNathan Whitehorn md = get_part_metadata(bootpartname, 0);
10136134b186SNathan Whitehorn if (md != NULL && md->newfs != NULL) {
10146134b186SNathan Whitehorn free(md->newfs);
10156134b186SNathan Whitehorn md->newfs = NULL;
10166134b186SNathan Whitehorn }
10176134b186SNathan Whitehorn }
10186134b186SNathan Whitehorn
10196134b186SNathan Whitehorn free(bootpartname);
102052f39da1SNathan Whitehorn
102152f39da1SNathan Whitehorn return (bootpart_size(scheme));
102252f39da1SNathan Whitehorn }
102352f39da1SNathan Whitehorn
102452f39da1SNathan Whitehorn return (0);
102552f39da1SNathan Whitehorn }
102652f39da1SNathan Whitehorn
10272118f387SNathan Whitehorn void
gpart_create(struct gprovider * pp,const char * default_type,const char * default_size,const char * default_mountpoint,char ** partname,int interactive)102852f39da1SNathan Whitehorn gpart_create(struct gprovider *pp, const char *default_type,
102952f39da1SNathan Whitehorn const char *default_size, const char *default_mountpoint,
103052f39da1SNathan Whitehorn char **partname, int interactive)
10312118f387SNathan Whitehorn {
10322118f387SNathan Whitehorn struct gctl_req *r;
10332118f387SNathan Whitehorn struct gconfig *gc;
10342118f387SNathan Whitehorn struct gconsumer *cp;
10352118f387SNathan Whitehorn struct ggeom *geom;
10362118f387SNathan Whitehorn const char *errstr, *scheme;
10377899c6c1SNathan Whitehorn char sizestr[32], startstr[32], output[64], *newpartname;
1038ae2fc74fSJohn Baldwin char *newfs, options_fstype[64];
10392118f387SNathan Whitehorn intmax_t maxsize, size, sector, firstfree, stripe;
10402118f387SNathan Whitehorn uint64_t bytes;
10412118f387SNathan Whitehorn int nitems, choice, junk;
10422118f387SNathan Whitehorn unsigned i;
104350e24496SAlfonso S. Siciliano bool init_allocated;
104450e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
10452118f387SNathan Whitehorn
104650e24496SAlfonso S. Siciliano struct bsddialog_formitem items[] = {
104750e24496SAlfonso S. Siciliano {"Type:", 1, 1, "freebsd-ufs", 1, 12, 12, 15, NULL, 0,
104850e24496SAlfonso S. Siciliano "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
104950e24496SAlfonso S. Siciliano "freebsd-swap)"},
105050e24496SAlfonso S. Siciliano {"Size:", 2, 1, "", 2, 12, 12, 15, NULL, 0,
105150e24496SAlfonso S. Siciliano "Partition size. Append K, M, G for kilobytes, "
105250e24496SAlfonso S. Siciliano "megabytes or gigabytes."},
105350e24496SAlfonso S. Siciliano {"Mountpoint:", 3, 1, "", 3, 12, 12, 15, NULL, 0,
105450e24496SAlfonso S. Siciliano "Path at which to mount partition (blank for "
105550e24496SAlfonso S. Siciliano "swap, / for root filesystem)"},
105650e24496SAlfonso S. Siciliano {"Label:", 4, 1, "", 4, 12, 12, 15, NULL, 0,
105750e24496SAlfonso S. Siciliano "Partition name. Not all partition schemes support this."},
10582118f387SNathan Whitehorn };
10592118f387SNathan Whitehorn
106050e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
106150e24496SAlfonso S. Siciliano
10622118f387SNathan Whitehorn if (partname != NULL)
10632118f387SNathan Whitehorn *partname = NULL;
10642118f387SNathan Whitehorn
10652118f387SNathan Whitehorn /* Record sector and stripe sizes */
10662118f387SNathan Whitehorn sector = pp->lg_sectorsize;
10672118f387SNathan Whitehorn stripe = pp->lg_stripesize;
10682118f387SNathan Whitehorn
10692118f387SNathan Whitehorn /*
10702118f387SNathan Whitehorn * Find the PART geom we are manipulating. This may be a consumer of
10712118f387SNathan Whitehorn * this provider, or its parent. Check the consumer case first.
10722118f387SNathan Whitehorn */
10732118f387SNathan Whitehorn geom = NULL;
10742118f387SNathan Whitehorn LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
10752118f387SNathan Whitehorn if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
10762118f387SNathan Whitehorn geom = cp->lg_geom;
10772118f387SNathan Whitehorn break;
10782118f387SNathan Whitehorn }
10792118f387SNathan Whitehorn
10802118f387SNathan Whitehorn if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
10812118f387SNathan Whitehorn geom = pp->lg_geom;
10822118f387SNathan Whitehorn
10832118f387SNathan Whitehorn /* Now get the partition scheme */
10842118f387SNathan Whitehorn scheme = NULL;
10852118f387SNathan Whitehorn if (geom != NULL) {
10862118f387SNathan Whitehorn LIST_FOREACH(gc, &geom->lg_config, lg_config)
10872118f387SNathan Whitehorn if (strcmp(gc->lg_name, "scheme") == 0)
10882118f387SNathan Whitehorn scheme = gc->lg_val;
10892118f387SNathan Whitehorn }
10902118f387SNathan Whitehorn
10912118f387SNathan Whitehorn if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
109250e24496SAlfonso S. Siciliano if (gpart_partition(pp->lg_name, NULL) == 0) {
109350e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf,
10942118f387SNathan Whitehorn "The partition table has been successfully created."
10952118f387SNathan Whitehorn " Please press Create again to create partitions.",
109650e24496SAlfonso S. Siciliano 0, 0);
109750e24496SAlfonso S. Siciliano }
10982118f387SNathan Whitehorn
10992118f387SNathan Whitehorn return;
11002118f387SNathan Whitehorn }
11012118f387SNathan Whitehorn
11022118f387SNathan Whitehorn /*
11032118f387SNathan Whitehorn * If we still don't have a geom, either the user has
11042118f387SNathan Whitehorn * canceled partitioning or there has been an error which has already
11052118f387SNathan Whitehorn * been displayed, so bail.
11062118f387SNathan Whitehorn */
11072118f387SNathan Whitehorn if (geom == NULL)
11082118f387SNathan Whitehorn return;
11092118f387SNathan Whitehorn
11102118f387SNathan Whitehorn maxsize = size = gpart_max_free(geom, &firstfree);
11112118f387SNathan Whitehorn if (size <= 0) {
111250e24496SAlfonso S. Siciliano conf .title = "Error";
111350e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "No free space left on device.", 0, 0);
11142118f387SNathan Whitehorn return;
11152118f387SNathan Whitehorn }
11162118f387SNathan Whitehorn
11172118f387SNathan Whitehorn humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
11182118f387SNathan Whitehorn HN_NOSPACE | HN_DECIMAL);
111950e24496SAlfonso S. Siciliano items[1].init = sizestr;
11202118f387SNathan Whitehorn
11212118f387SNathan Whitehorn /* Special-case the MBR default type for nested partitions */
11222b375b4eSYoshihiro Takahashi if (strcmp(scheme, "MBR") == 0) {
112350e24496SAlfonso S. Siciliano items[0].init = "freebsd";
112450e24496SAlfonso S. Siciliano items[0].bottomdesc = "Filesystem type (e.g. freebsd, fat32)";
1125bcc25b7eSNathan Whitehorn }
11262118f387SNathan Whitehorn
1127c67f41d0SNathan Whitehorn nitems = scheme_supports_labels(scheme) ? 4 : 3;
11282118f387SNathan Whitehorn
11292118f387SNathan Whitehorn if (default_type != NULL)
113050e24496SAlfonso S. Siciliano items[0].init = (char *)default_type;
11312118f387SNathan Whitehorn if (default_size != NULL)
113250e24496SAlfonso S. Siciliano items[1].init = (char *)default_size;
11332118f387SNathan Whitehorn if (default_mountpoint != NULL)
113450e24496SAlfonso S. Siciliano items[2].init = (char *)default_mountpoint;
11352118f387SNathan Whitehorn
113650de5d07SNathan Whitehorn /* Default options */
113750e24496SAlfonso S. Siciliano strncpy(options_fstype, items[0].init,
113850de5d07SNathan Whitehorn sizeof(options_fstype));
1139ae2fc74fSJohn Baldwin newfs = newfs_command(options_fstype, 1);
114050e24496SAlfonso S. Siciliano
114150e24496SAlfonso S. Siciliano init_allocated = false;
11422118f387SNathan Whitehorn addpartform:
11432118f387SNathan Whitehorn if (interactive) {
114450e24496SAlfonso S. Siciliano conf.button.with_extra = true;
114550e24496SAlfonso S. Siciliano conf.button.extra_label = "Options";
11467ef6e997SAlfonso S. Siciliano conf.button.always_active = true;
114750e24496SAlfonso S. Siciliano conf.title = "Add Partition";
114861ba55bcSBaptiste Daroussin choice = bsddialog_form(&conf, "", 0, 0, 0, nitems, items, NULL);
114950e24496SAlfonso S. Siciliano conf.button.with_extra = false;
115050e24496SAlfonso S. Siciliano conf.button.extra_label = NULL;
11517ef6e997SAlfonso S. Siciliano conf.button.always_active = false;
115250de5d07SNathan Whitehorn switch (choice) {
115350e24496SAlfonso S. Siciliano case BSDDIALOG_OK:
115450de5d07SNathan Whitehorn break;
115550e24496SAlfonso S. Siciliano case BSDDIALOG_CANCEL:
11562118f387SNathan Whitehorn return;
115750e24496SAlfonso S. Siciliano case BSDDIALOG_EXTRA: /* Options */
1158ae2fc74fSJohn Baldwin free(newfs);
115950e24496SAlfonso S. Siciliano strncpy(options_fstype, items[0].value,
116050de5d07SNathan Whitehorn sizeof(options_fstype));
1161ae2fc74fSJohn Baldwin newfs = newfs_command(options_fstype, 0);
116250e24496SAlfonso S. Siciliano for (i = 0; i < nitems(items); i++) {
116350e24496SAlfonso S. Siciliano if (init_allocated)
116450e24496SAlfonso S. Siciliano free((char*)items[i].init);
116550e24496SAlfonso S. Siciliano items[i].init = items[i].value;
116650e24496SAlfonso S. Siciliano }
116750e24496SAlfonso S. Siciliano init_allocated = true;
116850de5d07SNathan Whitehorn goto addpartform;
116950de5d07SNathan Whitehorn }
11709b4c606bSAlfonso S. Siciliano } else { /* auto partitioning */
11719b4c606bSAlfonso S. Siciliano items[0].value = strdup(items[0].init);
11729b4c606bSAlfonso S. Siciliano items[1].value = strdup(items[1].init);
11739b4c606bSAlfonso S. Siciliano items[2].value = strdup(items[2].init);
11749b4c606bSAlfonso S. Siciliano if (nitems > 3)
11759b4c606bSAlfonso S. Siciliano items[3].value = strdup(items[3].init);
117650de5d07SNathan Whitehorn }
117750de5d07SNathan Whitehorn
117850de5d07SNathan Whitehorn /*
117950de5d07SNathan Whitehorn * If the user changed the fs type after specifying options, undo
118050de5d07SNathan Whitehorn * their choices in favor of the new filesystem's defaults.
118150de5d07SNathan Whitehorn */
118250e24496SAlfonso S. Siciliano if (strcmp(options_fstype, items[0].value) != 0) {
1183ae2fc74fSJohn Baldwin free(newfs);
118450e24496SAlfonso S. Siciliano strncpy(options_fstype, items[0].value, sizeof(options_fstype));
1185ae2fc74fSJohn Baldwin newfs = newfs_command(options_fstype, 1);
11862118f387SNathan Whitehorn }
11872118f387SNathan Whitehorn
11882118f387SNathan Whitehorn size = maxsize;
118950e24496SAlfonso S. Siciliano if (strlen(items[1].value) > 0) {
119050e24496SAlfonso S. Siciliano if (expand_number(items[1].value, &bytes) != 0) {
11912118f387SNathan Whitehorn char error[512];
11922118f387SNathan Whitehorn
1193*6e8bf240SJohn Baldwin snprintf(error, sizeof(error), "Invalid size: %s\n",
1194*6e8bf240SJohn Baldwin strerror(errno));
119550e24496SAlfonso S. Siciliano conf.title = "Error";
119650e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, error, 0, 0);
11972118f387SNathan Whitehorn goto addpartform;
11982118f387SNathan Whitehorn }
11992118f387SNathan Whitehorn size = MIN((intmax_t)(bytes/sector), maxsize);
12002118f387SNathan Whitehorn }
12012118f387SNathan Whitehorn
12022118f387SNathan Whitehorn /* Check if the label has a / in it */
120350e24496SAlfonso S. Siciliano if (items[3].value != NULL && strchr(items[3].value, '/') != NULL) {
120450e24496SAlfonso S. Siciliano conf.title = "Error";
120550e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "Label contains a /, which is not an "
120650e24496SAlfonso S. Siciliano "allowed character.", 0, 0);
12072118f387SNathan Whitehorn goto addpartform;
12082118f387SNathan Whitehorn }
12092118f387SNathan Whitehorn
12102118f387SNathan Whitehorn /* Warn if no mountpoint set */
121150e24496SAlfonso S. Siciliano if (strcmp(items[0].value, "freebsd-ufs") == 0 &&
121250e24496SAlfonso S. Siciliano items[2].value[0] != '/') {
121340f0d8dcSRavi Pokala choice = 0;
121440f0d8dcSRavi Pokala if (interactive) {
121550e24496SAlfonso S. Siciliano conf.button.default_cancel = true;
121650e24496SAlfonso S. Siciliano conf.title = "Warning";
121750e24496SAlfonso S. Siciliano choice = bsddialog_yesno(&conf,
12182118f387SNathan Whitehorn "This partition does not have a valid mountpoint "
12192118f387SNathan Whitehorn "(for the partition from which you intend to boot the "
12202118f387SNathan Whitehorn "operating system, the mountpoint should be /). Are you "
12212118f387SNathan Whitehorn "sure you want to continue?"
12222118f387SNathan Whitehorn , 0, 0);
122350e24496SAlfonso S. Siciliano conf.button.default_cancel = false;
122440f0d8dcSRavi Pokala }
122550e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
12262118f387SNathan Whitehorn goto addpartform;
12272118f387SNathan Whitehorn }
12282118f387SNathan Whitehorn
1229a780c996SNathan Whitehorn /*
1230a780c996SNathan Whitehorn * Error if this scheme needs nested partitions, this is one, and
1231a780c996SNathan Whitehorn * a mountpoint was set.
1232a780c996SNathan Whitehorn */
123350e24496SAlfonso S. Siciliano if (strcmp(items[0].value, "freebsd") == 0 &&
123450e24496SAlfonso S. Siciliano strlen(items[2].value) > 0) {
123550e24496SAlfonso S. Siciliano conf.title = "Error";
123650e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "Partitions of type \"freebsd\" are "
1237a780c996SNathan Whitehorn "nested BSD-type partition schemes and cannot have "
1238a780c996SNathan Whitehorn "mountpoints. After creating one, select it and press "
123950e24496SAlfonso S. Siciliano "Create again to add the actual file systems.", 0, 0);
1240a780c996SNathan Whitehorn goto addpartform;
1241a780c996SNathan Whitehorn }
1242a780c996SNathan Whitehorn
12432118f387SNathan Whitehorn /* If this is the root partition, check that this scheme is bootable */
124450e24496SAlfonso S. Siciliano if (strcmp(items[2].value, "/") == 0 && !is_scheme_bootable(scheme)) {
12452118f387SNathan Whitehorn char message[512];
1246*6e8bf240SJohn Baldwin
1247*6e8bf240SJohn Baldwin snprintf(message, sizeof(message),
1248*6e8bf240SJohn Baldwin "This partition scheme (%s) is not bootable "
12492118f387SNathan Whitehorn "on this platform. Are you sure you want to proceed?",
12502118f387SNathan Whitehorn scheme);
125150e24496SAlfonso S. Siciliano conf.button.default_cancel = true;
125250e24496SAlfonso S. Siciliano conf.title = "Warning";
125350e24496SAlfonso S. Siciliano choice = bsddialog_yesno(&conf, message, 0, 0);
125450e24496SAlfonso S. Siciliano conf.button.default_cancel = false;
125550e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
12562118f387SNathan Whitehorn goto addpartform;
12572118f387SNathan Whitehorn }
12582118f387SNathan Whitehorn
12596e15678aSNathan Whitehorn /* If this is the root partition, check that this fs is bootable */
126050e24496SAlfonso S. Siciliano if (strcmp(items[2].value, "/") == 0 && !is_fs_bootable(scheme,
126150e24496SAlfonso S. Siciliano items[0].value)) {
12626e15678aSNathan Whitehorn char message[512];
1263*6e8bf240SJohn Baldwin
1264*6e8bf240SJohn Baldwin snprintf(message, sizeof(message),
1265*6e8bf240SJohn Baldwin "This file system (%s) is not bootable "
12666e15678aSNathan Whitehorn "on this system. Are you sure you want to proceed?",
126750e24496SAlfonso S. Siciliano items[0].value);
126850e24496SAlfonso S. Siciliano conf.button.default_cancel = true;
126950e24496SAlfonso S. Siciliano conf.title = "Warning";
127050e24496SAlfonso S. Siciliano choice = bsddialog_yesno(&conf, message, 0, 0);
127150e24496SAlfonso S. Siciliano conf.button.default_cancel = false;
127250e24496SAlfonso S. Siciliano if (choice == BSDDIALOG_CANCEL)
12736e15678aSNathan Whitehorn goto addpartform;
12746e15678aSNathan Whitehorn }
12756e15678aSNathan Whitehorn
12762118f387SNathan Whitehorn /*
12772118f387SNathan Whitehorn * If this is the root partition, and we need a boot partition, ask
12782118f387SNathan Whitehorn * the user to add one.
12792118f387SNathan Whitehorn */
1280a780c996SNathan Whitehorn
128150e24496SAlfonso S. Siciliano if ((strcmp(items[0].value, "freebsd") == 0 ||
128250e24496SAlfonso S. Siciliano strcmp(items[2].value, "/") == 0) && bootpart_size(scheme) > 0) {
128352f39da1SNathan Whitehorn size_t bytes = add_boot_partition(geom, pp, scheme,
128452f39da1SNathan Whitehorn interactive);
12852118f387SNathan Whitehorn
12862118f387SNathan Whitehorn /* Now adjust the part we are really adding forward */
128752f39da1SNathan Whitehorn if (bytes > 0) {
128852f39da1SNathan Whitehorn firstfree += bytes / sector;
128952f39da1SNathan Whitehorn size -= (bytes + stripe)/sector;
12902118f387SNathan Whitehorn if (stripe > 0 && (firstfree*sector % stripe) != 0)
12912118f387SNathan Whitehorn firstfree += (stripe - ((firstfree*sector) %
12922118f387SNathan Whitehorn stripe)) / sector;
12932118f387SNathan Whitehorn }
12942118f387SNathan Whitehorn }
12952118f387SNathan Whitehorn
12962117cdd4SAlexander Motin output[0] = '\0';
12972117cdd4SAlexander Motin
12982118f387SNathan Whitehorn r = gctl_get_handle();
12992118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
13002118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, geom->lg_name);
13012118f387SNathan Whitehorn gctl_ro_param(r, "flags", -1, GPART_FLAGS);
13022118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "add");
130350e24496SAlfonso S. Siciliano
130450e24496SAlfonso S. Siciliano gctl_ro_param(r, "type", -1, items[0].value);
13052118f387SNathan Whitehorn snprintf(sizestr, sizeof(sizestr), "%jd", size);
13062118f387SNathan Whitehorn gctl_ro_param(r, "size", -1, sizestr);
13072118f387SNathan Whitehorn snprintf(startstr, sizeof(startstr), "%jd", firstfree);
13082118f387SNathan Whitehorn gctl_ro_param(r, "start", -1, startstr);
130950e24496SAlfonso S. Siciliano if (items[3].value != NULL && items[3].value[0] != '\0')
131050e24496SAlfonso S. Siciliano gctl_ro_param(r, "label", -1, items[3].value);
13112117cdd4SAlexander Motin gctl_add_param(r, "output", sizeof(output), output,
13122117cdd4SAlexander Motin GCTL_PARAM_WR | GCTL_PARAM_ASCII);
13132118f387SNathan Whitehorn errstr = gctl_issue(r);
13142118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0') {
13152118f387SNathan Whitehorn gpart_show_error("Error", NULL, errstr);
13162118f387SNathan Whitehorn gctl_free(r);
13172118f387SNathan Whitehorn goto addpartform;
13182118f387SNathan Whitehorn }
13197899c6c1SNathan Whitehorn newpartname = strtok(output, " ");
13207899c6c1SNathan Whitehorn gctl_free(r);
13217899c6c1SNathan Whitehorn
13227899c6c1SNathan Whitehorn /*
13237899c6c1SNathan Whitehorn * Try to destroy any geom that gpart picked up already here from
13247899c6c1SNathan Whitehorn * dirty blocks.
13257899c6c1SNathan Whitehorn */
13267899c6c1SNathan Whitehorn r = gctl_get_handle();
13277899c6c1SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
13287899c6c1SNathan Whitehorn gctl_ro_param(r, "arg0", -1, newpartname);
13297899c6c1SNathan Whitehorn gctl_ro_param(r, "flags", -1, GPART_FLAGS);
13307899c6c1SNathan Whitehorn junk = 1;
13317899c6c1SNathan Whitehorn gctl_ro_param(r, "force", sizeof(junk), &junk);
13327899c6c1SNathan Whitehorn gctl_ro_param(r, "verb", -1, "destroy");
13337899c6c1SNathan Whitehorn gctl_issue(r); /* Error usually expected and non-fatal */
13347899c6c1SNathan Whitehorn gctl_free(r);
13352118f387SNathan Whitehorn
133652f39da1SNathan Whitehorn
133750e24496SAlfonso S. Siciliano if (strcmp(items[0].value, "freebsd") == 0)
13387899c6c1SNathan Whitehorn gpart_partition(newpartname, "BSD");
13392118f387SNathan Whitehorn else
13407899c6c1SNathan Whitehorn set_default_part_metadata(newpartname, scheme,
134150e24496SAlfonso S. Siciliano items[0].value, items[2].value, newfs);
1342ae2fc74fSJohn Baldwin free(newfs);
13432118f387SNathan Whitehorn
134450e24496SAlfonso S. Siciliano for (i = 0; i < nitems(items); i++) {
134550e24496SAlfonso S. Siciliano if (items[i].value != NULL) {
134650e24496SAlfonso S. Siciliano free(items[i].value);
134750e24496SAlfonso S. Siciliano if (init_allocated && items[i].init != NULL)
134850e24496SAlfonso S. Siciliano free((char*)items[i].init);
134950e24496SAlfonso S. Siciliano }
135050e24496SAlfonso S. Siciliano }
13512118f387SNathan Whitehorn
13522118f387SNathan Whitehorn if (partname != NULL)
13537899c6c1SNathan Whitehorn *partname = strdup(newpartname);
13542118f387SNathan Whitehorn }
13552118f387SNathan Whitehorn
13562118f387SNathan Whitehorn void
gpart_delete(struct gprovider * pp)13572118f387SNathan Whitehorn gpart_delete(struct gprovider *pp)
13582118f387SNathan Whitehorn {
13592118f387SNathan Whitehorn struct gconfig *gc;
13602118f387SNathan Whitehorn struct ggeom *geom;
13612118f387SNathan Whitehorn struct gconsumer *cp;
13622118f387SNathan Whitehorn struct gctl_req *r;
13632118f387SNathan Whitehorn const char *errstr;
13642118f387SNathan Whitehorn intmax_t idx;
1365f36a5e0fSNathan Whitehorn int is_partition;
136650e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
13672118f387SNathan Whitehorn
13682118f387SNathan Whitehorn /* Is it a partition? */
13692118f387SNathan Whitehorn is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
13702118f387SNathan Whitehorn
13712118f387SNathan Whitehorn /* Find out if this is the root of a gpart geom */
13722118f387SNathan Whitehorn geom = NULL;
13732118f387SNathan Whitehorn LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
13742118f387SNathan Whitehorn if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
13752118f387SNathan Whitehorn geom = cp->lg_geom;
13762118f387SNathan Whitehorn break;
13772118f387SNathan Whitehorn }
13782118f387SNathan Whitehorn
1379f36a5e0fSNathan Whitehorn /* If so, destroy all children */
13802118f387SNathan Whitehorn if (geom != NULL) {
1381f36a5e0fSNathan Whitehorn gpart_destroy(geom);
1382f36a5e0fSNathan Whitehorn
1383f36a5e0fSNathan Whitehorn /* If this is a partition, revert it, so it can be deleted */
13842118f387SNathan Whitehorn if (is_partition) {
1385f36a5e0fSNathan Whitehorn r = gctl_get_handle();
1386f36a5e0fSNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
1387f36a5e0fSNathan Whitehorn gctl_ro_param(r, "arg0", -1, geom->lg_name);
1388f36a5e0fSNathan Whitehorn gctl_ro_param(r, "verb", -1, "undo");
1389f36a5e0fSNathan Whitehorn gctl_issue(r); /* Ignore non-fatal errors */
1390f36a5e0fSNathan Whitehorn gctl_free(r);
13912118f387SNathan Whitehorn }
13922118f387SNathan Whitehorn }
13932118f387SNathan Whitehorn
13942118f387SNathan Whitehorn /*
13952118f387SNathan Whitehorn * If this is not a partition, see if that is a problem, complain if
13962118f387SNathan Whitehorn * necessary, and return always, since we need not do anything further,
13972118f387SNathan Whitehorn * error or no.
13982118f387SNathan Whitehorn */
13992118f387SNathan Whitehorn if (!is_partition) {
140050e24496SAlfonso S. Siciliano if (geom == NULL) {
140150e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
140250e24496SAlfonso S. Siciliano conf.title = "Error";
140350e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf,
140450e24496SAlfonso S. Siciliano "Only partitions can be deleted.", 0, 0);
140550e24496SAlfonso S. Siciliano }
14062118f387SNathan Whitehorn return;
14072118f387SNathan Whitehorn }
14082118f387SNathan Whitehorn
14092118f387SNathan Whitehorn r = gctl_get_handle();
14102118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
14112118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
14122118f387SNathan Whitehorn gctl_ro_param(r, "flags", -1, GPART_FLAGS);
14132118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "delete");
14142118f387SNathan Whitehorn
14152118f387SNathan Whitehorn LIST_FOREACH(gc, &pp->lg_config, lg_config) {
14162118f387SNathan Whitehorn if (strcmp(gc->lg_name, "index") == 0) {
14172118f387SNathan Whitehorn idx = atoi(gc->lg_val);
14182118f387SNathan Whitehorn gctl_ro_param(r, "index", sizeof(idx), &idx);
14192118f387SNathan Whitehorn break;
14202118f387SNathan Whitehorn }
14212118f387SNathan Whitehorn }
14222118f387SNathan Whitehorn
14232118f387SNathan Whitehorn errstr = gctl_issue(r);
14242118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0') {
14252118f387SNathan Whitehorn gpart_show_error("Error", NULL, errstr);
14262118f387SNathan Whitehorn gctl_free(r);
14272118f387SNathan Whitehorn return;
14282118f387SNathan Whitehorn }
14292118f387SNathan Whitehorn
14302118f387SNathan Whitehorn gctl_free(r);
14312118f387SNathan Whitehorn
14322118f387SNathan Whitehorn delete_part_metadata(pp->lg_name);
14332118f387SNathan Whitehorn }
14342118f387SNathan Whitehorn
14352118f387SNathan Whitehorn void
gpart_revert_all(struct gmesh * mesh)14362118f387SNathan Whitehorn gpart_revert_all(struct gmesh *mesh)
14372118f387SNathan Whitehorn {
14382118f387SNathan Whitehorn struct gclass *classp;
14392118f387SNathan Whitehorn struct gconfig *gc;
14402118f387SNathan Whitehorn struct ggeom *gp;
14412118f387SNathan Whitehorn struct gctl_req *r;
14422118f387SNathan Whitehorn const char *modified;
144350e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
14442118f387SNathan Whitehorn
14452118f387SNathan Whitehorn LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
14462118f387SNathan Whitehorn if (strcmp(classp->lg_name, "PART") == 0)
14472118f387SNathan Whitehorn break;
14482118f387SNathan Whitehorn }
14492118f387SNathan Whitehorn
14502118f387SNathan Whitehorn if (strcmp(classp->lg_name, "PART") != 0) {
145150e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
145250e24496SAlfonso S. Siciliano conf.title = "Error";
145350e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "gpart not found!", 0, 0);
14542118f387SNathan Whitehorn return;
14552118f387SNathan Whitehorn }
14562118f387SNathan Whitehorn
14572118f387SNathan Whitehorn LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
14582118f387SNathan Whitehorn modified = "true"; /* XXX: If we don't know (kernel too old),
14592118f387SNathan Whitehorn * assume there are modifications. */
14602118f387SNathan Whitehorn LIST_FOREACH(gc, &gp->lg_config, lg_config) {
14612118f387SNathan Whitehorn if (strcmp(gc->lg_name, "modified") == 0) {
14622118f387SNathan Whitehorn modified = gc->lg_val;
14632118f387SNathan Whitehorn break;
14642118f387SNathan Whitehorn }
14652118f387SNathan Whitehorn }
14662118f387SNathan Whitehorn
14672118f387SNathan Whitehorn if (strcmp(modified, "false") == 0)
14682118f387SNathan Whitehorn continue;
14692118f387SNathan Whitehorn
14702118f387SNathan Whitehorn r = gctl_get_handle();
14712118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
14722118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, gp->lg_name);
14732118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "undo");
14742118f387SNathan Whitehorn
14757899c6c1SNathan Whitehorn gctl_issue(r);
14762118f387SNathan Whitehorn gctl_free(r);
14772118f387SNathan Whitehorn }
14782118f387SNathan Whitehorn }
14792118f387SNathan Whitehorn
14802118f387SNathan Whitehorn void
gpart_commit(struct gmesh * mesh)14812118f387SNathan Whitehorn gpart_commit(struct gmesh *mesh)
14822118f387SNathan Whitehorn {
14832118f387SNathan Whitehorn struct partition_metadata *md;
14842118f387SNathan Whitehorn struct gclass *classp;
14852118f387SNathan Whitehorn struct ggeom *gp;
14862118f387SNathan Whitehorn struct gconfig *gc;
14872118f387SNathan Whitehorn struct gconsumer *cp;
14882118f387SNathan Whitehorn struct gprovider *pp;
14892118f387SNathan Whitehorn struct gctl_req *r;
14902118f387SNathan Whitehorn const char *errstr;
14912118f387SNathan Whitehorn const char *modified;
14926e15678aSNathan Whitehorn const char *rootfs;
149350e24496SAlfonso S. Siciliano struct bsddialog_conf conf;
14942118f387SNathan Whitehorn
14952118f387SNathan Whitehorn LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
14962118f387SNathan Whitehorn if (strcmp(classp->lg_name, "PART") == 0)
14972118f387SNathan Whitehorn break;
14982118f387SNathan Whitehorn }
14992118f387SNathan Whitehorn
15006e15678aSNathan Whitehorn /* Figure out what filesystem / uses */
15016e15678aSNathan Whitehorn rootfs = "ufs"; /* Assume ufs if nothing else present */
15026e15678aSNathan Whitehorn TAILQ_FOREACH(md, &part_metadata, metadata) {
15036e15678aSNathan Whitehorn if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
15046e15678aSNathan Whitehorn rootfs = md->fstab->fs_vfstype;
15056e15678aSNathan Whitehorn break;
15066e15678aSNathan Whitehorn }
15076e15678aSNathan Whitehorn }
15086e15678aSNathan Whitehorn
15092118f387SNathan Whitehorn if (strcmp(classp->lg_name, "PART") != 0) {
151050e24496SAlfonso S. Siciliano bsddialog_initconf(&conf);
151150e24496SAlfonso S. Siciliano conf.title = "Error";
151250e24496SAlfonso S. Siciliano bsddialog_msgbox(&conf, "gpart not found!", 0, 0);
15132118f387SNathan Whitehorn return;
15142118f387SNathan Whitehorn }
15152118f387SNathan Whitehorn
15162118f387SNathan Whitehorn LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
15172118f387SNathan Whitehorn modified = "true"; /* XXX: If we don't know (kernel too old),
15182118f387SNathan Whitehorn * assume there are modifications. */
15192118f387SNathan Whitehorn LIST_FOREACH(gc, &gp->lg_config, lg_config) {
15202118f387SNathan Whitehorn if (strcmp(gc->lg_name, "modified") == 0) {
15212118f387SNathan Whitehorn modified = gc->lg_val;
15222118f387SNathan Whitehorn break;
15232118f387SNathan Whitehorn }
15242118f387SNathan Whitehorn }
15252118f387SNathan Whitehorn
15262118f387SNathan Whitehorn if (strcmp(modified, "false") == 0)
15272118f387SNathan Whitehorn continue;
15282118f387SNathan Whitehorn
15292118f387SNathan Whitehorn /* Add bootcode if necessary, before the commit */
15302118f387SNathan Whitehorn md = get_part_metadata(gp->lg_name, 0);
15312118f387SNathan Whitehorn if (md != NULL && md->bootcode)
15322118f387SNathan Whitehorn gpart_bootcode(gp);
15332118f387SNathan Whitehorn
15342118f387SNathan Whitehorn /* Now install partcode on its partitions, if necessary */
15352118f387SNathan Whitehorn LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
15362118f387SNathan Whitehorn md = get_part_metadata(pp->lg_name, 0);
15372118f387SNathan Whitehorn if (md == NULL || !md->bootcode)
15382118f387SNathan Whitehorn continue;
15392118f387SNathan Whitehorn
15402118f387SNathan Whitehorn /* Mark this partition active if that's required */
15412118f387SNathan Whitehorn gpart_activate(pp);
15422118f387SNathan Whitehorn
15432118f387SNathan Whitehorn /* Check if the partition has sub-partitions */
15442118f387SNathan Whitehorn LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
15452118f387SNathan Whitehorn if (strcmp(cp->lg_geom->lg_class->lg_name,
15462118f387SNathan Whitehorn "PART") == 0)
15472118f387SNathan Whitehorn break;
15482118f387SNathan Whitehorn
15492118f387SNathan Whitehorn if (cp == NULL) /* No sub-partitions */
15506e15678aSNathan Whitehorn gpart_partcode(pp, rootfs);
15512118f387SNathan Whitehorn }
15522118f387SNathan Whitehorn
15532118f387SNathan Whitehorn r = gctl_get_handle();
15542118f387SNathan Whitehorn gctl_ro_param(r, "class", -1, "PART");
15552118f387SNathan Whitehorn gctl_ro_param(r, "arg0", -1, gp->lg_name);
15562118f387SNathan Whitehorn gctl_ro_param(r, "verb", -1, "commit");
15572118f387SNathan Whitehorn
15582118f387SNathan Whitehorn errstr = gctl_issue(r);
15592118f387SNathan Whitehorn if (errstr != NULL && errstr[0] != '\0')
15602118f387SNathan Whitehorn gpart_show_error("Error", NULL, errstr);
15612118f387SNathan Whitehorn gctl_free(r);
15622118f387SNathan Whitehorn }
15632118f387SNathan Whitehorn }
15642118f387SNathan Whitehorn
1565