Update configure message for name change
[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 blkdevalias map.
119
120 This will configure persistent aliases 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.
123
124 Ctrl-C will abort.
125
126 EOF
127
128     echo -n "Default user to own device nodes [$BA_USER]: "
129     read _BA_USER
130     if [ "$_BA_USER" != "" ]; then
131         BA_USER=$_BA_USER;
132     fi
133     echo -n "Default group to own device nodes [$BA_GROUP]: "
134     read _BA_GROUP
135     if [ "$_BA_GROUP" != "" ]; then
136         BA_GROUP=$_BA_GROUP;
137     fi
138     echo -n "Default device directory [$BA_DEV_PATH]: "
139     read _BA_DEV_PATH
140     if [ "$_BA_DEV_PATH" != "" ]; then
141         BA_DEV_PATH=$_BA_DEV_PATH;
142     fi
143     debug "BA_DEV_PATH=\"$BA_DEV_PATH\""
144     debug "BA_USER=\"$BA_USER\""
145     debug "BA_GROUP=\"$BA_GROUP\""
146     echo -n "Writing wwid map configuration: "
147     TMP_CONF=$(mktemp --tmpdir=$BA_CONF_PATH)
148     if [ ! -f "$TMP_CONF" ]; then
149         echo
150         error "could not write temporary file"
151         exit 1
152     fi
153     echo "BA_DEV_PATH=\"$BA_DEV_PATH\"" >> $TMP_CONF
154     echo "BA_USER=\"$BA_USER\"" >> $TMP_CONF
155     echo "BA_GROUP=\"$BA_GROUP\"" >> $TMP_CONF
156     mv $TMP_CONF $BA_CONF
157     if [ "$?" != 0 ]; then
158         error "could not create configuration file $BA_CONF"
159         exit
160     fi
161     echo -e "done\n"
162 }
163
164 # createdisk <nickname> <device>
165 ba_createdisk () {
166     local WWID DEV NNAME
167     NNAME="$1"
168     DEV="$2"
169     if [ "$DEV" == "" ] || [ "$NNAME" == "" ]; then
170         echo "Usage: $0 createdisk <nickname> <device>"
171         exit 2
172     fi
173     debug "ba_createdisk: WWID=\"$WWID\" DEV=\"$DEV\" NNAME=\"$NNAME\""
174     WWID=$(ba_scsi_id $DEV)
175     ba_add_wwid_mapping $WWID $NNAME "sd"
176     ba_store_wwid_map
177 }
178
179 ba_deletedisk () {
180     local WWID NNAME
181     NNAME=$1
182     if [ "$NNAME" == "" ]; then
183         echo "Usage: $0 deletedisk <nickname>"
184         exit 2
185     fi
186     WWID=${NAMEMAP[$NNAME]}
187     if [ "$WWID" == "" ]; then
188         error "\"$NNAME\" does not exist"
189         echo "Usage: $0 deletedisk <nickname>"
190         exit 2
191     fi
192     echo -n "Removing wwid map disk \"$NNAME\":"
193     ba_del_wwid_mapping $WWID \
194     && echo "                                  [  OK  ]"
195     ba_store_wwid_map
196 }
197
198 ba_list_disks () {
199     for D in ${WWIDMAP[@]}; do
200         echo $D
201     done | sort -V
202 }
203
204 ba_querydisk () {
205     local LISTDEV ONDEV WWID NNAME
206     if [ "$1" == "-d" ]; then
207         LISTDEV=1
208         shift
209     fi
210     NNAME=$1
211     if [ "$NNAME" == "" ]; then
212         echo "Usage: $0 querydisk <nickname>"
213         exit 2
214     fi
215     WWID=${NAMEMAP[$NNAME]}
216     debug "ba_querydisk: NNAME=\"$NNAME\" WWID=\"$WWID\" LISTDEV=\"$LISTDEV\""
217     if [ "$LISTDEV" == "1" ]; then
218         DEV="/dev/$BA_DEV_PATH/$(readlink "/dev/$BA_DEV_PATH/$NNAME")"
219         MAJ="$[0x$(stat --format "%t" $DEV)]"
220         MIN="$[0x$(stat --format "%T" $DEV)]"
221         ONDEV="on device [$MAJ,$MIN]"
222         debug "ba_querydisk: DEV=\"$DEV\" MAJ=\"$MAJ\" MIN=\"$MIN\""
223     fi
224     if [ "$WWID" != "" ]; then
225         echo "$NNAME is a valid blkdevalias disk$ONDEV"
226         return
227     fi
228     exit 1
229 }
230
231 ba_scandisks () {
232     warn "(unimplemented)"
233 }
234
235 ba_map () {
236     local WWID
237     WWID="$1"
238     if [ "$WWID" == "" ] || [ "${WWIDMAP[$WWID]}" == "" ]; then
239         echo "Usage: $0 map <wwid>" 1>&2
240         exit 2
241     fi
242     echo "BA_WWID=$WWID"
243     echo "BA_NAME=$BA_DEV_PATH/${WWIDMAP[$WWID]}"
244     echo "BA_TYPE=${TYPEMAP[$WWID]}"
245     echo "BA_USER=$BA_USER"
246     echo "BA_GROUP=$BA_GROUP"
247 }
248
249 # no point optimizing this out for configure
250 ba_load_wwid_map
251 case "$1" in
252     configure)
253         ba_configure
254         ;;
255     createdisk)
256         ba_createdisk $2 $3
257         ;;
258     deletedisk)
259         ba_deletedisk $2
260         ;;
261     listdisks)
262         ba_list_disks
263         ;;
264     querydisk)
265         ba_querydisk $2 $3
266         ;;
267     scandisks)
268         ba_scandisks
269         ;;
270     map)
271         ba_map $2
272         ;;
273     *)
274         echo -n "Usage: $0 "
275         echo "{configure|createdisk|deletedisk|scandisks|querydisk|map}"
276         exit 2
277 esac
278