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