svn commit: r229761 - stable/8/tools/tools/zfsboottest

Martin Matuska mm at FreeBSD.org
Sat Jan 7 02:35:01 UTC 2012


Author: mm
Date: Sat Jan  7 02:35:00 2012
New Revision: 229761
URL: http://svn.freebsd.org/changeset/base/229761

Log:
  MFC zfsboottest:
  r225608 (partial), r225609, r226611, r226612
  
  MFC r225608 (avg, tools/ part):
  zfstest: rename to zfsboottest and move to tools
  
  MFC r225609 (avg):
  zfsboottest: some additional enhancements
  
  - redirect diagnostics printfs in the boot code to stderr
  - do not read trailing garbage from a trailing block of a file
  
  Also add my copyright to the file after making so many changes.
  
  MFC r226611 (pjd):
  - 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.
  
  MFC r226612 (pjd):
  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.

Added:
  stable/8/tools/tools/zfsboottest/
     - copied from r225608, head/tools/tools/zfsboottest/
  stable/8/tools/tools/zfsboottest/zfsboottest.sh
     - copied unchanged from r226612, head/tools/tools/zfsboottest/zfsboottest.sh
Modified:
  stable/8/tools/tools/zfsboottest/Makefile
  stable/8/tools/tools/zfsboottest/zfsboottest.c
Directory Properties:
  stable/8/tools/   (props changed)
  stable/8/tools/tools/   (props changed)

Modified: stable/8/tools/tools/zfsboottest/Makefile
==============================================================================
--- head/tools/tools/zfsboottest/Makefile	Fri Sep 16 08:22:48 2011	(r225608)
+++ stable/8/tools/tools/zfsboottest/Makefile	Sat Jan  7 02:35:00 2012	(r229761)
@@ -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,8 +17,9 @@ CFLAGS=	-O1 \
 	-fdiagnostics-show-option \
 	-W -Wextra -Wno-sign-compare -Wno-unused-parameter \
 	-Werror
+LDFLAGS+=-lmd
 
-.if ${MACHINE_CPUARCH} == "amd64"
+.if ${MACHINE_ARCH} == "amd64"
 beforedepend zfsboottest.o: machine
 CLEANFILES+=	machine
 machine:

Modified: stable/8/tools/tools/zfsboottest/zfsboottest.c
==============================================================================
--- head/tools/tools/zfsboottest/zfsboottest.c	Fri Sep 16 08:22:48 2011	(r225608)
+++ stable/8/tools/tools/zfsboottest/zfsboottest.c	Sat Jan  7 02:35:00 2012	(r229761)
@@ -1,5 +1,7 @@
 /*-
  * Copyright (c) 2010 Doug Rabson
+ * Copyright (c) 2011 Andriy Gapon
+ * Copyright (c) 2011 Pawel Jakub Dawidek <pawel at dawidek.net>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -24,14 +26,13 @@
  * SUCH DAMAGE.
  */
 /* $FreeBSD$ */
-/*
- * Compile with 'cc -I. -I../../cddl/boot/zfs zfstest.c -o zfstest'
- */
 
 #include <sys/param.h>
 #include <sys/queue.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <md5.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
@@ -45,15 +46,19 @@
 void
 pager_output(const char *line)
 {
+
 	fprintf(stderr, "%s", line);
 }
 
+#define ZFS_TEST
+#define	printf(...)	 fprintf(stderr, __VA_ARGS__)
 #include "zfsimpl.c"
+#undef printf
 
 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);
@@ -72,7 +77,7 @@ zfs_read(spa_t *spa, dnode_phys_t *dn, v
 		n = zp->zp_size - off;
 
 	rc = dnode_read(spa, dn, off, buf, n);
-	if (rc)
+	if (rc != 0)
 		return (-rc);
 
 	return (n);
@@ -81,29 +86,49 @@ zfs_read(spa_t *spa, dnode_phys_t *dn, v
 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;
-	int i, n, off;
+	off_t off;
+	ssize_t n;
+	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();
 
@@ -118,27 +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 = zfs_read(spa, &dn, buf, 512, 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);
 }

Copied: stable/8/tools/tools/zfsboottest/zfsboottest.sh (from r226612, head/tools/tools/zfsboottest/zfsboottest.sh)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/8/tools/tools/zfsboottest/zfsboottest.sh	Sat Jan  7 02:35:00 2012	(r229761, copy of r226612, head/tools/tools/zfsboottest/zfsboottest.sh)
@@ -0,0 +1,130 @@
+#!/bin/sh
+#
+# Copyright (c) 2011 Pawel Jakub Dawidek <pawel at dawidek.net>
+# 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 <pool>" >&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"


More information about the svn-src-all mailing list