From owner-svn-src-all@freebsd.org Fri Jun 5 18:18:28 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 472AA333690; Fri, 5 Jun 2020 18:18:28 +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 49drVr116wz4TBy; Fri, 5 Jun 2020 18:18:28 +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 197D31502F; Fri, 5 Jun 2020 18:18:28 +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 055IIRsu085637; Fri, 5 Jun 2020 18:18:27 GMT (envelope-from tsoome@FreeBSD.org) Received: (from tsoome@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 055IIRB8085636; Fri, 5 Jun 2020 18:18:27 GMT (envelope-from tsoome@FreeBSD.org) Message-Id: <202006051818.055IIRB8085636@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tsoome set sender to tsoome@FreeBSD.org using -f From: Toomas Soome Date: Fri, 5 Jun 2020 18:18:27 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r361842 - head/sbin/zfsbootcfg X-SVN-Group: head X-SVN-Commit-Author: tsoome X-SVN-Commit-Paths: head/sbin/zfsbootcfg X-SVN-Commit-Revision: 361842 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: Fri, 05 Jun 2020 18:18:28 -0000 Author: tsoome Date: Fri Jun 5 18:18:27 2020 New Revision: 361842 URL: https://svnweb.freebsd.org/changeset/base/361842 Log: zfsbootcfg: use vfs.root.mountfrom and update nextboot on every vdev vfs.zfs.boot.primary_pool is only set on BIOS boot, use vfs.root.mountfrom instead and update all vdevs on pool. Reviewed by: allanjude Sponsored by: Netflix, Klara Inc. Differential Revision: https://reviews.freebsd.org/D25103 Modified: head/sbin/zfsbootcfg/zfsbootcfg.c Modified: head/sbin/zfsbootcfg/zfsbootcfg.c ============================================================================== --- head/sbin/zfsbootcfg/zfsbootcfg.c Fri Jun 5 18:00:36 2020 (r361841) +++ head/sbin/zfsbootcfg/zfsbootcfg.c Fri Jun 5 18:18:27 2020 (r361842) @@ -40,13 +40,44 @@ __FBSDID("$FreeBSD$"); /* Keep in sync with zfsboot.c. */ #define MAX_COMMAND_LEN 512 +int +install_bootonce(libzfs_handle_t *hdl, uint64_t pool_guid, nvlist_t *nv, + const char * const data) +{ + nvlist_t **child; + uint_t children = 0; + uint64_t guid; + int rv; + + (void) nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, &child, + &children); + + for (int c = 0; c < children; c++) { + rv = install_bootonce(hdl, pool_guid, child[c], data); + } + + if (children > 0) + return (rv); + + if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0) { + perror("can't get vdev guid"); + return (1); + } + if (zpool_nextboot(hdl, pool_guid, guid, data) != 0) { + perror("ZFS_IOC_NEXTBOOT failed"); + return (1); + } + return (0); +} + int main(int argc, const char * const *argv) { - char buf[32]; + char buf[32], *name; libzfs_handle_t *hdl; + zpool_handle_t *zphdl; uint64_t pool_guid; - uint64_t vdev_guid; - int zfs_fd; + nvlist_t *nv, *config; + int rv; int len; if (argc != 2) { @@ -60,39 +91,56 @@ int main(int argc, const char * const *argv) return (1); } - if (kenv(KENV_GET, "vfs.zfs.boot.primary_pool", buf, sizeof(buf)) <= 0) { - perror("can't get vfs.zfs.boot.primary_pool"); + if (kenv(KENV_GET, "vfs.root.mountfrom", buf, sizeof(buf)) <= 0) { + perror("can't get vfs.root.mountfrom"); return (1); } - pool_guid = strtoumax(buf, NULL, 10); - if (pool_guid == 0) { - perror("can't parse vfs.zfs.boot.primary_pool"); + + if (strncmp(buf, "zfs:", 4) == 0) { + name = strchr(buf + 4, '/'); + if (name != NULL) + *name = '\0'; + name = buf + 4; + } else { + perror("not a zfs root"); return (1); } - - if (kenv(KENV_GET, "vfs.zfs.boot.primary_vdev", buf, sizeof(buf)) <= 0) { - perror("can't get vfs.zfs.boot.primary_vdev"); + + if ((hdl = libzfs_init()) == NULL) { + (void) fprintf(stderr, "internal error: failed to " + "initialize ZFS library\n"); return (1); } - vdev_guid = strtoumax(buf, NULL, 10); - if (vdev_guid == 0) { - perror("can't parse vfs.zfs.boot.primary_vdev"); + + zphdl = zpool_open(hdl, name); + if (zphdl == NULL) { + perror("can't open pool"); + libzfs_fini(hdl); return (1); } - if ((hdl = libzfs_init()) == NULL) { - (void) fprintf(stderr, "internal error: failed to " - "initialize ZFS library\n"); + pool_guid = zpool_get_prop_int(zphdl, ZPOOL_PROP_GUID, NULL); + + config = zpool_get_config(zphdl, NULL); + if (config == NULL) { + perror("can't get pool config"); + zpool_close(zphdl); + libzfs_fini(hdl); return (1); } - if (zpool_nextboot(hdl, pool_guid, vdev_guid, argv[1]) != 0) { - perror("ZFS_IOC_NEXTBOOT failed"); + if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) != 0) { + perror("failed to get vdev tree"); + zpool_close(zphdl); libzfs_fini(hdl); return (1); } + rv = install_bootonce(hdl, pool_guid, nv, argv[1]); + + zpool_close(zphdl); libzfs_fini(hdl); - printf("zfs next boot options are successfully written\n"); - return (0); + if (rv == 0) + printf("zfs next boot options are successfully written\n"); + return (rv); }