From owner-svn-src-all@freebsd.org Mon Sep 21 09:01:15 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 9DAB83EB883; Mon, 21 Sep 2020 09:01:15 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bvz233ymJz3yVh; Mon, 21 Sep 2020 09:01:15 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 6AA2DBC46; Mon, 21 Sep 2020 09:01:15 +0000 (UTC) (envelope-from tsoome@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 08L91Fw0049575; Mon, 21 Sep 2020 09:01:15 GMT (envelope-from tsoome@FreeBSD.org) Received: (from tsoome@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 08L91AVe049551; Mon, 21 Sep 2020 09:01:10 GMT (envelope-from tsoome@FreeBSD.org) Message-Id: <202009210901.08L91AVe049551@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tsoome set sender to tsoome@FreeBSD.org using -f From: Toomas Soome Date: Mon, 21 Sep 2020 09:01:10 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r365938 - in head: . cddl/lib cddl/lib/libzfsbootenv cddl/lib/libzpool lib/libbe libexec/rc libexec/rc/rc.d rescue/rescue sbin/bectl sbin/reboot sbin/zfsbootcfg share/man/man5 share/mk ... X-SVN-Group: head X-SVN-Commit-Author: tsoome X-SVN-Commit-Paths: in head: . cddl/lib cddl/lib/libzfsbootenv cddl/lib/libzpool lib/libbe libexec/rc libexec/rc/rc.d rescue/rescue sbin/bectl sbin/reboot sbin/zfsbootcfg share/man/man5 share/mk stand stand/common stand/... X-SVN-Commit-Revision: 365938 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Sep 2020 09:01:15 -0000 Author: tsoome Date: Mon Sep 21 09:01:10 2020 New Revision: 365938 URL: https://svnweb.freebsd.org/changeset/base/365938 Log: loader: zfs should support bootonce an nextboot bootonce feature is temporary, one time boot, activated by "bectl activate -t BE", "bectl activate -T BE" will reset the bootonce flag. By default, the bootonce setting is reset on attempt to boot and the next boot will use previously active BE. By setting zfs_bootonce_activate="YES" in rc.conf, the bootonce BE will be set permanently active. bootonce dataset name is recorded in boot pool labels, bootenv area. in case of nextboot, the nextboot_enable boolean variable is recorded in freebsd:nvstore nvlist, also stored in boot pool label bootenv area. On boot, the loader will process /boot/nextboot.conf if nextboot_enable is "YES", and will set nextboot_enable to "NO", preventing /boot/nextboot.conf processing on next boot. bootonce and nextboot features are usable in both UEFI and BIOS boot. To use bootonce/nextboot features, the boot loader needs to be updated on disk; if loader.efi is stored on ESP, then ESP needs to be updated and for BIOS boot, stage2 (zfsboot or gptzfsboot) needs to be updated (gpart or other tools). At this time, only lua loader is updated. Sponsored by: Netflix, Klara Inc. Differential Revision: https://reviews.freebsd.org/D25512 Added: head/cddl/lib/libzfsbootenv/ head/cddl/lib/libzfsbootenv/Makefile (contents, props changed) head/stand/common/nvstore.c (contents, props changed) Replaced: head/sys/contrib/openzfs/lib/libzfsbootenv/.gitignore head/sys/contrib/openzfs/lib/libzfsbootenv/Makefile.am Modified: head/Makefile.inc1 head/cddl/lib/Makefile head/cddl/lib/libzpool/Makefile head/lib/libbe/Makefile head/lib/libbe/be.c head/lib/libbe/be.h head/lib/libbe/be_impl.h head/lib/libbe/be_info.c head/lib/libbe/libbe.3 head/libexec/rc/rc.conf head/libexec/rc/rc.d/zfsbe head/rescue/rescue/Makefile head/sbin/bectl/Makefile head/sbin/bectl/bectl.8 head/sbin/bectl/bectl.c head/sbin/bectl/bectl_list.c head/sbin/reboot/nextboot.8 head/sbin/reboot/nextboot.sh head/sbin/zfsbootcfg/Makefile head/sbin/zfsbootcfg/zfsbootcfg.8 head/sbin/zfsbootcfg/zfsbootcfg.c head/share/man/man5/rc.conf.5 head/share/mk/bsd.libnames.mk head/share/mk/src.libnames.mk head/stand/common/bootstrap.h head/stand/defaults/loader.conf head/stand/efi/boot1/Makefile head/stand/efi/boot1/zfs_module.c head/stand/efi/loader/Makefile head/stand/efi/loader/main.c head/stand/forth/support.4th head/stand/i386/gptzfsboot/Makefile head/stand/i386/loader/Makefile head/stand/i386/loader/main.c head/stand/i386/zfsboot/Makefile head/stand/i386/zfsboot/zfsboot.c head/stand/libsa/zfs/Makefile.inc head/stand/libsa/zfs/libzfs.h head/stand/libsa/zfs/nvlist.c head/stand/libsa/zfs/zfs.c head/stand/libsa/zfs/zfsimpl.c head/stand/loader.mk head/stand/lua/config.lua head/stand/userboot/test/test.c head/stand/userboot/userboot.h head/stand/userboot/userboot/Makefile head/stand/userboot/userboot/main.c head/stand/userboot/userboot/userboot_disk.c head/sys/cddl/boot/zfs/zfsimpl.h head/tools/tools/zfsboottest/zfsboottest.c Modified: head/Makefile.inc1 ============================================================================== --- head/Makefile.inc1 Mon Sep 21 08:16:42 2020 (r365937) +++ head/Makefile.inc1 Mon Sep 21 09:01:10 2020 (r365938) @@ -2835,7 +2835,7 @@ _prebuild_libs= ${_kerberos5_lib_libasn1} \ ${_cddl_lib_libtpool} \ ${_cddl_lib_libzfs_core} ${_cddl_lib_libzfs} \ ${_cddl_lib_libzutil} \ - ${_cddl_lib_libctf} \ + ${_cddl_lib_libctf} ${_cddl_lib_libzfsbootenv} \ lib/libufs \ lib/libutil lib/libpjdlog ${_lib_libypclnt} lib/libz lib/msun \ ${_secure_lib_libcrypto} ${_secure_lib_libssl} \ @@ -2915,6 +2915,7 @@ _cddl_lib_libtpool= cddl/lib/libtpool _cddl_lib_libzutil= cddl/lib/libzutil _cddl_lib_libzfs_core= cddl/lib/libzfs_core _cddl_lib_libzfs= cddl/lib/libzfs +_cddl_lib_libzfsbootenv= cddl/lib/libzfsbootenv cddl/lib/libtpool__L: cddl/lib/libspl__L @@ -2928,7 +2929,8 @@ cddl/lib/libzfs__L: cddl/lib/libuutil__L cddl/lib/liba cddl/lib/libzfs__L: cddl/lib/libnvpair__L cddl/lib/libzutil__L cddl/lib/libzfs__L: secure/lib/libcrypto__L -lib/libbe__L: cddl/lib/libzfs__L +cddl/lib/libzfsbootenv__L: cddl/lib/libzfs__L +lib/libbe__L: cddl/lib/libzfs__L cddl/lib/libzfsbootenv__L .endif _cddl_lib_libctf= cddl/lib/libctf _cddl_lib= cddl/lib Modified: head/cddl/lib/Makefile ============================================================================== --- head/cddl/lib/Makefile Mon Sep 21 08:16:42 2020 (r365937) +++ head/cddl/lib/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -15,6 +15,7 @@ SUBDIR= drti \ libuutil \ ${_libzfs_core} \ ${_libzfs} \ + ${_libzfsbootenv} \ ${_libzpool} \ ${_libzutil} @@ -26,6 +27,7 @@ _libicp= libicp _libicp_rescue= libicp_rescue _libzfs= libzfs _libzutil= libzutil +_libzfsbootenv= libzfsbootenv .if ${MK_LIBTHR} != "no" _libzpool= libzpool _libtpool= libtpool @@ -40,6 +42,7 @@ SUBDIR_DEPEND_libzfs_core= libnvpair SUBDIR_DEPEND_libzfs= libavl libnvpair libumem libuutil libzfs_core libzutil SUBDIR_DEPEND_libzpool= libavl libnvpair libumem libicp SUBDIR_DEPEND_libzutil= libavl libtpool +SUBDIR_DEPEND_libzfsbootenv= libzfs libnvpair SUBDIR_PARALLEL= Added: head/cddl/lib/libzfsbootenv/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/cddl/lib/libzfsbootenv/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -0,0 +1,33 @@ +# $FreeBSD$ + +.PATH: ${SRCTOP}/sys/contrib/openzfs/lib/libzfsbootenv +.PATH: ${SRCTOP}/sys/contrib/openzfs/include + +PACKAGE= runtime +LIB= zfsbootenv +SHLIB_MAJOR= 1 + +LIBADD= zfs +LIBADD+= nvpair + +INCS= libzfsbootenv.h +USER_C= \ + lzbe_device.c \ + lzbe_util.c \ + lzbe_pair.c + +SRCS= $(USER_C) + +CSTD= c99 +CFLAGS+= -DIN_BASE +CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include +CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/lib/libspl/include/ +CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/lib/libspl/include/os/freebsd +CFLAGS+= -I${SRCTOP}/cddl/compat/opensolaris/include +CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/module/icp/include +CFLAGS+= -include ${SRCTOP}/sys/contrib/openzfs/include/os/freebsd/spl/sys/ccompile.h +CFLAGS+= -DHAVE_ISSETUGID +CFLAGS+= -include ${SRCTOP}/sys/modules/zfs/zfs_config.h +CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include/os/freebsd/zfs + +.include Modified: head/cddl/lib/libzpool/Makefile ============================================================================== --- head/cddl/lib/libzpool/Makefile Mon Sep 21 08:16:42 2020 (r365937) +++ head/cddl/lib/libzpool/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -228,6 +228,7 @@ CFLAGS+= \ -I${ZFSTOP}/lib/libspl/include \ -I${ZFSTOP}/lib/libspl/include/os/freebsd \ -I${SRCTOP}/sys \ + -I${ZFSTOP}/include/os/freebsd/zfs \ -I${SRCTOP}/cddl/compat/opensolaris/include \ -I${ZFSTOP}/module/icp/include \ -include ${ZFSTOP}/include/os/freebsd/spl/sys/ccompile.h \ Modified: head/lib/libbe/Makefile ============================================================================== --- head/lib/libbe/Makefile Mon Sep 21 08:16:42 2020 (r365937) +++ head/lib/libbe/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -13,7 +13,9 @@ INCS= be.h MAN= libbe.3 LIBADD+= zfs -LIBADD+= nvpair spl +LIBADD+= nvpair +LIBADD+= spl +LIBADD+= zfsbootenv CFLAGS+= -DIN_BASE -DHAVE_RPC_TYPES CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include Modified: head/lib/libbe/be.c ============================================================================== --- head/lib/libbe/be.c Mon Sep 21 08:16:42 2020 (r365937) +++ head/lib/libbe/be.c Mon Sep 21 09:01:10 2020 (r365938) @@ -46,6 +46,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include "be.h" #include "be_impl.h" @@ -1221,43 +1222,20 @@ be_add_child(libbe_handle_t *lbh, const char *child_pa } #endif /* SOON */ -static int -be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid, - const char *zfsdev) -{ - nvlist_t **child; - uint64_t vdev_guid; - int c, children; - - if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child, - &children) == 0) { - for (c = 0; c < children; ++c) - if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0) - return (1); - return (0); - } - - if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, - &vdev_guid) != 0) { - return (1); - } - - if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) { - perror("ZFS_IOC_NEXTBOOT failed"); - return (1); - } - - return (0); -} - /* - * Deactivate old BE dataset; currently just sets canmount=noauto + * Deactivate old BE dataset; currently just sets canmount=noauto or + * resets boot once configuration. */ -static int -be_deactivate(libbe_handle_t *lbh, const char *ds) +int +be_deactivate(libbe_handle_t *lbh, const char *ds, bool temporary) { zfs_handle_t *zfs; + if (temporary) { + return (lzbe_set_boot_device( + zpool_get_name(lbh->active_phandle), lzbe_add, NULL)); + } + if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL) return (1); if (zfs_prop_set(zfs, "canmount", "noauto") != 0) @@ -1270,10 +1248,8 @@ int be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) { char be_path[BE_MAXPATHLEN]; - char buf[BE_MAXPATHLEN]; - nvlist_t *config, *dsprops, *vdevs; + nvlist_t *dsprops; char *origin; - uint64_t pool_guid; zfs_handle_t *zhp; int err; @@ -1284,27 +1260,10 @@ be_activate(libbe_handle_t *lbh, const char *bootenv, return (set_error(lbh, err)); if (temporary) { - config = zpool_get_config(lbh->active_phandle, NULL); - if (config == NULL) - /* config should be fetchable... */ - return (set_error(lbh, BE_ERR_UNKNOWN)); - - if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, - &pool_guid) != 0) - /* Similarly, it shouldn't be possible */ - return (set_error(lbh, BE_ERR_UNKNOWN)); - - /* Expected format according to zfsbootcfg(8) man */ - snprintf(buf, sizeof(buf), "zfs:%s:", be_path); - - /* We have no config tree */ - if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, - &vdevs) != 0) - return (set_error(lbh, BE_ERR_NOPOOL)); - - return (be_set_nextboot(lbh, vdevs, pool_guid, buf)); + return (lzbe_set_boot_device( + zpool_get_name(lbh->active_phandle), lzbe_add, be_path)); } else { - if (be_deactivate(lbh, lbh->bootfs) != 0) + if (be_deactivate(lbh, lbh->bootfs, false) != 0) return (-1); /* Obtain bootenv zpool */ Modified: head/lib/libbe/be.h ============================================================================== --- head/lib/libbe/be.h Mon Sep 21 08:16:42 2020 (r365937) +++ head/lib/libbe/be.h Mon Sep 21 09:01:10 2020 (r365938) @@ -81,6 +81,7 @@ int be_prop_list_alloc(nvlist_t **be_list); void be_prop_list_free(nvlist_t *be_list); int be_activate(libbe_handle_t *, const char *, bool); +int be_deactivate(libbe_handle_t *, const char *, bool); bool be_is_auto_snapshot_name(libbe_handle_t *, const char *); Modified: head/lib/libbe/be_impl.h ============================================================================== --- head/lib/libbe/be_impl.h Mon Sep 21 08:16:42 2020 (r365937) +++ head/lib/libbe/be_impl.h Mon Sep 21 09:01:10 2020 (r365938) @@ -63,6 +63,7 @@ typedef struct prop_data { nvlist_t *list; libbe_handle_t *lbh; bool single_object; /* list will contain props directly */ + char *bootonce; } prop_data_t; int prop_list_builder_cb(zfs_handle_t *, void *); Modified: head/lib/libbe/be_info.c ============================================================================== --- head/lib/libbe/be_info.c Mon Sep 21 08:16:42 2020 (r365937) +++ head/lib/libbe/be_info.c Mon Sep 21 09:01:10 2020 (r365938) @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include "be.h" #include "be_impl.h" @@ -108,6 +109,7 @@ be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *ds data.lbh = lbh; data.list = dsnvl; data.single_object = false; + data.bootonce = NULL; return (be_proplist_update(&data)); } @@ -121,6 +123,7 @@ be_get_dataset_props(libbe_handle_t *lbh, const char * data.lbh = lbh; data.list = props; data.single_object = true; + data.bootonce = NULL; if ((snap_hdl = zfs_open(lbh->lzh, name, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) return (BE_ERR_ZFSOPEN); @@ -140,6 +143,7 @@ be_get_dataset_snapshots(libbe_handle_t *lbh, const ch data.lbh = lbh; data.list = props; data.single_object = false; + data.bootonce = NULL; if ((ds_hdl = zfs_open(lbh->lzh, name, ZFS_TYPE_FILESYSTEM)) == NULL) return (BE_ERR_ZFSOPEN); @@ -179,6 +183,10 @@ prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data dataset = zfs_get_name(zfs_hdl); nvlist_add_string(props, "dataset", dataset); + if (data->bootonce != NULL && + strcmp(dataset, data->bootonce) == 0) + nvlist_add_boolean_value(props, "bootonce", true); + name = strrchr(dataset, '/') + 1; nvlist_add_string(props, "name", name); @@ -245,6 +253,9 @@ be_proplist_update(prop_data_t *data) if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root, ZFS_TYPE_FILESYSTEM)) == NULL) return (BE_ERR_ZFSOPEN); + + (void) lzbe_get_boot_device(zpool_get_name(data->lbh->active_phandle), + &data->bootonce); /* XXX TODO: some error checking here */ zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data); Modified: head/lib/libbe/libbe.3 ============================================================================== --- head/lib/libbe/libbe.3 Mon Sep 21 08:16:42 2020 (r365937) +++ head/lib/libbe/libbe.3 Mon Sep 21 09:01:10 2020 (r365938) @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October 16, 2019 +.Dd July 22, 2020 .Dt LIBBE 3 .Os .Sh NAME @@ -78,7 +78,11 @@ .Pp .Ft int .Fn be_activate "libbe_handle_t *hdl" "const char *be_name" "bool temporary" +.Pp .Ft int +.Fn be_deactivate "libbe_handle_t *hdl" "const char *be_name" "bool temporary" +.Pp +.Ft int .Fn be_destroy "libbe_handle_t *hdl" "const char *be_name" "int options" .Pp .Ft void @@ -270,8 +274,24 @@ If the .Fa temporary flag is set, then it will be active for the next boot only, as done by .Xr zfsbootcfg 8 . -Next boot functionality is currently only available when booting in x86 BIOS -mode. +.Pp +The +.Fn be_deactivate +function deactivates a boot environment. +If the +.Fa temporary +flag is set, then it will cause removal of boot once configuration, set by +.Fn be_activate +function or by +.Xr zfsbootcfg 8 . +If the +.Fa temporary +flag is not set, +.Fn be_deactivate +function will set zfs +.Dv canmount +property to +.Dv noauto . .Pp The .Fn be_destroy Modified: head/libexec/rc/rc.conf ============================================================================== --- head/libexec/rc/rc.conf Mon Sep 21 08:16:42 2020 (r365937) +++ head/libexec/rc/rc.conf Mon Sep 21 09:01:10 2020 (r365938) @@ -64,6 +64,7 @@ rc_conf_files="/etc/rc.conf /etc/rc.conf.local" # ZFS support zfs_enable="NO" # Set to YES to automatically mount ZFS file systems +zfs_bootonce_activate="NO" # Set YES to make successful bootonce BE permanent # ZFSD support zfsd_enable="NO" # Set to YES to automatically start the ZFS fault Modified: head/libexec/rc/rc.d/zfsbe ============================================================================== --- head/libexec/rc/rc.d/zfsbe Mon Sep 21 08:16:42 2020 (r365937) +++ head/libexec/rc/rc.d/zfsbe Mon Sep 21 09:01:10 2020 (r365938) @@ -48,6 +48,21 @@ mount_subordinate() done } +activate_bootonce() +{ + local _dev + local _bootonce + local _be + + _dev=$1 + _be=${_dev##*/} + + _bootonce="`kenv -q zfs-bootonce`" + if [ "$_bootonce" = "zfs:${_dev}:" ] ; then + bectl activate $_be + fi +} + be_start() { if [ `$SYSCTL_N security.jail.jailed` -eq 1 ]; then @@ -57,6 +72,9 @@ be_start() [ $_mp = "/" ] || continue if [ $_type = "zfs" ] ; then mount_subordinate $_dev + if checkyesno zfs_bootonce_activate; then + activate_bootonce $_dev + fi fi break done Modified: head/rescue/rescue/Makefile ============================================================================== --- head/rescue/rescue/Makefile Mon Sep 21 08:16:42 2020 (r365937) +++ head/rescue/rescue/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -129,7 +129,7 @@ CRUNCH_PROGS_usr.sbin+= zdb CRUNCH_LIBS+= -l80211 -lalias -lcam -lncursesw -ldevstat -lipsec -llzma .if ${MK_ZFS} != "no" CRUNCH_LIBS+= -lavl -lzpool -lzfs_core -lzfs -lnvpair -lpthread -luutil -lumem -CRUNCH_LIBS+= -lbe -lzutil -ltpool -lspl -licp_rescue +CRUNCH_LIBS+= -lbe -lzfsbootenv -lzutil -ltpool -lspl -licp_rescue .else # liblzma needs pthread CRUNCH_LIBS+= -lpthread Modified: head/sbin/bectl/Makefile ============================================================================== --- head/sbin/bectl/Makefile Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/bectl/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -12,6 +12,7 @@ LIBADD+= be \ nvpair \ spl \ util \ + zfsbootenv CFLAGS+= -DIN_BASE CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include Modified: head/sbin/bectl/bectl.8 ============================================================================== --- head/sbin/bectl/bectl.8 Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/bectl/bectl.8 Mon Sep 21 09:01:10 2020 (r365938) @@ -17,7 +17,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 17, 2020 +.Dd August 25, 2020 .Dt BECTL 8 .Os .Sh NAME @@ -26,7 +26,7 @@ .Sh SYNOPSIS .Nm .Cm activate -.Op Fl t +.Op Fl t | Fl T .Ar beName .Nm .Cm check @@ -95,7 +95,7 @@ The following commands are supported by .Bl -tag -width activate .It Xo .Cm activate -.Op Fl t +.Op Fl t | Fl T .Ar beName .Xc Activate the given @@ -104,6 +104,13 @@ as the default boot filesystem. If the .Fl t flag is given, this takes effect only for the next boot. +Flag +.Fl T +removes temporary boot once configuration. +Without temporary configuration, the next boot will use zfs dataset specified +in boot pool +.Ar bootfs +property. .It Xo .Cm check .Xc @@ -260,8 +267,10 @@ field indicates whether the boot environment is active .Pq Em \&N ; active on reboot .Pq Em \&R ; -or both -.Pq Em \&NR . +is used on next boot once +.Pq Em \&T ; +or combination of +.Pq Em \&NRT . .Pp .Bl -tag -width indent .It Fl a Modified: head/sbin/bectl/bectl.c ============================================================================== --- head/sbin/bectl/bectl.c Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/bectl/bectl.c Mon Sep 21 09:01:10 2020 (r365938) @@ -74,6 +74,7 @@ usage(bool explicit) "\tbectl add (path)*\n" #endif "\tbectl activate [-t] beName\n" + "\tbectl activate [-T]\n" "\tbectl check\n" "\tbectl create [-r] [-e {nonActiveBe | beName@snapshot}] beName\n" "\tbectl create [-r] beName@snapshot\n" @@ -141,14 +142,22 @@ static int bectl_cmd_activate(int argc, char *argv[]) { int err, opt; - bool temp; + bool temp, reset; temp = false; - while ((opt = getopt(argc, argv, "t")) != -1) { + reset = false; + while ((opt = getopt(argc, argv, "tT")) != -1) { switch (opt) { case 't': + if (reset) + return (usage(false)); temp = true; break; + case 'T': + if (temp) + return (usage(false)); + reset = true; + break; default: fprintf(stderr, "bectl activate: unknown option '-%c'\n", optopt); @@ -159,11 +168,18 @@ bectl_cmd_activate(int argc, char *argv[]) argc -= optind; argv += optind; - if (argc != 1) { + if (argc != 1 && (!reset || argc != 0)) { fprintf(stderr, "bectl activate: wrong number of arguments\n"); return (usage(false)); } + if (reset) { + if ((err = be_deactivate(be, NULL, reset)) == 0) + printf("Temporary activation removed\n"); + else + printf("Failed to remove temporary activation\n"); + return (err); + } /* activate logic goes here */ if ((err = be_activate(be, argv[0], temp)) != 0) Modified: head/sbin/bectl/bectl_list.c ============================================================================== --- head/sbin/bectl/bectl_list.c Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/bectl/bectl_list.c Mon Sep 21 09:01:10 2020 (r365938) @@ -182,7 +182,7 @@ print_info(const char *name, nvlist_t *dsprops, struct const char *oname; char *dsname, *propstr; int active_colsz; - boolean_t active_now, active_reboot; + boolean_t active_now, active_reboot, bootonce; dsname = NULL; originprops = NULL; @@ -228,6 +228,11 @@ print_info(const char *name, nvlist_t *dsprops, struct if (nvlist_lookup_boolean_value(dsprops, "nextboot", &active_reboot) == 0 && active_reboot) { printf("R"); + active_colsz--; + } + if (nvlist_lookup_boolean_value(dsprops, "bootonce", + &bootonce) == 0 && bootonce) { + printf("T"); active_colsz--; } if (active_colsz == pc->active_colsz_def) { Modified: head/sbin/reboot/nextboot.8 ============================================================================== --- head/sbin/reboot/nextboot.8 Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/reboot/nextboot.8 Mon Sep 21 09:01:10 2020 (r365938) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd April 9, 2016 +.Dd September 19, 2020 .Dt NEXTBOOT 8 .Os .Sh NAME @@ -136,8 +136,3 @@ It is also my first attempt to write in Forth. Finally, it does some evil things like writing to the file system before it has been checked. If it scrambles your file system, do not blame me. -.Pp -.Xr loader 8 -is only able to read ZFS, not write to it. -.Pa nextboot.conf -will NOT be reset in case of a kernel boot failure. Modified: head/sbin/reboot/nextboot.sh ============================================================================== --- head/sbin/reboot/nextboot.sh Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/reboot/nextboot.sh Mon Sep 21 09:01:10 2020 (r365938) @@ -33,6 +33,7 @@ delete="NO" kenv= force="NO" nextboot_file="/boot/nextboot.conf" +zfs= add_kenv() { @@ -106,26 +107,26 @@ if [ -n "${kernel}" -a ${force} = "NO" -a ! -d /boot/$ exit 1 fi -df -Tn "/boot/" 2>/dev/null | while read _fs _type _other ; do +zfs=$(df -Tn "/boot/" 2>/dev/null | while read _fs _type _other ; do [ "zfs" = "${_type}" ] || continue - cat 1>&2 <<-EOF - WARNING: loader(8) has only R/O support for ZFS - nextboot.conf will NOT be reset in case of kernel boot failure - EOF -done + echo "${_fs%/ROOT/*}" +done) set -e nextboot_tmp=$(mktemp $(dirname ${nextboot_file})/nextboot.XXXXXX) -if [ ${append} = "YES" -a -f ${nextboot_file} ]; then - cp -f ${nextboot_file} ${nextboot_tmp} -fi - +if [ -n ${zfs} ]; then + zfsbootcfg -z ${zfs} -n freebsd:nvstore -k nextboot_enable -v YES + cat >> ${nextboot_tmp} << EOF +$kenv +EOF +else cat >> ${nextboot_tmp} << EOF nextboot_enable="YES" $kenv EOF +fi fsync ${nextboot_tmp} Modified: head/sbin/zfsbootcfg/Makefile ============================================================================== --- head/sbin/zfsbootcfg/Makefile Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/zfsbootcfg/Makefile Mon Sep 21 09:01:10 2020 (r365938) @@ -2,14 +2,9 @@ # $FreeBSD$ PROG= zfsbootcfg -WARNS?= 2 MAN= zfsbootcfg.8 -LIBADD+=zfs -LIBADD+=nvpair -LIBADD+=umem -LIBADD+=uutil -LIBADD+=geom +LIBADD+=zfsbootenv CFLAGS+= -DIN_BASE CFLAGS+= -I${SRCTOP}/sys/contrib/openzfs/include Modified: head/sbin/zfsbootcfg/zfsbootcfg.8 ============================================================================== --- head/sbin/zfsbootcfg/zfsbootcfg.8 Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/zfsbootcfg/zfsbootcfg.8 Mon Sep 21 09:01:10 2020 (r365938) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd May 24, 2017 +.Dd July 22, 2020 .Dt ZFSBOOTCFG 8 .Os .Sh NAME @@ -33,39 +33,88 @@ .Sh SYNOPSIS .Nm .Ao Ar options Ac +.Nm +.Op Fl n Ar name +.Op Fl k Ar key +.Op Fl p +.Op Fl t Ar type +.Op Fl v Ar value +.Op Fl z Ar pool +.Nm .Sh DESCRIPTION .Nm is used to set .Xr boot.config 5 Ns -style options to be used by -.Xr zfsboot 8 -or +.Xr zfsboot 8 , .Xr gptzfsboot 8 +or +.Xr loader 8 the next time the machine is booted. Once .Xr zfsboot 8 or .Xr gptzfsboot 8 +or +.Xr loader 8 reads the information, it is deleted. If booting fails, the machine automatically reverts to the previous boot configuration. -The information is stored in a special reserved area of a ZFS pool. -.Xr zfsboot 8 -or -.Xr gptzfsboot 8 -read the boot option information from the first disk found in the first -ZFS pool found. +The information is stored in a special boot environment area of a ZFS pool. +.Pp +If used without arguments, +.Nm +will output the current boot configuration, if set. +.Pp +The following options are supported by +.Nm : +.Bl -tag -width indent +.It Fl k Ar key +Define key for +.Ao key , value Ac +pair. +.It Fl n Ar name +Update nvlist +.Ar name . +.It Fl p +Print all information stored in ZFS pool bootenv area. +.It Fl t Ar type +Set type of +.Ar value +used in +.Ao key , value Ac +pair. +Currently supported types are: +.Bl -tag -width indent -compact +.It Ar DATA_TYPE_BYTE +.It Ar DATA_TYPE_INT8 +.It Ar DATA_TYPE_UINT8 +.It Ar DATA_TYPE_INT16 +.It Ar DATA_TYPE_UINT16 +.It Ar DATA_TYPE_INT32 +.It Ar DATA_TYPE_UINT32 +.It Ar DATA_TYPE_INT64 +.It Ar DATA_TYPE_UINT64 +.It Ar DATA_TYPE_BOOLEAN_VALUE +.It Ar DATA_TYPE_STRING +.El +.Pp +If not specified, the default is +.Ar DATA_TYPE_STRING . +.It Fl v Ar value +Define value for +.Ao key , value Ac +pair. +.It Fl z Ar pool +Operate on +.Ar pool . +.El .Sh ENVIRONMENT -.Bl -tag -width vfs.zfs.boot.primary_pool -compact -.It Ev vfs.zfs.boot.primary_pool +.Bl -tag -width vfs.root.mountfrom -compact +.It Ev vfs.root.mountfrom The .Xr kenv 1 variable that identifies a pool for which the options are written. -.It Ev vfs.zfs.boot.primary_vdev -The -.Xr kenv 1 -variable that identifies a disk within the pool where the options -are written. .El .Sh EXAMPLES Try to boot to a new @@ -81,7 +130,9 @@ To clear the boot options: .Dl "zfsbootcfg """" .Sh SEE ALSO .Xr boot.config 5 , +.Xr bectl 8 , .Xr gptzfsboot 8 , +.Xr loader 8 , .Xr zfsboot 8 .Sh HISTORY .Nm @@ -90,23 +141,3 @@ appeared in .Sh AUTHORS This manual page was written by .An Andriy Gapon Aq Mt avg@FreeBSD.org . -.Sh CAVEATS -At the moment, -.Nm -uses the -.Ev vfs.zfs.boot.primary_pool -and -.Ev vfs.zfs.boot.primary_vdev -.Xr kenv 1 -variables to determine a ZFS pool and a disk in it where the options -are to be stored. -The variables are set by the ZFS boot chain, so there is an assumption -that the same boot disk is going to be used for the next reboot. -There is no -.Nm -option to specify a different pool or a different disk. -.Pp -.Nm -should be extended to install new -.Xr zfsboot 8 -blocks in a ZFS pool. Modified: head/sbin/zfsbootcfg/zfsbootcfg.c ============================================================================== --- head/sbin/zfsbootcfg/zfsbootcfg.c Mon Sep 21 08:16:42 2020 (r365937) +++ head/sbin/zfsbootcfg/zfsbootcfg.c Mon Sep 21 09:01:10 2020 (r365938) @@ -32,115 +32,257 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include +#include -#include +#include -/* Keep in sync with zfsboot.c. */ -#define MAX_COMMAND_LEN 512 +#ifndef ZFS_MAXNAMELEN +#define ZFS_MAXNAMELEN 256 +#endif -int -install_bootonce(libzfs_handle_t *hdl, uint64_t pool_guid, nvlist_t *nv, - const char * const data) +static int +add_pair(const char *name, const char *nvlist, const char *key, + const char *type, const char *value) { - nvlist_t **child; - uint_t children = 0; - uint64_t guid; + void *data, *nv; + size_t size; int rv; + char *end; - (void) nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, - &children); + rv = lzbe_nvlist_get(name, nvlist, &nv); + if (rv != 0) + return (rv); - for (int c = 0; c < children; c++) { - rv = install_bootonce(hdl, pool_guid, child[c], data); - } + data = NULL; + rv = EINVAL; + if (strcmp(type, "DATA_TYPE_STRING") == 0) { + data = __DECONST(void *, value); + size = strlen(data) + 1; + rv = lzbe_add_pair(nv, key, type, data, size); + } else if (strcmp(type, "DATA_TYPE_UINT64") == 0) { + uint64_t v; - if (children > 0) - return (rv); + v = strtoull(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_INT64") == 0) { + int64_t v; - if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) { - perror("can't get vdev guid"); - return (1); + v = strtoll(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_UINT32") == 0) { + uint32_t v; + + v = strtoul(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_INT32") == 0) { + int32_t v; + + v = strtol(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_UINT16") == 0) { + uint16_t v; + + v = strtoul(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_INT16") == 0) { + int16_t v; + + v = strtol(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_UINT8") == 0) { + uint8_t v; + + v = strtoul(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_INT8") == 0) { + int8_t v; + + v = strtol(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_BYTE") == 0) { + uint8_t v; + + v = strtoul(value, &end, 0); + if (errno != 0 || *end != '\0') + goto done; + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); + } else if (strcmp(type, "DATA_TYPE_BOOLEAN_VALUE") == 0) { + int32_t v; + + v = strtol(value, &end, 0); + if (errno != 0 || *end != '\0') { + if (strcasecmp(value, "YES") == 0) + v = 1; + else if (strcasecmp(value, "NO") == 0) + v = 0; + if (strcasecmp(value, "true") == 0) + v = 1; + else if (strcasecmp(value, "false") == 0) + v = 0; + else goto done; + } + size = sizeof (v); + rv = lzbe_add_pair(nv, key, type, &v, size); } - if (zpool_nextboot(hdl, pool_guid, guid, data) != 0) { - perror("ZFS_IOC_NEXTBOOT failed"); - return (1); - } - return (0); + + if (rv == 0) + rv = lzbe_nvlist_set(name, nvlist, nv); + +done: + lzbe_nvlist_free(nv); + return (rv); } -int main(int argc, const char * const *argv) +static int +delete_pair(const char *name, const char *nvlist, const char *key) { - char buf[32], *name; - libzfs_handle_t *hdl; - zpool_handle_t *zphdl; - uint64_t pool_guid; - nvlist_t *nv, *config; + void *nv; int rv; - int len; - if (argc != 2) { - fprintf(stderr, "usage: zfsbootcfg \n"); - return (1); + rv = lzbe_nvlist_get(name, nvlist, &nv); + if (rv == 0) { + rv = lzbe_remove_pair(nv, key); } + if (rv == 0) + rv = lzbe_nvlist_set(name, nvlist, nv); - len = strlen(argv[1]); - if (len >= MAX_COMMAND_LEN) { - fprintf(stderr, "options string is too long\n"); - return (1); - } + lzbe_nvlist_free(nv); + return (rv); +} - if (kenv(KENV_GET, "vfs.root.mountfrom", buf, sizeof(buf)) <= 0) { - perror("can't get vfs.root.mountfrom"); - return (1); +/* + * Usage: zfsbootcfg [-z pool] [-d key] [-k key -t type -v value] [-p] + * zfsbootcfg [-z pool] -n nvlist [-d key] [-k key -t type -v value] [-p] + * + * if nvlist is set, we will update nvlist in bootenv. + * if nvlist is not set, we update pairs in bootenv. + */ +int +main(int argc, char * const *argv) +{ + char buf[ZFS_MAXNAMELEN], *name; + const char *key, *value, *type, *nvlist; + int rv; + bool print, delete; + + nvlist = NULL; + name = NULL; + key = NULL; + type = NULL; + value = NULL; + print = delete = false; + while ((rv = getopt(argc, argv, "d:k:n:pt:v:z:")) != -1) { + switch (rv) { + case 'd': + delete = true; + key = optarg; + break; + case 'k': *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***