Fix parameter validation and error reporting in ba_map()
authorBryn M. Reeves <bmr@redhat.com>
Wed, 6 Feb 2013 18:01:14 +0000 (18:01 +0000)
committerBryn M. Reeves <bmr@redhat.com>
Wed, 6 Feb 2013 18:01:14 +0000 (18:01 +0000)
ba_map() tried to use parameters before validating them leading
to user-visible bash errors.

Also make it clear that a device that does not match on partition
or type is not an error (even though it's a 'failure' for the
udev rule).

This helps when manually debugging mapping by handcrafting udev
environment variables, e.g.:

BA_WWID=0QEMU_QEMU_HARDDISK_drive-scsi0-0-3
BA_NAME=oracleasm/disks/DSK4
BA_TYPE=mpath
BA_USER=oracle
BA_GROUP=oracle
BA_MODE=0620

BA_WWID=0QEMU_QEMU_HARDDISK_drive-scsi0-0-0
BA_NAME=oracleasm/disks/DSK1
BA_TYPE=sd
BA_USER=oracle
BA_GROUP=oracle
BA_MODE=0620

Usage: /sbin/blkdevalias map <wwid>
ERROR: WWID "qux" is not mapped to any blkdevalias device
Usage: /sbin/blkdevalias map <wwid>

blkdevalias

index 0802953..300d60c 100755 (executable)
@@ -520,12 +520,17 @@ ba_scandisks () {
 ba_map () {
     local _WWID _TYPE _PART
     _WWID="$1"
-    _debug "$FUNCNAME: $_WWID -> ${WWIDMAP[$_WWID]}"
-    if [ "$_WWID" == "" ] || [ "${WWIDMAP[$_WWID]}" == "" ]; then
+    if [ "$_WWID" == "" ]; then 
+        ba_usage "map <wwid>"
+        exit 2
+    fi
+    if [ "${WWIDMAP[$_WWID]}" == "" ]; then
+        _error "WWID \"$_WWID\" is not mapped to any blkdevalias device"
         ba_usage "map <wwid>"
         exit 2
     fi
-    _PART=$(ba_get_partnum $DEVNAME)
+    _debug "$FUNCNAME: $_WWID -> ${WWIDMAP[$_WWID]}"
+    _PART=$(ba_get_partnum "$DEVNAME")
     if [ "$ID_BUS" == "scsi" ]; then
         _TYPE=sd
     else
@@ -534,6 +539,10 @@ ba_map () {
     _debug "$FUNCNAME: WWID=$_WWID PART=$_PART TYPE=$_TYPE"
     if [ "$_PART" != ${PARTMAP[$_WWID]} ] \
         || [ "$_TYPE" != ${TYPEMAP[$_WWID]} ] ; then
+        # this is not necessarily an error: udev may be
+        # calling us for a partition on a device that is
+        # not the partition registered for the matching
+        # WWID
         exit 1
     fi
     echo "BA_WWID=$_WWID"