1b179da01SKyle Evans /*- 2b179da01SKyle Evans * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 328f16a0fSKyle Evans * 428f16a0fSKyle Evans * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 528f16a0fSKyle Evans * All rights reserved. 628f16a0fSKyle Evans * 728f16a0fSKyle Evans * Redistribution and use in source and binary forms, with or without 828f16a0fSKyle Evans * modification, are permitted provided that the following conditions 928f16a0fSKyle Evans * are met: 1028f16a0fSKyle Evans * 1. Redistributions of source code must retain the above copyright 1128f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer. 1228f16a0fSKyle Evans * 2. Redistributions in binary form must reproduce the above copyright 1328f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer in the 1428f16a0fSKyle Evans * documentation and/or other materials provided with the distribution. 1528f16a0fSKyle Evans * 1628f16a0fSKyle Evans * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1728f16a0fSKyle Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1828f16a0fSKyle Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1928f16a0fSKyle Evans * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2028f16a0fSKyle Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2128f16a0fSKyle Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2228f16a0fSKyle Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2328f16a0fSKyle Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2428f16a0fSKyle Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2528f16a0fSKyle Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2628f16a0fSKyle Evans * SUCH DAMAGE. 2728f16a0fSKyle Evans */ 2828f16a0fSKyle Evans 29b6e7c421SKyle Evans #include <sys/cdefs.h> 30b6e7c421SKyle Evans __FBSDID("$FreeBSD$"); 31b6e7c421SKyle Evans 3228f16a0fSKyle Evans #include <sys/stat.h> 3328f16a0fSKyle Evans #include <sys/types.h> 3428f16a0fSKyle Evans 3528f16a0fSKyle Evans #include <ctype.h> 3628f16a0fSKyle Evans #include <kenv.h> 3728f16a0fSKyle Evans #include <libgen.h> 3828f16a0fSKyle Evans #include <libzfs_core.h> 3928f16a0fSKyle Evans #include <stdio.h> 4028f16a0fSKyle Evans #include <stdlib.h> 4128f16a0fSKyle Evans #include <time.h> 4228f16a0fSKyle Evans #include <unistd.h> 4328f16a0fSKyle Evans 4428f16a0fSKyle Evans #include "be.h" 4528f16a0fSKyle Evans #include "be_impl.h" 4628f16a0fSKyle Evans 4728f16a0fSKyle Evans /* 48ee16b7c9SKyle Evans * Iterator function for locating the rootfs amongst the children of the 49ee16b7c9SKyle Evans * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. 50ee16b7c9SKyle Evans */ 51ee16b7c9SKyle Evans static int 52ee16b7c9SKyle Evans be_locate_rootfs(zfs_handle_t *chkds, void *data) 53ee16b7c9SKyle Evans { 54ee16b7c9SKyle Evans libbe_handle_t *lbh; 55ee16b7c9SKyle Evans char *mntpoint; 56ee16b7c9SKyle Evans 57ee16b7c9SKyle Evans lbh = (libbe_handle_t *)data; 58ee16b7c9SKyle Evans if (lbh == NULL) 59ee16b7c9SKyle Evans return (1); 60ee16b7c9SKyle Evans 61ee16b7c9SKyle Evans if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) { 62ee16b7c9SKyle Evans strncpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN); 63ee16b7c9SKyle Evans return (1); 64ee16b7c9SKyle Evans } 65ee16b7c9SKyle Evans 66ee16b7c9SKyle Evans return (0); 67ee16b7c9SKyle Evans } 68ee16b7c9SKyle Evans 69ee16b7c9SKyle Evans /* 7028f16a0fSKyle Evans * Initializes the libbe context to operate in the root boot environment 7128f16a0fSKyle Evans * dataset, for example, zroot/ROOT. 7228f16a0fSKyle Evans */ 7328f16a0fSKyle Evans libbe_handle_t * 7428f16a0fSKyle Evans libbe_init(void) 7528f16a0fSKyle Evans { 7628f16a0fSKyle Evans struct stat sb; 7728f16a0fSKyle Evans dev_t root_dev, boot_dev; 7828f16a0fSKyle Evans libbe_handle_t *lbh; 79ee16b7c9SKyle Evans zfs_handle_t *rootds; 80c3a34c08SKyle Evans char *poolname, *pos; 81c3a34c08SKyle Evans int pnamelen; 8228f16a0fSKyle Evans 83c3a34c08SKyle Evans lbh = NULL; 84c3a34c08SKyle Evans poolname = pos = NULL; 85c3a34c08SKyle Evans pnamelen = 0; 86ee16b7c9SKyle Evans rootds = NULL; 87ee16b7c9SKyle Evans 8828f16a0fSKyle Evans /* Verify that /boot and / are mounted on the same filesystem */ 89c3a34c08SKyle Evans /* TODO: use errno here?? */ 90c3a34c08SKyle Evans if (stat("/", &sb) != 0) 91c3a34c08SKyle Evans goto err; 9228f16a0fSKyle Evans 9328f16a0fSKyle Evans root_dev = sb.st_dev; 9428f16a0fSKyle Evans 95c3a34c08SKyle Evans if (stat("/boot", &sb) != 0) 96c3a34c08SKyle Evans goto err; 9728f16a0fSKyle Evans 9828f16a0fSKyle Evans boot_dev = sb.st_dev; 9928f16a0fSKyle Evans 10028f16a0fSKyle Evans if (root_dev != boot_dev) { 10128f16a0fSKyle Evans fprintf(stderr, "/ and /boot not on same device, quitting\n"); 102c3a34c08SKyle Evans goto err; 10328f16a0fSKyle Evans } 10428f16a0fSKyle Evans 105c3a34c08SKyle Evans if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL) 106c3a34c08SKyle Evans goto err; 10728f16a0fSKyle Evans 108c3a34c08SKyle Evans if ((lbh->lzh = libzfs_init()) == NULL) 109c3a34c08SKyle Evans goto err; 11028f16a0fSKyle Evans 11128f16a0fSKyle Evans /* Obtain path to boot environment root */ 112c3a34c08SKyle Evans if ((kenv(KENV_GET, "zfs_be_root", lbh->root, BE_MAXPATHLEN)) == -1) 113c3a34c08SKyle Evans goto err; 11428f16a0fSKyle Evans 11528f16a0fSKyle Evans /* Remove leading 'zfs:' if present, otherwise use value as-is */ 116c3a34c08SKyle Evans if (strcmp(lbh->root, "zfs:") == 0) 117c3a34c08SKyle Evans strncpy(lbh->root, strchr(lbh->root, ':') + sizeof(char), 118c3a34c08SKyle Evans BE_MAXPATHLEN); 119c3a34c08SKyle Evans 120c3a34c08SKyle Evans if ((pos = strchr(lbh->root, '/')) == NULL) 121c3a34c08SKyle Evans goto err; 122c3a34c08SKyle Evans 123c3a34c08SKyle Evans pnamelen = pos - lbh->root; 124c3a34c08SKyle Evans poolname = malloc(pnamelen + 1); 125c3a34c08SKyle Evans if (poolname == NULL) 126c3a34c08SKyle Evans goto err; 127c3a34c08SKyle Evans 128c3a34c08SKyle Evans strncpy(poolname, lbh->root, pnamelen); 129c3a34c08SKyle Evans poolname[pnamelen] = '\0'; 130c3a34c08SKyle Evans if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL) 131c3a34c08SKyle Evans goto err; 132c3a34c08SKyle Evans 133c3a34c08SKyle Evans if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs, 134c3a34c08SKyle Evans BE_MAXPATHLEN, NULL, true) != 0) 135c3a34c08SKyle Evans goto err; 136c3a34c08SKyle Evans 137c3a34c08SKyle Evans /* Obtain path to boot environment rootfs (currently booted) */ 138c3a34c08SKyle Evans /* XXX Get dataset mounted at / by kenv/GUID from mountroot? */ 139ee16b7c9SKyle Evans if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL) 140c3a34c08SKyle Evans goto err; 141c3a34c08SKyle Evans 142ee16b7c9SKyle Evans zfs_iter_filesystems(rootds, be_locate_rootfs, lbh); 143ee16b7c9SKyle Evans zfs_close(rootds); 144ff8676ccSKyle Evans rootds = NULL; 145b29bf2f8SKyle Evans if (*lbh->rootfs == '\0') 146ff8676ccSKyle Evans goto err; 14728f16a0fSKyle Evans 14828f16a0fSKyle Evans return (lbh); 149c3a34c08SKyle Evans err: 150c3a34c08SKyle Evans if (lbh != NULL) { 151c3a34c08SKyle Evans if (lbh->active_phandle != NULL) 152c3a34c08SKyle Evans zpool_close(lbh->active_phandle); 153c3a34c08SKyle Evans if (lbh->lzh != NULL) 154c3a34c08SKyle Evans libzfs_fini(lbh->lzh); 155c3a34c08SKyle Evans free(lbh); 156c3a34c08SKyle Evans } 157ee16b7c9SKyle Evans if (rootds != NULL) 158ee16b7c9SKyle Evans zfs_close(rootds); 159c3a34c08SKyle Evans free(poolname); 160c3a34c08SKyle Evans return (NULL); 16128f16a0fSKyle Evans } 16228f16a0fSKyle Evans 16328f16a0fSKyle Evans 16428f16a0fSKyle Evans /* 16528f16a0fSKyle Evans * Free memory allocated by libbe_init() 16628f16a0fSKyle Evans */ 16728f16a0fSKyle Evans void 16828f16a0fSKyle Evans libbe_close(libbe_handle_t *lbh) 16928f16a0fSKyle Evans { 170bfe0869cSKyle Evans 171c3a34c08SKyle Evans if (lbh->active_phandle != NULL) 172c3a34c08SKyle Evans zpool_close(lbh->active_phandle); 17328f16a0fSKyle Evans libzfs_fini(lbh->lzh); 17428f16a0fSKyle Evans free(lbh); 17528f16a0fSKyle Evans } 17628f16a0fSKyle Evans 1779b1662e6SKyle Evans /* 1789b1662e6SKyle Evans * Proxy through to libzfs for the moment. 1799b1662e6SKyle Evans */ 1809b1662e6SKyle Evans void 1819b1662e6SKyle Evans be_nicenum(uint64_t num, char *buf, size_t buflen) 1829b1662e6SKyle Evans { 1839b1662e6SKyle Evans 1849b1662e6SKyle Evans zfs_nicenum(num, buf, buflen); 1859b1662e6SKyle Evans } 18628f16a0fSKyle Evans 187920abf4dSKyle Evans static int 188920abf4dSKyle Evans be_destroy_cb(zfs_handle_t *zfs_hdl, void *data) 189920abf4dSKyle Evans { 190920abf4dSKyle Evans int err; 191920abf4dSKyle Evans 192920abf4dSKyle Evans if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0) 193920abf4dSKyle Evans return (err); 194920abf4dSKyle Evans if ((err = zfs_destroy(zfs_hdl, false)) != 0) 195920abf4dSKyle Evans return (err); 196920abf4dSKyle Evans return (0); 197920abf4dSKyle Evans } 198920abf4dSKyle Evans 19928f16a0fSKyle Evans /* 20028f16a0fSKyle Evans * Destroy the boot environment or snapshot specified by the name 20128f16a0fSKyle Evans * parameter. Options are or'd together with the possible values: 20228f16a0fSKyle Evans * BE_DESTROY_FORCE : forces operation on mounted datasets 20328f16a0fSKyle Evans */ 20428f16a0fSKyle Evans int 205*73c3d608SKyle Evans be_destroy(libbe_handle_t *lbh, const char *name, int options) 20628f16a0fSKyle Evans { 20728f16a0fSKyle Evans zfs_handle_t *fs; 20828f16a0fSKyle Evans char path[BE_MAXPATHLEN]; 209bfe0869cSKyle Evans char *p; 210bfe0869cSKyle Evans int err, force, mounted; 21128f16a0fSKyle Evans 212bfe0869cSKyle Evans p = path; 213bfe0869cSKyle Evans force = options & BE_DESTROY_FORCE; 214bfe0869cSKyle Evans err = BE_ERR_SUCCESS; 21528f16a0fSKyle Evans 21628f16a0fSKyle Evans be_root_concat(lbh, name, path); 21728f16a0fSKyle Evans 21828f16a0fSKyle Evans if (strchr(name, '@') == NULL) { 219bfe0869cSKyle Evans if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM)) 22028f16a0fSKyle Evans return (set_error(lbh, BE_ERR_NOENT)); 22128f16a0fSKyle Evans 222bfe0869cSKyle Evans if (strcmp(path, lbh->rootfs) == 0) 22328f16a0fSKyle Evans return (set_error(lbh, BE_ERR_DESTROYACT)); 22428f16a0fSKyle Evans 22528f16a0fSKyle Evans fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM); 22628f16a0fSKyle Evans } else { 22728f16a0fSKyle Evans 228bfe0869cSKyle Evans if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 22928f16a0fSKyle Evans return (set_error(lbh, BE_ERR_NOENT)); 23028f16a0fSKyle Evans 23128f16a0fSKyle Evans fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT); 23228f16a0fSKyle Evans } 23328f16a0fSKyle Evans 23428f16a0fSKyle Evans if (fs == NULL) 23528f16a0fSKyle Evans return (set_error(lbh, BE_ERR_ZFSOPEN)); 23628f16a0fSKyle Evans 23728f16a0fSKyle Evans /* Check if mounted, unmount if force is specified */ 238b29bf2f8SKyle Evans if ((mounted = zfs_is_mounted(fs, NULL)) != 0) { 239bfe0869cSKyle Evans if (force) 24028f16a0fSKyle Evans zfs_unmount(fs, NULL, 0); 241bfe0869cSKyle Evans else 24228f16a0fSKyle Evans return (set_error(lbh, BE_ERR_DESTROYMNT)); 24328f16a0fSKyle Evans } 24428f16a0fSKyle Evans 245920abf4dSKyle Evans if ((err = be_destroy_cb(fs, NULL)) != 0) { 246920abf4dSKyle Evans /* Children are still present or the mount is referenced */ 247920abf4dSKyle Evans if (err == EBUSY) 248920abf4dSKyle Evans return (set_error(lbh, BE_ERR_DESTROYMNT)); 249920abf4dSKyle Evans return (set_error(lbh, BE_ERR_UNKNOWN)); 250920abf4dSKyle Evans } 25128f16a0fSKyle Evans 252920abf4dSKyle Evans return (0); 25328f16a0fSKyle Evans } 25428f16a0fSKyle Evans 25528f16a0fSKyle Evans 25628f16a0fSKyle Evans int 257b29bf2f8SKyle Evans be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name, 25828f16a0fSKyle Evans bool recursive, char *result) 25928f16a0fSKyle Evans { 26028f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 26128f16a0fSKyle Evans time_t rawtime; 26228f16a0fSKyle Evans int len, err; 26328f16a0fSKyle Evans 26428f16a0fSKyle Evans be_root_concat(lbh, source, buf); 26528f16a0fSKyle Evans 266bfe0869cSKyle Evans if (!be_exists(lbh, buf)) 26728f16a0fSKyle Evans return (BE_ERR_NOENT); 26828f16a0fSKyle Evans 26928f16a0fSKyle Evans if (snap_name != NULL) { 27028f16a0fSKyle Evans strcat(buf, "@"); 27128f16a0fSKyle Evans strcat(buf, snap_name); 272bfe0869cSKyle Evans if (result != NULL) 27328f16a0fSKyle Evans snprintf(result, BE_MAXPATHLEN, "%s@%s", source, 27428f16a0fSKyle Evans snap_name); 27528f16a0fSKyle Evans } else { 27628f16a0fSKyle Evans time(&rawtime); 27728f16a0fSKyle Evans len = strlen(buf); 27828f16a0fSKyle Evans strftime(buf + len, BE_MAXPATHLEN - len, 27928f16a0fSKyle Evans "@%F-%T", localtime(&rawtime)); 280bfe0869cSKyle Evans if (result != NULL) 28128f16a0fSKyle Evans strcpy(result, strrchr(buf, '/') + 1); 28228f16a0fSKyle Evans } 28328f16a0fSKyle Evans 284b29bf2f8SKyle Evans if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) { 28528f16a0fSKyle Evans switch (err) { 28628f16a0fSKyle Evans case EZFS_INVALIDNAME: 28728f16a0fSKyle Evans return (set_error(lbh, BE_ERR_INVALIDNAME)); 28828f16a0fSKyle Evans 28928f16a0fSKyle Evans default: 2902989df09SKyle Evans /* 2912989df09SKyle Evans * The other errors that zfs_ioc_snapshot might return 2922989df09SKyle Evans * shouldn't happen if we've set things up properly, so 2932989df09SKyle Evans * we'll gloss over them and call it UNKNOWN as it will 2942989df09SKyle Evans * require further triage. 2952989df09SKyle Evans */ 2962989df09SKyle Evans if (errno == ENOTSUP) 2972989df09SKyle Evans return (set_error(lbh, BE_ERR_NOPOOL)); 29828f16a0fSKyle Evans return (set_error(lbh, BE_ERR_UNKNOWN)); 29928f16a0fSKyle Evans } 30028f16a0fSKyle Evans } 30128f16a0fSKyle Evans 30228f16a0fSKyle Evans return (BE_ERR_SUCCESS); 30328f16a0fSKyle Evans } 30428f16a0fSKyle Evans 30528f16a0fSKyle Evans 30628f16a0fSKyle Evans /* 30728f16a0fSKyle Evans * Create the boot environment specified by the name parameter 30828f16a0fSKyle Evans */ 30928f16a0fSKyle Evans int 310*73c3d608SKyle Evans be_create(libbe_handle_t *lbh, const char *name) 31128f16a0fSKyle Evans { 31228f16a0fSKyle Evans int err; 31328f16a0fSKyle Evans 314b29bf2f8SKyle Evans err = be_create_from_existing(lbh, name, be_active_path(lbh)); 31528f16a0fSKyle Evans 31628f16a0fSKyle Evans return (set_error(lbh, err)); 31728f16a0fSKyle Evans } 31828f16a0fSKyle Evans 31928f16a0fSKyle Evans 32028f16a0fSKyle Evans static int 32128f16a0fSKyle Evans be_deep_clone_prop(int prop, void *cb) 32228f16a0fSKyle Evans { 32328f16a0fSKyle Evans int err; 324bfe0869cSKyle Evans struct libbe_dccb *dccb; 32528f16a0fSKyle Evans zprop_source_t src; 32628f16a0fSKyle Evans char pval[BE_MAXPATHLEN]; 32728f16a0fSKyle Evans char source[BE_MAXPATHLEN]; 32828f16a0fSKyle Evans 329bfe0869cSKyle Evans dccb = cb; 33028f16a0fSKyle Evans /* Skip some properties we don't want to touch */ 331*73c3d608SKyle Evans if (prop == ZFS_PROP_CANMOUNT) 33228f16a0fSKyle Evans return (ZPROP_CONT); 33328f16a0fSKyle Evans 33428f16a0fSKyle Evans /* Don't copy readonly properties */ 335bfe0869cSKyle Evans if (zfs_prop_readonly(prop)) 33628f16a0fSKyle Evans return (ZPROP_CONT); 33728f16a0fSKyle Evans 33828f16a0fSKyle Evans if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval, 339bfe0869cSKyle Evans sizeof(pval), &src, (char *)&source, sizeof(source), false))) 34028f16a0fSKyle Evans /* Just continue if we fail to read a property */ 34128f16a0fSKyle Evans return (ZPROP_CONT); 342bfe0869cSKyle Evans 34328f16a0fSKyle Evans /* Only copy locally defined properties */ 344bfe0869cSKyle Evans if (src != ZPROP_SRC_LOCAL) 34528f16a0fSKyle Evans return (ZPROP_CONT); 34628f16a0fSKyle Evans 34728f16a0fSKyle Evans nvlist_add_string(dccb->props, zfs_prop_to_name(prop), (char *)pval); 34828f16a0fSKyle Evans 34928f16a0fSKyle Evans return (ZPROP_CONT); 35028f16a0fSKyle Evans } 35128f16a0fSKyle Evans 35228f16a0fSKyle Evans static int 35328f16a0fSKyle Evans be_deep_clone(zfs_handle_t *ds, void *data) 35428f16a0fSKyle Evans { 35528f16a0fSKyle Evans int err; 35628f16a0fSKyle Evans char be_path[BE_MAXPATHLEN]; 35728f16a0fSKyle Evans char snap_path[BE_MAXPATHLEN]; 35828f16a0fSKyle Evans const char *dspath; 35928f16a0fSKyle Evans char *dsname; 36028f16a0fSKyle Evans zfs_handle_t *snap_hdl; 36128f16a0fSKyle Evans nvlist_t *props; 362bfe0869cSKyle Evans struct libbe_deep_clone *isdc, sdc; 36328f16a0fSKyle Evans struct libbe_dccb dccb; 36428f16a0fSKyle Evans 365bfe0869cSKyle Evans isdc = (struct libbe_deep_clone *)data; 36628f16a0fSKyle Evans dspath = zfs_get_name(ds); 367bfe0869cSKyle Evans if ((dsname = strrchr(dspath, '/')) == NULL) 36828f16a0fSKyle Evans return (BE_ERR_UNKNOWN); 36928f16a0fSKyle Evans dsname++; 370bfe0869cSKyle Evans 371bfe0869cSKyle Evans if (isdc->bename == NULL) 37228f16a0fSKyle Evans snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname); 373bfe0869cSKyle Evans else 37428f16a0fSKyle Evans snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename); 375bfe0869cSKyle Evans 37628f16a0fSKyle Evans snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname); 37728f16a0fSKyle Evans 378bfe0869cSKyle Evans if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET)) 37928f16a0fSKyle Evans return (set_error(isdc->lbh, BE_ERR_EXISTS)); 38028f16a0fSKyle Evans 38128f16a0fSKyle Evans if ((snap_hdl = 382bfe0869cSKyle Evans zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL) 38328f16a0fSKyle Evans return (set_error(isdc->lbh, BE_ERR_ZFSOPEN)); 38428f16a0fSKyle Evans 38528f16a0fSKyle Evans nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 38628f16a0fSKyle Evans nvlist_add_string(props, "canmount", "noauto"); 38728f16a0fSKyle Evans 38828f16a0fSKyle Evans dccb.zhp = ds; 38928f16a0fSKyle Evans dccb.props = props; 39028f16a0fSKyle Evans if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE, 391bfe0869cSKyle Evans ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL) 39228f16a0fSKyle Evans return (-1); 39328f16a0fSKyle Evans 394b29bf2f8SKyle Evans if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) { 39528f16a0fSKyle Evans switch (err) { 39628f16a0fSKyle Evans case EZFS_SUCCESS: 39728f16a0fSKyle Evans err = BE_ERR_SUCCESS; 39828f16a0fSKyle Evans break; 39928f16a0fSKyle Evans default: 40028f16a0fSKyle Evans err = BE_ERR_ZFSCLONE; 40128f16a0fSKyle Evans break; 40228f16a0fSKyle Evans } 40328f16a0fSKyle Evans } 40428f16a0fSKyle Evans 40528f16a0fSKyle Evans nvlist_free(props); 40628f16a0fSKyle Evans zfs_close(snap_hdl); 40728f16a0fSKyle Evans 40828f16a0fSKyle Evans sdc.lbh = isdc->lbh; 40928f16a0fSKyle Evans sdc.bename = NULL; 41028f16a0fSKyle Evans sdc.snapname = isdc->snapname; 41128f16a0fSKyle Evans sdc.be_root = (char *)&be_path; 41228f16a0fSKyle Evans 41328f16a0fSKyle Evans err = zfs_iter_filesystems(ds, be_deep_clone, &sdc); 41428f16a0fSKyle Evans 41528f16a0fSKyle Evans return (err); 41628f16a0fSKyle Evans } 41728f16a0fSKyle Evans 41828f16a0fSKyle Evans /* 41928f16a0fSKyle Evans * Create the boot environment from pre-existing snapshot 42028f16a0fSKyle Evans */ 42128f16a0fSKyle Evans int 422b29bf2f8SKyle Evans be_create_from_existing_snap(libbe_handle_t *lbh, const char *name, 423b29bf2f8SKyle Evans const char *snap) 42428f16a0fSKyle Evans { 42528f16a0fSKyle Evans int err; 42628f16a0fSKyle Evans char be_path[BE_MAXPATHLEN]; 42728f16a0fSKyle Evans char snap_path[BE_MAXPATHLEN]; 428b29bf2f8SKyle Evans const char *bename; 429b29bf2f8SKyle Evans char *parentname, *snapname; 43028f16a0fSKyle Evans zfs_handle_t *parent_hdl; 43128f16a0fSKyle Evans struct libbe_deep_clone sdc; 43228f16a0fSKyle Evans 433b29bf2f8SKyle Evans if ((err = be_validate_name(lbh, name)) != 0) 43428f16a0fSKyle Evans return (set_error(lbh, err)); 435b29bf2f8SKyle Evans if ((err = be_root_concat(lbh, snap, snap_path)) != 0) 43628f16a0fSKyle Evans return (set_error(lbh, err)); 437b29bf2f8SKyle Evans if ((err = be_validate_snap(lbh, snap_path)) != 0) 43828f16a0fSKyle Evans return (set_error(lbh, err)); 43928f16a0fSKyle Evans 440b29bf2f8SKyle Evans if ((err = be_root_concat(lbh, name, be_path)) != 0) 44128f16a0fSKyle Evans return (set_error(lbh, err)); 44228f16a0fSKyle Evans 443bfe0869cSKyle Evans if ((bename = strrchr(name, '/')) == NULL) 44428f16a0fSKyle Evans bename = name; 445bfe0869cSKyle Evans else 44628f16a0fSKyle Evans bename++; 447bfe0869cSKyle Evans 44828f16a0fSKyle Evans if ((parentname = strdup(snap_path)) == NULL) { 44928f16a0fSKyle Evans err = BE_ERR_UNKNOWN; 45028f16a0fSKyle Evans return (set_error(lbh, err)); 45128f16a0fSKyle Evans } 45228f16a0fSKyle Evans snapname = strchr(parentname, '@'); 45328f16a0fSKyle Evans if (snapname == NULL) { 45428f16a0fSKyle Evans err = BE_ERR_UNKNOWN; 45528f16a0fSKyle Evans return (set_error(lbh, err)); 45628f16a0fSKyle Evans } 45728f16a0fSKyle Evans *snapname = '\0'; 45828f16a0fSKyle Evans snapname++; 45928f16a0fSKyle Evans 46028f16a0fSKyle Evans sdc.lbh = lbh; 46128f16a0fSKyle Evans sdc.bename = bename; 46228f16a0fSKyle Evans sdc.snapname = snapname; 46328f16a0fSKyle Evans sdc.be_root = lbh->root; 46428f16a0fSKyle Evans 46528f16a0fSKyle Evans parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET); 46628f16a0fSKyle Evans err = be_deep_clone(parent_hdl, &sdc); 46728f16a0fSKyle Evans 46828f16a0fSKyle Evans return (set_error(lbh, err)); 46928f16a0fSKyle Evans } 47028f16a0fSKyle Evans 47128f16a0fSKyle Evans 47228f16a0fSKyle Evans /* 47328f16a0fSKyle Evans * Create a boot environment from an existing boot environment 47428f16a0fSKyle Evans */ 47528f16a0fSKyle Evans int 476b29bf2f8SKyle Evans be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old) 47728f16a0fSKyle Evans { 47828f16a0fSKyle Evans int err; 47928f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 48028f16a0fSKyle Evans 481bfe0869cSKyle Evans if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf))) 48228f16a0fSKyle Evans return (set_error(lbh, err)); 48328f16a0fSKyle Evans 48428f16a0fSKyle Evans err = be_create_from_existing_snap(lbh, name, (char *)buf); 48528f16a0fSKyle Evans 48628f16a0fSKyle Evans return (set_error(lbh, err)); 48728f16a0fSKyle Evans } 48828f16a0fSKyle Evans 48928f16a0fSKyle Evans 49028f16a0fSKyle Evans /* 49128f16a0fSKyle Evans * Verifies that a snapshot has a valid name, exists, and has a mountpoint of 49228f16a0fSKyle Evans * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon 49328f16a0fSKyle Evans * failure. Does not set the internal library error state. 49428f16a0fSKyle Evans */ 49528f16a0fSKyle Evans int 496b29bf2f8SKyle Evans be_validate_snap(libbe_handle_t *lbh, const char *snap_name) 49728f16a0fSKyle Evans { 49828f16a0fSKyle Evans zfs_handle_t *zfs_hdl; 49928f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 50028f16a0fSKyle Evans char *delim_pos; 50128f16a0fSKyle Evans int err = BE_ERR_SUCCESS; 50228f16a0fSKyle Evans 503bfe0869cSKyle Evans if (strlen(snap_name) >= BE_MAXPATHLEN) 50428f16a0fSKyle Evans return (BE_ERR_PATHLEN); 50528f16a0fSKyle Evans 50628f16a0fSKyle Evans if (!zfs_dataset_exists(lbh->lzh, snap_name, 507bfe0869cSKyle Evans ZFS_TYPE_SNAPSHOT)) 50828f16a0fSKyle Evans return (BE_ERR_NOENT); 50928f16a0fSKyle Evans 51028f16a0fSKyle Evans strncpy(buf, snap_name, BE_MAXPATHLEN); 51128f16a0fSKyle Evans 51228f16a0fSKyle Evans /* Find the base filesystem of the snapshot */ 513bfe0869cSKyle Evans if ((delim_pos = strchr(buf, '@')) == NULL) 51428f16a0fSKyle Evans return (BE_ERR_INVALIDNAME); 51528f16a0fSKyle Evans *delim_pos = '\0'; 51628f16a0fSKyle Evans 51728f16a0fSKyle Evans if ((zfs_hdl = 518bfe0869cSKyle Evans zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 51928f16a0fSKyle Evans return (BE_ERR_NOORIGIN); 52028f16a0fSKyle Evans 521b29bf2f8SKyle Evans if ((err = zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, BE_MAXPATHLEN, 522b29bf2f8SKyle Evans NULL, NULL, 0, 1)) != 0) 52328f16a0fSKyle Evans err = BE_ERR_INVORIGIN; 52428f16a0fSKyle Evans 525bfe0869cSKyle Evans if ((err != 0) && (strncmp(buf, "/", BE_MAXPATHLEN) != 0)) 52628f16a0fSKyle Evans err = BE_ERR_INVORIGIN; 52728f16a0fSKyle Evans 52828f16a0fSKyle Evans zfs_close(zfs_hdl); 52928f16a0fSKyle Evans 53028f16a0fSKyle Evans return (err); 53128f16a0fSKyle Evans } 53228f16a0fSKyle Evans 53328f16a0fSKyle Evans 53428f16a0fSKyle Evans /* 53528f16a0fSKyle Evans * Idempotently appends the name argument to the root boot environment path 53628f16a0fSKyle Evans * and copies the resulting string into the result buffer (which is assumed 53728f16a0fSKyle Evans * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon 53828f16a0fSKyle Evans * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN, 53928f16a0fSKyle Evans * or BE_ERR_INVALIDNAME if the name is a path that does not begin with 54028f16a0fSKyle Evans * zfs_be_root. Does not set internal library error state. 54128f16a0fSKyle Evans */ 54228f16a0fSKyle Evans int 543b29bf2f8SKyle Evans be_root_concat(libbe_handle_t *lbh, const char *name, char *result) 54428f16a0fSKyle Evans { 54528f16a0fSKyle Evans size_t name_len, root_len; 54628f16a0fSKyle Evans 54728f16a0fSKyle Evans name_len = strlen(name); 54828f16a0fSKyle Evans root_len = strlen(lbh->root); 54928f16a0fSKyle Evans 55028f16a0fSKyle Evans /* Act idempotently; return be name if it is already a full path */ 55128f16a0fSKyle Evans if (strrchr(name, '/') != NULL) { 552bfe0869cSKyle Evans if (strstr(name, lbh->root) != name) 55328f16a0fSKyle Evans return (BE_ERR_INVALIDNAME); 55428f16a0fSKyle Evans 555bfe0869cSKyle Evans if (name_len >= BE_MAXPATHLEN) 55628f16a0fSKyle Evans return (BE_ERR_PATHLEN); 55728f16a0fSKyle Evans 55828f16a0fSKyle Evans strncpy(result, name, BE_MAXPATHLEN); 55928f16a0fSKyle Evans return (BE_ERR_SUCCESS); 56028f16a0fSKyle Evans } else if (name_len + root_len + 1 < BE_MAXPATHLEN) { 56128f16a0fSKyle Evans snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root, 56228f16a0fSKyle Evans name); 56328f16a0fSKyle Evans return (BE_ERR_SUCCESS); 56428f16a0fSKyle Evans } 56528f16a0fSKyle Evans 56628f16a0fSKyle Evans return (BE_ERR_PATHLEN); 56728f16a0fSKyle Evans } 56828f16a0fSKyle Evans 56928f16a0fSKyle Evans 57028f16a0fSKyle Evans /* 57128f16a0fSKyle Evans * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns 57228f16a0fSKyle Evans * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME. 57328f16a0fSKyle Evans * Does not set internal library error state. 57428f16a0fSKyle Evans */ 57528f16a0fSKyle Evans int 576b29bf2f8SKyle Evans be_validate_name(libbe_handle_t *lbh __unused, const char *name) 57728f16a0fSKyle Evans { 57828f16a0fSKyle Evans for (int i = 0; *name; i++) { 57928f16a0fSKyle Evans char c = *(name++); 580bfe0869cSKyle Evans if (isalnum(c) || (c == '-') || (c == '_') || (c == '.')) 58128f16a0fSKyle Evans continue; 58228f16a0fSKyle Evans return (BE_ERR_INVALIDNAME); 58328f16a0fSKyle Evans } 58428f16a0fSKyle Evans 58528f16a0fSKyle Evans return (BE_ERR_SUCCESS); 58628f16a0fSKyle Evans } 58728f16a0fSKyle Evans 58828f16a0fSKyle Evans 58928f16a0fSKyle Evans /* 59028f16a0fSKyle Evans * usage 59128f16a0fSKyle Evans */ 59228f16a0fSKyle Evans int 593*73c3d608SKyle Evans be_rename(libbe_handle_t *lbh, const char *old, const char *new) 59428f16a0fSKyle Evans { 59528f16a0fSKyle Evans char full_old[BE_MAXPATHLEN]; 59628f16a0fSKyle Evans char full_new[BE_MAXPATHLEN]; 59728f16a0fSKyle Evans zfs_handle_t *zfs_hdl; 59828f16a0fSKyle Evans int err; 59928f16a0fSKyle Evans 600b29bf2f8SKyle Evans if ((err = be_root_concat(lbh, old, full_old)) != 0) 60128f16a0fSKyle Evans return (set_error(lbh, err)); 602b29bf2f8SKyle Evans if ((err = be_root_concat(lbh, new, full_new)) != 0) 60328f16a0fSKyle Evans return (set_error(lbh, err)); 60428f16a0fSKyle Evans 6052989df09SKyle Evans if ((err = be_validate_name(lbh, new)) != 0) 6062989df09SKyle Evans return (err); 60728f16a0fSKyle Evans 608bfe0869cSKyle Evans /* Check if old is active BE */ 6092989df09SKyle Evans if (strcmp(full_old, be_active_path(lbh)) == 0) 6102989df09SKyle Evans return (set_error(lbh, BE_ERR_MOUNTED)); 61128f16a0fSKyle Evans 612bfe0869cSKyle Evans if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET)) 6132989df09SKyle Evans return (set_error(lbh, BE_ERR_NOENT)); 61428f16a0fSKyle Evans 615bfe0869cSKyle Evans if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET)) 6162989df09SKyle Evans return (set_error(lbh, BE_ERR_EXISTS)); 61728f16a0fSKyle Evans 618bfe0869cSKyle Evans /* XXX TODO 619bfe0869cSKyle Evans * - What about mounted BEs? 620bfe0869cSKyle Evans * - if mounted error out unless a force flag is set? 621bfe0869cSKyle Evans */ 62228f16a0fSKyle Evans if ((zfs_hdl = zfs_open(lbh->lzh, full_old, 623bfe0869cSKyle Evans ZFS_TYPE_FILESYSTEM)) == NULL) 6242989df09SKyle Evans return (set_error(lbh, BE_ERR_ZFSOPEN)); 62528f16a0fSKyle Evans 626bfe0869cSKyle Evans /* recurse, nounmount, forceunmount */ 62728f16a0fSKyle Evans struct renameflags flags = { 0, 0, 0 }; 62828f16a0fSKyle Evans 62928f16a0fSKyle Evans err = zfs_rename(zfs_hdl, NULL, full_new, flags); 63028f16a0fSKyle Evans 63128f16a0fSKyle Evans zfs_close(zfs_hdl); 63228f16a0fSKyle Evans 63328f16a0fSKyle Evans return (set_error(lbh, err)); 63428f16a0fSKyle Evans } 63528f16a0fSKyle Evans 63628f16a0fSKyle Evans 63728f16a0fSKyle Evans int 638*73c3d608SKyle Evans be_export(libbe_handle_t *lbh, const char *bootenv, int fd) 63928f16a0fSKyle Evans { 64028f16a0fSKyle Evans char snap_name[BE_MAXPATHLEN]; 64128f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 64228f16a0fSKyle Evans zfs_handle_t *zfs; 64328f16a0fSKyle Evans int err; 64428f16a0fSKyle Evans 645b29bf2f8SKyle Evans if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0) 646bfe0869cSKyle Evans /* XXX TODO error handle */ 64728f16a0fSKyle Evans return (-1); 64828f16a0fSKyle Evans 64928f16a0fSKyle Evans be_root_concat(lbh, snap_name, buf); 65028f16a0fSKyle Evans 651bfe0869cSKyle Evans if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 652506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_ZFSOPEN)); 65328f16a0fSKyle Evans 65428f16a0fSKyle Evans err = zfs_send_one(zfs, NULL, fd, 0); 65528f16a0fSKyle Evans return (err); 65628f16a0fSKyle Evans } 65728f16a0fSKyle Evans 65828f16a0fSKyle Evans 65928f16a0fSKyle Evans int 660*73c3d608SKyle Evans be_import(libbe_handle_t *lbh, const char *bootenv, int fd) 66128f16a0fSKyle Evans { 66228f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 66328f16a0fSKyle Evans time_t rawtime; 66428f16a0fSKyle Evans nvlist_t *props; 66528f16a0fSKyle Evans zfs_handle_t *zfs; 66628f16a0fSKyle Evans int err, len; 667506f5fdfSKyle Evans char nbuf[24]; 66828f16a0fSKyle Evans 669bfe0869cSKyle Evans /* 670506f5fdfSKyle Evans * We don't need this to be incredibly random, just unique enough that 671506f5fdfSKyle Evans * it won't conflict with an existing dataset name. Chopping time 672506f5fdfSKyle Evans * down to 32 bits is probably good enough for this. 673bfe0869cSKyle Evans */ 674506f5fdfSKyle Evans snprintf(nbuf, 24, "tmp%u", 675506f5fdfSKyle Evans (uint32_t)(time(NULL) & 0xFFFFFFFF)); 676506f5fdfSKyle Evans if ((err = be_root_concat(lbh, nbuf, buf)) != 0) 677506f5fdfSKyle Evans /* 678506f5fdfSKyle Evans * Technically this is our problem, but we try to use short 679506f5fdfSKyle Evans * enough names that we won't run into problems except in 680506f5fdfSKyle Evans * worst-case BE root approaching MAXPATHLEN. 681506f5fdfSKyle Evans */ 682506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_PATHLEN)); 68328f16a0fSKyle Evans 68428f16a0fSKyle Evans time(&rawtime); 68528f16a0fSKyle Evans len = strlen(buf); 68628f16a0fSKyle Evans strftime(buf + len, BE_MAXPATHLEN - len, 68728f16a0fSKyle Evans "@%F-%T", localtime(&rawtime)); 68828f16a0fSKyle Evans 689bfe0869cSKyle Evans /* lzc_receive(SNAPNAME, PROPS, ORIGIN, FORCE, fd)) { */ 690b29bf2f8SKyle Evans if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) { 691506f5fdfSKyle Evans switch (err) { 692506f5fdfSKyle Evans case EINVAL: 693506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_NOORIGIN)); 694506f5fdfSKyle Evans case ENOENT: 695506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_NOENT)); 696506f5fdfSKyle Evans case EIO: 697506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_IO)); 698506f5fdfSKyle Evans default: 699506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_UNKNOWN)); 700506f5fdfSKyle Evans } 70128f16a0fSKyle Evans } 70228f16a0fSKyle Evans 703bfe0869cSKyle Evans if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 704506f5fdfSKyle Evans return (set_error(lbh, BE_ERR_ZFSOPEN)); 70528f16a0fSKyle Evans 70628f16a0fSKyle Evans nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 70728f16a0fSKyle Evans nvlist_add_string(props, "canmount", "noauto"); 70828f16a0fSKyle Evans nvlist_add_string(props, "mountpoint", "/"); 70928f16a0fSKyle Evans 71028f16a0fSKyle Evans be_root_concat(lbh, bootenv, buf); 71128f16a0fSKyle Evans 71228f16a0fSKyle Evans err = zfs_clone(zfs, buf, props); 71328f16a0fSKyle Evans zfs_close(zfs); 71428f16a0fSKyle Evans 71528f16a0fSKyle Evans nvlist_free(props); 71628f16a0fSKyle Evans 717506f5fdfSKyle Evans /* XXX TODO: recursively delete nbuf dataset */ 71828f16a0fSKyle Evans return (err); 71928f16a0fSKyle Evans } 72028f16a0fSKyle Evans 72128f16a0fSKyle Evans 72228f16a0fSKyle Evans int 723*73c3d608SKyle Evans be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists) 72428f16a0fSKyle Evans { 725*73c3d608SKyle Evans struct stat sb; 72628f16a0fSKyle Evans char active[BE_MAXPATHLEN]; 72728f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 72828f16a0fSKyle Evans nvlist_t *props; 729*73c3d608SKyle Evans const char *s; 73028f16a0fSKyle Evans zfs_handle_t *zfs; 731*73c3d608SKyle Evans long snap_name; 732*73c3d608SKyle Evans int err, pos; 73328f16a0fSKyle Evans 73428f16a0fSKyle Evans /* Require absolute paths */ 735bfe0869cSKyle Evans if (*child_path != '/') 736bfe0869cSKyle Evans /* XXX TODO: create appropriate error */ 73728f16a0fSKyle Evans return (-1); 73828f16a0fSKyle Evans 73928f16a0fSKyle Evans strncpy(active, be_active_path(lbh), BE_MAXPATHLEN); 74028f16a0fSKyle Evans strcpy(buf, active); 74128f16a0fSKyle Evans 74228f16a0fSKyle Evans /* Create non-mountable parent dataset(s) */ 743*73c3d608SKyle Evans s = child_path; 74428f16a0fSKyle Evans for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) { 74528f16a0fSKyle Evans size_t len = p - s; 74628f16a0fSKyle Evans strncat(buf, s, len); 74728f16a0fSKyle Evans 74828f16a0fSKyle Evans nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 74928f16a0fSKyle Evans nvlist_add_string(props, "canmount", "off"); 75028f16a0fSKyle Evans nvlist_add_string(props, "mountpoint", "none"); 75128f16a0fSKyle Evans zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props); 75228f16a0fSKyle Evans nvlist_free(props); 75328f16a0fSKyle Evans } 75428f16a0fSKyle Evans 75528f16a0fSKyle Evans /* Path does not exist as a descendent of / yet */ 756*73c3d608SKyle Evans pos = strlen(active); 75728f16a0fSKyle Evans 758bfe0869cSKyle Evans /* XXX TODO: Verify that resulting str is less than BE_MAXPATHLEN */ 75928f16a0fSKyle Evans strncpy(&active[pos], child_path, BE_MAXPATHLEN-pos); 76028f16a0fSKyle Evans 76128f16a0fSKyle Evans if (stat(child_path, &sb) != 0) { 76228f16a0fSKyle Evans /* Verify that error is ENOENT */ 763bfe0869cSKyle Evans if (errno != 2) 764bfe0869cSKyle Evans /* XXX TODO: create appropriate error */ 76528f16a0fSKyle Evans return (-1); 76628f16a0fSKyle Evans 76728f16a0fSKyle Evans nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 76828f16a0fSKyle Evans nvlist_add_string(props, "canmount", "noauto"); 76928f16a0fSKyle Evans nvlist_add_string(props, "mountpoint", child_path); 77028f16a0fSKyle Evans 771bfe0869cSKyle Evans /* Create */ 772b29bf2f8SKyle Evans if ((err = 773b29bf2f8SKyle Evans zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET, props)) != 0) 774bfe0869cSKyle Evans /* XXX TODO handle error */ 77528f16a0fSKyle Evans return (-1); 77628f16a0fSKyle Evans nvlist_free(props); 77728f16a0fSKyle Evans 77828f16a0fSKyle Evans if ((zfs = 779bfe0869cSKyle Evans zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL) 780bfe0869cSKyle Evans /* XXX TODO handle error */ 78128f16a0fSKyle Evans return (-1); 78228f16a0fSKyle Evans 783bfe0869cSKyle Evans /* Set props */ 784b29bf2f8SKyle Evans if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) 78528f16a0fSKyle Evans /* TODO handle error */ 78628f16a0fSKyle Evans return (-1); 78728f16a0fSKyle Evans } else if (cp_if_exists) { 78828f16a0fSKyle Evans /* Path is already a descendent of / and should be copied */ 78928f16a0fSKyle Evans 790bfe0869cSKyle Evans /* XXX TODO ? */ 79128f16a0fSKyle Evans 79228f16a0fSKyle Evans /* 79328f16a0fSKyle Evans * Establish if the existing path is a zfs dataset or just 79428f16a0fSKyle Evans * the subdirectory of one 79528f16a0fSKyle Evans */ 79628f16a0fSKyle Evans 797bfe0869cSKyle Evans /* XXX TODO: use mktemp */ 798*73c3d608SKyle Evans snap_name = random(); 79928f16a0fSKyle Evans 80028f16a0fSKyle Evans snprintf(buf, BE_MAXPATHLEN, "%s@%ld", child_path, snap_name); 80128f16a0fSKyle Evans 802b29bf2f8SKyle Evans if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) 803bfe0869cSKyle Evans /* XXX TODO correct error */ 80428f16a0fSKyle Evans return (-1); 80528f16a0fSKyle Evans 806bfe0869cSKyle Evans /* Clone */ 80728f16a0fSKyle Evans if ((zfs = 808bfe0869cSKyle Evans zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 809bfe0869cSKyle Evans /* XXX TODO correct error */ 81028f16a0fSKyle Evans return (-1); 81128f16a0fSKyle Evans 812b29bf2f8SKyle Evans if ((err = zfs_clone(zfs, active, NULL)) != 0) 813bfe0869cSKyle Evans /* XXX TODO correct error */ 81428f16a0fSKyle Evans return (-1); 81528f16a0fSKyle Evans 816bfe0869cSKyle Evans /* set props */ 817bfe0869cSKyle Evans } else 81828f16a0fSKyle Evans /* TODO: error code for exists, but not cp? */ 81928f16a0fSKyle Evans return (-1); 82028f16a0fSKyle Evans 82128f16a0fSKyle Evans return (BE_ERR_SUCCESS); 82228f16a0fSKyle Evans } 82328f16a0fSKyle Evans 824d06f7103SKyle Evans static int 825d06f7103SKyle Evans be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid, 826d06f7103SKyle Evans const char *zfsdev) 827d06f7103SKyle Evans { 828d06f7103SKyle Evans nvlist_t **child; 829d06f7103SKyle Evans uint64_t vdev_guid; 830d06f7103SKyle Evans int c, children; 831d06f7103SKyle Evans 832d06f7103SKyle Evans if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child, 833d06f7103SKyle Evans &children) == 0) { 834d06f7103SKyle Evans for (c = 0; c < children; ++c) 835d06f7103SKyle Evans if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0) 836d06f7103SKyle Evans return (1); 837d06f7103SKyle Evans return (0); 838d06f7103SKyle Evans } 839d06f7103SKyle Evans 840d06f7103SKyle Evans if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 841d06f7103SKyle Evans &vdev_guid) != 0) { 842d06f7103SKyle Evans return (1); 843d06f7103SKyle Evans } 844d06f7103SKyle Evans 845d06f7103SKyle Evans if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) { 846d06f7103SKyle Evans perror("ZFS_IOC_NEXTBOOT failed"); 847d06f7103SKyle Evans return (1); 848d06f7103SKyle Evans } 849d06f7103SKyle Evans 850d06f7103SKyle Evans return (0); 851d06f7103SKyle Evans } 852d06f7103SKyle Evans 85328f16a0fSKyle Evans 85428f16a0fSKyle Evans int 855*73c3d608SKyle Evans be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) 85628f16a0fSKyle Evans { 85728f16a0fSKyle Evans char be_path[BE_MAXPATHLEN]; 85828f16a0fSKyle Evans char buf[BE_MAXPATHLEN]; 85928f16a0fSKyle Evans uint64_t pool_guid; 860d06f7103SKyle Evans nvlist_t *config, *vdevs; 86128f16a0fSKyle Evans int err; 86228f16a0fSKyle Evans 86328f16a0fSKyle Evans be_root_concat(lbh, bootenv, be_path); 86428f16a0fSKyle Evans 86528f16a0fSKyle Evans /* Note: be_exists fails if mountpoint is not / */ 866bfe0869cSKyle Evans if (!be_exists(lbh, be_path)) 86728f16a0fSKyle Evans return (BE_ERR_NOENT); 86828f16a0fSKyle Evans 86928f16a0fSKyle Evans if (temporary) { 870d06f7103SKyle Evans config = zpool_get_config(lbh->active_phandle, NULL); 871d06f7103SKyle Evans if (config == NULL) { 872d06f7103SKyle Evans printf("no config\n"); 87328f16a0fSKyle Evans return (1); 87428f16a0fSKyle Evans } 87528f16a0fSKyle Evans 876d06f7103SKyle Evans if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 877d06f7103SKyle Evans &pool_guid) != 0) 878d06f7103SKyle Evans return (1); 879d06f7103SKyle Evans 88028f16a0fSKyle Evans /* Expected format according to zfsbootcfg(8) man */ 88128f16a0fSKyle Evans strcpy(buf, "zfs:"); 88228f16a0fSKyle Evans strcat(buf, be_path); 88328f16a0fSKyle Evans strcat(buf, ":"); 88428f16a0fSKyle Evans 885d06f7103SKyle Evans if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vdevs) != 0) 88628f16a0fSKyle Evans return (1); 88728f16a0fSKyle Evans 888d06f7103SKyle Evans return (be_set_nextboot(lbh, vdevs, pool_guid, buf)); 88928f16a0fSKyle Evans } else { 89028f16a0fSKyle Evans /* Obtain bootenv zpool */ 891c3a34c08SKyle Evans err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path); 89228f16a0fSKyle Evans 89328f16a0fSKyle Evans switch (err) { 89428f16a0fSKyle Evans case 0: 89528f16a0fSKyle Evans return (BE_ERR_SUCCESS); 89628f16a0fSKyle Evans 89728f16a0fSKyle Evans default: 898bfe0869cSKyle Evans /* XXX TODO correct errors */ 89928f16a0fSKyle Evans return (-1); 90028f16a0fSKyle Evans } 90128f16a0fSKyle Evans } 90228f16a0fSKyle Evans } 903