]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - tools/tools/zfsboottest/zfsboottest.sh
MFC r226549,r226550,r226551,r226552,r226553,r226554,r226568,r226569,r226611,
[FreeBSD/releng/9.0.git] / tools / tools / zfsboottest / zfsboottest.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28
29 if [ $# -ne 1 ]; then
30         echo "usage: zfsboottest.sh <pool>" >&2
31         exit 1
32 fi
33
34 which -s zfsboottest
35 if [ $? -eq 0 ]; then
36         zfsboottest="zfsboottest"
37 else
38         if [ ! -x "/usr/src/tools/tools/zfsboottest/zfsboottest" ]; then
39                 echo "Unable to find \"zfsboottest\" utility." >&2
40                 exit 1
41         fi
42         zfsboottest="/usr/src/tools/tools/zfsboottest/zfsboottest"
43 fi
44
45 startdir="/boot"
46
47 pool="${1}"
48 zpool list "${pool}" >/dev/null 2>&1
49 if [ $? -ne 0 ]; then
50         echo "No such pool \"${pool}\"." >&2
51         exit 1
52 fi
53 bootfs=`zpool get bootfs "${pool}" | tail -1 | awk '{print $3}'`
54 if [ "${bootfs}" = "-" ]; then
55         echo "The \"bootfs\" property is not configured for pool \"${pool}\"." >&2
56         exit 1
57 fi
58 # Dataset's mountpoint property should be set to 'legacy'.
59 if [ "`zfs get -H -o value mountpoint ${bootfs}`" != "legacy" ]; then
60         echo "The \"mountpoint\" property of dataset \"${bootfs}\" should be set to \"legacy\"." >&2
61         exit 1
62 fi
63 mountpoint=`df -t zfs "${bootfs}" 2>/dev/null | tail -1 | awk '{print $6}'`
64 if [ -z "${mountpoint}" ]; then
65         echo "The \"${bootfs}\" dataset is not mounted." >&2
66         exit 1
67 fi
68 if [ ! -d "${mountpoint}${startdir}" ]; then
69         echo "The \"${mountpoint}${startdir}\" directory doesn't exist." >&2
70         exit 1
71 fi
72 # To be able to mount root ZFS file system we need either /etc/fstab entry
73 # or vfs.root.mountfrom variable set in /boot/loader.conf.
74 egrep -q '^'"${bootfs}"'[[:space:]]+/[[:space:]]+zfs[[:space:]]+' "${mountpoint}/etc/fstab" 2>/dev/null
75 if [ $? -ne 0 ]; then
76         egrep -q 'vfs.root.mountfrom="?'"${bootfs}"'"?[[:space:]]*$' "${mountpoint}/boot/loader.conf" 2>/dev/null
77         if [ $? -ne 0 ]; then
78                 echo "To be able to boot from \"${bootfs}\", you need to declare" >&2
79                 echo "\"${bootfs}\" as being root file system in ${mountpoint}/etc/fstab" >&2
80                 echo "or add \"vfs.root.mountfrom\" variable set to \"${bootfs}\" to" >&2
81                 echo "${mountpoint}/boot/loader.conf." >&2
82                 exit 1
83         fi
84 fi
85 vdevs=""
86 for vdev in `zpool status "${pool}" | grep ONLINE | awk '{print $1}'`; do
87         vdev="/dev/${vdev#/dev/}"
88         if [ -c "${vdev}" ]; then
89                 if [ -z "${vdevs}" ]; then
90                         vdevs="${vdev}"
91                 else
92                         vdevs="${vdevs} ${vdev}"
93                 fi
94         fi
95 done
96
97 list0=`mktemp /tmp/zfsboottest.XXXXXXXXXX`
98 if [ $? -ne 0 ]; then
99         echo "Unable to create temporary file." >&2
100         exit 1
101 fi
102 list1=`mktemp /tmp/zfsboottest.XXXXXXXXXX`
103 if [ $? -ne 0 ]; then
104         echo "Unable to create temporary file." >&2
105         rm -f "${list0}"
106         exit 1
107 fi
108
109 echo "zfsboottest.sh is reading all the files in ${mountpoint}${startdir} using"
110 echo "boot code and using file system code."
111 echo "It calculates MD5 checksums for all the files and will compare them."
112 echo "If all files can be properly read using boot code, it is very likely you"
113 echo "will be able to boot from \"${pool}\" pool>:> Good luck!"
114 echo
115
116 "${zfsboottest}" ${vdevs} - `find "${mountpoint}${startdir}" -type f | sed "s@^${mountpoint}@@"` | egrep '^[0-9a-z]{32} /' | sort -k 2 >"${list0}"
117 find "${mountpoint}${startdir}" -type f | xargs md5 -r | sed "s@ ${mountpoint}@ @" | egrep '^[0-9a-z]{32} /' | sort -k 2 >"${list1}"
118
119 diff -u "${list0}" "${list1}"
120 ec=$?
121
122 rm -f "${list0}" "${list1}"
123
124 if [ $? -ne 0 ]; then
125         echo >&2
126         echo "You may not be able to boot." >&2
127         exit 1
128 fi
129
130 echo "OK"