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$");
32 #include <sys/param.h>
33 #include <sys/mount.h>
35 #include <sys/ucred.h>
39 #include <libzfs_core.h>
49 static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
50 const char *child_path);
51 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
55 * Iterator function for locating the rootfs amongst the children of the
56 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *.
59 be_locate_rootfs(libbe_handle_t *lbh)
62 struct extmnttab entry;
66 * Check first if root is ZFS; if not, we'll bail on rootfs capture.
67 * Unfortunately needed because zfs_path_to_zhandle will emit to
68 * stderr if / isn't actually a ZFS filesystem, which we'd like
71 if (statfs("/", &sfs) == 0) {
72 statfs2mnttab(&sfs, &entry);
73 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
77 zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
81 strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
87 * Initializes the libbe context to operate in the root boot environment
88 * dataset, for example, zroot/ROOT.
91 libbe_init(const char *root)
93 char altroot[MAXPATHLEN];
99 poolname = pos = NULL;
101 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
104 if ((lbh->lzh = libzfs_init()) == NULL)
108 * Grab rootfs, we'll work backwards from there if an optional BE root
109 * has not been passed in.
111 if (be_locate_rootfs(lbh) != 0) {
117 /* Strip off the final slash from rootfs to get the be root */
118 strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
119 pos = strrchr(lbh->root, '/');
124 strlcpy(lbh->root, root, sizeof(lbh->root));
126 if ((pos = strchr(lbh->root, '/')) == NULL)
129 pnamelen = pos - lbh->root;
130 poolname = malloc(pnamelen + 1);
131 if (poolname == NULL)
134 strlcpy(poolname, lbh->root, pnamelen + 1);
135 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
140 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
141 sizeof(lbh->bootfs), NULL, true) != 0)
144 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT,
145 altroot, sizeof(altroot), NULL, true) == 0 &&
146 strcmp(altroot, "-") != 0)
147 lbh->altroot_len = strlen(altroot);
152 if (lbh->active_phandle != NULL)
153 zpool_close(lbh->active_phandle);
154 if (lbh->lzh != NULL)
155 libzfs_fini(lbh->lzh);
164 * Free memory allocated by libbe_init()
167 libbe_close(libbe_handle_t *lbh)
170 if (lbh->active_phandle != NULL)
171 zpool_close(lbh->active_phandle);
172 libzfs_fini(lbh->lzh);
177 * Proxy through to libzfs for the moment.
180 be_nicenum(uint64_t num, char *buf, size_t buflen)
183 zfs_nicenum(num, buf, buflen);
187 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
191 if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
193 if ((err = zfs_destroy(zfs_hdl, false)) != 0)
199 * Destroy the boot environment or snapshot specified by the name
200 * parameter. Options are or'd together with the possible values:
201 * BE_DESTROY_FORCE : forces operation on mounted datasets
204 be_destroy(libbe_handle_t *lbh, const char *name, int options)
207 char path[BE_MAXPATHLEN];
209 int err, force, mounted;
212 force = options & BE_DESTROY_FORCE;
214 be_root_concat(lbh, name, path);
216 if (strchr(name, '@') == NULL) {
217 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
218 return (set_error(lbh, BE_ERR_NOENT));
220 if (strcmp(path, lbh->rootfs) == 0 ||
221 strcmp(path, lbh->bootfs) == 0)
222 return (set_error(lbh, BE_ERR_DESTROYACT));
224 fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
227 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
228 return (set_error(lbh, BE_ERR_NOENT));
230 fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
234 return (set_error(lbh, BE_ERR_ZFSOPEN));
236 /* Check if mounted, unmount if force is specified */
237 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
239 zfs_unmount(fs, NULL, 0);
241 return (set_error(lbh, BE_ERR_DESTROYMNT));
244 if ((err = be_destroy_cb(fs, NULL)) != 0) {
245 /* Children are still present or the mount is referenced */
247 return (set_error(lbh, BE_ERR_DESTROYMNT));
248 return (set_error(lbh, BE_ERR_UNKNOWN));
256 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
257 bool recursive, char *result)
259 char buf[BE_MAXPATHLEN];
263 be_root_concat(lbh, source, buf);
265 if ((err = be_exists(lbh, buf)) != 0)
266 return (set_error(lbh, err));
268 if (snap_name != NULL) {
269 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
270 return (set_error(lbh, BE_ERR_INVALIDNAME));
272 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
273 return (set_error(lbh, BE_ERR_INVALIDNAME));
276 snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
281 strftime(buf + len, sizeof(buf) - len,
282 "@%F-%T", localtime(&rawtime));
283 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
284 sizeof(buf)) >= sizeof(buf))
285 return (set_error(lbh, BE_ERR_INVALIDNAME));
288 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
290 case EZFS_INVALIDNAME:
291 return (set_error(lbh, BE_ERR_INVALIDNAME));
295 * The other errors that zfs_ioc_snapshot might return
296 * shouldn't happen if we've set things up properly, so
297 * we'll gloss over them and call it UNKNOWN as it will
298 * require further triage.
300 if (errno == ENOTSUP)
301 return (set_error(lbh, BE_ERR_NOPOOL));
302 return (set_error(lbh, BE_ERR_UNKNOWN));
306 return (BE_ERR_SUCCESS);
311 * Create the boot environment specified by the name parameter
314 be_create(libbe_handle_t *lbh, const char *name)
318 err = be_create_from_existing(lbh, name, be_active_path(lbh));
320 return (set_error(lbh, err));
324 be_deep_clone_prop(int prop, void *cb)
327 struct libbe_dccb *dccb;
329 char pval[BE_MAXPATHLEN];
330 char source[BE_MAXPATHLEN];
334 /* Skip some properties we don't want to touch */
335 if (prop == ZFS_PROP_CANMOUNT)
338 /* Don't copy readonly properties */
339 if (zfs_prop_readonly(prop))
342 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
343 sizeof(pval), &src, (char *)&source, sizeof(source), false)))
344 /* Just continue if we fail to read a property */
347 /* Only copy locally defined properties */
348 if (src != ZPROP_SRC_LOCAL)
351 /* Augment mountpoint with altroot, if needed */
353 if (prop == ZFS_PROP_MOUNTPOINT)
354 val = be_mountpoint_augmented(dccb->lbh, val);
356 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
362 be_deep_clone(zfs_handle_t *ds, void *data)
365 char be_path[BE_MAXPATHLEN];
366 char snap_path[BE_MAXPATHLEN];
369 zfs_handle_t *snap_hdl;
371 struct libbe_deep_clone *isdc, sdc;
372 struct libbe_dccb dccb;
374 isdc = (struct libbe_deep_clone *)data;
375 dspath = zfs_get_name(ds);
376 if ((dsname = strrchr(dspath, '/')) == NULL)
377 return (BE_ERR_UNKNOWN);
380 if (isdc->bename == NULL)
381 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
383 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
385 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
387 if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
388 return (set_error(isdc->lbh, BE_ERR_EXISTS));
391 zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
392 return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
394 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
395 nvlist_add_string(props, "canmount", "noauto");
397 dccb.lbh = isdc->lbh;
400 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
401 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
404 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
405 err = BE_ERR_ZFSCLONE;
410 /* Failed to clone */
411 if (err != BE_ERR_SUCCESS)
412 return (set_error(isdc->lbh, err));
416 sdc.snapname = isdc->snapname;
417 sdc.be_root = (char *)&be_path;
419 err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
425 * Create the boot environment from pre-existing snapshot
428 be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
432 char be_path[BE_MAXPATHLEN];
433 char snap_path[BE_MAXPATHLEN];
435 char *parentname, *snapname;
436 zfs_handle_t *parent_hdl;
437 struct libbe_deep_clone sdc;
439 if ((err = be_validate_name(lbh, name)) != 0)
440 return (set_error(lbh, err));
441 if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
442 return (set_error(lbh, err));
443 if ((err = be_validate_snap(lbh, snap_path)) != 0)
444 return (set_error(lbh, err));
446 if ((err = be_root_concat(lbh, name, be_path)) != 0)
447 return (set_error(lbh, err));
449 if ((bename = strrchr(name, '/')) == NULL)
454 if ((parentname = strdup(snap_path)) == NULL)
455 return (set_error(lbh, BE_ERR_UNKNOWN));
457 snapname = strchr(parentname, '@');
458 if (snapname == NULL) {
460 return (set_error(lbh, BE_ERR_UNKNOWN));
467 sdc.snapname = snapname;
468 sdc.be_root = lbh->root;
470 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
471 err = be_deep_clone(parent_hdl, &sdc);
474 return (set_error(lbh, err));
479 * Create a boot environment from an existing boot environment
482 be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
485 char buf[BE_MAXPATHLEN];
487 if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)) != 0)
488 return (set_error(lbh, err));
490 err = be_create_from_existing_snap(lbh, name, (char *)buf);
492 return (set_error(lbh, err));
497 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
498 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
499 * failure. Does not set the internal library error state.
502 be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
505 if (strlen(snap_name) >= BE_MAXPATHLEN)
506 return (BE_ERR_PATHLEN);
508 if (!zfs_dataset_exists(lbh->lzh, snap_name,
510 return (BE_ERR_NOENT);
512 return (BE_ERR_SUCCESS);
517 * Idempotently appends the name argument to the root boot environment path
518 * and copies the resulting string into the result buffer (which is assumed
519 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
520 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
521 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
522 * zfs_be_root. Does not set internal library error state.
525 be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
527 size_t name_len, root_len;
529 name_len = strlen(name);
530 root_len = strlen(lbh->root);
532 /* Act idempotently; return be name if it is already a full path */
533 if (strrchr(name, '/') != NULL) {
534 if (strstr(name, lbh->root) != name)
535 return (BE_ERR_INVALIDNAME);
537 if (name_len >= BE_MAXPATHLEN)
538 return (BE_ERR_PATHLEN);
540 strlcpy(result, name, BE_MAXPATHLEN);
541 return (BE_ERR_SUCCESS);
542 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
543 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
545 return (BE_ERR_SUCCESS);
548 return (BE_ERR_PATHLEN);
553 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
554 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
556 * Does not set internal library error state.
559 be_validate_name(libbe_handle_t *lbh, const char *name)
561 for (int i = 0; *name; i++) {
563 if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
565 return (BE_ERR_INVALIDNAME);
569 * Impose the additional restriction that the entire dataset name must
570 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
572 if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
573 return (BE_ERR_PATHLEN);
574 return (BE_ERR_SUCCESS);
582 be_rename(libbe_handle_t *lbh, const char *old, const char *new)
584 char full_old[BE_MAXPATHLEN];
585 char full_new[BE_MAXPATHLEN];
586 zfs_handle_t *zfs_hdl;
590 * be_validate_name is documented not to set error state, so we should
593 if ((err = be_validate_name(lbh, new)) != 0)
594 return (set_error(lbh, err));
595 if ((err = be_root_concat(lbh, old, full_old)) != 0)
596 return (set_error(lbh, err));
597 if ((err = be_root_concat(lbh, new, full_new)) != 0)
598 return (set_error(lbh, err));
600 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
601 return (set_error(lbh, BE_ERR_NOENT));
603 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
604 return (set_error(lbh, BE_ERR_EXISTS));
606 if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
607 ZFS_TYPE_FILESYSTEM)) == NULL)
608 return (set_error(lbh, BE_ERR_ZFSOPEN));
610 /* recurse, nounmount, forceunmount */
611 struct renameflags flags = {
615 err = zfs_rename(zfs_hdl, NULL, full_new, flags);
619 return (set_error(lbh, BE_ERR_UNKNOWN));
625 be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
627 char snap_name[BE_MAXPATHLEN];
628 char buf[BE_MAXPATHLEN];
632 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
633 /* Use the error set by be_snapshot */
636 be_root_concat(lbh, snap_name, buf);
638 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
639 return (set_error(lbh, BE_ERR_ZFSOPEN));
641 err = zfs_send_one(zfs, NULL, fd, 0);
649 be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
651 char buf[BE_MAXPATHLEN];
654 recvflags_t flags = { .nomount = 1 };
657 be_root_concat(lbh, bootenv, buf);
659 if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) {
662 return (set_error(lbh, BE_ERR_NOORIGIN));
664 return (set_error(lbh, BE_ERR_NOENT));
666 return (set_error(lbh, BE_ERR_IO));
668 return (set_error(lbh, BE_ERR_UNKNOWN));
672 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL)
673 return (set_error(lbh, BE_ERR_ZFSOPEN));
675 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
676 nvlist_add_string(props, "canmount", "noauto");
677 nvlist_add_string(props, "mountpoint", "/");
679 err = zfs_prop_set_list(zfs, props);
685 return (set_error(lbh, BE_ERR_UNKNOWN));
692 be_create_child_noent(libbe_handle_t *lbh, const char *active,
693 const char *child_path)
699 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
700 nvlist_add_string(props, "canmount", "noauto");
701 nvlist_add_string(props, "mountpoint", child_path);
704 if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
708 return (set_error(lbh, BE_ERR_EXISTS));
710 return (set_error(lbh, BE_ERR_NOENT));
712 case EZFS_BADVERSION:
713 return (set_error(lbh, BE_ERR_NOPOOL));
716 /* We set something up wrong, probably... */
717 return (set_error(lbh, BE_ERR_UNKNOWN));
722 if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
723 return (set_error(lbh, BE_ERR_ZFSOPEN));
726 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
729 * Similar to other cases, this shouldn't fail unless we've
730 * done something wrong. This is a new dataset that shouldn't
731 * have been mounted anywhere between creation and now.
733 if (err == EZFS_NOMEM)
734 return (set_error(lbh, BE_ERR_NOMEM));
735 return (set_error(lbh, BE_ERR_UNKNOWN));
738 return (BE_ERR_SUCCESS);
742 be_create_child_cloned(libbe_handle_t *lbh, const char *active)
744 char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
751 * Establish if the existing path is a zfs dataset or just
752 * the subdirectory of one
754 strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
755 if (mktemp(tmp) == NULL)
756 return (set_error(lbh, BE_ERR_UNKNOWN));
758 be_root_concat(lbh, tmp, buf);
759 printf("Here %s?\n", buf);
760 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
762 case EZFS_INVALIDNAME:
763 return (set_error(lbh, BE_ERR_INVALIDNAME));
767 * The other errors that zfs_ioc_snapshot might return
768 * shouldn't happen if we've set things up properly, so
769 * we'll gloss over them and call it UNKNOWN as it will
770 * require further triage.
772 if (errno == ENOTSUP)
773 return (set_error(lbh, BE_ERR_NOPOOL));
774 return (set_error(lbh, BE_ERR_UNKNOWN));
779 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
780 return (BE_ERR_ZFSOPEN);
782 if ((err = zfs_clone(zfs, active, NULL)) != 0)
783 /* XXX TODO correct error */
784 return (set_error(lbh, BE_ERR_UNKNOWN));
788 return (BE_ERR_SUCCESS);
792 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
795 char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
799 /* Require absolute paths */
800 if (*child_path != '/')
801 return (set_error(lbh, BE_ERR_BADPATH));
803 strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
806 /* Create non-mountable parent dataset(s) */
808 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
810 strncat(buf, s, len);
812 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
813 nvlist_add_string(props, "canmount", "off");
814 nvlist_add_string(props, "mountpoint", "none");
815 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
819 /* Path does not exist as a descendent of / yet */
820 if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
821 return (set_error(lbh, BE_ERR_PATHLEN));
823 if (stat(child_path, &sb) != 0) {
824 /* Verify that error is ENOENT */
826 return (set_error(lbh, BE_ERR_UNKNOWN));
827 return (be_create_child_noent(lbh, active, child_path));
828 } else if (cp_if_exists)
829 /* Path is already a descendent of / and should be copied */
830 return (be_create_child_cloned(lbh, active));
831 return (set_error(lbh, BE_ERR_EXISTS));
836 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
843 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
845 for (c = 0; c < children; ++c)
846 if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
851 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
856 if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
857 perror("ZFS_IOC_NEXTBOOT failed");
865 * Deactivate old BE dataset; currently just sets canmount=noauto
868 be_deactivate(libbe_handle_t *lbh, const char *ds)
872 if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL)
874 if (zfs_prop_set(zfs, "canmount", "noauto") != 0)
881 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
883 char be_path[BE_MAXPATHLEN];
884 char buf[BE_MAXPATHLEN];
885 nvlist_t *config, *dsprops, *vdevs;
891 be_root_concat(lbh, bootenv, be_path);
893 /* Note: be_exists fails if mountpoint is not / */
894 if ((err = be_exists(lbh, be_path)) != 0)
895 return (set_error(lbh, err));
898 config = zpool_get_config(lbh->active_phandle, NULL);
900 /* config should be fetchable... */
901 return (set_error(lbh, BE_ERR_UNKNOWN));
903 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
905 /* Similarly, it shouldn't be possible */
906 return (set_error(lbh, BE_ERR_UNKNOWN));
908 /* Expected format according to zfsbootcfg(8) man */
909 snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
911 /* We have no config tree */
912 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
914 return (set_error(lbh, BE_ERR_NOPOOL));
916 return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
918 if (be_deactivate(lbh, lbh->bootfs) != 0)
921 /* Obtain bootenv zpool */
922 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
926 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
930 if (be_prop_list_alloc(&dsprops) != 0)
933 if (be_get_dataset_props(lbh, be_path, dsprops) != 0) {
934 nvlist_free(dsprops);
938 if (nvlist_lookup_string(dsprops, "origin", &origin) == 0)
939 err = zfs_promote(zhp);
940 nvlist_free(dsprops);
948 return (BE_ERR_SUCCESS);