Index: stable/12/lib/libbe/be.h =================================================================== --- stable/12/lib/libbe/be.h (revision 367682) +++ stable/12/lib/libbe/be.h (revision 367683) @@ -1,137 +1,137 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2017 Kyle J. Kneitinger * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _LIBBE_H #define _LIBBE_H #include #include #define BE_MAXPATHLEN 512 typedef struct libbe_handle libbe_handle_t; typedef enum be_error { BE_ERR_SUCCESS = 0, /* No error */ BE_ERR_INVALIDNAME, /* invalid boot env name */ BE_ERR_EXISTS, /* boot env name already taken */ BE_ERR_NOENT, /* boot env doesn't exist */ BE_ERR_PERMS, /* insufficient permissions */ BE_ERR_DESTROYACT, /* cannot destroy active boot env */ BE_ERR_DESTROYMNT, /* destroying a mounted be requires force */ BE_ERR_BADPATH, /* path not suitable for operation */ BE_ERR_PATHBUSY, /* requested path is busy */ BE_ERR_PATHLEN, /* provided name exceeds maximum length limit */ BE_ERR_BADMOUNT, /* mountpoint is not '/' */ BE_ERR_NOORIGIN, /* could not open snapshot's origin */ BE_ERR_MOUNTED, /* boot environment is already mounted */ BE_ERR_NOMOUNT, /* boot environment is not mounted */ BE_ERR_ZFSOPEN, /* calling zfs_open() failed */ BE_ERR_ZFSCLONE, /* error when calling zfs_clone to create be */ BE_ERR_IO, /* error when doing some I/O operation */ BE_ERR_NOPOOL, /* operation not supported on this pool */ BE_ERR_NOMEM, /* insufficient memory */ BE_ERR_UNKNOWN, /* unknown error */ BE_ERR_INVORIGIN, /* invalid origin */ BE_ERR_HASCLONES, /* snapshot has clones */ } be_error_t; /* Library handling functions: be.c */ libbe_handle_t *libbe_init(const char *root); void libbe_close(libbe_handle_t *); /* Bootenv information functions: be_info.c */ const char *be_active_name(libbe_handle_t *); const char *be_active_path(libbe_handle_t *); const char *be_nextboot_name(libbe_handle_t *); const char *be_nextboot_path(libbe_handle_t *); const char *be_root_path(libbe_handle_t *); int be_get_bootenv_props(libbe_handle_t *, nvlist_t *); int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *); int be_get_dataset_snapshots(libbe_handle_t *, const char *, nvlist_t *); int be_prop_list_alloc(nvlist_t **be_list); void be_prop_list_free(nvlist_t *be_list); int be_activate(libbe_handle_t *, const char *, bool); bool be_is_auto_snapshot_name(libbe_handle_t *, const char *); /* Bootenv creation functions */ int be_create(libbe_handle_t *, const char *); int be_create_depth(libbe_handle_t *, const char *, const char *, int); int be_create_from_existing(libbe_handle_t *, const char *, const char *); int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *); int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *); /* Bootenv manipulation functions */ int be_rename(libbe_handle_t *, const char *, const char *); /* Bootenv removal functions */ typedef enum { BE_DESTROY_FORCE = 1 << 0, BE_DESTROY_ORIGIN = 1 << 1, BE_DESTROY_AUTOORIGIN = 1 << 2, } be_destroy_opt_t; int be_destroy(libbe_handle_t *, const char *, int); /* Bootenv mounting functions: be_access.c */ typedef enum { BE_MNT_FORCE = 1 << 0, BE_MNT_DEEP = 1 << 1, } be_mount_opt_t; -int be_mount(libbe_handle_t *, char *, char *, int, char *); -int be_unmount(libbe_handle_t *, char *, int); +int be_mount(libbe_handle_t *, const char *, const char *, int, char *); +int be_unmount(libbe_handle_t *, const char *, int); int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *); /* Error related functions: be_error.c */ int libbe_errno(libbe_handle_t *); const char *libbe_error_description(libbe_handle_t *); void libbe_print_on_error(libbe_handle_t *, bool); /* Utility Functions */ int be_root_concat(libbe_handle_t *, const char *, char *); int be_validate_name(libbe_handle_t * __unused, const char *); int be_validate_snap(libbe_handle_t *, const char *); -int be_exists(libbe_handle_t *, char *); +int be_exists(libbe_handle_t *, const char *); int be_export(libbe_handle_t *, const char *, int fd); int be_import(libbe_handle_t *, const char *, int fd); #if SOON int be_add_child(libbe_handle_t *, const char *, bool); #endif void be_nicenum(uint64_t num, char *buf, size_t buflen); #endif /* _LIBBE_H */ Index: stable/12/lib/libbe/be_access.c =================================================================== --- stable/12/lib/libbe/be_access.c (revision 367682) +++ stable/12/lib/libbe/be_access.c (revision 367683) @@ -1,339 +1,339 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2017 Kyle J. Kneitinger * Copyright (c) 2018 Kyle Evans * Copyright (c) 2019 Wes Maag * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include "be.h" #include "be_impl.h" struct be_mountcheck_info { const char *path; char *name; }; struct be_mount_info { libbe_handle_t *lbh; const char *be; const char *mountpoint; int mntflags; int deepmount; int depth; }; static int be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data) { struct be_mountcheck_info *info; char *mountpoint; if (data == NULL) return (1); info = (struct be_mountcheck_info *)data; if (!zfs_is_mounted(zfs_hdl, &mountpoint)) return (0); if (strcmp(mountpoint, info->path) == 0) { info->name = strdup(zfs_get_name(zfs_hdl)); free(mountpoint); return (1); } free(mountpoint); return (0); } /* * Called from be_mount, uses the given zfs_handle and attempts to * mount it at the passed mountpoint. If the deepmount flag is set, continue * calling the function for each child dataset. */ static int be_mount_iter(zfs_handle_t *zfs_hdl, void *data) { int err; char *mountpoint; char tmp[BE_MAXPATHLEN], zfs_mnt[BE_MAXPATHLEN]; struct be_mount_info *info; info = (struct be_mount_info *)data; if (zfs_is_mounted(zfs_hdl, &mountpoint)) { free(mountpoint); return (0); } /* * canmount and mountpoint are both ignored for the BE dataset, because * the rest of the system (kernel and loader) will effectively do the * same. */ if (info->depth == 0) { snprintf(tmp, BE_MAXPATHLEN, "%s", info->mountpoint); } else { if (zfs_prop_get_int(zfs_hdl, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) return (0); if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, zfs_mnt, BE_MAXPATHLEN, NULL, NULL, 0, 1)) return (1); /* * We've encountered mountpoint=none at some intermediate * dataset (e.g. zroot/var) that will have children that may * need to be mounted. Skip mounting it, but iterate through * the children. */ if (strcmp("none", zfs_mnt) == 0) goto skipmount; mountpoint = be_mountpoint_augmented(info->lbh, zfs_mnt); snprintf(tmp, BE_MAXPATHLEN, "%s%s", info->mountpoint, mountpoint); } if ((err = zfs_mount_at(zfs_hdl, NULL, info->mntflags, tmp)) != 0) { switch (errno) { case ENAMETOOLONG: return (set_error(info->lbh, BE_ERR_PATHLEN)); case ELOOP: case ENOENT: case ENOTDIR: return (set_error(info->lbh, BE_ERR_BADPATH)); case EPERM: return (set_error(info->lbh, BE_ERR_PERMS)); case EBUSY: return (set_error(info->lbh, BE_ERR_PATHBUSY)); default: return (set_error(info->lbh, BE_ERR_UNKNOWN)); } } if (!info->deepmount) return (0); skipmount: ++info->depth; err = zfs_iter_filesystems(zfs_hdl, be_mount_iter, info); --info->depth; return (err); } static int be_umount_iter(zfs_handle_t *zfs_hdl, void *data) { int err; char *mountpoint; struct be_mount_info *info; info = (struct be_mount_info *)data; ++info->depth; if((err = zfs_iter_filesystems(zfs_hdl, be_umount_iter, info)) != 0) { return (err); } --info->depth; if (!zfs_is_mounted(zfs_hdl, &mountpoint)) { return (0); } free(mountpoint); if (zfs_unmount(zfs_hdl, NULL, info->mntflags) != 0) { switch (errno) { case ENAMETOOLONG: return (set_error(info->lbh, BE_ERR_PATHLEN)); case ELOOP: case ENOENT: case ENOTDIR: return (set_error(info->lbh, BE_ERR_BADPATH)); case EPERM: return (set_error(info->lbh, BE_ERR_PERMS)); case EBUSY: return (set_error(info->lbh, BE_ERR_PATHBUSY)); default: return (set_error(info->lbh, BE_ERR_UNKNOWN)); } } return (0); } /* * usage */ int be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details) { char be[BE_MAXPATHLEN]; zfs_handle_t *root_hdl; struct be_mountcheck_info info; prop_data_t propinfo; bzero(&be, BE_MAXPATHLEN); if ((root_hdl = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_FILESYSTEM)) == NULL) return (BE_ERR_ZFSOPEN); info.path = path; info.name = NULL; zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info); zfs_close(root_hdl); if (info.name != NULL) { if (details != NULL) { if ((root_hdl = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_FILESYSTEM)) == NULL) { free(info.name); return (BE_ERR_ZFSOPEN); } propinfo.lbh = lbh; propinfo.list = details; propinfo.single_object = false; prop_list_builder_cb(root_hdl, &propinfo); zfs_close(root_hdl); } free(info.name); return (0); } return (1); } /* * usage */ int -be_mount(libbe_handle_t *lbh, char *bootenv, char *mountpoint, int flags, - char *result_loc) +be_mount(libbe_handle_t *lbh, const char *bootenv, const char *mountpoint, + int flags, char *result_loc) { char be[BE_MAXPATHLEN]; char mnt_temp[BE_MAXPATHLEN]; int mntflags, mntdeep; int err; struct be_mount_info info; zfs_handle_t *zhdl; if ((err = be_root_concat(lbh, bootenv, be)) != 0) return (set_error(lbh, err)); if ((err = be_exists(lbh, bootenv)) != 0) return (set_error(lbh, err)); if (is_mounted(lbh->lzh, be, NULL)) return (set_error(lbh, BE_ERR_MOUNTED)); mntdeep = (flags & BE_MNT_DEEP) ? 1 : 0; mntflags = (flags & BE_MNT_FORCE) ? MNT_FORCE : 0; /* Create mountpoint if it is not specified */ if (mountpoint == NULL) { strlcpy(mnt_temp, "/tmp/be_mount.XXXX", sizeof(mnt_temp)); if (mkdtemp(mnt_temp) == NULL) return (set_error(lbh, BE_ERR_IO)); } if ((zhdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL) return (set_error(lbh, BE_ERR_ZFSOPEN)); info.lbh = lbh; info.be = be; info.mountpoint = (mountpoint == NULL) ? mnt_temp : mountpoint; info.mntflags = mntflags; info.deepmount = mntdeep; info.depth = 0; if((err = be_mount_iter(zhdl, &info) != 0)) { zfs_close(zhdl); return (err); } zfs_close(zhdl); if (result_loc != NULL) strlcpy(result_loc, mountpoint == NULL ? mnt_temp : mountpoint, BE_MAXPATHLEN); return (BE_ERR_SUCCESS); } /* * usage */ int -be_unmount(libbe_handle_t *lbh, char *bootenv, int flags) +be_unmount(libbe_handle_t *lbh, const char *bootenv, int flags) { int err; char be[BE_MAXPATHLEN]; zfs_handle_t *root_hdl; struct be_mount_info info; if ((err = be_root_concat(lbh, bootenv, be)) != 0) return (set_error(lbh, err)); if ((root_hdl = zfs_open(lbh->lzh, be, ZFS_TYPE_FILESYSTEM)) == NULL) return (set_error(lbh, BE_ERR_ZFSOPEN)); info.lbh = lbh; info.be = be; info.mountpoint = NULL; info.mntflags = (flags & BE_MNT_FORCE) ? MS_FORCE : 0; info.depth = 0; if ((err = be_umount_iter(root_hdl, &info)) != 0) { zfs_close(root_hdl); return (err); } zfs_close(root_hdl); return (BE_ERR_SUCCESS); } /* * This function will blow away the input buffer as needed if we're discovered * to be looking at a root-mount. If the mountpoint is naturally beyond the * root, however, the buffer may be left intact and a pointer to the section * past altroot will be returned instead for the caller's perusal. */ char * be_mountpoint_augmented(libbe_handle_t *lbh, char *mountpoint) { if (lbh->altroot_len == 0) return (mountpoint); if (mountpoint == NULL || *mountpoint == '\0') return (mountpoint); if (mountpoint[lbh->altroot_len] == '\0') { *(mountpoint + 1) = '\0'; return (mountpoint); } else return (mountpoint + lbh->altroot_len); } Index: stable/12/lib/libbe/be_info.c =================================================================== --- stable/12/lib/libbe/be_info.c (revision 367682) +++ stable/12/lib/libbe/be_info.c (revision 367683) @@ -1,307 +1,307 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2017 Kyle J. Kneitinger * Copyright (c) 2018 Kyle Evans * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include "be.h" #include "be_impl.h" static int snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data); /* * Returns the name of the active boot environment */ const char * be_active_name(libbe_handle_t *lbh) { if (*lbh->rootfs != '\0') return (strrchr(lbh->rootfs, '/') + sizeof(char)); else return (lbh->rootfs); } /* * Returns full path of the active boot environment */ const char * be_active_path(libbe_handle_t *lbh) { return (lbh->rootfs); } /* * Returns the name of the next active boot environment */ const char * be_nextboot_name(libbe_handle_t *lbh) { if (*lbh->bootfs != '\0') return (strrchr(lbh->bootfs, '/') + sizeof(char)); else return (lbh->bootfs); } /* * Returns full path of the active boot environment */ const char * be_nextboot_path(libbe_handle_t *lbh) { return (lbh->bootfs); } /* * Returns the path of the boot environment root dataset */ const char * be_root_path(libbe_handle_t *lbh) { return (lbh->root); } /* * Populates dsnvl with one nvlist per bootenv dataset describing the properties * of that dataset that we've declared ourselves to care about. */ int be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl) { prop_data_t data; data.lbh = lbh; data.list = dsnvl; data.single_object = false; return (be_proplist_update(&data)); } int be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props) { zfs_handle_t *snap_hdl; prop_data_t data; int ret; data.lbh = lbh; data.list = props; data.single_object = true; if ((snap_hdl = zfs_open(lbh->lzh, name, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) return (BE_ERR_ZFSOPEN); ret = prop_list_builder_cb(snap_hdl, &data); zfs_close(snap_hdl); return (ret); } int be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props) { zfs_handle_t *ds_hdl; prop_data_t data; int ret; data.lbh = lbh; data.list = props; data.single_object = false; if ((ds_hdl = zfs_open(lbh->lzh, name, ZFS_TYPE_FILESYSTEM)) == NULL) return (BE_ERR_ZFSOPEN); ret = snapshot_proplist_update(ds_hdl, &data); zfs_close(ds_hdl); return (ret); } /* * Internal callback function used by zfs_iter_filesystems. For each dataset in * the bootenv root, populate an nvlist_t of its relevant properties. */ int prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p) { char buf[512], *mountpoint; prop_data_t *data; libbe_handle_t *lbh; nvlist_t *props; const char *dataset, *name; boolean_t mounted; /* * XXX TODO: * some system for defining constants for the nvlist keys * error checking */ data = (prop_data_t *)data_p; lbh = data->lbh; if (data->single_object) props = data->list; else nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); dataset = zfs_get_name(zfs_hdl); nvlist_add_string(props, "dataset", dataset); name = strrchr(dataset, '/') + 1; nvlist_add_string(props, "name", name); mounted = zfs_is_mounted(zfs_hdl, &mountpoint); if (mounted) nvlist_add_string(props, "mounted", mountpoint); if (zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "mountpoint", buf); if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "origin", buf); if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "creation", buf); nvlist_add_boolean_value(props, "active", (strcmp(be_active_path(lbh), dataset) == 0)); if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "used", buf); if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "usedds", buf); if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "usedsnap", buf); if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "usedrefreserv", buf); if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512, NULL, NULL, 0, 1) == 0) nvlist_add_string(props, "referenced", buf); nvlist_add_boolean_value(props, "nextboot", (strcmp(be_nextboot_path(lbh), dataset) == 0)); if (!data->single_object) nvlist_add_nvlist(data->list, name, props); return (0); } /* * Updates the properties of each bootenv in the libbe handle * XXX TODO: ensure that this is always consistent (run after adds, deletes, * renames,etc */ int be_proplist_update(prop_data_t *data) { zfs_handle_t *root_hdl; if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root, ZFS_TYPE_FILESYSTEM)) == NULL) return (BE_ERR_ZFSOPEN); /* XXX TODO: some error checking here */ zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data); zfs_close(root_hdl); return (0); } static int snapshot_proplist_update(zfs_handle_t *hdl, prop_data_t *data) { return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data, 0, 0)); } int be_prop_list_alloc(nvlist_t **be_list) { return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP)); } /* * frees property list and its children */ void be_prop_list_free(nvlist_t *be_list) { nvlist_t *prop_list; nvpair_t *be_pair; be_pair = nvlist_next_nvpair(be_list, NULL); if (nvpair_value_nvlist(be_pair, &prop_list) == 0) nvlist_free(prop_list); while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) { if (nvpair_value_nvlist(be_pair, &prop_list) == 0) nvlist_free(prop_list); } } /* * Usage */ int -be_exists(libbe_handle_t *lbh, char *be) +be_exists(libbe_handle_t *lbh, const char *be) { char buf[BE_MAXPATHLEN]; be_root_concat(lbh, be, buf); if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET)) return (BE_ERR_NOENT); return (BE_ERR_SUCCESS); } Index: stable/12/lib/libbe/libbe.3 =================================================================== --- stable/12/lib/libbe/libbe.3 (revision 367682) +++ stable/12/lib/libbe/libbe.3 (revision 367683) @@ -1,558 +1,558 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD .\" .\" Copyright (c) 2017 Kyle Kneitinger .\" Copyright (c) 2018 Kyle Evans .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" -.Dd October 16, 2019 +.Dd November 14, 2020 .Dt LIBBE 3 .Os .Sh NAME .Nm libbe .Nd library for creating, destroying and modifying ZFS boot environments .Sh LIBRARY .Lb libbe .Sh SYNOPSIS .In be.h .Ft "libbe_handle_t *hdl" Ns .Fn libbe_init "const char *be_root" .Pp .Ft void .Fn libbe_close "libbe_handle_t *hdl" .Pp .Ft const char * Ns .Fn be_active_name "libbe_handle_t *hdl" .Pp .Ft const char * Ns .Fn be_active_path "libbe_handle_t *hdl" .Pp .Ft const char * Ns .Fn be_nextboot_name "libbe_handle_t *hdl" .Pp .Ft const char * Ns .Fn be_nextboot_path "libbe_handle_t *hdl" .Pp .Ft const char * Ns .Fn be_root_path "libbe_handle_t *hdl" .Pp .Ft int Ns .Fn be_snapshot "libbe_handle_t *hdl" "const char *be_name" "const char *snap_name" "bool recursive" "char *result" .Pp .Ft bool Ns .Fn be_is_auto_snapshot_name "libbe_handle_t *hdl" "const char *snap" .Pp .Ft int .Fn be_create "libbe_handle_t *hdl" "const char *be_name" .Pp .Ft int .Fn be_create_depth "libbe_handle_t *hdl" "const char *be_name" "const char *snap" "int depth" .Pp .Ft int .Fn be_create_from_existing "libbe_handle_t *hdl" "const char *be_name" "const char *be_origin" .Pp .Ft int .Fn be_create_from_existing_snap "libbe_handle_t *hdl" "const char *be_name" "const char *snap" .Pp .Ft int .Fn be_rename "libbe_handle_t *hdl" "const char *be_old" "const char *be_new" .Pp .Ft int .Fn be_activate "libbe_handle_t *hdl" "const char *be_name" "bool temporary" .Ft int .Fn be_destroy "libbe_handle_t *hdl" "const char *be_name" "int options" .Pp .Ft void .Fn be_nicenum "uint64_t num" "char *buf" "size_t bufsz" .Pp .\" TODO: Write up of mount options .\" typedef enum { .\" BE_MNT_FORCE = 1 << 0, .\" BE_MNT_DEEP = 1 << 1, .\" } be_mount_opt_t .Ft int -.Fn be_mount "libbe_handle_t *hdl" "char *be_name" "char *mntpoint" "int flags" "char *result" +.Fn be_mount "libbe_handle_t *hdl" "const char *be_name" "const char *mntpoint" "int flags" "char *result" .Pp .Ft int .Fn be_mounted_at "libbe_handle_t *hdl" "const char *path" "nvlist_t *details" .Pp .Ft int -.Fn be_unmount "libbe_handle_t *hdl" "char *be_name" "int flags" +.Fn be_unmount "libbe_handle_t *hdl" "const char *be_name" "int flags" .Pp .Ft int .Fn libbe_errno "libbe_handle_t *hdl" .Pp .Ft const char * Ns .Fn libbe_error_description "libbe_handle_t *hdl" .Pp .Ft void .Fn libbe_print_on_error "libbe_handle_t *hdl" "bool doprint" .Pp .Ft int .Fn be_root_concat "libbe_handle_t *hdl" "const char *be_name" "char *result" .Pp .Ft int .Fn be_validate_name "libbe_handle_t *hdl" "const char *be_name" .Pp .Ft int .Fn be_validate_snap "libbe_handle_t *hdl" "const char *snap" .Pp .Ft int -.Fn be_exists "libbe_handle_t *hdl" "char *be_name" +.Fn be_exists "libbe_handle_t *hdl" "const char *be_name" .Pp .Ft int .Fn be_export "libbe_handle_t *hdl" "const char *be_name" "int fd" .Pp .Ft int .Fn be_import "libbe_handle_t *hdl" "const char *be_name" "int fd" .Pp .Ft int .Fn be_prop_list_alloc "nvlist_t **prop_list" .Pp .Ft int .Fn be_get_bootenv_props "libbe_handle_t *hdl" "nvlist_t *be_list" .Pp .Ft int .Fn be_get_dataset_props "libbe_handle_t *hdl" "const char *ds_name" "nvlist_t *props" .Pp .Ft int .Fn be_get_dataset_snapshots "libbe_handle_t *hdl" "const char *ds_name" "nvlist_t *snap_list" .Pp .Ft void .Fn be_prop_list_free "nvlist_t *prop_list" .Sh DESCRIPTION .Nm interfaces with libzfs to provide a set of functions for various operations regarding ZFS boot environments including "deep" boot environments in which a boot environments has child datasets. .Pp A context structure is passed to each function, allowing for a small amount of state to be retained, such as errors from previous operations. .Nm may be configured to print the corresponding error message to .Dv stderr when an error is encountered with .Fn libbe_print_on_error . .Pp All functions returning an .Vt int return 0 on success, or a .Nm errno otherwise as described in .Sx DIAGNOSTICS . .Pp The .Fn libbe_init function takes an optional BE root and initializes .Nm , returning a .Vt "libbe_handle_t *" on success, or .Dv NULL on error. If a BE root is supplied, .Nm will only operate out of that pool and BE root. An error may occur if: .Bl -column .It /boot and / are not on the same filesystem and device, .It libzfs fails to initialize, .It The system has not been properly booted with a ZFS boot environment, .It Nm fails to open the zpool the active boot environment resides on, or .It Nm fails to locate the boot environment that is currently mounted. .El .Pp The .Fn libbe_close function frees all resources previously acquired in .Fn libbe_init , invalidating the handle in the process. .Pp The .Fn be_active_name function returns the name of the currently booted boot environment. This boot environment may not belong to the same BE root as the root libbe is operating on! .Pp The .Fn be_active_path function returns the full path of the currently booted boot environment. This boot environment may not belong to the same BE root as the root libbe is operating on! .Pp The .Fn be_nextboot_name function returns the name of the boot environment that will be active on reboot. .Pp The .Fn be_nextboot_path function returns the full path of the boot environment that will be active on reboot. .Pp The .Fn be_root_path function returns the boot environment root path. .Pp The .Fn be_snapshot function creates a snapshot of .Fa be_name named .Fa snap_name . A .Dv NULL .Fa snap_name may be used, indicating that .Fn be_snaphot should derive the snapshot name from the current date and time. If .Fa recursive is set, then .Fn be_snapshot will recursively snapshot the dataset. If .Fa result is not .Dv NULL , then it will be populated with the final .Dq Fa be_name Ns @ Ns Fa snap_name . .Pp The .Fn be_is_auto_snapshot_name function is used to determine if the given snapshot name matches the format that the .Fn be_snapshot function will use by default if it is not given a snapshot name to use. It returns .Dv true if the name matches the format, and .Dv false if it does not. .Pp The .Fn be_create function creates a boot environment with the given name. The new boot environment will be created from a recursive snapshot of the currently booted boot environment. .Pp The .Fn be_create_depth function creates a boot environment with the given name from an existing snapshot. The depth parameter specifies the depth of recursion that will be cloned from the existing snapshot. A depth of '0' is no recursion and '-1' is unlimited (i.e., a recursive boot environment). .Pp The .Fn be_create_from_existing function creates a boot environment with the given name from the name of an existing boot environment. A recursive snapshot will be made of the origin boot environment, and the new boot environment will be created from that. .Pp The .Fn be_create_from_existing_snap function creates a recursive boot environment with the given name from an existing snapshot. .Pp The .Fn be_rename function renames a boot environment without unmounting it, as if renamed with the .Fl u argument were passed to .Nm zfs .Cm rename .Pp The .Fn be_activate function makes a boot environment active on the next boot. If the .Fa temporary flag is set, then it will be active for the next boot only, as done by .Xr zfsbootcfg 8 . Next boot functionality is currently only available when booting in x86 BIOS mode. .Pp The .Fn be_destroy function will recursively destroy the given boot environment. It will not destroy a mounted boot environment unless the .Dv BE_DESTROY_FORCE option is set in .Fa options . If the .Dv BE_DESTROY_ORIGIN option is set in .Fa options , the .Fn be_destroy function will destroy the origin snapshot to this boot environment as well. .Pp The .Fn be_nicenum function will format .Fa name in a traditional ZFS humanized format, similar to .Xr humanize_number 3 . This function effectively proxies .Fn zfs_nicenum from libzfs. .Pp The .Fn be_mount function will mount the given boot environment. If .Fa mountpoint is .Dv NULL , a mount point will be generated in .Pa /tmp using .Xr mkdtemp 3 . If .Fa result is not .Dv NULL , it should be large enough to accommodate .Dv BE_MAXPATHLEN including the null terminator. the final mount point will be copied into it. Setting the .Dv BE_MNT_FORCE flag will pass .Dv MNT_FORCE to the underlying .Xr mount 2 call. .Pp The .Fn be_mounted_at function will check if there is a boot environment mounted at the given .Fa path . If .Fa details is not .Dv NULL , it will be populated with a list of the mounted dataset's properties. This list of properties matches the properties collected by .Fn be_get_bootenv_props . .Pp The .Fn be_unmount function will unmount the given boot environment. Setting the .Dv BE_MNT_FORCE flag will pass .Dv MNT_FORCE to the underlying .Xr mount 2 call. .Pp The .Fn libbe_errno function returns the .Nm errno. .Pp The .Fn libbe_error_description function returns a string description of the currently set .Nm errno. .Pp The .Fn libbe_print_on_error function will change whether or not .Nm prints the description of any encountered error to .Dv stderr , based on .Fa doprint . .Pp The .Fn be_root_concat function will concatenate the boot environment root and the given boot environment name into .Fa result . .Pp The .Fn be_validate_name function will validate the given boot environment name for both length restrictions as well as valid character restrictions. This function does not set the internal library error state. .Pp The .Fn be_validate_snap function will validate the given snapshot name. The snapshot must have a valid name, exist, and have a mountpoint of .Pa / . This function does not set the internal library error state. .Pp The .Fn be_exists function will check whether the given boot environment exists and has a mountpoint of .Pa / . This function does not set the internal library error state, but will return the appropriate error. .Pp The .Fn be_export function will export the given boot environment to the file specified by .Fa fd . A snapshot will be created of the boot environment prior to export. .Pp The .Fn be_import function will import the boot environment in the file specified by .Fa fd , and give it the name .Fa be_name . .Pp The .Fn be_prop_list_alloc function allocates a property list suitable for passing to .Fn be_get_bootenv_props , .Fn be_get_dataset_props , or .Fn be_get_dataset_snapshots . It should be freed later by .Fa be_prop_list_free . .Pp The .Fn be_get_bootenv_props function will populate .Fa be_list with .Vt nvpair_t of boot environment names paired with an .Vt nvlist_t of their properties. The following properties are currently collected as appropriate: .Bl -column "Returned name" .It Sy Returned name Ta Sy Description .It dataset Ta - .It name Ta Boot environment name .It mounted Ta Current mount point .It mountpoint Ta Do mountpoint Dc property .It origin Ta Do origin Dc property .It creation Ta Do creation Dc property .It active Ta Currently booted environment .It used Ta Literal Do used Dc property .It usedds Ta Literal Do usedds Dc property .It usedsnap Ta Literal Do usedrefreserv Dc property .It referenced Ta Literal Do referenced Dc property .It nextboot Ta Active on next boot .El .Pp Only the .Dq dataset , .Dq name , .Dq active , and .Dq nextboot returned values will always be present. All other properties may be omitted if not available. .Pp The .Fn be_get_dataset_props function will get properties of the specified dataset. .Fa props is populated directly with a list of the properties as returned by .Fn be_get_bootenv_props . .Pp The .Fn be_get_dataset_snapshots function will retrieve all snapshots of the given dataset. .Fa snap_list will be populated with a list of .Vt nvpair_t exactly as specified by .Fn be_get_bootenv_props . .Pp The .Fn be_prop_list_free function will free the property list. .Sh DIAGNOSTICS Upon error, one of the following values will be returned: .Bl -dash -offset indent -compact .It BE_ERR_SUCCESS .It BE_ERR_INVALIDNAME .It BE_ERR_EXISTS .It BE_ERR_NOENT .It BE_ERR_PERMS .It BE_ERR_DESTROYACT .It BE_ERR_DESTROYMNT .It BE_ERR_BADPATH .It BE_ERR_PATHBUSY .It BE_ERR_PATHLEN .It BE_ERR_BADMOUNT .It BE_ERR_NOORIGIN .It BE_ERR_MOUNTED .It BE_ERR_NOMOUNT .It BE_ERR_ZFSOPEN .It BE_ERR_ZFSCLONE .It BE_ERR_IO .It BE_ERR_NOPOOL .It BE_ERR_NOMEM .It BE_ERR_UNKNOWN .It BE_ERR_INVORIGIN .El .Sh SEE ALSO .Xr bectl 8 .Sh HISTORY .Nm and its corresponding command, .Xr bectl 8 , were written as a 2017 Google Summer of Code project with Allan Jude serving as a mentor. Later work was done by .An Kyle Evans Aq Mt kevans@FreeBSD.org . Index: stable/12 =================================================================== --- stable/12 (revision 367682) +++ stable/12 (revision 367683) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r366819