From owner-svn-src-projects@freebsd.org Wed Jul 25 03:30:02 2018 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 63260103C77E for ; Wed, 25 Jul 2018 03:30:02 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 161EB8CD51; Wed, 25 Jul 2018 03:30:02 +0000 (UTC) (envelope-from kevans@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 EB76014C3E; Wed, 25 Jul 2018 03:30:01 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w6P3U1p5074117; Wed, 25 Jul 2018 03:30:01 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w6P3U1Wi074116; Wed, 25 Jul 2018 03:30:01 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201807250330.w6P3U1Wi074116@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Wed, 25 Jul 2018 03:30:01 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r336699 - projects/bectl/lib/libbe X-SVN-Group: projects X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: projects/bectl/lib/libbe X-SVN-Commit-Revision: 336699 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Jul 2018 03:30:02 -0000 Author: kevans Date: Wed Jul 25 03:30:01 2018 New Revision: 336699 URL: https://svnweb.freebsd.org/changeset/base/336699 Log: libbe(3): Find rootfs instead by enumerating child datasets of BE root This makes us more resilient to a rename of the bootfs, but still wouldn't withstand pool renames or guid renames. More importantly, this allows `bectl create ` work out of the box to create a boot environment based on the currently booted one. Modified: projects/bectl/lib/libbe/be.c Modified: projects/bectl/lib/libbe/be.c ============================================================================== --- projects/bectl/lib/libbe/be.c Wed Jul 25 03:29:29 2018 (r336698) +++ projects/bectl/lib/libbe/be.c Wed Jul 25 03:30:01 2018 (r336699) @@ -42,22 +42,46 @@ #include "be_impl.h" /* + * Iterator function for locating the rootfs amongst the children of the + * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. + */ +static int +be_locate_rootfs(zfs_handle_t *chkds, void *data) +{ + libbe_handle_t *lbh; + char *mntpoint; + + lbh = (libbe_handle_t *)data; + if (lbh == NULL) + return (1); + + if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) { + strncpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN); + return (1); + } + + return (0); +} + +/* * Initializes the libbe context to operate in the root boot environment * dataset, for example, zroot/ROOT. */ libbe_handle_t * libbe_init(void) { - char buf[BE_MAXPATHLEN]; struct stat sb; dev_t root_dev, boot_dev; libbe_handle_t *lbh; + zfs_handle_t *rootds; char *poolname, *pos; int pnamelen; lbh = NULL; poolname = pos = NULL; pnamelen = 0; + rootds = NULL; + /* Verify that /boot and / are mounted on the same filesystem */ /* TODO: use errno here?? */ if (stat("/", &sb) != 0) @@ -109,13 +133,11 @@ libbe_init(void) /* Obtain path to boot environment rootfs (currently booted) */ /* XXX Get dataset mounted at / by kenv/GUID from mountroot? */ - if ((kenv(KENV_GET, "zfs_be_active", lbh->rootfs, BE_MAXPATHLEN)) == -1) + if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL) goto err; - /* Remove leading 'zfs:' if present, otherwise use value as-is */ - if (strcmp(lbh->rootfs, "zfs:") == 0) - strncpy(lbh->rootfs, strchr(lbh->rootfs, ':') + sizeof(char), - BE_MAXPATHLEN); + zfs_iter_filesystems(rootds, be_locate_rootfs, lbh); + zfs_close(rootds); return (lbh); err: @@ -126,6 +148,8 @@ err: libzfs_fini(lbh->lzh); free(lbh); } + if (rootds != NULL) + zfs_close(rootds); free(poolname); return (NULL); }