f74504cfb4156878eeebc58cf816a74045fe5047
[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 # Use udev's path_id to generate a unique persistent key
43 eval `${PATH_ID} ${DEVICE}`
44 [ -z ${ID_PATH} ] && die "Missing ID_PATH for ${DEVICE}"
45
46 # Use the persistent key to lookup the zpool device id in the
47 # configuration file which is of the format <device id> <key>.
48 # Lines starting with #'s are treated as comments and ignored.
49 # Exact matches are required, wild cards are not supported,
50 # and only the first match is returned.  Also note the following
51 # regex pattern only appears to work with gawk, not mawk or awk.
52 ID_ZPOOL=`${AWK} "/\<${ID_PATH}\>/ && !/^#/ { print \\$1; exit }" ${CONFIG}`
53 [ -z ${ID_ZPOOL} ] && die "Missing ID_ZPOOL for ID_PATH: ${ID_PATH}"
54
55 if [ ${ID_ZPOOL} ]; then
56         echo "ID_PATH=${ID_PATH}"
57         echo "ID_ZPOOL=${ID_ZPOOL}"
58         echo "ID_ZPOOL_PATH=disk/zpool/${ID_ZPOOL}"
59 fi
60
61 exit 0