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>
37 #include <sys/zfs_context.h>
38 #include <sys/mntent.h>
42 #include <libzfs_core.h>
51 struct be_destroy_data {
57 static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
58 const char *child_path);
59 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
62 /* Arbitrary... should tune */
63 #define BE_SNAP_SERIAL_MAX 1024
66 * Iterator function for locating the rootfs amongst the children of the
67 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *.
70 be_locate_rootfs(libbe_handle_t *lbh)
77 * Check first if root is ZFS; if not, we'll bail on rootfs capture.
78 * Unfortunately needed because zfs_path_to_zhandle will emit to
79 * stderr if / isn't actually a ZFS filesystem, which we'd like
82 if (statfs("/", &sfs) == 0) {
83 statfs2mnttab(&sfs, &entry);
84 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
88 zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
92 strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
98 * Initializes the libbe context to operate in the root boot environment
99 * dataset, for example, zroot/ROOT.
102 libbe_init(const char *root)
104 char altroot[MAXPATHLEN];
106 char *poolname, *pos;
110 poolname = pos = NULL;
112 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
115 if ((lbh->lzh = libzfs_init()) == NULL)
119 * Grab rootfs, we'll work backwards from there if an optional BE root
120 * has not been passed in.
122 if (be_locate_rootfs(lbh) != 0) {
128 /* Strip off the final slash from rootfs to get the be root */
129 strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
130 pos = strrchr(lbh->root, '/');
135 strlcpy(lbh->root, root, sizeof(lbh->root));
137 if ((pos = strchr(lbh->root, '/')) == NULL)
140 pnamelen = pos - lbh->root;
141 poolname = malloc(pnamelen + 1);
142 if (poolname == NULL)
145 strlcpy(poolname, lbh->root, pnamelen + 1);
146 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
151 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
152 sizeof(lbh->bootfs), NULL, true) != 0)
155 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT,
156 altroot, sizeof(altroot), NULL, true) == 0 &&
157 strcmp(altroot, "-") != 0)
158 lbh->altroot_len = strlen(altroot);
163 if (lbh->active_phandle != NULL)
164 zpool_close(lbh->active_phandle);
165 if (lbh->lzh != NULL)
166 libzfs_fini(lbh->lzh);
175 * Free memory allocated by libbe_init()
178 libbe_close(libbe_handle_t *lbh)
181 if (lbh->active_phandle != NULL)
182 zpool_close(lbh->active_phandle);
183 libzfs_fini(lbh->lzh);
188 * Proxy through to libzfs for the moment.
191 be_nicenum(uint64_t num, char *buf, size_t buflen)
194 zfs_nicenum(num, buf, buflen);
198 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
200 char path[BE_MAXPATHLEN];
201 struct be_destroy_data *bdd;
205 bdd = (struct be_destroy_data *)data;
206 if (bdd->snapname == NULL) {
207 err = zfs_iter_children(zfs_hdl, be_destroy_cb, data);
210 return (zfs_destroy(zfs_hdl, false));
212 /* If we're dealing with snapshots instead, delete that one alone */
213 err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data);
217 * This part is intentionally glossing over any potential errors,
218 * because there's a lot less potential for errors when we're cleaning
219 * up snapshots rather than a full deep BE. The primary error case
220 * here being if the snapshot doesn't exist in the first place, which
221 * the caller will likely deem insignificant as long as it doesn't
222 * exist after the call. Thus, such a missing snapshot shouldn't jam
223 * up the destruction.
225 snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl),
227 if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
229 snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT);
231 zfs_destroy(snap, false);
235 #define BE_DESTROY_WANTORIGIN (BE_DESTROY_ORIGIN | BE_DESTROY_AUTOORIGIN)
237 * Destroy the boot environment or snapshot specified by the name
238 * parameter. Options are or'd together with the possible values:
239 * BE_DESTROY_FORCE : forces operation on mounted datasets
240 * BE_DESTROY_ORIGIN: destroy the origin snapshot as well
243 be_destroy(libbe_handle_t *lbh, const char *name, int options)
245 struct be_destroy_data bdd;
246 char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN];
249 int err, force, mounted;
254 force = options & BE_DESTROY_FORCE;
257 be_root_concat(lbh, name, path);
259 if ((snapdelim = strchr(path, '@')) == NULL) {
260 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
261 return (set_error(lbh, BE_ERR_NOENT));
263 if (strcmp(path, lbh->rootfs) == 0 ||
264 strcmp(path, lbh->bootfs) == 0)
265 return (set_error(lbh, BE_ERR_DESTROYACT));
267 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM);
269 return (set_error(lbh, BE_ERR_ZFSOPEN));
271 if ((options & BE_DESTROY_WANTORIGIN) != 0 &&
272 zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin),
273 NULL, NULL, 0, 1) != 0 &&
274 (options & BE_DESTROY_ORIGIN) != 0)
275 return (set_error(lbh, BE_ERR_NOORIGIN));
278 * If the caller wants auto-origin destruction and the origin
279 * name matches one of our automatically created snapshot names
280 * (i.e. strftime("%F-%T") with a serial at the end), then
281 * we'll set the DESTROY_ORIGIN flag and nuke it
282 * be_is_auto_snapshot_name is exported from libbe(3) so that
283 * the caller can determine if it needs to warn about the origin
284 * not being destroyed or not.
286 if ((options & BE_DESTROY_AUTOORIGIN) != 0 && *origin != '\0' &&
287 be_is_auto_snapshot_name(lbh, origin))
288 options |= BE_DESTROY_ORIGIN;
290 /* Don't destroy a mounted dataset unless force is specified */
291 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
293 zfs_unmount(fs, NULL, 0);
296 return (set_error(lbh, BE_ERR_DESTROYMNT));
300 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
301 return (set_error(lbh, BE_ERR_NOENT));
303 bdd.snapname = strdup(snapdelim + 1);
304 if (bdd.snapname == NULL)
305 return (set_error(lbh, BE_ERR_NOMEM));
307 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET);
310 return (set_error(lbh, BE_ERR_ZFSOPEN));
314 err = be_destroy_cb(fs, &bdd);
318 /* Children are still present or the mount is referenced */
320 return (set_error(lbh, BE_ERR_DESTROYMNT));
321 return (set_error(lbh, BE_ERR_UNKNOWN));
324 if ((options & BE_DESTROY_ORIGIN) == 0)
327 /* The origin can't possibly be shorter than the BE root */
328 rootlen = strlen(lbh->root);
329 if (*origin == '\0' || strlen(origin) <= rootlen + 1)
330 return (set_error(lbh, BE_ERR_INVORIGIN));
333 * We'll be chopping off the BE root and running this back through
334 * be_destroy, so that we properly handle the origin snapshot whether
335 * it be that of a deep BE or not.
337 if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/')
340 return (be_destroy(lbh, origin + rootlen + 1,
341 options & ~BE_DESTROY_ORIGIN));
345 be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen)
352 len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime));
353 /* No room for serial... caller will do its best */
354 if (buflen - len < 2)
357 for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) {
358 snprintf(buf + len, buflen - len, "-%d", serial);
359 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT))
365 be_is_auto_snapshot_name(libbe_handle_t *lbh, const char *name)
368 int day, hour, minute, month, second, serial, year;
370 if ((snap = strchr(name, '@')) == NULL)
373 /* We'll grab the individual components and do some light validation. */
374 if (sscanf(snap, "%d-%d-%d-%d:%d:%d-%d", &year, &month, &day, &hour,
375 &minute, &second, &serial) != 7)
377 return (year >= 1970) && (month >= 1 && month <= 12) &&
378 (day >= 1 && day <= 31) && (hour >= 0 && hour <= 23) &&
379 (minute >= 0 && minute <= 59) && (second >= 0 && second <= 60) &&
384 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
385 bool recursive, char *result)
387 char buf[BE_MAXPATHLEN];
390 be_root_concat(lbh, source, buf);
392 if ((err = be_exists(lbh, buf)) != 0)
393 return (set_error(lbh, err));
395 if (snap_name != NULL) {
396 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
397 return (set_error(lbh, BE_ERR_INVALIDNAME));
399 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
400 return (set_error(lbh, BE_ERR_INVALIDNAME));
403 snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
406 be_setup_snapshot_name(lbh, buf, sizeof(buf));
408 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
409 sizeof(buf)) >= sizeof(buf))
410 return (set_error(lbh, BE_ERR_INVALIDNAME));
412 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
414 case EZFS_INVALIDNAME:
415 return (set_error(lbh, BE_ERR_INVALIDNAME));
419 * The other errors that zfs_ioc_snapshot might return
420 * shouldn't happen if we've set things up properly, so
421 * we'll gloss over them and call it UNKNOWN as it will
422 * require further triage.
424 if (errno == ENOTSUP)
425 return (set_error(lbh, BE_ERR_NOPOOL));
426 return (set_error(lbh, BE_ERR_UNKNOWN));
430 return (BE_ERR_SUCCESS);
435 * Create the boot environment specified by the name parameter
438 be_create(libbe_handle_t *lbh, const char *name)
442 err = be_create_from_existing(lbh, name, be_active_path(lbh));
444 return (set_error(lbh, err));
448 be_deep_clone_prop(int prop, void *cb)
451 struct libbe_dccb *dccb;
453 char pval[BE_MAXPATHLEN];
454 char source[BE_MAXPATHLEN];
458 /* Skip some properties we don't want to touch */
459 if (prop == ZFS_PROP_CANMOUNT)
462 /* Don't copy readonly properties */
463 if (zfs_prop_readonly(prop))
466 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
467 sizeof(pval), &src, (char *)&source, sizeof(source), false)))
468 /* Just continue if we fail to read a property */
472 * Only copy locally defined or received properties. This continues
473 * to avoid temporary/default/local properties intentionally without
474 * breaking received datasets.
476 if (src != ZPROP_SRC_LOCAL && src != ZPROP_SRC_RECEIVED)
479 /* Augment mountpoint with altroot, if needed */
481 if (prop == ZFS_PROP_MOUNTPOINT)
482 val = be_mountpoint_augmented(dccb->lbh, val);
484 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
490 * Return the corresponding boot environment path for a given
491 * dataset path, the constructed path is placed in 'result'.
493 * example: say our new boot environment name is 'bootenv' and
494 * the dataset path is 'zroot/ROOT/default/data/set'.
496 * result should produce: 'zroot/ROOT/bootenv/data/set'
499 be_get_path(struct libbe_deep_clone *ldc, const char *dspath, char *result, int result_size)
504 /* match the root path for the boot environments */
505 pos = strstr(dspath, ldc->lbh->root);
507 /* no match, different pools? */
509 return (BE_ERR_BADPATH);
511 /* root path of the new boot environment */
512 snprintf(result, result_size, "%s/%s", ldc->lbh->root, ldc->bename);
514 /* gets us to the parent dataset, the +1 consumes a trailing slash */
515 pos += strlen(ldc->lbh->root) + 1;
517 /* skip the parent dataset */
518 if ((child_dataset = strchr(pos, '/')) != NULL)
519 strlcat(result, child_dataset, result_size);
521 return (BE_ERR_SUCCESS);
525 be_clone_cb(zfs_handle_t *ds, void *data)
528 char be_path[BE_MAXPATHLEN];
529 char snap_path[BE_MAXPATHLEN];
531 zfs_handle_t *snap_hdl;
533 struct libbe_deep_clone *ldc;
534 struct libbe_dccb dccb;
536 ldc = (struct libbe_deep_clone *)data;
537 dspath = zfs_get_name(ds);
539 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, ldc->snapname);
541 /* construct the boot environment path from the dataset we're cloning */
542 if (be_get_path(ldc, dspath, be_path, sizeof(be_path)) != BE_ERR_SUCCESS)
543 return (set_error(ldc->lbh, BE_ERR_UNKNOWN));
545 /* the dataset to be created (i.e. the boot environment) already exists */
546 if (zfs_dataset_exists(ldc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
547 return (set_error(ldc->lbh, BE_ERR_EXISTS));
549 /* no snapshot found for this dataset, silently skip it */
550 if (!zfs_dataset_exists(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT))
554 zfs_open(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
555 return (set_error(ldc->lbh, BE_ERR_ZFSOPEN));
557 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
558 nvlist_add_string(props, "canmount", "noauto");
563 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
564 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
567 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
568 return (set_error(ldc->lbh, BE_ERR_ZFSCLONE));
573 if (ldc->depth_limit == -1 || ldc->depth < ldc->depth_limit) {
575 err = zfs_iter_filesystems(ds, be_clone_cb, ldc);
579 return (set_error(ldc->lbh, err));
583 * Create a boot environment with a given name from a given snapshot.
584 * Snapshots can be in the format 'zroot/ROOT/default@snapshot' or
585 * 'default@snapshot'. In the latter case, 'default@snapshot' will be prepended
586 * with the root path that libbe was initailized with.
589 be_clone(libbe_handle_t *lbh, const char *bename, const char *snapshot, int depth)
592 char snap_path[BE_MAXPATHLEN];
593 char *parentname, *snapname;
594 zfs_handle_t *parent_hdl;
595 struct libbe_deep_clone ldc;
597 /* ensure the boot environment name is valid */
598 if ((err = be_validate_name(lbh, bename)) != 0)
599 return (set_error(lbh, err));
602 * prepend the boot environment root path if we're
603 * given a partial snapshot name.
605 if ((err = be_root_concat(lbh, snapshot, snap_path)) != 0)
606 return (set_error(lbh, err));
608 /* ensure the snapshot exists */
609 if ((err = be_validate_snap(lbh, snap_path)) != 0)
610 return (set_error(lbh, err));
612 /* get a copy of the snapshot path so we can disect it */
613 if ((parentname = strdup(snap_path)) == NULL)
614 return (set_error(lbh, BE_ERR_UNKNOWN));
616 /* split dataset name from snapshot name */
617 snapname = strchr(parentname, '@');
618 if (snapname == NULL) {
620 return (set_error(lbh, BE_ERR_UNKNOWN));
625 /* set-up the boot environment */
628 ldc.snapname = snapname;
630 ldc.depth_limit = depth;
632 /* the boot environment will be cloned from this dataset */
633 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
635 /* create the boot environment */
636 err = be_clone_cb(parent_hdl, &ldc);
639 return (set_error(lbh, err));
643 * Create a boot environment from pre-existing snapshot, specifying a depth.
645 int be_create_depth(libbe_handle_t *lbh, const char *bename,
646 const char *snap, int depth)
648 return (be_clone(lbh, bename, snap, depth));
652 * Create the boot environment from pre-existing snapshot
655 be_create_from_existing_snap(libbe_handle_t *lbh, const char *bename,
658 return (be_clone(lbh, bename, snap, -1));
663 * Create a boot environment from an existing boot environment
666 be_create_from_existing(libbe_handle_t *lbh, const char *bename, const char *old)
669 char snap[BE_MAXPATHLEN];
671 if ((err = be_snapshot(lbh, old, NULL, true, snap)) != 0)
672 return (set_error(lbh, err));
674 err = be_clone(lbh, bename, snap, -1);
676 return (set_error(lbh, err));
681 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
682 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
683 * failure. Does not set the internal library error state.
686 be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
689 if (strlen(snap_name) >= BE_MAXPATHLEN)
690 return (BE_ERR_PATHLEN);
692 if (!zfs_name_valid(snap_name, ZFS_TYPE_SNAPSHOT))
693 return (BE_ERR_INVALIDNAME);
695 if (!zfs_dataset_exists(lbh->lzh, snap_name,
697 return (BE_ERR_NOENT);
699 return (BE_ERR_SUCCESS);
704 * Idempotently appends the name argument to the root boot environment path
705 * and copies the resulting string into the result buffer (which is assumed
706 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
707 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
708 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
709 * zfs_be_root. Does not set internal library error state.
712 be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
714 size_t name_len, root_len;
716 name_len = strlen(name);
717 root_len = strlen(lbh->root);
719 /* Act idempotently; return be name if it is already a full path */
720 if (strrchr(name, '/') != NULL) {
721 if (strstr(name, lbh->root) != name)
722 return (BE_ERR_INVALIDNAME);
724 if (name_len >= BE_MAXPATHLEN)
725 return (BE_ERR_PATHLEN);
727 strlcpy(result, name, BE_MAXPATHLEN);
728 return (BE_ERR_SUCCESS);
729 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
730 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
732 return (BE_ERR_SUCCESS);
735 return (BE_ERR_PATHLEN);
740 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
741 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
743 * Does not set internal library error state.
746 be_validate_name(libbe_handle_t *lbh, const char *name)
750 * Impose the additional restriction that the entire dataset name must
751 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
753 if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
754 return (BE_ERR_PATHLEN);
756 if (!zfs_name_valid(name, ZFS_TYPE_DATASET))
757 return (BE_ERR_INVALIDNAME);
759 return (BE_ERR_SUCCESS);
767 be_rename(libbe_handle_t *lbh, const char *old, const char *new)
769 char full_old[BE_MAXPATHLEN];
770 char full_new[BE_MAXPATHLEN];
771 zfs_handle_t *zfs_hdl;
775 * be_validate_name is documented not to set error state, so we should
778 if ((err = be_validate_name(lbh, new)) != 0)
779 return (set_error(lbh, err));
780 if ((err = be_root_concat(lbh, old, full_old)) != 0)
781 return (set_error(lbh, err));
782 if ((err = be_root_concat(lbh, new, full_new)) != 0)
783 return (set_error(lbh, err));
785 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
786 return (set_error(lbh, BE_ERR_NOENT));
788 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
789 return (set_error(lbh, BE_ERR_EXISTS));
791 if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
792 ZFS_TYPE_FILESYSTEM)) == NULL)
793 return (set_error(lbh, BE_ERR_ZFSOPEN));
795 /* recurse, nounmount, forceunmount */
796 struct renameflags flags = {
800 err = zfs_rename(zfs_hdl, NULL, full_new, flags);
804 return (set_error(lbh, BE_ERR_UNKNOWN));
810 be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
812 char snap_name[BE_MAXPATHLEN];
813 char buf[BE_MAXPATHLEN];
815 sendflags_t flags = { 0 };
818 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
819 /* Use the error set by be_snapshot */
822 be_root_concat(lbh, snap_name, buf);
824 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
825 return (set_error(lbh, BE_ERR_ZFSOPEN));
827 err = zfs_send_one(zfs, NULL, fd, flags);
835 be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
837 char buf[BE_MAXPATHLEN];
840 recvflags_t flags = { .nomount = 1 };
843 be_root_concat(lbh, bootenv, buf);
845 if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) {
848 return (set_error(lbh, BE_ERR_NOORIGIN));
850 return (set_error(lbh, BE_ERR_NOENT));
852 return (set_error(lbh, BE_ERR_IO));
854 return (set_error(lbh, BE_ERR_UNKNOWN));
858 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL)
859 return (set_error(lbh, BE_ERR_ZFSOPEN));
861 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
862 nvlist_add_string(props, "canmount", "noauto");
863 nvlist_add_string(props, "mountpoint", "none");
865 err = zfs_prop_set_list(zfs, props);
871 return (set_error(lbh, BE_ERR_UNKNOWN));
878 be_create_child_noent(libbe_handle_t *lbh, const char *active,
879 const char *child_path)
885 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
886 nvlist_add_string(props, "canmount", "noauto");
887 nvlist_add_string(props, "mountpoint", child_path);
890 if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
894 return (set_error(lbh, BE_ERR_EXISTS));
896 return (set_error(lbh, BE_ERR_NOENT));
898 case EZFS_BADVERSION:
899 return (set_error(lbh, BE_ERR_NOPOOL));
902 /* We set something up wrong, probably... */
903 return (set_error(lbh, BE_ERR_UNKNOWN));
908 if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
909 return (set_error(lbh, BE_ERR_ZFSOPEN));
912 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
915 * Similar to other cases, this shouldn't fail unless we've
916 * done something wrong. This is a new dataset that shouldn't
917 * have been mounted anywhere between creation and now.
919 if (err == EZFS_NOMEM)
920 return (set_error(lbh, BE_ERR_NOMEM));
921 return (set_error(lbh, BE_ERR_UNKNOWN));
924 return (BE_ERR_SUCCESS);
928 be_create_child_cloned(libbe_handle_t *lbh, const char *active)
930 char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
937 * Establish if the existing path is a zfs dataset or just
938 * the subdirectory of one
940 strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
941 if (mktemp(tmp) == NULL)
942 return (set_error(lbh, BE_ERR_UNKNOWN));
944 be_root_concat(lbh, tmp, buf);
945 printf("Here %s?\n", buf);
946 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
948 case EZFS_INVALIDNAME:
949 return (set_error(lbh, BE_ERR_INVALIDNAME));
953 * The other errors that zfs_ioc_snapshot might return
954 * shouldn't happen if we've set things up properly, so
955 * we'll gloss over them and call it UNKNOWN as it will
956 * require further triage.
958 if (errno == ENOTSUP)
959 return (set_error(lbh, BE_ERR_NOPOOL));
960 return (set_error(lbh, BE_ERR_UNKNOWN));
965 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
966 return (BE_ERR_ZFSOPEN);
968 if ((err = zfs_clone(zfs, active, NULL)) != 0)
969 /* XXX TODO correct error */
970 return (set_error(lbh, BE_ERR_UNKNOWN));
974 return (BE_ERR_SUCCESS);
978 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
981 char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
985 /* Require absolute paths */
986 if (*child_path != '/')
987 return (set_error(lbh, BE_ERR_BADPATH));
989 strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
992 /* Create non-mountable parent dataset(s) */
994 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
996 strncat(buf, s, len);
998 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
999 nvlist_add_string(props, "canmount", "off");
1000 nvlist_add_string(props, "mountpoint", "none");
1001 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
1005 /* Path does not exist as a descendent of / yet */
1006 if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
1007 return (set_error(lbh, BE_ERR_PATHLEN));
1009 if (stat(child_path, &sb) != 0) {
1010 /* Verify that error is ENOENT */
1011 if (errno != ENOENT)
1012 return (set_error(lbh, BE_ERR_UNKNOWN));
1013 return (be_create_child_noent(lbh, active, child_path));
1014 } else if (cp_if_exists)
1015 /* Path is already a descendent of / and should be copied */
1016 return (be_create_child_cloned(lbh, active));
1017 return (set_error(lbh, BE_ERR_EXISTS));
1022 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
1029 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
1031 for (c = 0; c < children; ++c)
1032 if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
1037 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1042 if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
1043 perror("ZFS_IOC_NEXTBOOT failed");
1051 * Deactivate old BE dataset; currently just sets canmount=noauto
1054 be_deactivate(libbe_handle_t *lbh, const char *ds)
1058 if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL)
1060 if (zfs_prop_set(zfs, "canmount", "noauto") != 0)
1067 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
1069 char be_path[BE_MAXPATHLEN];
1070 char buf[BE_MAXPATHLEN];
1071 nvlist_t *config, *dsprops, *vdevs;
1077 be_root_concat(lbh, bootenv, be_path);
1079 /* Note: be_exists fails if mountpoint is not / */
1080 if ((err = be_exists(lbh, be_path)) != 0)
1081 return (set_error(lbh, err));
1084 config = zpool_get_config(lbh->active_phandle, NULL);
1086 /* config should be fetchable... */
1087 return (set_error(lbh, BE_ERR_UNKNOWN));
1089 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1091 /* Similarly, it shouldn't be possible */
1092 return (set_error(lbh, BE_ERR_UNKNOWN));
1094 /* Expected format according to zfsbootcfg(8) man */
1095 snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
1097 /* We have no config tree */
1098 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1100 return (set_error(lbh, BE_ERR_NOPOOL));
1102 return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
1104 if (be_deactivate(lbh, lbh->bootfs) != 0)
1107 /* Obtain bootenv zpool */
1108 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
1112 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
1116 if (be_prop_list_alloc(&dsprops) != 0)
1119 if (be_get_dataset_props(lbh, be_path, dsprops) != 0) {
1120 nvlist_free(dsprops);
1124 if (nvlist_lookup_string(dsprops, "origin", &origin) == 0)
1125 err = zfs_promote(zhp);
1126 nvlist_free(dsprops);
1134 return (BE_ERR_SUCCESS);