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