2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
38 #include <libzfs_core.h>
48 static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
49 const char *child_path);
50 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
54 * Iterator function for locating the rootfs amongst the children of the
55 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *.
58 be_locate_rootfs(zfs_handle_t *chkds, void *data)
63 lbh = (libbe_handle_t *)data;
68 if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) {
69 strlcpy(lbh->rootfs, zfs_get_name(chkds), sizeof(lbh->rootfs));
72 } else if(mntpoint != NULL)
79 * Initializes the libbe context to operate in the root boot environment
80 * dataset, for example, zroot/ROOT.
86 dev_t root_dev, boot_dev;
93 poolname = pos = NULL;
96 /* Verify that /boot and / are mounted on the same filesystem */
97 /* TODO: use errno here?? */
98 if (stat("/", &sb) != 0)
101 root_dev = sb.st_dev;
103 if (stat("/boot", &sb) != 0)
106 boot_dev = sb.st_dev;
108 if (root_dev != boot_dev) {
109 fprintf(stderr, "/ and /boot not on same device, quitting\n");
113 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
116 if ((lbh->lzh = libzfs_init()) == NULL)
119 /* Obtain path to boot environment root */
120 if ((kenv(KENV_GET, "zfs_be_root", lbh->root,
121 sizeof(lbh->root))) == -1)
124 /* Remove leading 'zfs:' if present, otherwise use value as-is */
125 if (strcmp(lbh->root, "zfs:") == 0)
126 strlcpy(lbh->root, strchr(lbh->root, ':') + sizeof(char),
129 if ((pos = strchr(lbh->root, '/')) == NULL)
132 pnamelen = pos - lbh->root;
133 poolname = malloc(pnamelen + 1);
134 if (poolname == NULL)
137 strlcpy(poolname, lbh->root, pnamelen + 1);
138 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
143 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
144 sizeof(lbh->bootfs), NULL, true) != 0)
147 /* Obtain path to boot environment rootfs (currently booted) */
148 /* XXX Get dataset mounted at / by kenv/GUID from mountroot? */
149 if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL)
152 zfs_iter_filesystems(rootds, be_locate_rootfs, lbh);
155 if (*lbh->rootfs == '\0')
161 if (lbh->active_phandle != NULL)
162 zpool_close(lbh->active_phandle);
163 if (lbh->lzh != NULL)
164 libzfs_fini(lbh->lzh);
173 * Free memory allocated by libbe_init()
176 libbe_close(libbe_handle_t *lbh)
179 if (lbh->active_phandle != NULL)
180 zpool_close(lbh->active_phandle);
181 libzfs_fini(lbh->lzh);
186 * Proxy through to libzfs for the moment.
189 be_nicenum(uint64_t num, char *buf, size_t buflen)
192 zfs_nicenum(num, buf, buflen);
196 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
200 if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
202 if ((err = zfs_destroy(zfs_hdl, false)) != 0)
208 * Destroy the boot environment or snapshot specified by the name
209 * parameter. Options are or'd together with the possible values:
210 * BE_DESTROY_FORCE : forces operation on mounted datasets
213 be_destroy(libbe_handle_t *lbh, const char *name, int options)
216 char path[BE_MAXPATHLEN];
218 int err, force, mounted;
221 force = options & BE_DESTROY_FORCE;
223 be_root_concat(lbh, name, path);
225 if (strchr(name, '@') == NULL) {
226 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
227 return (set_error(lbh, BE_ERR_NOENT));
229 if (strcmp(path, lbh->rootfs) == 0)
230 return (set_error(lbh, BE_ERR_DESTROYACT));
232 fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
235 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
236 return (set_error(lbh, BE_ERR_NOENT));
238 fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
242 return (set_error(lbh, BE_ERR_ZFSOPEN));
244 /* Check if mounted, unmount if force is specified */
245 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
247 zfs_unmount(fs, NULL, 0);
249 return (set_error(lbh, BE_ERR_DESTROYMNT));
252 if ((err = be_destroy_cb(fs, NULL)) != 0) {
253 /* Children are still present or the mount is referenced */
255 return (set_error(lbh, BE_ERR_DESTROYMNT));
256 return (set_error(lbh, BE_ERR_UNKNOWN));
264 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
265 bool recursive, char *result)
267 char buf[BE_MAXPATHLEN];
271 be_root_concat(lbh, source, buf);
273 if ((err = be_exists(lbh, buf)) != 0)
274 return (set_error(lbh, err));
276 if (snap_name != NULL) {
277 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
278 return (set_error(lbh, BE_ERR_INVALIDNAME));
280 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
281 return (set_error(lbh, BE_ERR_INVALIDNAME));
284 snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
289 strftime(buf + len, sizeof(buf) - len,
290 "@%F-%T", localtime(&rawtime));
291 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
292 sizeof(buf)) >= sizeof(buf))
293 return (set_error(lbh, BE_ERR_INVALIDNAME));
296 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
298 case EZFS_INVALIDNAME:
299 return (set_error(lbh, BE_ERR_INVALIDNAME));
303 * The other errors that zfs_ioc_snapshot might return
304 * shouldn't happen if we've set things up properly, so
305 * we'll gloss over them and call it UNKNOWN as it will
306 * require further triage.
308 if (errno == ENOTSUP)
309 return (set_error(lbh, BE_ERR_NOPOOL));
310 return (set_error(lbh, BE_ERR_UNKNOWN));
314 return (BE_ERR_SUCCESS);
319 * Create the boot environment specified by the name parameter
322 be_create(libbe_handle_t *lbh, const char *name)
326 err = be_create_from_existing(lbh, name, be_active_path(lbh));
328 return (set_error(lbh, err));
333 be_deep_clone_prop(int prop, void *cb)
336 struct libbe_dccb *dccb;
338 char pval[BE_MAXPATHLEN];
339 char source[BE_MAXPATHLEN];
342 /* Skip some properties we don't want to touch */
343 if (prop == ZFS_PROP_CANMOUNT)
346 /* Don't copy readonly properties */
347 if (zfs_prop_readonly(prop))
350 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
351 sizeof(pval), &src, (char *)&source, sizeof(source), false)))
352 /* Just continue if we fail to read a property */
355 /* Only copy locally defined properties */
356 if (src != ZPROP_SRC_LOCAL)
359 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), (char *)pval);
365 be_deep_clone(zfs_handle_t *ds, void *data)
368 char be_path[BE_MAXPATHLEN];
369 char snap_path[BE_MAXPATHLEN];
372 zfs_handle_t *snap_hdl;
374 struct libbe_deep_clone *isdc, sdc;
375 struct libbe_dccb dccb;
377 isdc = (struct libbe_deep_clone *)data;
378 dspath = zfs_get_name(ds);
379 if ((dsname = strrchr(dspath, '/')) == NULL)
380 return (BE_ERR_UNKNOWN);
383 if (isdc->bename == NULL)
384 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
386 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
388 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
390 if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
391 return (set_error(isdc->lbh, BE_ERR_EXISTS));
394 zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
395 return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
397 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
398 nvlist_add_string(props, "canmount", "noauto");
402 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
403 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
406 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
407 err = BE_ERR_ZFSCLONE;
412 /* Failed to clone */
413 if (err != BE_ERR_SUCCESS)
414 return (set_error(isdc->lbh, err));
418 sdc.snapname = isdc->snapname;
419 sdc.be_root = (char *)&be_path;
421 err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
427 * Create the boot environment from pre-existing snapshot
430 be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
434 char be_path[BE_MAXPATHLEN];
435 char snap_path[BE_MAXPATHLEN];
437 char *parentname, *snapname;
438 zfs_handle_t *parent_hdl;
439 struct libbe_deep_clone sdc;
441 if ((err = be_validate_name(lbh, name)) != 0)
442 return (set_error(lbh, err));
443 if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
444 return (set_error(lbh, err));
445 if ((err = be_validate_snap(lbh, snap_path)) != 0)
446 return (set_error(lbh, err));
448 if ((err = be_root_concat(lbh, name, be_path)) != 0)
449 return (set_error(lbh, err));
451 if ((bename = strrchr(name, '/')) == NULL)
456 if ((parentname = strdup(snap_path)) == NULL)
457 return (set_error(lbh, BE_ERR_UNKNOWN));
459 snapname = strchr(parentname, '@');
460 if (snapname == NULL) {
462 return (set_error(lbh, BE_ERR_UNKNOWN));
469 sdc.snapname = snapname;
470 sdc.be_root = lbh->root;
472 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
473 err = be_deep_clone(parent_hdl, &sdc);
476 return (set_error(lbh, err));
481 * Create a boot environment from an existing boot environment
484 be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
487 char buf[BE_MAXPATHLEN];
489 if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)) != 0)
490 return (set_error(lbh, err));
492 err = be_create_from_existing_snap(lbh, name, (char *)buf);
494 return (set_error(lbh, err));
499 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
500 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
501 * failure. Does not set the internal library error state.
504 be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
506 zfs_handle_t *zfs_hdl;
507 char buf[BE_MAXPATHLEN];
509 int err = BE_ERR_SUCCESS;
511 if (strlen(snap_name) >= BE_MAXPATHLEN)
512 return (BE_ERR_PATHLEN);
514 if (!zfs_dataset_exists(lbh->lzh, snap_name,
516 return (BE_ERR_NOENT);
518 strlcpy(buf, snap_name, sizeof(buf));
520 /* Find the base filesystem of the snapshot */
521 if ((delim_pos = strchr(buf, '@')) == NULL)
522 return (BE_ERR_INVALIDNAME);
526 zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
527 return (BE_ERR_NOORIGIN);
529 if ((err = zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf,
530 sizeof(buf), NULL, NULL, 0, 1)) != 0)
531 err = BE_ERR_BADMOUNT;
533 if ((err != 0) && (strncmp(buf, "/", sizeof(buf)) != 0))
534 err = BE_ERR_BADMOUNT;
543 * Idempotently appends the name argument to the root boot environment path
544 * and copies the resulting string into the result buffer (which is assumed
545 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
546 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
547 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
548 * zfs_be_root. Does not set internal library error state.
551 be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
553 size_t name_len, root_len;
555 name_len = strlen(name);
556 root_len = strlen(lbh->root);
558 /* Act idempotently; return be name if it is already a full path */
559 if (strrchr(name, '/') != NULL) {
560 if (strstr(name, lbh->root) != name)
561 return (BE_ERR_INVALIDNAME);
563 if (name_len >= BE_MAXPATHLEN)
564 return (BE_ERR_PATHLEN);
566 strlcpy(result, name, BE_MAXPATHLEN);
567 return (BE_ERR_SUCCESS);
568 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
569 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
571 return (BE_ERR_SUCCESS);
574 return (BE_ERR_PATHLEN);
579 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
580 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
582 * Does not set internal library error state.
585 be_validate_name(libbe_handle_t *lbh, const char *name)
587 for (int i = 0; *name; i++) {
589 if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
591 return (BE_ERR_INVALIDNAME);
595 * Impose the additional restriction that the entire dataset name must
596 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
598 if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
599 return (BE_ERR_PATHLEN);
600 return (BE_ERR_SUCCESS);
608 be_rename(libbe_handle_t *lbh, const char *old, const char *new)
610 char full_old[BE_MAXPATHLEN];
611 char full_new[BE_MAXPATHLEN];
612 zfs_handle_t *zfs_hdl;
616 * be_validate_name is documented not to set error state, so we should
619 if ((err = be_validate_name(lbh, new)) != 0)
620 return (set_error(lbh, err));
621 if ((err = be_root_concat(lbh, old, full_old)) != 0)
622 return (set_error(lbh, err));
623 if ((err = be_root_concat(lbh, new, full_new)) != 0)
624 return (set_error(lbh, err));
626 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
627 return (set_error(lbh, BE_ERR_NOENT));
629 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
630 return (set_error(lbh, BE_ERR_EXISTS));
632 if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
633 ZFS_TYPE_FILESYSTEM)) == NULL)
634 return (set_error(lbh, BE_ERR_ZFSOPEN));
636 /* recurse, nounmount, forceunmount */
637 struct renameflags flags = {
641 err = zfs_rename(zfs_hdl, NULL, full_new, flags);
645 return (set_error(lbh, BE_ERR_UNKNOWN));
651 be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
653 char snap_name[BE_MAXPATHLEN];
654 char buf[BE_MAXPATHLEN];
658 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
659 /* Use the error set by be_snapshot */
662 be_root_concat(lbh, snap_name, buf);
664 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
665 return (set_error(lbh, BE_ERR_ZFSOPEN));
667 err = zfs_send_one(zfs, NULL, fd, 0);
675 be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
677 char buf[BE_MAXPATHLEN];
685 * We don't need this to be incredibly random, just unique enough that
686 * it won't conflict with an existing dataset name. Chopping time
687 * down to 32 bits is probably good enough for this.
689 snprintf(nbuf, 24, "tmp%u",
690 (uint32_t)(time(NULL) & 0xFFFFFFFF));
691 if ((err = be_root_concat(lbh, nbuf, buf)) != 0)
693 * Technically this is our problem, but we try to use short
694 * enough names that we won't run into problems except in
695 * worst-case BE root approaching MAXPATHLEN.
697 return (set_error(lbh, BE_ERR_PATHLEN));
701 strftime(buf + len, sizeof(buf) - len, "@%F-%T", localtime(&rawtime));
703 if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) {
706 return (set_error(lbh, BE_ERR_NOORIGIN));
708 return (set_error(lbh, BE_ERR_NOENT));
710 return (set_error(lbh, BE_ERR_IO));
712 return (set_error(lbh, BE_ERR_UNKNOWN));
716 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
717 return (set_error(lbh, BE_ERR_ZFSOPEN));
719 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
720 nvlist_add_string(props, "canmount", "noauto");
721 nvlist_add_string(props, "mountpoint", "/");
723 be_root_concat(lbh, bootenv, buf);
725 err = zfs_clone(zfs, buf, props);
730 return (set_error(lbh, BE_ERR_UNKNOWN));
733 * Finally, we open up the dataset we just cloned the snapshot so that
734 * we may promote it. This is necessary in order to clean up the ghost
735 * snapshot that doesn't need to be seen after the operation is
738 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
739 return (set_error(lbh, BE_ERR_ZFSOPEN));
741 err = zfs_promote(zfs);
745 return (set_error(lbh, BE_ERR_UNKNOWN));
747 /* Clean up the temporary snapshot */
748 return (be_destroy(lbh, nbuf, 0));
753 be_create_child_noent(libbe_handle_t *lbh, const char *active,
754 const char *child_path)
760 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
761 nvlist_add_string(props, "canmount", "noauto");
762 nvlist_add_string(props, "mountpoint", child_path);
765 if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
769 return (set_error(lbh, BE_ERR_EXISTS));
771 return (set_error(lbh, BE_ERR_NOENT));
773 case EZFS_BADVERSION:
774 return (set_error(lbh, BE_ERR_NOPOOL));
777 /* We set something up wrong, probably... */
778 return (set_error(lbh, BE_ERR_UNKNOWN));
783 if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
784 return (set_error(lbh, BE_ERR_ZFSOPEN));
787 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
790 * Similar to other cases, this shouldn't fail unless we've
791 * done something wrong. This is a new dataset that shouldn't
792 * have been mounted anywhere between creation and now.
794 if (err == EZFS_NOMEM)
795 return (set_error(lbh, BE_ERR_NOMEM));
796 return (set_error(lbh, BE_ERR_UNKNOWN));
799 return (BE_ERR_SUCCESS);
803 be_create_child_cloned(libbe_handle_t *lbh, const char *active)
805 char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
812 * Establish if the existing path is a zfs dataset or just
813 * the subdirectory of one
815 strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
816 if (mktemp(tmp) == NULL)
817 return (set_error(lbh, BE_ERR_UNKNOWN));
819 be_root_concat(lbh, tmp, buf);
820 printf("Here %s?\n", buf);
821 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
823 case EZFS_INVALIDNAME:
824 return (set_error(lbh, BE_ERR_INVALIDNAME));
828 * The other errors that zfs_ioc_snapshot might return
829 * shouldn't happen if we've set things up properly, so
830 * we'll gloss over them and call it UNKNOWN as it will
831 * require further triage.
833 if (errno == ENOTSUP)
834 return (set_error(lbh, BE_ERR_NOPOOL));
835 return (set_error(lbh, BE_ERR_UNKNOWN));
840 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
841 return (BE_ERR_ZFSOPEN);
843 if ((err = zfs_clone(zfs, active, NULL)) != 0)
844 /* XXX TODO correct error */
845 return (set_error(lbh, BE_ERR_UNKNOWN));
849 return (BE_ERR_SUCCESS);
853 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
856 char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
860 /* Require absolute paths */
861 if (*child_path != '/')
862 return (set_error(lbh, BE_ERR_BADPATH));
864 strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
867 /* Create non-mountable parent dataset(s) */
869 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
871 strncat(buf, s, len);
873 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
874 nvlist_add_string(props, "canmount", "off");
875 nvlist_add_string(props, "mountpoint", "none");
876 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
880 /* Path does not exist as a descendent of / yet */
881 if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
882 return (set_error(lbh, BE_ERR_PATHLEN));
884 if (stat(child_path, &sb) != 0) {
885 /* Verify that error is ENOENT */
887 return (set_error(lbh, BE_ERR_UNKNOWN));
888 return (be_create_child_noent(lbh, active, child_path));
889 } else if (cp_if_exists)
890 /* Path is already a descendent of / and should be copied */
891 return (be_create_child_cloned(lbh, active));
892 return (set_error(lbh, BE_ERR_EXISTS));
897 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
904 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
906 for (c = 0; c < children; ++c)
907 if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
912 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
917 if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
918 perror("ZFS_IOC_NEXTBOOT failed");
927 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
929 char be_path[BE_MAXPATHLEN];
930 char buf[BE_MAXPATHLEN];
931 nvlist_t *config, *vdevs;
936 be_root_concat(lbh, bootenv, be_path);
938 /* Note: be_exists fails if mountpoint is not / */
939 if ((err = be_exists(lbh, be_path)) != 0)
940 return (set_error(lbh, err));
943 config = zpool_get_config(lbh->active_phandle, NULL);
945 /* config should be fetchable... */
946 return (set_error(lbh, BE_ERR_UNKNOWN));
948 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
950 /* Similarly, it shouldn't be possible */
951 return (set_error(lbh, BE_ERR_UNKNOWN));
953 /* Expected format according to zfsbootcfg(8) man */
954 snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
956 /* We have no config tree */
957 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
959 return (set_error(lbh, BE_ERR_NOPOOL));
961 return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
963 /* Obtain bootenv zpool */
964 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
968 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
972 err = zfs_promote(zhp);
979 return (BE_ERR_SUCCESS);