3658c713114c6eaf79b92e01a34ddaf74b92a5a1
[blkdevalias.git] / wwidmap
1 #!/bin/bash
2 declare -A WWIDMAP
3 declare -A NAMEMAP
4 declare -A TYPEMAP
5
6 WM_DEBUG=yes
7 WM_DEFAULT_USER="oracle"
8 WM_DEFAULT_GROUP="oracle"
9 WM_CONF_PATH="/etc/wwidmap"
10 WM_DEV_PATH="oracleasm/disks"
11 WM_MAP_PATH="${WM_CONF_PATH}/map"
12 WM_CONF="${WM_CONF_PATH}/conf"
13 WM_USER="$WM_DEFAULT_USER"
14 WM_GROUP="$WM_DEFAULT_GROUP"
15 WM_LOCK="/var/lock/wm.lock"
16 WM_SCSI_ID="/sbin/scsi_id"
17 WM_SCSI_ID_FLAGS="-g -u"
18
19 # load config
20 . $WM_CONF
21
22 error () {
23     echo "ERROR: $@" 1>&2
24 }
25
26 warn () {
27     echo "WARNING: $@" 1>&2
28 }
29
30 debug () {
31     [ "$WM_DEBUG" == "yes" ] && echo "$@" 1>&2
32 }
33
34 # debug print
35 print_wwid_mapping() {
36     local WWID=$1
37     if [ "$WWID" == "" ]; then
38         return
39     fi
40     debug "NNAME for WWID '$WWID' is: '${WWIDMAP[$WWID]}' (${TYPEMAP[$WWID]})"
41 }
42
43 # store the WWID map to disk
44 wm_store_wwid_map() {
45     TMP_MAP=$(mktemp --tmpdir=$WM_CONF_DIR)
46     if [ ! -f $TMP_MAP ]; then
47         error "could not write temporary file"
48         exit 1
49     fi
50     for WWID in ${!WWIDMAP[@]}; do
51         echo "$WWID ${WWIDMAP[$WWID]} ${TYPEMAP[$WWID]}"
52     done  >> $TMP_MAP
53     mv $TMP_MAP $WM_MAP_PATH
54 }
55
56 # read the WWID map from disk
57 wm_load_wwid_map() {
58     if [ ! -f $WM_MAP_PATH ] || [ ! -r $WM_MAP_PATH  ]; then
59         error "$WM_MAP_PATH does not exist or is not readable"
60         exit 1
61     fi
62     while read WWID NNAME TYPE; do
63         if [[ $WWID == \#* ]]; then
64             continue
65         fi
66         WWIDMAP[$WWID]="$NNAME"
67         NAMEMAP[$NNAME]="$WWID"
68         TYPEMAP[$WWID]="$TYPE"
69     done < $WM_MAP_PATH
70 }
71
72 # add_wwid_mapping <wwid> <nickname> <sd|mpath>
73 add_wwid_mapping() {
74     local WWID
75     WWID="$1"
76     NNAME="$2"
77     if [ "$WWID" == "" ]; then
78         warn "no wwid given"
79         return
80     fi
81     if [ "$NNAME" == "" ]; then
82         warn "no nickname given"
83         return
84     fi
85     if [ "${NAMEMAP[$WWID]}" != "" ]; then
86         warn "$WWID already mapped to name ${NAMEMAP[$WWID]}; replacing it."
87     fi
88     WWIDMAP[$WWID]="$NNAME"
89     NAMEMAP[$NNAME]="$WWID"
90     TYPEMAP[$WWID]="sd"
91 }
92
93 # del_wwid_mapping <wwid>
94 del_wwid_mapping() {
95     local WWID NNAME
96     WWID="$1"
97     if [ "$WWID" == "" ]; then
98         return 1
99     fi
100     if [ "${WWIDMAP[$WWID]}" == "" ]; then
101         warn "WARNING: $WWID does not exist; doing nothing."
102         return 1
103     fi
104     NNAME=${WWIDMAP[$WWID]}
105     unset WWIDMAP[$WWID]
106     unset NAMEMAP[$NNAME]
107     unset TYPEMAP[$WWID]
108 }
109
110 wm_scsi_id () {
111     $WM_SCSI_ID $WM_SCSI_ID_FLAGS $1
112 }
113
114 wm_configure () {
115     cat <<EOF
116 Configuring the udev wwid map.
117
118 This will configure persistent mappings and permissions for storage
119 devices. The current values will be shown in brackets ('[]'). Hitting
120 <ENTER> without typing an answer will keep the current value. Ctrl-C
121 will abort.
122
123 EOF
124
125     echo -n "Default user to own device nodes [$WM_USER]: "
126     read _WM_USER
127     if [ "$_WM_USER" != "" ]; then
128         WM_USER=$_WM_USER;
129     fi
130     echo -n "Default group to own device nodes [$WM_GROUP]: "
131     read _WM_GROUP
132     if [ "$_WM_GROUP" != "" ]; then
133         WM_GROUP=$_WM_GROUP;
134     fi
135     echo -n "Default device directory [$WM_DEV_PATH]: "
136     read _WM_DEV_PATH
137     if [ "$_WM_DEV_PATH" != "" ]; then
138         WM_DEV_PATH=$_WM_DEV_PATH;
139     fi
140     debug "WM_DEV_PATH=\"$WM_DEV_PATH\""
141     debug "WM_USER=\"$WM_USER\""
142     debug "WM_GROUP=\"$WM_GROUP\""
143     echo -n "Writing wwid map configuration: "
144     TMP_CONF=$(mktemp --tmpdir=$WM_CONF_PATH)
145     if [ ! -f $TMP_CONF ]; then
146         echo
147         error "could not write temporary file"
148         exit 1
149     fi
150     echo "WM_DEV_PATH=\"$WM_DEV_PATH\"" >> $TMP_CONF
151     echo "WM_USER=\"$WM_USER\"" >> $TMP_CONF
152     echo "WM_GROUP=\"$WM_GROUP\"" >> $TMP_CONF
153     mv $TMP_CONF $WM_CONF
154     if [ "$?" != 0 ]; then
155         error "could not create configuration file $WM_CONF"
156         exit
157     fi
158     echo "done"
159 }
160
161 # createdisk <nickname> <device>
162 wm_createdisk () {
163     local WWID DEV NNAME
164     NNAME="$1"
165     DEV="$2"
166     if [ "$DEV" == "" ] || [ "$NNAME" == "" ]; then
167         echo "Usage: $0 createdisk <nickname> <device>"
168         exit 2
169     fi
170     WWID=$(wm_scsi_id $DEV)
171     add_wwid_mapping $WWID $NNAME "sd"
172     wm_store_wwid_map
173 }
174
175 wm_deletedisk () {
176     local WWID NNAME
177     NNAME=$1
178     if [ "$NNAME" == "" ]; then
179         echo "Usage: $0 deletedisk <nickname>"
180         exit 2
181     fi
182     WWID=${NAMEMAP[$NNAME]}
183     if [ "$WWID" == "" ]; then
184         error "\"$NNAME\" does not exist"
185         echo "Usage: $0 deletedisk <nickname>"
186         exit 2
187     fi
188     echo -n "Removing wwid map disk \"$NNAME\":"
189     del_wwid_mapping $WWID \
190     && echo "                                  [  OK  ]"
191     wm_store_wwid_map
192 }
193
194 wm_list_disks () {
195     for D in ${WWIDMAP[@]}; do
196         echo $D
197     done | sort -V
198 }
199
200 wm_querydisk () {
201     warn "(unimplemented)"
202 }
203
204 wm_scandisks () {
205     warn "(unimplemented)"
206 }
207
208 wm_map () {
209     local WWID
210     WWID="$1"
211     if [ "$WWID" == "" ] || [ "${WWIDMAP[$WWID]}" == "" ]; then
212         echo "Usage: $0 map <wwid>" 1>&2
213         exit 2
214     fi
215     echo "WM_WWID=$WWID"
216     echo "WM_NAME=$WM_DEV_PATH/${WWIDMAP[$WWID]}"
217     echo "WM_TYPE=${TYPEMAP[$WWID]}"
218     echo "WM_USER=$WM_USER"
219     echo "WM_GROUP=$WM_GROUP"
220 }
221
222 case "$1" in
223     configure)
224         wm_configure
225         ;;
226     createdisk)
227         wm_load_wwid_map
228         wm_createdisk $2 $3
229         ;;
230     deletedisk)
231         wm_load_wwid_map
232         wm_deletedisk $2
233         ;;
234     listdisks)
235         wm_load_wwid_map
236         wm_list_disks
237         ;;
238     querydisk)
239         wm_querydisk
240         ;;
241     scandisks)
242         wm_scandisks
243         ;;
244     map)
245         wm_load_wwid_map
246         wm_map $2
247         ;;
248     *)
249         echo -n "Usage: $0 "
250         echo "{configure|createdisk|deletedisk|scandisks|querydisk|map}"
251         exit 2
252 esac
253