Path: blob/main/usr.sbin/bsdinstall/partedit/gpart_ops.c
105517 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2011 Nathan Whitehorn4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/stat.h>3031#include <bsddialog.h>32#include <ctype.h>33#include <errno.h>34#include <fcntl.h>35#include <libutil.h>36#include <inttypes.h>37#include <stdio.h>38#include <stdlib.h>39#include <string.h>40#include <unistd.h>4142#include <libgeom.h>4344#include "partedit.h"4546#define GPART_FLAGS "x" /* Do not commit changes by default */4748static void49gpart_show_error(const char *title, const char *explanation, const char *errstr)50{51char *errmsg;52char message[512];53int error;54struct bsddialog_conf conf;5556if (explanation == NULL)57explanation = "";5859error = strtol(errstr, &errmsg, 0);60if (errmsg != errstr) {61while (errmsg[0] == ' ')62errmsg++;63if (errmsg[0] != '\0')64snprintf(message, sizeof(message), "%s%s. %s",65explanation, strerror(error), errmsg);66else67snprintf(message, sizeof(message), "%s%s", explanation,68strerror(error));69} else {70snprintf(message, sizeof(message), "%s%s", explanation, errmsg);71}7273bsddialog_initconf(&conf);74conf.title = title;75bsddialog_msgbox(&conf, message, 0, 0);76}7778static int79scheme_supports_labels(const char *scheme)80{81if (strcmp(scheme, "APM") == 0)82return (1);83if (strcmp(scheme, "GPT") == 0)84return (1);8586return (0);87}8889static char *90newfs_command(const char *fstype, int use_default)91{92struct bsddialog_conf conf;93FILE *fp;94char *buf;95size_t len;9697bsddialog_initconf(&conf);98fp = open_memstream(&buf, &len);99100if (strcmp(fstype, "freebsd-ufs") == 0) {101int i;102struct bsddialog_menuitem items[] = {103{"", false, 0, "UFS1", "UFS Version 1",104"Use version 1 of the UFS file system instead "105"of version 2 (not recommended)"},106{"", true, 0, "SU", "Softupdates",107"Enable softupdates (default)"},108{"", true, 0, "SUJ", "Softupdates journaling",109"Enable file system journaling (default - "110"turn off for SSDs)"},111{"", false, 0, "TRIM", "Enable SSD TRIM support",112"Enable TRIM support, useful on solid-state "113"drives" },114};115116if (!use_default) {117int choice;118conf.title = "UFS Options";119choice = bsddialog_checklist(&conf, "", 0, 0, 0,120nitems(items), items, NULL);121if (choice == BSDDIALOG_CANCEL)122goto out;123}124125fputs("newfs ", fp);126for (i = 0; i < (int)nitems(items); i++) {127if (items[i].on == false)128continue;129if (strcmp(items[i].name, "UFS1") == 0)130fputs("-O1 ", fp);131else if (strcmp(items[i].name, "SU") == 0)132fputs("-U ", fp);133else if (strcmp(items[i].name, "SUJ") == 0)134fputs("-j ", fp);135else if (strcmp(items[i].name, "TRIM") == 0)136fputs("-t ", fp);137}138} else if (strcmp(fstype, "freebsd-zfs") == 0) {139int i;140struct bsddialog_menuitem items[] = {141{"", true, 0, "fletcher4", "checksum algorithm: fletcher4",142"Use fletcher4 for data integrity checking. "143"(default)"},144{"", false, 0, "fletcher2", "checksum algorithm: fletcher2",145"Use fletcher2 for data integrity checking. "146"(not recommended)"},147{"", false, 0, "sha256", "checksum algorithm: sha256",148"Use sha256 for data integrity checking. "149"(not recommended)"},150{"", false, 0, "atime", "Update atimes for files",151"Disable atime update"},152};153154if (!use_default) {155int choice;156conf.title = "ZFS Options";157choice = bsddialog_checklist(&conf, "", 0, 0, 0,158nitems(items), items, NULL);159if (choice == BSDDIALOG_CANCEL)160goto out;161}162163fputs("zpool create -f -m none ", fp);164if (getenv("BSDINSTALL_TMPBOOT") != NULL) {165char zfsboot_path[MAXPATHLEN];166167snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs",168getenv("BSDINSTALL_TMPBOOT"));169mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |170S_IROTH | S_IXOTH);171fprintf(fp, " -o cachefile=%s/zpool.cache ",172zfsboot_path);173}174for (i = 0; i < (int)nitems(items); i++) {175if (items[i].on == false)176continue;177if (strcmp(items[i].name, "fletcher4") == 0)178fputs("-O checksum=fletcher4 ", fp);179else if (strcmp(items[i].name, "fletcher2") == 0)180fputs("-O checksum=fletcher2 ", fp);181else if (strcmp(items[i].name, "sha256") == 0)182fputs("-O checksum=sha256 ", fp);183else if (strcmp(items[i].name, "atime") == 0)184fputs("-O atime=off ", fp);185}186} else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0 ||187strcmp(fstype, "ms-basic-data") == 0) {188int i;189struct bsddialog_menuitem items[] = {190{"", true, 0, "FAT32", "FAT Type 32",191"Create a FAT32 filesystem (default)"},192{"", false, 0, "FAT16", "FAT Type 16",193"Create a FAT16 filesystem"},194{"", false, 0, "FAT12", "FAT Type 12",195"Create a FAT12 filesystem"},196};197198if (!use_default) {199int choice;200conf.title = "FAT Options";201choice = bsddialog_radiolist(&conf, "", 0, 0, 0,202nitems(items), items, NULL);203if (choice == BSDDIALOG_CANCEL)204goto out;205}206207fputs("newfs_msdos ", fp);208for (i = 0; i < (int)nitems(items); i++) {209if (items[i].on == false)210continue;211if (strcmp(items[i].name, "FAT32") == 0)212fputs("-F 32 -c 1", fp);213else if (strcmp(items[i].name, "FAT16") == 0)214fputs("-F 16 ", fp);215else if (strcmp(items[i].name, "FAT12") == 0)216fputs("-F 12 ", fp);217}218} else {219if (!use_default) {220conf.title = "Error";221bsddialog_msgbox(&conf, "No configurable options exist "222"for this filesystem.", 0, 0);223}224}225226out:227fclose(fp);228return (buf);229}230231const char *232choose_part_type(const char *def_scheme)233{234int button, choice, i;235const char *scheme = NULL;236struct bsddialog_conf conf;237238struct bsddialog_menuitem items[] = {239{"", false, 0, "APM", "Apple Partition Map",240"Bootable on PowerPC Apple Hardware" },241{"", false, 0, "BSD", "BSD Labels",242"Bootable on most x86 systems" },243{"", false, 0, "GPT", "GUID Partition Table",244"Bootable on most x86 systems and EFI aware ARM64" },245{"", false, 0, "MBR", "DOS Partitions",246"Bootable on most x86 systems" },247};248249for (i = 0; i < (int)nitems(items); i++)250if (strcmp(items[i].name, def_scheme) == 0)251choice = i;252253bsddialog_initconf(&conf);254255parttypemenu:256conf.title = "Partition Scheme";257button = bsddialog_menu(&conf,258"Select a partition scheme for this volume:", 0, 0, 0,259nitems(items), items, &choice);260261if (button == BSDDIALOG_CANCEL)262return NULL;263264if (!is_scheme_bootable(items[choice].name)) {265char message[512];266267snprintf(message, sizeof(message),268"This partition scheme (%s) is not "269"bootable on this platform. Are you sure you want "270"to proceed?", items[choice].name);271conf.button.default_cancel = true;272conf.title = "Warning";273button = bsddialog_yesno(&conf, message, 0, 0);274conf.button.default_cancel = false;275if (button == BSDDIALOG_NO)276goto parttypemenu;277}278279scheme = items[choice].name;280281return scheme;282}283284int285gpart_partition(const char *lg_name, const char *scheme)286{287int button;288struct gctl_req *r;289const char *errstr;290struct bsddialog_conf conf;291292bsddialog_initconf(&conf);293294schememenu:295if (scheme == NULL) {296scheme = choose_part_type(default_scheme());297298if (scheme == NULL)299return (-1);300301if (!is_scheme_bootable(scheme)) {302char message[512];303304snprintf(message, sizeof(message),305"This partition scheme (%s) is not "306"bootable on this platform. Are you sure you want "307"to proceed?", scheme);308conf.button.default_cancel = true;309conf.title = "Warning";310button = bsddialog_yesno(&conf, message, 0, 0);311conf.button.default_cancel = false;312if (button == BSDDIALOG_NO) {313/* Reset scheme so user can choose another */314scheme = NULL;315goto schememenu;316}317}318}319320r = gctl_get_handle();321gctl_ro_param(r, "class", -1, "PART");322gctl_ro_param(r, "arg0", -1, lg_name);323gctl_ro_param(r, "flags", -1, GPART_FLAGS);324gctl_ro_param(r, "scheme", -1, scheme);325gctl_ro_param(r, "verb", -1, "create");326327errstr = gctl_issue(r);328if (errstr != NULL && errstr[0] != '\0') {329gpart_show_error("Error", NULL, errstr);330gctl_free(r);331scheme = NULL;332goto schememenu;333}334gctl_free(r);335336if (bootcode_path(scheme) != NULL)337get_part_metadata(lg_name, 1)->bootcode = 1;338return (0);339}340341static void342gpart_activate(struct gprovider *pp)343{344struct gconfig *gc;345struct gctl_req *r;346const char *errstr, *scheme;347const char *attribute = NULL;348intmax_t idx;349350/*351* Some partition schemes need this partition to be marked 'active'352* for it to be bootable.353*/354LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {355if (strcmp(gc->lg_name, "scheme") == 0) {356scheme = gc->lg_val;357break;358}359}360361if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)362attribute = "active";363else364return;365366LIST_FOREACH(gc, &pp->lg_config, lg_config) {367if (strcmp(gc->lg_name, "index") == 0) {368idx = atoi(gc->lg_val);369break;370}371}372373r = gctl_get_handle();374gctl_ro_param(r, "class", -1, "PART");375gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);376gctl_ro_param(r, "verb", -1, "set");377gctl_ro_param(r, "attrib", -1, attribute);378gctl_ro_param(r, "index", sizeof(idx), &idx);379380errstr = gctl_issue(r);381if (errstr != NULL && errstr[0] != '\0')382gpart_show_error("Error", "Error marking partition active:",383errstr);384gctl_free(r);385}386387void388gpart_set_root(const char *lg_name, const char *attribute)389{390struct gctl_req *r;391const char *errstr;392393r = gctl_get_handle();394gctl_ro_param(r, "class", -1, "PART");395gctl_ro_param(r, "arg0", -1, lg_name);396gctl_ro_param(r, "flags", -1, "C");397gctl_ro_param(r, "verb", -1, "set");398gctl_ro_param(r, "attrib", -1, attribute);399400errstr = gctl_issue(r);401if (errstr != NULL && errstr[0] != '\0')402gpart_show_error("Error", "Error setting parameter on disk:",403errstr);404gctl_free(r);405}406407static void408gpart_bootcode(struct ggeom *gp)409{410const char *bootcode;411struct gconfig *gc;412struct gctl_req *r;413const char *errstr, *scheme;414uint8_t *boot;415size_t bootsize, bytes;416int bootfd;417struct bsddialog_conf conf;418419/*420* Write default bootcode to the newly partitioned disk, if that421* applies on this platform.422*/423LIST_FOREACH(gc, &gp->lg_config, lg_config) {424if (strcmp(gc->lg_name, "scheme") == 0) {425scheme = gc->lg_val;426break;427}428}429430bootcode = bootcode_path(scheme);431if (bootcode == NULL)432return;433434bootfd = open(bootcode, O_RDONLY);435if (bootfd < 0) {436bsddialog_initconf(&conf);437conf.title = "Bootcode Error";438bsddialog_msgbox(&conf, strerror(errno), 0, 0);439return;440}441442bootsize = lseek(bootfd, 0, SEEK_END);443boot = malloc(bootsize);444lseek(bootfd, 0, SEEK_SET);445bytes = 0;446while (bytes < bootsize)447bytes += read(bootfd, boot + bytes, bootsize - bytes);448close(bootfd);449450r = gctl_get_handle();451gctl_ro_param(r, "class", -1, "PART");452gctl_ro_param(r, "arg0", -1, gp->lg_name);453gctl_ro_param(r, "verb", -1, "bootcode");454gctl_ro_param(r, "bootcode", bootsize, boot);455456errstr = gctl_issue(r);457if (errstr != NULL && errstr[0] != '\0')458gpart_show_error("Bootcode Error", NULL, errstr);459gctl_free(r);460free(boot);461}462463static void464gpart_partcode(struct gprovider *pp, const char *fstype)465{466struct gconfig *gc;467const char *scheme;468const char *indexstr;469char message[255], command[255];470struct bsddialog_conf conf;471472LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {473if (strcmp(gc->lg_name, "scheme") == 0) {474scheme = gc->lg_val;475break;476}477}478479/* Make sure this partition scheme needs partcode on this platform */480if (partcode_path(scheme, fstype) == NULL)481return;482483LIST_FOREACH(gc, &pp->lg_config, lg_config) {484if (strcmp(gc->lg_name, "index") == 0) {485indexstr = gc->lg_val;486break;487}488}489490/* Shell out to gpart for partcode for now */491snprintf(command, sizeof(command), "gpart bootcode -p %s -i %s %s",492partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);493if (system(command) != 0) {494snprintf(message, sizeof(message),495"Error installing partcode on partition %s",496pp->lg_name);497bsddialog_initconf(&conf);498conf.title = "Error";499bsddialog_msgbox(&conf, message, 0, 0);500}501}502503void504gpart_destroy(struct ggeom *lg_geom)505{506struct gctl_req *r;507struct gprovider *pp;508const char *errstr;509int force = 1;510511/* Delete all child metadata */512LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)513gpart_delete(pp);514515/* Revert any local changes to get this geom into a pristine state */516r = gctl_get_handle();517gctl_ro_param(r, "class", -1, "PART");518gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);519gctl_ro_param(r, "verb", -1, "undo");520gctl_issue(r); /* Ignore errors -- these are non-fatal */521gctl_free(r);522523/* Now destroy the geom itself */524r = gctl_get_handle();525gctl_ro_param(r, "class", -1, "PART");526gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);527gctl_ro_param(r, "flags", -1, GPART_FLAGS);528gctl_ro_param(r, "force", sizeof(force), &force);529gctl_ro_param(r, "verb", -1, "destroy");530errstr = gctl_issue(r);531if (errstr != NULL && errstr[0] != '\0') {532/*533* Check if we reverted away the existence of the geom534* altogether. Show all other errors to the user.535*/536if (strtol(errstr, NULL, 0) != EINVAL)537gpart_show_error("Error", NULL, errstr);538}539gctl_free(r);540541/* And any metadata associated with the partition scheme itself */542delete_part_metadata(lg_geom->lg_name);543}544545void546gpart_edit(struct gprovider *pp)547{548struct gctl_req *r;549struct gconfig *gc;550struct gconsumer *cp;551struct ggeom *geom;552const char *errstr, *oldtype, *scheme;553struct partition_metadata *md;554char sizestr[32];555char *newfs;556intmax_t idx;557int hadlabel, choice, nitems;558unsigned i;559struct bsddialog_conf conf;560561struct bsddialog_formitem items[] = {562{ "Type:", 1, 1, "", 1, 12, 12, 15, NULL, 0,563"Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "564"freebsd-swap)"},565{ "Size:", 2, 1, "", 2, 12, 12, 15, NULL, 0,566"Partition size. Append K, M, G for kilobytes, "567"megabytes or gigabytes."},568{ "Mountpoint:", 3, 1, "", 3, 12, 12, 15, NULL, 0,569"Path at which to mount this partition (leave blank "570"for swap, set to / for root filesystem)"},571{ "Label:", 4, 1, "", 4, 12, 12, 15, NULL, 0,572"Partition name. Not all partition schemes support this."},573};574575bsddialog_initconf(&conf);576577/*578* Find the PART geom we are manipulating. This may be a consumer of579* this provider, or its parent. Check the consumer case first.580*/581geom = NULL;582LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)583if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {584/* Check for zombie geoms, treating them as blank */585scheme = NULL;586LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {587if (strcmp(gc->lg_name, "scheme") == 0) {588scheme = gc->lg_val;589break;590}591}592if (scheme == NULL || strcmp(scheme, "(none)") == 0) {593gpart_partition(cp->lg_geom->lg_name, NULL);594return;595}596597/* If this is a nested partition, edit as usual */598if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)599break;600601/* Destroy the geom and all sub-partitions */602gpart_destroy(cp->lg_geom);603604/* Now re-partition and return */605gpart_partition(cp->lg_geom->lg_name, NULL);606return;607}608609if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)610geom = pp->lg_geom;611612if (geom == NULL) {613/* Disk not partitioned, so partition it */614gpart_partition(pp->lg_name, NULL);615return;616}617618LIST_FOREACH(gc, &geom->lg_config, lg_config) {619if (strcmp(gc->lg_name, "scheme") == 0) {620scheme = gc->lg_val;621break;622}623}624625nitems = scheme_supports_labels(scheme) ? 4 : 3;626627/* Edit editable parameters of a partition */628hadlabel = 0;629LIST_FOREACH(gc, &pp->lg_config, lg_config) {630if (strcmp(gc->lg_name, "type") == 0) {631oldtype = gc->lg_val;632items[0].init = gc->lg_val;633}634if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {635hadlabel = 1;636items[3].init = gc->lg_val;637}638if (strcmp(gc->lg_name, "index") == 0)639idx = atoi(gc->lg_val);640}641642TAILQ_FOREACH(md, &part_metadata, metadata) {643if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {644if (md->fstab != NULL)645items[2].init = md->fstab->fs_file;646break;647}648}649650humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,651HN_NOSPACE | HN_DECIMAL);652items[1].init = sizestr;653654editpart:655conf.button.always_active = true;656conf.title = "Edit Partition";657choice = bsddialog_form(&conf, "", 0, 0, 0, nitems, items, NULL);658conf.button.always_active = false;659660if (choice == BSDDIALOG_CANCEL)661goto endedit;662663/* If this is the root partition, check that this fs is bootable */664if (strcmp(items[2].value, "/") == 0 && !is_fs_bootable(scheme,665items[0].value)) {666char message[512];667668snprintf(message, sizeof(message),669"This file system (%s) is not bootable "670"on this system. Are you sure you want to proceed?",671items[0].value);672conf.button.default_cancel = true;673conf.title = "Warning";674choice = bsddialog_yesno(&conf, message, 0, 0);675conf.button.default_cancel = false;676if (choice == BSDDIALOG_CANCEL)677goto editpart;678}679680/* Check if the label has a / in it */681if (items[3].value != NULL && strchr(items[3].value, '/') != NULL) {682conf.title = "Error";683bsddialog_msgbox(&conf, "Label contains a /, which is not an "684"allowed character.", 0, 0);685goto editpart;686}687688r = gctl_get_handle();689gctl_ro_param(r, "class", -1, "PART");690gctl_ro_param(r, "arg0", -1, geom->lg_name);691gctl_ro_param(r, "flags", -1, GPART_FLAGS);692gctl_ro_param(r, "verb", -1, "modify");693gctl_ro_param(r, "index", sizeof(idx), &idx);694if (items[3].value != NULL && (hadlabel || items[3].value[0] != '\0'))695gctl_ro_param(r, "label", -1, items[3].value);696gctl_ro_param(r, "type", -1, items[0].value);697errstr = gctl_issue(r);698if (errstr != NULL && errstr[0] != '\0') {699gpart_show_error("Error", NULL, errstr);700gctl_free(r);701goto editpart;702}703gctl_free(r);704705newfs = newfs_command(items[0].value, 1);706set_default_part_metadata(pp->lg_name, scheme, items[0].value,707items[2].value, (strcmp(oldtype, items[0].value) != 0) ?708newfs : NULL);709free(newfs);710711endedit:712if (strcmp(oldtype, items[0].value) != 0 && cp != NULL)713gpart_destroy(cp->lg_geom);714if (strcmp(oldtype, items[0].value) != 0 && strcmp(items[0].value,715"freebsd") == 0)716gpart_partition(pp->lg_name, "BSD");717718for (i = 0; i < nitems(items); i++)719if (items[i].value != NULL)720free(items[i].value);721}722723void724set_default_part_metadata(const char *name, const char *scheme,725const char *type, const char *mountpoint, const char *newfs)726{727struct partition_metadata *md;728char *zpool_name = NULL;729const char *default_bootmount = NULL;730int i;731732/* Set part metadata */733md = get_part_metadata(name, 1);734735if (newfs) {736if (md->newfs != NULL) {737free(md->newfs);738md->newfs = NULL;739}740741if (newfs != NULL && newfs[0] != '\0') {742if (strcmp("freebsd-zfs", type) == 0) {743zpool_name = strdup((strlen(mountpoint) == 1) ?744"root" : &mountpoint[1]);745for (i = 0; zpool_name[i] != 0; i++)746if (!isalnum(zpool_name[i]))747zpool_name[i] = '_';748asprintf(&md->newfs, "%s %s /dev/%s", newfs,749zpool_name, name);750} else {751asprintf(&md->newfs, "%s /dev/%s", newfs, name);752}753}754}755756if (strcmp(type, "freebsd-swap") == 0)757mountpoint = "none";758if (strcmp(type, bootpart_type(scheme, &default_bootmount)) == 0) {759if (default_bootmount == NULL)760md->bootcode = 1;761else if (mountpoint == NULL || strlen(mountpoint) == 0)762mountpoint = default_bootmount;763}764765if (mountpoint == NULL || mountpoint[0] == '\0') {766if (md->fstab != NULL) {767free(md->fstab->fs_spec);768free(md->fstab->fs_file);769free(md->fstab->fs_vfstype);770free(md->fstab->fs_mntops);771free(md->fstab->fs_type);772free(md->fstab);773md->fstab = NULL;774}775} else {776if (md->fstab == NULL) {777md->fstab = malloc(sizeof(struct fstab));778} else {779free(md->fstab->fs_spec);780free(md->fstab->fs_file);781free(md->fstab->fs_vfstype);782free(md->fstab->fs_mntops);783free(md->fstab->fs_type);784}785if (strcmp("freebsd-zfs", type) == 0) {786md->fstab->fs_spec = strdup(zpool_name);787} else {788asprintf(&md->fstab->fs_spec, "/dev/%s", name);789}790md->fstab->fs_file = strdup(mountpoint);791/* Get VFS from text after freebsd-, if possible */792if (strncmp("freebsd-", type, 8) == 0)793md->fstab->fs_vfstype = strdup(&type[8]);794else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0795|| strcmp("ms-basic-data", type) == 0)796md->fstab->fs_vfstype = strdup("msdosfs");797else798md->fstab->fs_vfstype = strdup(type); /* Guess */799if (strcmp(type, "freebsd-swap") == 0) {800md->fstab->fs_type = strdup(FSTAB_SW);801md->fstab->fs_freq = 0;802md->fstab->fs_passno = 0;803} else if (strcmp(type, "freebsd-zfs") == 0) {804md->fstab->fs_type = strdup(FSTAB_RW);805md->fstab->fs_freq = 0;806md->fstab->fs_passno = 0;807} else {808md->fstab->fs_type = strdup(FSTAB_RW);809if (strcmp(mountpoint, "/") == 0) {810md->fstab->fs_freq = 1;811md->fstab->fs_passno = 1;812} else {813md->fstab->fs_freq = 2;814md->fstab->fs_passno = 2;815}816}817md->fstab->fs_mntops = strdup(md->fstab->fs_type);818}819820if (zpool_name != NULL)821free(zpool_name);822}823824static825int part_compare(const void *xa, const void *xb)826{827struct gprovider **a = (struct gprovider **)xa;828struct gprovider **b = (struct gprovider **)xb;829intmax_t astart, bstart;830struct gconfig *gc;831832astart = bstart = 0;833LIST_FOREACH(gc, &(*a)->lg_config, lg_config)834if (strcmp(gc->lg_name, "start") == 0) {835astart = strtoimax(gc->lg_val, NULL, 0);836break;837}838LIST_FOREACH(gc, &(*b)->lg_config, lg_config)839if (strcmp(gc->lg_name, "start") == 0) {840bstart = strtoimax(gc->lg_val, NULL, 0);841break;842}843844if (astart < bstart)845return -1;846else if (astart > bstart)847return 1;848else849return 0;850}851852intmax_t853gpart_max_free(struct ggeom *geom, intmax_t *npartstart)854{855struct gconfig *gc;856struct gprovider *pp, **providers;857intmax_t sectorsize, stripesize, offset;858intmax_t lastend;859intmax_t start, end;860intmax_t maxsize, maxstart;861intmax_t partstart, partend;862int i, nparts;863864/* Now get the maximum free size and free start */865start = end = 0;866LIST_FOREACH(gc, &geom->lg_config, lg_config) {867if (strcmp(gc->lg_name, "first") == 0)868start = strtoimax(gc->lg_val, NULL, 0);869if (strcmp(gc->lg_name, "last") == 0)870end = strtoimax(gc->lg_val, NULL, 0);871}872873i = nparts = 0;874LIST_FOREACH(pp, &geom->lg_provider, lg_provider)875nparts++;876providers = calloc(nparts, sizeof(providers[0]));877LIST_FOREACH(pp, &geom->lg_provider, lg_provider)878providers[i++] = pp;879qsort(providers, nparts, sizeof(providers[0]), part_compare);880881lastend = start - 1;882maxsize = 0;883for (i = 0; i < nparts; i++) {884pp = providers[i];885886LIST_FOREACH(gc, &pp->lg_config, lg_config) {887if (strcmp(gc->lg_name, "start") == 0)888partstart = strtoimax(gc->lg_val, NULL, 0);889if (strcmp(gc->lg_name, "end") == 0)890partend = strtoimax(gc->lg_val, NULL, 0);891}892893if (partstart - lastend > maxsize) {894maxsize = partstart - lastend - 1;895maxstart = lastend + 1;896}897898lastend = partend;899}900901if (end - lastend > maxsize) {902maxsize = end - lastend;903maxstart = lastend + 1;904}905906pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;907908/*909* Round the start and size of the largest available space up to910* the nearest multiple of the adjusted stripe size.911*912* The adjusted stripe size is the least common multiple of the913* actual stripe size, or the sector size if no stripe size was914* reported, and 4096. The reason for this is that contemporary915* disks often have 4096-byte physical sectors but report 512916* bytes instead for compatibility with older / broken operating917* systems and BIOSes. For the same reasons, virtualized storage918* may also report a 512-byte stripe size, or none at all.919*/920sectorsize = pp->lg_sectorsize;921if ((stripesize = pp->lg_stripesize) == 0)922stripesize = sectorsize;923while (stripesize % 4096 != 0)924stripesize *= 2;925if ((offset = maxstart * sectorsize % stripesize) != 0) {926offset = (stripesize - offset) / sectorsize;927maxstart += offset;928maxsize -= offset;929}930931if (npartstart != NULL)932*npartstart = maxstart;933934return (maxsize);935}936937static size_t938add_boot_partition(struct ggeom *geom, struct gprovider *pp,939const char *scheme, int interactive)940{941struct gconfig *gc;942struct gprovider *ppi;943int choice;944struct bsddialog_conf conf;945946/* Check for existing freebsd-boot partition */947LIST_FOREACH(ppi, &geom->lg_provider, lg_provider) {948struct partition_metadata *md;949const char *bootmount = NULL;950951LIST_FOREACH(gc, &ppi->lg_config, lg_config)952if (strcmp(gc->lg_name, "type") == 0)953break;954if (gc == NULL)955continue;956if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)957continue;958959/*960* If the boot partition is not mountable and needs partcode,961* but doesn't have it, it doesn't satisfy our requirements.962*/963md = get_part_metadata(ppi->lg_name, 0);964if (bootmount == NULL && (md == NULL || !md->bootcode))965continue;966967/* If it is mountable, but mounted somewhere else, remount */968if (bootmount != NULL && md != NULL && md->fstab != NULL969&& strlen(md->fstab->fs_file) > 0970&& strcmp(md->fstab->fs_file, bootmount) != 0)971continue;972973/* If it is mountable, but mountpoint is not set, mount it */974if (bootmount != NULL && md == NULL)975set_default_part_metadata(ppi->lg_name, scheme,976gc->lg_val, bootmount, NULL);977978/* Looks good at this point, no added data needed */979return (0);980}981982if (interactive) {983bsddialog_initconf(&conf);984conf.title = "Boot Partition";985choice = bsddialog_yesno(&conf,986"This partition scheme requires a boot partition "987"for the disk to be bootable. Would you like to "988"make one now?", 0, 0);989} else {990choice = BSDDIALOG_YES;991}992993if (choice == BSDDIALOG_YES) {994struct partition_metadata *md;995const char *bootmount = NULL;996char *bootpartname = NULL;997char sizestr[7];998999humanize_number(sizestr, 7,1000bootpart_size(scheme), "B", HN_AUTOSCALE,1001HN_NOSPACE | HN_DECIMAL);10021003gpart_create(pp, bootpart_type(scheme, &bootmount),1004sizestr, bootmount, &bootpartname, 0);10051006if (bootpartname == NULL) /* Error reported to user already */1007return 0;10081009/* If the part is not mountable, make sure newfs isn't set */1010if (bootmount == NULL) {1011md = get_part_metadata(bootpartname, 0);1012if (md != NULL && md->newfs != NULL) {1013free(md->newfs);1014md->newfs = NULL;1015}1016}10171018free(bootpartname);10191020return (bootpart_size(scheme));1021}10221023return (0);1024}10251026void1027gpart_create(struct gprovider *pp, const char *default_type,1028const char *default_size, const char *default_mountpoint,1029char **partname, int interactive)1030{1031struct gctl_req *r;1032struct gconfig *gc;1033struct gconsumer *cp;1034struct ggeom *geom;1035const char *errstr, *scheme;1036char sizestr[32], startstr[32], output[64], *newpartname;1037char *newfs, options_fstype[64];1038intmax_t maxsize, size, sector, firstfree, stripe;1039uint64_t bytes;1040int nitems, choice, junk;1041unsigned i;1042bool init_allocated;1043struct bsddialog_conf conf;10441045struct bsddialog_formitem items[] = {1046{"Type:", 1, 1, "freebsd-ufs", 1, 12, 12, 15, NULL, 0,1047"Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "1048"freebsd-swap)"},1049{"Size:", 2, 1, "", 2, 12, 12, 15, NULL, 0,1050"Partition size. Append K, M, G for kilobytes, "1051"megabytes or gigabytes."},1052{"Mountpoint:", 3, 1, "", 3, 12, 12, 15, NULL, 0,1053"Path at which to mount partition (blank for "1054"swap, / for root filesystem)"},1055{"Label:", 4, 1, "", 4, 12, 12, 15, NULL, 0,1056"Partition name. Not all partition schemes support this."},1057};10581059bsddialog_initconf(&conf);10601061if (partname != NULL)1062*partname = NULL;10631064/* Record sector and stripe sizes */1065sector = pp->lg_sectorsize;1066stripe = pp->lg_stripesize;10671068/*1069* Find the PART geom we are manipulating. This may be a consumer of1070* this provider, or its parent. Check the consumer case first.1071*/1072geom = NULL;1073LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)1074if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {1075geom = cp->lg_geom;1076break;1077}10781079if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)1080geom = pp->lg_geom;10811082/* Now get the partition scheme */1083scheme = NULL;1084if (geom != NULL) {1085LIST_FOREACH(gc, &geom->lg_config, lg_config)1086if (strcmp(gc->lg_name, "scheme") == 0)1087scheme = gc->lg_val;1088}10891090if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {1091if (gpart_partition(pp->lg_name, NULL) == 0) {1092bsddialog_msgbox(&conf,1093"The partition table has been successfully created."1094" Please press Create again to create partitions.",10950, 0);1096}10971098return;1099}11001101/*1102* If we still don't have a geom, either the user has1103* canceled partitioning or there has been an error which has already1104* been displayed, so bail.1105*/1106if (geom == NULL)1107return;11081109maxsize = size = gpart_max_free(geom, &firstfree);1110if (size <= 0) {1111conf .title = "Error";1112bsddialog_msgbox(&conf, "No free space left on device.", 0, 0);1113return;1114}11151116humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,1117HN_NOSPACE | HN_DECIMAL);1118items[1].init = sizestr;11191120/* Special-case the MBR default type for nested partitions */1121if (strcmp(scheme, "MBR") == 0) {1122items[0].init = "freebsd";1123items[0].bottomdesc = "Filesystem type (e.g. freebsd, fat32)";1124}11251126nitems = scheme_supports_labels(scheme) ? 4 : 3;11271128if (default_type != NULL)1129items[0].init = (char *)default_type;1130if (default_size != NULL)1131items[1].init = (char *)default_size;1132if (default_mountpoint != NULL)1133items[2].init = (char *)default_mountpoint;11341135/* Default options */1136strncpy(options_fstype, items[0].init,1137sizeof(options_fstype));1138newfs = newfs_command(options_fstype, 1);11391140init_allocated = false;1141addpartform:1142if (interactive) {1143conf.button.with_extra = true;1144conf.button.extra_label = "Options";1145conf.button.always_active = true;1146conf.title = "Add Partition";1147choice = bsddialog_form(&conf, "", 0, 0, 0, nitems, items, NULL);1148conf.button.with_extra = false;1149conf.button.extra_label = NULL;1150conf.button.always_active = false;1151switch (choice) {1152case BSDDIALOG_OK:1153break;1154case BSDDIALOG_CANCEL:1155return;1156case BSDDIALOG_EXTRA: /* Options */1157free(newfs);1158strncpy(options_fstype, items[0].value,1159sizeof(options_fstype));1160newfs = newfs_command(options_fstype, 0);1161for (i = 0; i < nitems(items); i++) {1162if (init_allocated)1163free((char*)items[i].init);1164items[i].init = items[i].value;1165}1166init_allocated = true;1167goto addpartform;1168}1169} else { /* auto partitioning */1170items[0].value = strdup(items[0].init);1171items[1].value = strdup(items[1].init);1172items[2].value = strdup(items[2].init);1173if (nitems > 3)1174items[3].value = strdup(items[3].init);1175}11761177/*1178* If the user changed the fs type after specifying options, undo1179* their choices in favor of the new filesystem's defaults.1180*/1181if (strcmp(options_fstype, items[0].value) != 0) {1182free(newfs);1183strncpy(options_fstype, items[0].value, sizeof(options_fstype));1184newfs = newfs_command(options_fstype, 1);1185}11861187size = maxsize;1188if (strlen(items[1].value) > 0) {1189if (expand_number(items[1].value, &bytes) != 0) {1190char error[512];11911192snprintf(error, sizeof(error), "Invalid size: %s\n",1193strerror(errno));1194conf.title = "Error";1195bsddialog_msgbox(&conf, error, 0, 0);1196goto addpartform;1197}1198size = MIN((intmax_t)(bytes/sector), maxsize);1199}12001201/* Check if the label has a / in it */1202if (items[3].value != NULL && strchr(items[3].value, '/') != NULL) {1203conf.title = "Error";1204bsddialog_msgbox(&conf, "Label contains a /, which is not an "1205"allowed character.", 0, 0);1206goto addpartform;1207}12081209/* Warn if no mountpoint set */1210if (strcmp(items[0].value, "freebsd-ufs") == 0 &&1211items[2].value[0] != '/') {1212choice = 0;1213if (interactive) {1214conf.button.default_cancel = true;1215conf.title = "Warning";1216choice = bsddialog_yesno(&conf,1217"This partition does not have a valid mountpoint "1218"(for the partition from which you intend to boot the "1219"operating system, the mountpoint should be /). Are you "1220"sure you want to continue?"1221, 0, 0);1222conf.button.default_cancel = false;1223}1224if (choice == BSDDIALOG_CANCEL)1225goto addpartform;1226}12271228/*1229* Error if this scheme needs nested partitions, this is one, and1230* a mountpoint was set.1231*/1232if (strcmp(items[0].value, "freebsd") == 0 &&1233strlen(items[2].value) > 0) {1234conf.title = "Error";1235bsddialog_msgbox(&conf, "Partitions of type \"freebsd\" are "1236"nested BSD-type partition schemes and cannot have "1237"mountpoints. After creating one, select it and press "1238"Create again to add the actual file systems.", 0, 0);1239goto addpartform;1240}12411242/* If this is the root partition, check that this scheme is bootable */1243if (strcmp(items[2].value, "/") == 0 && !is_scheme_bootable(scheme)) {1244char message[512];12451246snprintf(message, sizeof(message),1247"This partition scheme (%s) is not bootable "1248"on this platform. Are you sure you want to proceed?",1249scheme);1250conf.button.default_cancel = true;1251conf.title = "Warning";1252choice = bsddialog_yesno(&conf, message, 0, 0);1253conf.button.default_cancel = false;1254if (choice == BSDDIALOG_CANCEL)1255goto addpartform;1256}12571258/* If this is the root partition, check that this fs is bootable */1259if (strcmp(items[2].value, "/") == 0 && !is_fs_bootable(scheme,1260items[0].value)) {1261char message[512];12621263snprintf(message, sizeof(message),1264"This file system (%s) is not bootable "1265"on this system. Are you sure you want to proceed?",1266items[0].value);1267conf.button.default_cancel = true;1268conf.title = "Warning";1269choice = bsddialog_yesno(&conf, message, 0, 0);1270conf.button.default_cancel = false;1271if (choice == BSDDIALOG_CANCEL)1272goto addpartform;1273}12741275/*1276* If this is the root partition, and we need a boot partition, ask1277* the user to add one.1278*/12791280if ((strcmp(items[0].value, "freebsd") == 0 ||1281strcmp(items[2].value, "/") == 0) && bootpart_size(scheme) > 0) {1282size_t bytes = add_boot_partition(geom, pp, scheme,1283interactive);12841285/* Now adjust the part we are really adding forward */1286if (bytes > 0) {1287firstfree += bytes / sector;1288size -= (bytes + stripe)/sector;1289if (stripe > 0 && (firstfree*sector % stripe) != 0)1290firstfree += (stripe - ((firstfree*sector) %1291stripe)) / sector;1292}1293}12941295output[0] = '\0';12961297r = gctl_get_handle();1298gctl_ro_param(r, "class", -1, "PART");1299gctl_ro_param(r, "arg0", -1, geom->lg_name);1300gctl_ro_param(r, "flags", -1, GPART_FLAGS);1301gctl_ro_param(r, "verb", -1, "add");13021303gctl_ro_param(r, "type", -1, items[0].value);1304snprintf(sizestr, sizeof(sizestr), "%jd", size);1305gctl_ro_param(r, "size", -1, sizestr);1306snprintf(startstr, sizeof(startstr), "%jd", firstfree);1307gctl_ro_param(r, "start", -1, startstr);1308if (items[3].value != NULL && items[3].value[0] != '\0')1309gctl_ro_param(r, "label", -1, items[3].value);1310gctl_add_param(r, "output", sizeof(output), output,1311GCTL_PARAM_WR | GCTL_PARAM_ASCII);1312errstr = gctl_issue(r);1313if (errstr != NULL && errstr[0] != '\0') {1314gpart_show_error("Error", NULL, errstr);1315gctl_free(r);1316goto addpartform;1317}1318newpartname = strtok(output, " ");1319gctl_free(r);13201321/*1322* Try to destroy any geom that gpart picked up already here from1323* dirty blocks.1324*/1325r = gctl_get_handle();1326gctl_ro_param(r, "class", -1, "PART");1327gctl_ro_param(r, "arg0", -1, newpartname);1328gctl_ro_param(r, "flags", -1, GPART_FLAGS);1329junk = 1;1330gctl_ro_param(r, "force", sizeof(junk), &junk);1331gctl_ro_param(r, "verb", -1, "destroy");1332gctl_issue(r); /* Error usually expected and non-fatal */1333gctl_free(r);133413351336if (strcmp(items[0].value, "freebsd") == 0)1337gpart_partition(newpartname, "BSD");1338else1339set_default_part_metadata(newpartname, scheme,1340items[0].value, items[2].value, newfs);1341free(newfs);13421343for (i = 0; i < nitems(items); i++) {1344if (items[i].value != NULL) {1345free(items[i].value);1346if (init_allocated && items[i].init != NULL)1347free((char*)items[i].init);1348}1349}13501351if (partname != NULL)1352*partname = strdup(newpartname);1353}13541355void1356gpart_delete(struct gprovider *pp)1357{1358struct gconfig *gc;1359struct ggeom *geom;1360struct gconsumer *cp;1361struct gctl_req *r;1362const char *errstr;1363intmax_t idx;1364int is_partition;1365struct bsddialog_conf conf;13661367/* Is it a partition? */1368is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);13691370/* Find out if this is the root of a gpart geom */1371geom = NULL;1372LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)1373if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {1374geom = cp->lg_geom;1375break;1376}13771378/* If so, destroy all children */1379if (geom != NULL) {1380gpart_destroy(geom);13811382/* If this is a partition, revert it, so it can be deleted */1383if (is_partition) {1384r = gctl_get_handle();1385gctl_ro_param(r, "class", -1, "PART");1386gctl_ro_param(r, "arg0", -1, geom->lg_name);1387gctl_ro_param(r, "verb", -1, "undo");1388gctl_issue(r); /* Ignore non-fatal errors */1389gctl_free(r);1390}1391}13921393/*1394* If this is not a partition, see if that is a problem, complain if1395* necessary, and return always, since we need not do anything further,1396* error or no.1397*/1398if (!is_partition) {1399if (geom == NULL) {1400bsddialog_initconf(&conf);1401conf.title = "Error";1402bsddialog_msgbox(&conf,1403"Only partitions can be deleted.", 0, 0);1404}1405return;1406}14071408r = gctl_get_handle();1409gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);1410gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);1411gctl_ro_param(r, "flags", -1, GPART_FLAGS);1412gctl_ro_param(r, "verb", -1, "delete");14131414LIST_FOREACH(gc, &pp->lg_config, lg_config) {1415if (strcmp(gc->lg_name, "index") == 0) {1416idx = atoi(gc->lg_val);1417gctl_ro_param(r, "index", sizeof(idx), &idx);1418break;1419}1420}14211422errstr = gctl_issue(r);1423if (errstr != NULL && errstr[0] != '\0') {1424gpart_show_error("Error", NULL, errstr);1425gctl_free(r);1426return;1427}14281429gctl_free(r);14301431delete_part_metadata(pp->lg_name);1432}14331434void1435gpart_revert_all(struct gmesh *mesh)1436{1437struct gclass *classp;1438struct gconfig *gc;1439struct ggeom *gp;1440struct gctl_req *r;1441const char *modified;1442struct bsddialog_conf conf;14431444LIST_FOREACH(classp, &mesh->lg_class, lg_class) {1445if (strcmp(classp->lg_name, "PART") == 0)1446break;1447}14481449if (strcmp(classp->lg_name, "PART") != 0) {1450bsddialog_initconf(&conf);1451conf.title = "Error";1452bsddialog_msgbox(&conf, "gpart not found!", 0, 0);1453return;1454}14551456LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {1457modified = "true"; /* XXX: If we don't know (kernel too old),1458* assume there are modifications. */1459LIST_FOREACH(gc, &gp->lg_config, lg_config) {1460if (strcmp(gc->lg_name, "modified") == 0) {1461modified = gc->lg_val;1462break;1463}1464}14651466if (strcmp(modified, "false") == 0)1467continue;14681469r = gctl_get_handle();1470gctl_ro_param(r, "class", -1, "PART");1471gctl_ro_param(r, "arg0", -1, gp->lg_name);1472gctl_ro_param(r, "verb", -1, "undo");14731474gctl_issue(r);1475gctl_free(r);1476}1477}14781479void1480gpart_commit(struct gmesh *mesh)1481{1482struct partition_metadata *md;1483struct gclass *classp;1484struct ggeom *gp;1485struct gconfig *gc;1486struct gconsumer *cp;1487struct gprovider *pp;1488struct gctl_req *r;1489const char *errstr;1490const char *modified;1491const char *rootfs;1492struct bsddialog_conf conf;14931494LIST_FOREACH(classp, &mesh->lg_class, lg_class) {1495if (strcmp(classp->lg_name, "PART") == 0)1496break;1497}14981499/* Figure out what filesystem / uses */1500rootfs = "ufs"; /* Assume ufs if nothing else present */1501TAILQ_FOREACH(md, &part_metadata, metadata) {1502if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {1503rootfs = md->fstab->fs_vfstype;1504break;1505}1506}15071508if (strcmp(classp->lg_name, "PART") != 0) {1509bsddialog_initconf(&conf);1510conf.title = "Error";1511bsddialog_msgbox(&conf, "gpart not found!", 0, 0);1512return;1513}15141515LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {1516modified = "true"; /* XXX: If we don't know (kernel too old),1517* assume there are modifications. */1518LIST_FOREACH(gc, &gp->lg_config, lg_config) {1519if (strcmp(gc->lg_name, "modified") == 0) {1520modified = gc->lg_val;1521break;1522}1523}15241525if (strcmp(modified, "false") == 0)1526continue;15271528/* Add bootcode if necessary, before the commit */1529md = get_part_metadata(gp->lg_name, 0);1530if (md != NULL && md->bootcode)1531gpart_bootcode(gp);15321533/* Now install partcode on its partitions, if necessary */1534LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {1535md = get_part_metadata(pp->lg_name, 0);1536if (md == NULL || !md->bootcode)1537continue;15381539/* Mark this partition active if that's required */1540gpart_activate(pp);15411542/* Check if the partition has sub-partitions */1543LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)1544if (strcmp(cp->lg_geom->lg_class->lg_name,1545"PART") == 0)1546break;15471548if (cp == NULL) /* No sub-partitions */1549gpart_partcode(pp, rootfs);1550}15511552r = gctl_get_handle();1553gctl_ro_param(r, "class", -1, "PART");1554gctl_ro_param(r, "arg0", -1, gp->lg_name);1555gctl_ro_param(r, "verb", -1, "commit");15561557errstr = gctl_issue(r);1558if (errstr != NULL && errstr[0] != '\0')1559gpart_show_error("Error", NULL, errstr);1560gctl_free(r);1561}1562}1563156415651566