Deal with duplicate WWIDs and aliases correctly
authorBryn M. Reeves <bmr@redhat.com>
Thu, 15 Nov 2012 23:18:17 +0000 (23:18 +0000)
committerBryn M. Reeves <bmr@redhat.com>
Thu, 15 Nov 2012 23:18:17 +0000 (23:18 +0000)
If a name or WWID is already present when creating a new alias
warn the user and replace it with the new values.

blkdevalias

index 47ed9ca..e715183 100755 (executable)
@@ -90,6 +90,7 @@ ba_add_wwid_mapping() {
     ALIAS="$2"
     TYPE="$3"
     PART="$4"
+    DEV="$5"
     _debug "ba_add_wwid_mapping: WWID=\"$WWID\" ALIAS=\"$ALIAS\"" \
         "TYPE=\"$TYPE\" PART=\"$PART\""
     if [ "$WWID" == "" ]; then
@@ -103,8 +104,15 @@ ba_add_wwid_mapping() {
     if [ "$PART" == "" ]; then
         _warn "no partition number given"
     fi
-    if [ "${NAMEMAP[$WWID]}" != "" ]; then
-        _warn "$WWID already mapped to alias ${NAMEMAP[$WWID]}; replacing it."
+    if [ "${WWIDMAP[$WWID]}" != "" ]; then
+        _debug "ba_add_wwid_mapping: WWIDMAP[\$WWID]=${WWIDMAP[$WWID]}"
+        _warn "$DEV already mapped to alias ${WWIDMAP[$WWID]}; replacing it."
+        ba_del_wwid_mapping "$WWID"
+    fi
+    if [ "${NAMEMAP[$ALIAS]}" != "" ]; then
+        _debug "ba_add_wwid_mapping: NAMEMAP[\$ALIAS]=${NAMEMAP[$ALIAS]}"
+        _warn "$DEV already mapped to disk ${NAMEMAP[$ALIAS]}; replacing it."
+        ba_del_wwid_mapping ${NAMEMAP[$ALIAS]}
     fi
     WWIDMAP[$WWID]="$ALIAS"
     NAMEMAP[$ALIAS]="$WWID"
@@ -123,6 +131,7 @@ ba_del_wwid_mapping() {
         _warn "WARNING: $WWID does not exist; doing nothing."
         return 1
     fi
+    _debug "ba_del_wwid_mapping: unsetting WWIDMAP[$WWID]"
     ALIAS=${WWIDMAP[$WWID]}
     unset WWIDMAP[$WWID]
     unset NAMEMAP[$ALIAS]
@@ -234,7 +243,7 @@ ba_createdisk () {
 
     PART=$(ba_get_partnum $DEV) 
 
-    ba_add_wwid_mapping $WWID $ALIAS $TYPE $PART
+    ba_add_wwid_mapping $WWID $ALIAS $TYPE $PART $DEV
     ba_store_wwid_map
 }