diff --git a/lib/libbe/be.c b/lib/libbe/be.c index 753fde746e31..405aeeee7f6c 100644 --- a/lib/libbe/be.c +++ b/lib/libbe/be.c @@ -1266,12 +1266,27 @@ be_deactivate(libbe_handle_t *lbh, const char *ds, bool temporary) return (0); } +static int +be_zfs_promote_cb(zfs_handle_t *zhp, void *data __unused) +{ + int err; + + if ((err = zfs_promote(zhp)) != 0) + return (err); + + return (zfs_iter_filesystems(zhp, be_zfs_promote_cb, NULL)); +} + +static int +be_zfs_promote(zfs_handle_t *zhp) +{ + return (be_zfs_promote_cb(zhp, NULL)); +} + int be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) { - char be_path[BE_MAXPATHLEN]; - nvlist_t *dsprops; - const char *origin; + char be_path[BE_MAXPATHLEN], origin[BE_MAXPATHLEN]; zfs_handle_t *zhp; int err; @@ -1294,24 +1309,23 @@ be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) if (err) return (-1); - zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM); - if (zhp == NULL) - return (-1); + for (;;) { + zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM); + if (zhp == NULL) + return (-1); - if (be_prop_list_alloc(&dsprops) != 0) - return (-1); + if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof(origin), + NULL, NULL, 0, 1) != 0) { + zfs_close(zhp); + break; + } - if (be_get_dataset_props(lbh, be_path, dsprops) != 0) { - nvlist_free(dsprops); - return (-1); + err = be_zfs_promote(zhp); + zfs_close(zhp); + if (err) + break; } - if (nvlist_lookup_string(dsprops, "origin", &origin) == 0) - err = zfs_promote(zhp); - nvlist_free(dsprops); - - zfs_close(zhp); - if (err) return (-1); } diff --git a/sbin/bectl/tests/bectl_test.sh b/sbin/bectl/tests/bectl_test.sh index 5f865b86954e..5299238d9380 100755 --- a/sbin/bectl/tests/bectl_test.sh +++ b/sbin/bectl/tests/bectl_test.sh @@ -525,6 +525,59 @@ bectl_jail_cleanup() bectl_cleanup ${zpool} } +atf_test_case bectl_promotion cleanup +bectl_promotion_head() +{ + atf_set "descr" "Check bectl promotion upon activation" + atf_set "require.user" root +} +bectl_promotion_body() +{ + if [ "$(atf_config_get ci false)" = "true" ] && \ + [ "$(uname -p)" = "i386" ]; then + atf_skip "https://bugs.freebsd.org/249055" + fi + + if [ "$(atf_config_get ci false)" = "true" ] && \ + [ "$(uname -p)" = "armv7" ]; then + atf_skip "https://bugs.freebsd.org/249229" + fi + + cwd=$(realpath .) + zpool=$(make_zpool_name) + disk=${cwd}/disk.img + mount=${cwd}/mnt + root=${mount}/root + + bectl_create_deep_setup ${zpool} ${disk} ${mount} + atf_check mkdir -p ${root} + + # Sleeps interspersed to workaround some naming quirks; notably, + # bectl will append a serial if two snapshots were created within + # the same second, but it can only do that for the one root it's + # operating on. It won't check that other roots don't have a snpshot + # with the same name, and the promotion will fail. + atf_check bectl -r ${zpool}/ROOT rename default A + sleep 1 + atf_check bectl -r ${zpool}/ROOT create -r -e A B + sleep 1 + atf_check bectl -r ${zpool}/ROOT create -r -e B C + + # C should be a clone of B to start with + atf_check -o not-inline:"-" zfs list -Hr -o origin ${zpool}/ROOT/C + + # Activating it should then promote it all the way out of clone-hood. + # This entails two promotes internally, as the first would promote it to + # a snapshot of A before finally promoting it the second time out of + # clone status. + atf_check -o not-empty bectl -r ${zpool}/ROOT activate C + atf_check -o inline:"-\n-\n" zfs list -Hr -o origin ${zpool}/ROOT/C +} +bectl_promotion_cleanup() +{ + bectl_cleanup $(get_zpool_name) +} + atf_init_test_cases() { atf_add_test_case bectl_create @@ -534,4 +587,5 @@ atf_init_test_cases() atf_add_test_case bectl_mount atf_add_test_case bectl_rename atf_add_test_case bectl_jail + atf_add_test_case bectl_promotion }