From 666edc60b81fe60fa0a0262148dee78aabae6552 Mon Sep 17 00:00:00 2001 From: pjd Date: Sat, 19 Nov 2011 11:47:15 +0000 Subject: [PATCH] MFC r226549,r226550,r226551,r226552,r226553,r226554,r226568,r226569,r226611, r226612: r226549: Remove redundant size calculation. r226550: Initialize 'rc' properly before using it. This error could lead to infinite loop when data reconstruction was needed. r226551: Don't mark vdev as healthy too soon, so we won't try to use invalid vdevs. r226552: Never pass NULL block pointer when reading. This is neither expected nor handled by lower layers like vdev_raidz, which uses bp for checksum verification. This bug could lead to NULL pointer reference and resets during boot. r226553: Always pass data size for checksum verification function, as using physical block size declared in bp may not always be what we want. For example in case of gang block header physical block size declared in bp is much larger than SPA_GANGBLOCKSIZE (512 bytes) and checksum calculation failed. This bug could lead to accessing unallocated memory and resets/failures during boot. r226554: Fix missing return when LOADER_GPT_SUPPORT is defined, but LOADER_MBR_SUPPORT is not. r226568: - Correctly read gang header from raidz. - Decompress assembled gang block data if compressed. - Verify checksum of a gang header. - Verify checksum of assembled gang block data. - Verify checksum of uber block. Submitted by: avg r226569: With LOADER_MBR_SUPPORT defined and LOADER_GPT_SUPPORT undefined we would never call disk_openmbr(). Submitted by: avg r226611: - Allow to specify multiple files to check, eg. zfsboottest gpt/system0 gpt/system1 - /boot/kernel/kernel /boot/zfsloader - Instead of printing file's content calculate MD5 hash of the file, so it can be easly compared to the hash calculated via file system. - Some other minor improvements. r226612: Because ZFS boot code was very fragile in the past and real PITA to debug, introduce zfsboottest.sh script that will verify if it will be possible to boot from the given pool. # zfsboottest.sh system Where "system" is pool name of the pool we want to boot from. What is being verified by the script: - Does the pool exist? - Does it have bootfs property configured? - Is mountpoint property of the boot dataset set to 'legacy'? Dataset configured in bootfs property has to be mounted to perform more checks: - Does the /boot directory in boot dataset exist? - Is this dataset configured as root file system in /etc/fstab or set in vfs.root.mountfrom variable in /boot/loader.conf? By using zfsboottest tool the script will read all the files in /boot directory using ZFS boot code and calculate their checksums. Then, it will walk /boot directory using find(1) though regular file sytem and also read all the files in /boot directory and calculate their checksums. If any of the files cannot be looked up, read or checksum is invalid it will be reported and booting off of this pool is probably not possible. Some additional checks may be interesting as well. For example if the disks contain proper pmbr and gptzfsboot code or if all expected files in /boot/ are present. When upgrading FreeBSD, one should snapshot datasets that contain operating system, upgrade (install new world and kernel) and use zfsboottest.sh to verify if it will be possible to boot from new configuration. If all is good one should upgrade boot blocks, by eg.: # gpart -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada1 If something is wrong, one should rollback datasets and report the problems. Approved by: re (kib) git-svn-id: svn://svn.freebsd.org/base/releng/9.0@227710 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/boot/common/disk.c | 5 +- sys/boot/zfs/zfsimpl.c | 104 ++++++++++---------- sys/cddl/boot/zfs/zfssubr.c | 38 ++++---- tools/tools/zfsboottest/Makefile | 6 ++ tools/tools/zfsboottest/zfsboottest.c | 103 +++++++++++++------- tools/tools/zfsboottest/zfsboottest.sh | 130 +++++++++++++++++++++++++ 6 files changed, 278 insertions(+), 108 deletions(-) create mode 100755 tools/tools/zfsboottest/zfsboottest.sh diff --git a/sys/boot/common/disk.c b/sys/boot/common/disk.c index e1de3821..81902c0f 100644 --- a/sys/boot/common/disk.c +++ b/sys/boot/common/disk.c @@ -776,10 +776,11 @@ disk_open(struct disk_devdesc *dev) #ifdef LOADER_GPT_SUPPORT rc = disk_opengpt(dev); - if (rc) + if (rc == 0) + return (0); #endif #ifdef LOADER_MBR_SUPPORT - rc = disk_openmbr(dev); + rc = disk_openmbr(dev); #endif return (rc); diff --git a/sys/boot/zfs/zfsimpl.c b/sys/boot/zfs/zfsimpl.c index 497667a8..c97c2dee 100644 --- a/sys/boot/zfs/zfsimpl.c +++ b/sys/boot/zfs/zfsimpl.c @@ -347,7 +347,7 @@ vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf, rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize); if (rc) return (rc); - if (bp && zio_checksum_error(bp, buf, offset)) + if (bp && zio_checksum_verify(bp, buf)) return (EIO); return (0); @@ -543,8 +543,6 @@ vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev, vdev->v_state = VDEV_STATE_DEGRADED; else if (isnt_present) vdev->v_state = VDEV_STATE_CANT_OPEN; - else - vdev->v_state = VDEV_STATE_HEALTHY; } rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, @@ -800,6 +798,7 @@ vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap) BP_SET_PSIZE(&bp, sizeof(vdev_phys_t)); BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL); BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); + DVA_SET_OFFSET(BP_IDENTITY(&bp), off); ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0); if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0)) return (EIO); @@ -912,6 +911,7 @@ vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap) if (vdev) { vdev->v_phys_read = read; vdev->v_read_priv = read_priv; + vdev->v_state = VDEV_STATE_HEALTHY; } else { printf("ZFS: inconsistent nvlist contents\n"); return (EIO); @@ -941,7 +941,7 @@ vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap) BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0); - if (vdev_read_phys(vdev, NULL, upbuf, off, VDEV_UBERBLOCK_SIZE(vdev))) + if (vdev_read_phys(vdev, &bp, upbuf, off, 0)) continue; if (up->ub_magic != UBERBLOCK_MAGIC) @@ -974,34 +974,39 @@ ilog2(int n) } static int -zio_read_gang(spa_t *spa, const blkptr_t *bp, const dva_t *dva, void *buf) +zio_read_gang(spa_t *spa, const blkptr_t *bp, void *buf) { + blkptr_t gbh_bp; zio_gbh_phys_t zio_gb; - vdev_t *vdev; - int vdevid; - off_t offset; + char *pbuf; int i; - vdevid = DVA_GET_VDEV(dva); - offset = DVA_GET_OFFSET(dva); - STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) - if (vdev->v_id == vdevid) - break; - if (!vdev || !vdev->v_read) - return (EIO); - if (vdev->v_read(vdev, NULL, &zio_gb, offset, SPA_GANGBLOCKSIZE)) + /* Artificial BP for gang block header. */ + gbh_bp = *bp; + BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE); + BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE); + BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER); + BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF); + for (i = 0; i < SPA_DVAS_PER_BP; i++) + DVA_SET_GANG(&gbh_bp.blk_dva[i], 0); + + /* Read gang header block using the artificial BP. */ + if (zio_read(spa, &gbh_bp, &zio_gb)) return (EIO); + pbuf = buf; for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { blkptr_t *gbp = &zio_gb.zg_blkptr[i]; if (BP_IS_HOLE(gbp)) continue; - if (zio_read(spa, gbp, buf)) + if (zio_read(spa, gbp, pbuf)) return (EIO); - buf = (char*)buf + BP_GET_PSIZE(gbp); + pbuf += BP_GET_PSIZE(gbp); } - + + if (zio_checksum_verify(bp, buf)) + return (EIO); return (0); } @@ -1024,46 +1029,41 @@ zio_read(spa_t *spa, const blkptr_t *bp, void *buf) if (!dva->dva_word[0] && !dva->dva_word[1]) continue; - if (DVA_GET_GANG(dva)) { - error = zio_read_gang(spa, bp, dva, buf); - if (error != 0) - continue; - } else { - vdevid = DVA_GET_VDEV(dva); - offset = DVA_GET_OFFSET(dva); - STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) { - if (vdev->v_id == vdevid) - break; - } - if (!vdev || !vdev->v_read) - continue; + vdevid = DVA_GET_VDEV(dva); + offset = DVA_GET_OFFSET(dva); + STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) { + if (vdev->v_id == vdevid) + break; + } + if (!vdev || !vdev->v_read) + continue; - size = BP_GET_PSIZE(bp); + size = BP_GET_PSIZE(bp); + if (vdev->v_read == vdev_raidz_read) { align = 1ULL << vdev->v_top->v_ashift; if (P2PHASE(size, align) != 0) size = P2ROUNDUP(size, align); - if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF) - pbuf = zfs_alloc(size); - else - pbuf = buf; + } + if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF) + pbuf = zfs_alloc(size); + else + pbuf = buf; + if (DVA_GET_GANG(dva)) + error = zio_read_gang(spa, bp, pbuf); + else error = vdev->v_read(vdev, bp, pbuf, offset, size); - if (error == 0) { - if (cpfunc != ZIO_COMPRESS_OFF) { - error = zio_decompress_data(cpfunc, - pbuf, BP_GET_PSIZE(bp), buf, - BP_GET_LSIZE(bp)); - } else if (size != BP_GET_PSIZE(bp)) { - bcopy(pbuf, buf, BP_GET_PSIZE(bp)); - } - } - if (buf != pbuf) - zfs_free(pbuf, size); - if (error != 0) - continue; + if (error == 0) { + if (cpfunc != ZIO_COMPRESS_OFF) + error = zio_decompress_data(cpfunc, pbuf, + BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp)); + else if (size != BP_GET_PSIZE(bp)) + bcopy(pbuf, buf, BP_GET_PSIZE(bp)); } - error = 0; - break; + if (buf != pbuf) + zfs_free(pbuf, size); + if (error == 0) + break; } if (error != 0) printf("ZFS: i/o error - all block copies unavailable\n"); diff --git a/sys/cddl/boot/zfs/zfssubr.c b/sys/cddl/boot/zfs/zfssubr.c index e76c2738..b2d2e13d 100644 --- a/sys/cddl/boot/zfs/zfssubr.c +++ b/sys/cddl/boot/zfs/zfssubr.c @@ -181,14 +181,17 @@ zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset) } static int -zio_checksum_error(const blkptr_t *bp, void *data, uint64_t offset) +zio_checksum_verify(const blkptr_t *bp, void *data) { - unsigned int checksum = BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp); - uint64_t size = BP_GET_PSIZE(bp); + uint64_t size; + unsigned int checksum; zio_checksum_info_t *ci; zio_cksum_t actual_cksum, expected_cksum, verifier; int byteswap; + checksum = BP_GET_CHECKSUM(bp); + size = BP_GET_PSIZE(bp); + if (checksum >= ZIO_CHECKSUM_FUNCTIONS) return (EINVAL); ci = &zio_checksum_table[checksum]; @@ -206,7 +209,8 @@ zio_checksum_error(const blkptr_t *bp, void *data, uint64_t offset) if (checksum == ZIO_CHECKSUM_GANG_HEADER) zio_checksum_gang_verifier(&verifier, bp); else if (checksum == ZIO_CHECKSUM_LABEL) - zio_checksum_label_verifier(&verifier, offset); + zio_checksum_label_verifier(&verifier, + DVA_GET_OFFSET(BP_IDENTITY(bp))); else verifier = bp->blk_cksum; @@ -224,7 +228,6 @@ zio_checksum_error(const blkptr_t *bp, void *data, uint64_t offset) byteswap_uint64_array(&expected_cksum, sizeof (zio_cksum_t)); } else { - ASSERT(!BP_IS_GANG(bp)); expected_cksum = bp->blk_cksum; ci->ci_func[0](data, size, &actual_cksum); } @@ -1215,15 +1218,10 @@ static void vdev_raidz_map_free(raidz_map_t *rm) { int c; - size_t size; for (c = rm->rm_firstdatacol - 1; c >= 0; c--) zfs_free(rm->rm_col[c].rc_data, rm->rm_col[c].rc_size); - size = 0; - for (c = rm->rm_firstdatacol; c < rm->rm_cols; c++) - size += rm->rm_col[c].rc_size; - zfs_free(rm, offsetof(raidz_map_t, rm_col[rm->rm_scols])); } @@ -1245,10 +1243,10 @@ vdev_child(vdev_t *pvd, uint64_t devidx) * any ereports we generate can note it. */ static int -raidz_checksum_verify(const blkptr_t *bp, void *data) +raidz_checksum_verify(const blkptr_t *bp, void *data, uint64_t size) { - return (zio_checksum_error(bp, data, 0)); + return (zio_checksum_verify(bp, data)); } /* @@ -1298,7 +1296,7 @@ raidz_parity_verify(raidz_map_t *rm) */ static int vdev_raidz_combrec(raidz_map_t *rm, const blkptr_t *bp, void *data, - off_t offset, int total_errors, int data_errors) + off_t offset, uint64_t bytes, int total_errors, int data_errors) { raidz_col_t *rc; void *orig[VDEV_RAIDZ_MAXPARITY]; @@ -1377,7 +1375,7 @@ vdev_raidz_combrec(raidz_map_t *rm, const blkptr_t *bp, void *data, * success. */ code = vdev_raidz_reconstruct(rm, tgts, n); - if (raidz_checksum_verify(bp, data) == 0) { + if (raidz_checksum_verify(bp, data, bytes) == 0) { for (i = 0; i < n; i++) { c = tgts[i]; rc = &rm->rm_col[c]; @@ -1548,7 +1546,7 @@ reconstruct: */ if (total_errors <= rm->rm_firstdatacol - parity_untried) { if (data_errors == 0) { - if (raidz_checksum_verify(bp, data) == 0) { + if (raidz_checksum_verify(bp, data, bytes) == 0) { /* * If we read parity information (unnecessarily * as it happens since no reconstruction was @@ -1593,7 +1591,7 @@ reconstruct: code = vdev_raidz_reconstruct(rm, tgts, n); - if (raidz_checksum_verify(bp, data) == 0) { + if (raidz_checksum_verify(bp, data, bytes) == 0) { /* * If we read more parity disks than were used * for reconstruction, confirm that the other @@ -1633,7 +1631,9 @@ reconstruct: n = 0; for (c = 0; c < rm->rm_cols; c++) { - if (rm->rm_col[c].rc_tried) + rc = &rm->rm_col[c]; + + if (rc->rc_tried) continue; cvd = vdev_child(vd, rc->rc_devidx); @@ -1665,8 +1665,8 @@ reconstruct: if (total_errors > rm->rm_firstdatacol) { error = EIO; } else if (total_errors < rm->rm_firstdatacol && - (code = vdev_raidz_combrec(rm, bp, data, offset, total_errors, - data_errors)) != 0) { + (code = vdev_raidz_combrec(rm, bp, data, offset, bytes, + total_errors, data_errors)) != 0) { /* * If we didn't use all the available parity for the * combinatorial reconstruction, verify that the remaining diff --git a/tools/tools/zfsboottest/Makefile b/tools/tools/zfsboottest/Makefile index 2a6f54d3..c0c7d92c 100644 --- a/tools/tools/zfsboottest/Makefile +++ b/tools/tools/zfsboottest/Makefile @@ -2,7 +2,12 @@ .PATH: ${.CURDIR}/../../../sys/boot/zfs ${.CURDIR}/../../../sys/cddl/boot/zfs +BINDIR?= /usr/bin +SCRIPTSDIR?= /usr/bin + PROG= zfsboottest +SCRIPTS= zfsboottest.sh +SCRIPTSNAME= zfsboottest.sh NO_MAN= CFLAGS= -O1 \ @@ -12,6 +17,7 @@ CFLAGS= -O1 \ -fdiagnostics-show-option \ -W -Wextra -Wno-sign-compare -Wno-unused-parameter \ -Werror +LDFLAGS+=-lmd .if ${MACHINE_CPUARCH} == "amd64" beforedepend zfsboottest.o: machine diff --git a/tools/tools/zfsboottest/zfsboottest.c b/tools/tools/zfsboottest/zfsboottest.c index 78a67bcf..4b29f949 100644 --- a/tools/tools/zfsboottest/zfsboottest.c +++ b/tools/tools/zfsboottest/zfsboottest.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2010 Doug Rabson * Copyright (c) 2011 Andriy Gapon + * Copyright (c) 2011 Pawel Jakub Dawidek * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,8 +29,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -43,6 +46,7 @@ void pager_output(const char *line) { + fprintf(stderr, "%s", line); } @@ -54,7 +58,7 @@ pager_output(const char *line) static int vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes) { - int fd = *(int *) priv; + int fd = *(int *)priv; if (pread(fd, buf, bytes, off) != bytes) return (-1); @@ -73,7 +77,7 @@ zfs_read(spa_t *spa, dnode_phys_t *dn, void *buf, size_t size, off_t off) n = zp->zp_size - off; rc = dnode_read(spa, dn, off, buf, n); - if (rc) + if (rc != 0) return (-rc); return (n); @@ -82,31 +86,49 @@ zfs_read(spa_t *spa, dnode_phys_t *dn, void *buf, size_t size, off_t off) int main(int argc, char** argv) { - char buf[512]; - int fd[100]; + char buf[512], hash[33]; + MD5_CTX ctx; struct stat sb; dnode_phys_t dn; spa_t *spa; off_t off; ssize_t n; - int i; + int i, failures, *fd; zfs_init(); if (argc == 1) { static char *av[] = { - "zfstest", "COPYRIGHT", - "/dev/da0p2", "/dev/da1p2", "/dev/da2p2", + "zfsboottest", + "/dev/gpt/system0", + "/dev/gpt/system1", + "-", + "/boot/zfsloader", + "/boot/support.4th", + "/boot/kernel/kernel", NULL, }; - argc = 5; + argc = sizeof(av) / sizeof(av[0]) - 1; argv = av; } - for (i = 2; i < argc; i++) { - fd[i] = open(argv[i], O_RDONLY); - if (fd[i] < 0) + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-") == 0) + break; + } + fd = malloc(sizeof(fd[0]) * (i - 1)); + if (fd == NULL) + errx(1, "Unable to allocate memory."); + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-") == 0) + break; + fd[i - 1] = open(argv[i], O_RDONLY); + if (fd[i - 1] == -1) { + warn("open(%s) failed", argv[i]); continue; - if (vdev_probe(vdev_read, &fd[i], NULL) != 0) - close(fd[i]); + } + if (vdev_probe(vdev_read, &fd[i - 1], NULL) != 0) { + warnx("vdev_probe(%s) failed", argv[i]); + close(fd[i - 1]); + } } spa_all_status(); @@ -121,29 +143,40 @@ main(int argc, char** argv) exit(1); } - if (zfs_lookup(spa, argv[1], &dn)) { - fprintf(stderr, "can't lookup\n"); - exit(1); - } - - if (zfs_dnode_stat(spa, &dn, &sb)) { - fprintf(stderr, "can't stat\n"); - exit(1); - } - + printf("\n"); + for (++i, failures = 0; i < argc; i++) { + if (zfs_lookup(spa, argv[i], &dn)) { + fprintf(stderr, "%s: can't lookup\n", argv[i]); + failures++; + continue; + } - off = 0; - do { - n = sb.st_size - off; - n = n > sizeof(buf) ? sizeof(buf) : n; - n = zfs_read(spa, &dn, buf, n, off); - if (n < 0) { - fprintf(stderr, "zfs_read failed\n"); - exit(1); + if (zfs_dnode_stat(spa, &dn, &sb)) { + fprintf(stderr, "%s: can't stat\n", argv[i]); + failures++; + continue; } - write(1, buf, n); - off += n; - } while (off < sb.st_size); - return (0); + off = 0; + MD5Init(&ctx); + do { + n = sb.st_size - off; + n = n > sizeof(buf) ? sizeof(buf) : n; + n = zfs_read(spa, &dn, buf, n, off); + if (n < 0) { + fprintf(stderr, "%s: zfs_read failed\n", + argv[i]); + failures++; + break; + } + MD5Update(&ctx, buf, n); + off += n; + } while (off < sb.st_size); + if (off < sb.st_size) + continue; + MD5End(&ctx, hash); + printf("%s %s\n", hash, argv[i]); + } + + return (failures == 0 ? 0 : 1); } diff --git a/tools/tools/zfsboottest/zfsboottest.sh b/tools/tools/zfsboottest/zfsboottest.sh new file mode 100755 index 00000000..e1bcf8be --- /dev/null +++ b/tools/tools/zfsboottest/zfsboottest.sh @@ -0,0 +1,130 @@ +#!/bin/sh +# +# Copyright (c) 2011 Pawel Jakub Dawidek +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ + +if [ $# -ne 1 ]; then + echo "usage: zfsboottest.sh " >&2 + exit 1 +fi + +which -s zfsboottest +if [ $? -eq 0 ]; then + zfsboottest="zfsboottest" +else + if [ ! -x "/usr/src/tools/tools/zfsboottest/zfsboottest" ]; then + echo "Unable to find \"zfsboottest\" utility." >&2 + exit 1 + fi + zfsboottest="/usr/src/tools/tools/zfsboottest/zfsboottest" +fi + +startdir="/boot" + +pool="${1}" +zpool list "${pool}" >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "No such pool \"${pool}\"." >&2 + exit 1 +fi +bootfs=`zpool get bootfs "${pool}" | tail -1 | awk '{print $3}'` +if [ "${bootfs}" = "-" ]; then + echo "The \"bootfs\" property is not configured for pool \"${pool}\"." >&2 + exit 1 +fi +# Dataset's mountpoint property should be set to 'legacy'. +if [ "`zfs get -H -o value mountpoint ${bootfs}`" != "legacy" ]; then + echo "The \"mountpoint\" property of dataset \"${bootfs}\" should be set to \"legacy\"." >&2 + exit 1 +fi +mountpoint=`df -t zfs "${bootfs}" 2>/dev/null | tail -1 | awk '{print $6}'` +if [ -z "${mountpoint}" ]; then + echo "The \"${bootfs}\" dataset is not mounted." >&2 + exit 1 +fi +if [ ! -d "${mountpoint}${startdir}" ]; then + echo "The \"${mountpoint}${startdir}\" directory doesn't exist." >&2 + exit 1 +fi +# To be able to mount root ZFS file system we need either /etc/fstab entry +# or vfs.root.mountfrom variable set in /boot/loader.conf. +egrep -q '^'"${bootfs}"'[[:space:]]+/[[:space:]]+zfs[[:space:]]+' "${mountpoint}/etc/fstab" 2>/dev/null +if [ $? -ne 0 ]; then + egrep -q 'vfs.root.mountfrom="?'"${bootfs}"'"?[[:space:]]*$' "${mountpoint}/boot/loader.conf" 2>/dev/null + if [ $? -ne 0 ]; then + echo "To be able to boot from \"${bootfs}\", you need to declare" >&2 + echo "\"${bootfs}\" as being root file system in ${mountpoint}/etc/fstab" >&2 + echo "or add \"vfs.root.mountfrom\" variable set to \"${bootfs}\" to" >&2 + echo "${mountpoint}/boot/loader.conf." >&2 + exit 1 + fi +fi +vdevs="" +for vdev in `zpool status "${pool}" | grep ONLINE | awk '{print $1}'`; do + vdev="/dev/${vdev#/dev/}" + if [ -c "${vdev}" ]; then + if [ -z "${vdevs}" ]; then + vdevs="${vdev}" + else + vdevs="${vdevs} ${vdev}" + fi + fi +done + +list0=`mktemp /tmp/zfsboottest.XXXXXXXXXX` +if [ $? -ne 0 ]; then + echo "Unable to create temporary file." >&2 + exit 1 +fi +list1=`mktemp /tmp/zfsboottest.XXXXXXXXXX` +if [ $? -ne 0 ]; then + echo "Unable to create temporary file." >&2 + rm -f "${list0}" + exit 1 +fi + +echo "zfsboottest.sh is reading all the files in ${mountpoint}${startdir} using" +echo "boot code and using file system code." +echo "It calculates MD5 checksums for all the files and will compare them." +echo "If all files can be properly read using boot code, it is very likely you" +echo "will be able to boot from \"${pool}\" pool>:> Good luck!" +echo + +"${zfsboottest}" ${vdevs} - `find "${mountpoint}${startdir}" -type f | sed "s@^${mountpoint}@@"` | egrep '^[0-9a-z]{32} /' | sort -k 2 >"${list0}" +find "${mountpoint}${startdir}" -type f | xargs md5 -r | sed "s@ ${mountpoint}@ @" | egrep '^[0-9a-z]{32} /' | sort -k 2 >"${list1}" + +diff -u "${list0}" "${list1}" +ec=$? + +rm -f "${list0}" "${list1}" + +if [ $? -ne 0 ]; then + echo >&2 + echo "You may not be able to boot." >&2 + exit 1 +fi + +echo "OK" -- 2.42.0