Demote egrep to grep in the zpool_id script.
[zfs.git] / cmd / zpool_id / zpool_id
1 #!/bin/bash
2
3 CONFIG="${CONFIG:-/etc/zfs/zdev.conf}"
4 AWK="${AWK:-/usr/bin/awk}"
5
6 if [ -z "${PATH_ID}" ]; then
7         # The path_id helper became a builtin command in udev 174.
8         if [ -x '/lib/udev/path_id' ]; then
9                 PATH_ID='/lib/udev/path_id'
10         else
11                 PATH_ID='udevadm test-builtin path_id'
12         fi
13 fi
14
15 die() {
16         echo "Error: $*"
17         exit 1
18 }
19
20 usage() {
21         cat << EOF
22 Usage: zpool_id [-h] [-c configfile] <devpath>
23   -c    Alternate config file [default /etc/zfs/zdev.conf]
24   -d    Use path_id from device as the mapping key
25   -h    Show this message
26 EOF
27         exit 1
28 }
29
30 while getopts 'c:d:h' OPTION; do
31         case ${OPTION} in
32         c)
33                 CONFIG="${OPTARG}"
34                 ;;
35         d)
36                 DEVICE="${OPTARG}"
37                 ;;
38         h)
39                 usage
40                 ;;
41         esac
42 done
43
44 # Check that a device was requested
45 [ -z "${DEVICE}" ] && usage
46
47 # Check for the existence of a configuration file
48 [ ! -f "${CONFIG}" ] && die "Missing config file: ${CONFIG}"
49
50 # If we are handling a multipath device then $DM_UUID will be
51 # exported and we'll use its value (prefixed with dm-uuid per
52 # multipathd's naming convention) as our unique persistent key.
53 # For traditional devices we'll obtain the key from udev's
54 # path_id.
55 if [ -n "${DM_UUID}" ] && echo "${DM_UUID}" | grep -q -e '^mpath' ; then
56         ID_PATH="dm-uuid-${DM_UUID}"
57 else
58         eval `${PATH_ID} ${DEVICE}`
59         [ -z "${ID_PATH}" ] && die "Missing ID_PATH for ${DEVICE}"
60 fi
61
62 # Use the persistent key to lookup the zpool device id in the
63 # configuration file which is of the format <device id> <key>.
64 # Lines starting with #'s are treated as comments and ignored.
65 # Exact matches are required, wild cards are not supported,
66 # and only the first match is returned.  Also note the following
67 # regex pattern only appears to work with gawk, not mawk or awk.
68 ID_ZPOOL=`${AWK} "/\<${ID_PATH}\>/ && !/^#/ { print \\$1; exit }" "${CONFIG}"`
69 [ -z "${ID_ZPOOL}" ] && die "Missing ID_ZPOOL for ID_PATH: ${ID_PATH}"
70
71 if [ -n "${ID_ZPOOL}" ]; then
72         echo "ID_PATH=${ID_PATH}"
73         echo "ID_ZPOOL=${ID_ZPOOL}"
74         echo "ID_ZPOOL_PATH=disk/zpool/${ID_ZPOOL}"
75 fi
76
77 exit 0