The branch main has been updated by kevans:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=2e020c84cb5ee1452e448f27ff7a7b6076c0147a

commit 2e020c84cb5ee1452e448f27ff7a7b6076c0147a
Author:     Pat Maddox <[email protected]>
AuthorDate: 2026-02-05 03:54:09 +0000
Commit:     Kyle Evans <[email protected]>
CommitDate: 2026-02-05 03:54:45 +0000

    bectl: Add -E flag to create an empty boot environment
    
    Signed-off-by: Pat Maddox <[email protected]>
    Reviewed by:    kevans
    MFC after:      1 week
    Closes:         https://github.com/freebsd/freebsd-src/pull/1975
---
 lib/libbe/be.c                 | 64 +++++++++++++++++++++++++++++++++---------
 lib/libbe/be.h                 |  1 +
 lib/libbe/libbe.3              |  9 +++++-
 lib/libbe/tests/Makefile       |  2 +-
 lib/libbe/tests/be_create.sh   |  9 ++++++
 lib/libbe/tests/target_prog.c  | 11 +++++---
 sbin/bectl/bectl.8             | 14 ++++++++-
 sbin/bectl/bectl.c             | 16 +++++++++--
 sbin/bectl/tests/bectl_test.sh |  8 ++++++
 9 files changed, 111 insertions(+), 23 deletions(-)

diff --git a/lib/libbe/be.c b/lib/libbe/be.c
index 4a7c2e43b2c1..2147435ebf9b 100644
--- a/lib/libbe/be.c
+++ b/lib/libbe/be.c
@@ -891,6 +891,54 @@ be_create_from_existing(libbe_handle_t *lbh, const char 
*bename, const char *old
        return (set_error(lbh, err));
 }
 
+/*
+ * Create a zfs dataset and map the return to libbe error.
+ */
+static int
+be_zfs_create(libzfs_handle_t *lzh, const char *buf, zfs_type_t t,
+    nvlist_t *props)
+{
+       int err;
+
+       if ((err = zfs_create(lzh, buf, t, props)) != 0) {
+               switch (err) {
+               case EZFS_EXISTS:
+                       return (BE_ERR_EXISTS);
+               case EZFS_NOENT:
+                       return (BE_ERR_NOENT);
+               case EZFS_BADTYPE:
+               case EZFS_BADVERSION:
+                       return (BE_ERR_NOPOOL);
+               case EZFS_BADPROP:
+               default:
+                       /* We set something up wrong, probably... */
+                       return (BE_ERR_UNKNOWN);
+               }
+       }
+
+       return (BE_ERR_SUCCESS);
+}
+
+
+/*
+ * Create an empty boot environment.
+ */
+int
+be_create_empty(libbe_handle_t *lbh, const char *bename)
+{
+       char buf[BE_MAXPATHLEN];
+       int err;
+
+       if ((err = be_validate_name(lbh, bename)) != 0)
+               return (set_error(lbh, err));
+
+       if ((err = be_root_concat(lbh, bename, buf)) != 0)
+               return (set_error(lbh, err));
+
+       err = be_zfs_create(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM, NULL);
+       return (set_error(lbh, err));
+}
+
 
 /*
  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
@@ -1118,21 +1166,9 @@ be_create_child_noent(libbe_handle_t *lbh, const char 
*active,
        nvlist_add_string(props, "mountpoint", child_path);
 
        /* Create */
-       if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
+       if ((err = be_zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
            props)) != 0) {
-               switch (err) {
-               case EZFS_EXISTS:
-                       return (set_error(lbh, BE_ERR_EXISTS));
-               case EZFS_NOENT:
-                       return (set_error(lbh, BE_ERR_NOENT));
-               case EZFS_BADTYPE:
-               case EZFS_BADVERSION:
-                       return (set_error(lbh, BE_ERR_NOPOOL));
-               case EZFS_BADPROP:
-               default:
-                       /* We set something up wrong, probably... */
-                       return (set_error(lbh, BE_ERR_UNKNOWN));
-               }
+               return (set_error(lbh, err));
        }
        nvlist_free(props);
 
diff --git a/lib/libbe/be.h b/lib/libbe/be.h
index d3f47c0604fe..18be067c4410 100644
--- a/lib/libbe/be.h
+++ b/lib/libbe/be.h
@@ -65,6 +65,7 @@ bool be_is_auto_snapshot_name(libbe_handle_t *, const char *);
 /* Bootenv creation functions */
 int be_create(libbe_handle_t *, const char *);
 int be_create_depth(libbe_handle_t *, const char *, const char *, int);
+int be_create_empty(libbe_handle_t *, const char *);
 int be_create_from_existing(libbe_handle_t *, const char *, const char *);
 int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *);
 int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *);
diff --git a/lib/libbe/libbe.3 b/lib/libbe/libbe.3
index 4331713e9227..f65da5ef1090 100644
--- a/lib/libbe/libbe.3
+++ b/lib/libbe/libbe.3
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 11, 2025
+.Dd February 4, 2026
 .Dt LIBBE 3
 .Os
 .Sh NAME
@@ -69,6 +69,9 @@
 .Fn be_create_depth "libbe_handle_t *hdl" "const char *be_name" "const char 
*snap" "int depth"
 .Pp
 .Ft int
+.Fn be_create_empty "libbe_handle_t *hdl" "const char *be_name"
+.Pp
+.Ft int
 .Fn be_create_from_existing "libbe_handle_t *hdl" "const char *be_name" "const 
char *be_origin"
 .Pp
 .Ft int
@@ -282,6 +285,10 @@ A depth of '0' is no recursion and '-1' is unlimited 
(i.e., a recursive boot
 environment).
 .Pp
 The
+.Fn be_create_empty
+function creates an empty boot environment with the given name.
+.Pp
+The
 .Fn be_create_from_existing
 function creates a boot environment with the given name from the name of an
 existing boot environment.
diff --git a/lib/libbe/tests/Makefile b/lib/libbe/tests/Makefile
index dfe49bd7f3e5..57e33b75aabc 100644
--- a/lib/libbe/tests/Makefile
+++ b/lib/libbe/tests/Makefile
@@ -20,6 +20,6 @@ CFLAGS+= -I${ZFSTOP}/lib/libspl/include/os/freebsd
 CFLAGS+= -I${SRCTOP}/sys
 CFLAGS+= -I${SRCTOP}/cddl/compat/opensolaris/include
 CFLAGS+= -include ${ZFSTOP}/include/os/freebsd/spl/sys/ccompile.h
-CFLAGS+= -DHAVE_ISSETUGID 
+CFLAGS+= -DHAVE_ISSETUGID -DHAVE_STRLCAT -DHAVE_STRLCPY
 
 .include <bsd.test.mk>
diff --git a/lib/libbe/tests/be_create.sh b/lib/libbe/tests/be_create.sh
index e1342368995d..e39ce1a0ccb4 100755
--- a/lib/libbe/tests/be_create.sh
+++ b/lib/libbe/tests/be_create.sh
@@ -174,6 +174,15 @@ libbe_create_body()
        # the child dataset should exist
        atf_check -o not-empty \
                  zfs list "${zpool}/ROOT/relative-snap/usr"
+
+       # test empty BE creation.
+       atf_check $prog "${zpool}/ROOT" \
+               empty \
+               ignored \
+               0
+       # the dataset should exist
+       atf_check -o not-empty \
+               zfs list "${zpool}/ROOT/empty"
 }
 
 libbe_create_cleanup()
diff --git a/lib/libbe/tests/target_prog.c b/lib/libbe/tests/target_prog.c
index 4c79d1a30714..83ef68ab2890 100644
--- a/lib/libbe/tests/target_prog.c
+++ b/lib/libbe/tests/target_prog.c
@@ -27,6 +27,7 @@
 
 #include <sys/cdefs.h>
 #include <be.h>
+#include <string.h>
 
 /*
  * argv[1] = root boot environment (e.g. zroot/ROOT),
@@ -36,15 +37,17 @@
  */
 int main(int argc, char *argv[]) {
 
-        libbe_handle_t *lbh;
+       libbe_handle_t *lbh;
 
        if (argc != 5)
-               return -1;
+               return (-1);
 
-        if ((lbh = libbe_init(argv[1])) == NULL)
-                return -1;
+       if ((lbh = libbe_init(argv[1])) == NULL)
+               return (-1);
 
        libbe_print_on_error(lbh, true);
 
+       if (strcmp(argv[2], "empty") == 0)
+               return (be_create_empty(lbh, argv[2]));
        return (be_create_depth(lbh, argv[2], argv[3], atoi(argv[4])));
 }
diff --git a/sbin/bectl/bectl.8 b/sbin/bectl/bectl.8
index 0e08b3383e9a..7614bea2ddad 100644
--- a/sbin/bectl/bectl.8
+++ b/sbin/bectl/bectl.8
@@ -3,7 +3,7 @@
 .\"
 .\" SPDX-License-Identifier: BSD-2-Clause
 .\"
-.Dd June 13, 2025
+.Dd February 4, 2026
 .Dt BECTL 8
 .Os
 .Sh NAME
@@ -33,6 +33,11 @@
 .Ar beName@snapshot
 .Nm
 .Op Fl r Ar beroot
+.Cm create
+.Fl E
+.Ar newBeName
+.Nm
+.Op Fl r Ar beroot
 .Cm destroy
 .Op Fl \&Fo
 .Ar beName Ns Op Cm @ Ns Ar snapshot
@@ -181,6 +186,13 @@ for a discussion on different layouts.
 .Pp
 No new boot environment is created with this subcommand.
 .It Xo
+.Cm create
+.Fl E
+.Ar newBeName
+.Xc
+Create a new empty boot environment named
+.Ar newBeName .
+.It Xo
 .Cm destroy
 .Op Fl \&Fo
 .Ar beName Ns Op Cm @ Ns Ar snapshot
diff --git a/sbin/bectl/bectl.c b/sbin/bectl/bectl.c
index 28483dae17b2..7f462984267f 100644
--- a/sbin/bectl/bectl.c
+++ b/sbin/bectl/bectl.c
@@ -53,6 +53,7 @@ usage(bool explicit)
            "\tbectl [-r beroot] check\n"
            "\tbectl [-r beroot] create [-r] [-e {nonActiveBe | 
beName@snapshot}] beName\n"
            "\tbectl [-r beroot] create [-r] beName@snapshot\n"
+           "\tbectl [-r beroot] create -E beName\n"
            "\tbectl [-r beroot] destroy [-Fo] {beName | beName@snapshot}\n"
            "\tbectl [-r beroot] export sourceBe\n"
            "\tbectl [-r beroot] import targetBe\n"
@@ -184,15 +185,19 @@ bectl_cmd_create(int argc, char *argv[])
        char snapshot[BE_MAXPATHLEN];
        char *atpos, *bootenv, *snapname;
        int err, opt;
-       bool recursive;
+       bool empty, recursive;
 
        snapname = NULL;
+       empty = false;
        recursive = false;
-       while ((opt = getopt(argc, argv, "e:r")) != -1) {
+       while ((opt = getopt(argc, argv, "e:Er")) != -1) {
                switch (opt) {
                case 'e':
                        snapname = optarg;
                        break;
+               case 'E':
+                       empty = true;
+                       break;
                case 'r':
                        recursive = true;
                        break;
@@ -221,6 +226,13 @@ bectl_cmd_create(int argc, char *argv[])
                 */
                *atpos++ = '\0';
                err = be_snapshot(be, bootenv, atpos, recursive, NULL);
+       } else if (empty) {
+               if (snapname || recursive) {
+                       fprintf(stderr,
+                           "bectl create: -E cannot be combined with -e or 
-r\n");
+                       return (usage(false));
+               }
+               err = be_create_empty(be, bootenv);
        } else {
                if (snapname == NULL)
                        /* Create from currently booted BE */
diff --git a/sbin/bectl/tests/bectl_test.sh b/sbin/bectl/tests/bectl_test.sh
index 8084b0a173f4..6aa94127aa36 100755
--- a/sbin/bectl/tests/bectl_test.sh
+++ b/sbin/bectl/tests/bectl_test.sh
@@ -126,6 +126,14 @@ bectl_create_body()
                zfs list "${zpool}/ROOT/recursive/usr"
        atf_check -o not-empty \
                zfs list "${zpool}/ROOT/recursive-snap/usr"
+
+       # Test creation of an empty BE.
+       atf_check bectl -r ${zpool}/ROOT create -E empty
+       atf_check -o inline:"-\n" zfs get -H -o value origin ${zpool}/ROOT/empty
+       atf_check -e match:"cannot be combined" -s not-exit:0 \
+               bectl -r ${zpool}/ROOT create -E -e not-allowed empty-existing
+       atf_check -e match:"cannot be combined" -s not-exit:0 \
+               bectl -r ${zpool}/ROOT create -E -r empty-recursive
 }
 bectl_create_cleanup()
 {

Reply via email to