Rename wwid add/del functions to match ba_ prefix
[blkdevalias.git] / blkdevalias
1 #!/bin/bash
2 declare -A WWIDMAP
3 declare -A NAMEMAP
4 declare -A TYPEMAP
5
6 BA_DEBUG=yes
7 BA_DEFAULT_USER="oracle"
8 BA_DEFAULT_GROUP="oracle"
9 BA_CONF_PATH="/etc/blkdevalias"
10 BA_DEV_PATH="oracleasm/disks"
11 BA_MAP_PATH="${BA_CONF_PATH}/map"
12 BA_CONF="${BA_CONF_PATH}/conf"
13 BA_USER="$BA_DEFAULT_USER"
14 BA_GROUP="$BA_DEFAULT_GROUP"
15 BA_LOCK="/var/lock/blkdevalias"
16 BA_SCSI_ID="/sbin/scsi_id"
17 BA_SCSI_ID_FLAGS="-g -u"
18
19 # load config
20 . $BA_CONF
21
22 error () {
23     echo "ERROR: $@" 1>&2
24 }
25
26 warn () {
27     echo "WARNING: $@" 1>&2
28 }
29
30 debug () {
31     [ "$BA_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 ba_store_wwid_map() {
45     TMP_MAP=$(mktemp --tmpdir=$BA_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 $BA_MAP_PATH
54 }
55
56 # read the WWID map from disk
57 ba_load_wwid_map() {
58     if [ ! -f "$BA_MAP_PATH" ] || [ ! -r "$BA_MAP_PATH"  ]; then
59         error "$BA_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 < $BA_MAP_PATH
70 }
71
72 # ba_add_wwid_mapping <wwid> <nickname> <sd|mpath>
73 ba_add_wwid_mapping() {
74     local WWID
75     WWID="$1"
76     NNAME="$2"
77     TYPE="$3"
78     debug "ba_add_wwid_mapping WWID=\"$WWID\" NNAME=\"$NNAME\""
79     if [ "$WWID" == "" ]; then
80         warn "no wwid given"
81         return
82     fi
83     if [ "$NNAME" == "" ]; then
84         warn "no nickname given"
85         return
86     fi
87     if [ "${NAMEMAP[$WWID]}" != "" ]; then
88         warn "$WWID already mapped to name ${NAMEMAP[$WWID]}; replacing it."
89     fi
90     WWIDMAP[$WWID]="$NNAME"
91     NAMEMAP[$NNAME]="$WWID"
92     TYPEMAP[$WWID]=${TYPE:-"sd"}
93 }
94
95 # ba_del_wwid_mapping <wwid>
96 ba_del_wwid_mapping() {
97     local WWID NNAME
98     WWID="$1"
99     if [ "$WWID" == "" ]; then
100         return 1
101     fi
102     if [ "${WWIDMAP[$WWID]}" == "" ]; then
103         warn "WARNING: $WWID does not exist; doing nothing."
104         return 1
105     fi
106     NNAME=${WWIDMAP[$WWID]}
107     unset WWIDMAP[$WWID]
108     unset NAMEMAP[$NNAME]
109     unset TYPEMAP[$WWID]
110 }
111
112 ba_scsi_id () {
113     $BA_SCSI_ID $BA_SCSI_ID_FLAGS $1
114 }
115
116 ba_configure () {
117     cat <<EOF
118 Configuring the udev wwid map.
119
120 This will configure persistent mappings and permissions for storage
121 devices. The current values will be shown in brackets ('[]'). Hitting
122 <ENTER> without typing an answer will keep the current value. Ctrl-C
123 will abort.
124
125 EOF
126
127     echo -n "Default user to own device nodes [$BA_USER]: "
128     read _BA_USER
129     if [ "$_BA_USER" != "" ]; then
130         BA_USER=$_BA_USER;
131     fi
132     echo -n "Default group to own device nodes [$BA_GROUP]: "
133     read _BA_GROUP
134     if [ "$_BA_GROUP" != "" ]; then
135         BA_GROUP=$_BA_GROUP;
136     fi
137     echo -n "Default device directory [$BA_DEV_PATH]: "
138     read _BA_DEV_PATH
139     if [ "$_BA_DEV_PATH" != "" ]; then
140         BA_DEV_PATH=$_BA_DEV_PATH;
141     fi
142     debug "BA_DEV_PATH=\"$BA_DEV_PATH\""
143     debug "BA_USER=\"$BA_USER\""
144     debug "BA_GROUP=\"$BA_GROUP\""
145     echo -n "Writing wwid map configuration: "
146     TMP_CONF=$(mktemp --tmpdir=$BA_CONF_PATH)
147     if [ ! -f "$TMP_CONF" ]; then
148         echo
149         error "could not write temporary file"
150         exit 1
151     fi
152     echo "BA_DEV_PATH=\"$BA_DEV_PATH\"" >> $TMP_CONF
153     echo "BA_USER=\"$BA_USER\"" >> $TMP_CONF
154     echo "BA_GROUP=\"$BA_GROUP\"" >> $TMP_CONF
155     mv $TMP_CONF $BA_CONF
156     if [ "$?" != 0 ]; then
157         error "could not create configuration file $BA_CONF"
158         exit
159     fi
160     echo -e "done\n"
161 }
162
163 # createdisk <nickname> <device>
164 ba_createdisk () {
165     local WWID DEV NNAME
166     NNAME="$1"
167     DEV="$2"
168     if [ "$DEV" == "" ] || [ "$NNAME" == "" ]; then
169         echo "Usage: $0 createdisk <nickname> <device>"
170         exit 2
171     fi
172     debug "ba_createdisk: WWID=\"$WWID\" DEV=\"$DEV\" NNAME=\"$NNAME\""
173     WWID=$(ba_scsi_id $DEV)
174     ba_add_wwid_mapping $WWID $NNAME "sd"
175     ba_store_wwid_map
176 }
177
178 ba_deletedisk () {
179     local WWID NNAME
180     NNAME=$1
181     if [ "$NNAME" == "" ]; then
182         echo "Usage: $0 deletedisk <nickname>"
183         exit 2
184     fi
185     WWID=${NAMEMAP[$NNAME]}
186     if [ "$WWID" == "" ]; then
187         error "\"$NNAME\" does not exist"
188         echo "Usage: $0 deletedisk <nickname>"
189         exit 2
190     fi
191     echo -n "Removing wwid map disk \"$NNAME\":"
192     ba_del_wwid_mapping $WWID \
193     && echo "                                  [  OK  ]"
194     ba_store_wwid_map
195 }
196
197 ba_list_disks () {
198     for D in ${WWIDMAP[@]}; do
199         echo $D
200     done | sort -V
201 }
202
203 ba_querydisk () {
204     local LISTDEV ONDEV WWID NNAME
205     if [ "$1" == "-d" ]; then
206         LISTDEV=1
207         shift
208     fi
209     NNAME=$1
210     if [ "$NNAME" == "" ]; then
211         echo "Usage: $0 querydisk <nickname>"
212         exit 2
213     fi
214     WWID=${NAMEMAP[$NNAME]}
215     debug "ba_querydisk: NNAME=\"$NNAME\" WWID=\"$WWID\" LISTDEV=\"$LISTDEV\""
216     if [ "$LISTDEV" == "1" ]; then
217         DEV="/dev/$BA_DEV_PATH/$(readlink "/dev/$BA_DEV_PATH/$NNAME")"
218         MAJ="$[0x$(stat --format "%t" $DEV)]"
219         MIN="$[0x$(stat --format "%T" $DEV)]"
220         ONDEV="on device [$MAJ,$MIN]"
221         debug "ba_querydisk: DEV=\"$DEV\" MAJ=\"$MAJ\" MIN=\"$MIN\""
222     fi
223     if [ "$WWID" != "" ]; then
224         echo "$NNAME is a valid blkdevalias disk$ONDEV"
225         return
226     fi
227     exit 1
228 }
229
230 ba_scandisks () {
231     warn "(unimplemented)"
232 }
233
234 ba_map () {
235     local WWID
236     WWID="$1"
237     if [ "$WWID" == "" ] || [ "${WWIDMAP[$WWID]}" == "" ]; then
238         echo "Usage: $0 map <wwid>" 1>&2
239         exit 2
240     fi
241     echo "BA_WWID=$WWID"
242     echo "BA_NAME=$BA_DEV_PATH/${WWIDMAP[$WWID]}"
243     echo "BA_TYPE=${TYPEMAP[$WWID]}"
244     echo "BA_USER=$BA_USER"
245     echo "BA_GROUP=$BA_GROUP"
246 }
247
248 # no point optimizing this out for configure
249 ba_load_wwid_map
250 case "$1" in
251     configure)
252         ba_configure
253         ;;
254     createdisk)
255         ba_createdisk $2 $3
256         ;;
257     deletedisk)
258         ba_deletedisk $2
259         ;;
260     listdisks)
261         ba_list_disks
262         ;;
263     querydisk)
264         ba_querydisk $2 $3
265         ;;
266     scandisks)
267         ba_scandisks
268         ;;
269     map)
270         ba_map $2
271         ;;
272     *)
273         echo -n "Usage: $0 "
274         echo "{configure|createdisk|deletedisk|scandisks|querydisk|map}"
275         exit 2
276 esac
277