]> git.proxmox.com Git - mirror_zfs-debian.git/blame - cmd/zpool_id/zpool_id
Merge branch 'upstream'
[mirror_zfs-debian.git] / cmd / zpool_id / zpool_id
CommitLineData
db7c1771 1#!/bin/sh
c9c0d073 2
04bf5ecc 3CONFIG="${CONFIG:-/etc/zfs/zdev.conf}"
c9c0d073 4
9c8254f6
DH
5if [ -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
12fi
13
c9c0d073
BB
14die() {
15 echo "Error: $*"
16 exit 1
17}
18
19usage() {
20 cat << EOF
04bf5ecc 21Usage: zpool_id [-h] [-c configfile] <devpath>
c9c0d073
BB
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
25EOF
26 exit 1
27}
28
29while getopts 'c:d:h' OPTION; do
30 case ${OPTION} in
31 c)
04bf5ecc 32 CONFIG="${OPTARG}"
c9c0d073
BB
33 ;;
34 d)
04bf5ecc 35 DEVICE="${OPTARG}"
c9c0d073
BB
36 ;;
37 h)
38 usage
39 ;;
40 esac
41done
42
43# Check that a device was requested
04bf5ecc 44[ -z "${DEVICE}" ] && usage
c9c0d073
BB
45
46# Check for the existence of a configuration file
04bf5ecc 47[ ! -f "${CONFIG}" ] && die "Missing config file: ${CONFIG}"
c9c0d073 48
560bcf9d
NB
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.
87193e2b 54if [ -n "${DM_UUID}" ] && echo "${DM_UUID}" | grep -q -e '^mpath' ; then
560bcf9d
NB
55 ID_PATH="dm-uuid-${DM_UUID}"
56else
57 eval `${PATH_ID} ${DEVICE}`
04bf5ecc 58 [ -z "${ID_PATH}" ] && die "Missing ID_PATH for ${DEVICE}"
560bcf9d 59fi
c9c0d073
BB
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,
b97f368d
DH
65# and only the first match is returned.
66ID_ZPOOL=''
67while 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
76done <"${CONFIG}"
77
04bf5ecc 78[ -z "${ID_ZPOOL}" ] && die "Missing ID_ZPOOL for ID_PATH: ${ID_PATH}"
c9c0d073 79
04bf5ecc 80if [ -n "${ID_ZPOOL}" ]; then
c9c0d073
BB
81 echo "ID_PATH=${ID_PATH}"
82 echo "ID_ZPOOL=${ID_ZPOOL}"
83 echo "ID_ZPOOL_PATH=disk/zpool/${ID_ZPOOL}"
84fi
85
86exit 0