Blame SOURCES/rear-bz1659137.patch

e8a0f5
diff --git a/usr/share/rear/skel/default/etc/scripts/system-setup.d/55-migrate-network-devices.sh b/usr/share/rear/skel/default/etc/scripts/system-setup.d/55-migrate-network-devices.sh
e8a0f5
index e3ebacce..17cd1dd6 100644
e8a0f5
--- a/usr/share/rear/skel/default/etc/scripts/system-setup.d/55-migrate-network-devices.sh
e8a0f5
+++ b/usr/share/rear/skel/default/etc/scripts/system-setup.d/55-migrate-network-devices.sh
e8a0f5
@@ -26,18 +26,47 @@ ORIGINAL_MACS=()
e8a0f5
 # The ORIGINAL_DEVICES collects the original device names:
e8a0f5
 ORIGINAL_DEVICES=()
e8a0f5
 # The ORIG_MACS_FILE contains lines of the form: network_interface mac_address
e8a0f5
+
e8a0f5
+# Temporary rear_mappings_mac used when interfaces have been renamed
e8a0f5
+tmp_mac_mapping_file=$(mktemp)
e8a0f5
+
e8a0f5
 # TODO: What should happen if there is no ORIG_MACS_FILE or when it is empty?
e8a0f5
 while read orig_dev orig_mac junk ; do
e8a0f5
     ORIGINAL_DEVICES=( "${ORIGINAL_DEVICES[@]}" "$orig_dev")
e8a0f5
     ORIGINAL_MACS=( "${ORIGINAL_MACS[@]}" "$orig_mac" )
e8a0f5
-    # Continue with the next original MAC address if it is found on the current system:
e8a0f5
-    ip link show | grep -q "$orig_mac" && continue
e8a0f5
-    MIGRATE_DEVNAMES=( "${MIGRATE_DEVNAMES[@]}" "$orig_dev" )
e8a0f5
-    MIGRATE_MACS=( "${MIGRATE_MACS[@]}" "$orig_mac" )
e8a0f5
+    # Continue with the next original MAC address if it is found on the current
e8a0f5
+    # system, otherwise we consider it needs migration:
e8a0f5
+    new_dev=$( get_device_by_hwaddr "$orig_mac" )
e8a0f5
+    if [ -n "$new_dev" ] ; then
e8a0f5
+        [ "$new_dev" = "$orig_dev" ] && continue
e8a0f5
+        # The device was found but has been renamed (it was customized in
e8a0f5
+        # source system).
e8a0f5
+        # Create a temporary mac mapping, we don't want finalize() to update
e8a0f5
+        # the ifcfg-* files!
e8a0f5
+        echo "$orig_mac $orig_mac $orig_dev" >> $tmp_mac_mapping_file
e8a0f5
+    else
e8a0f5
+        MIGRATE_MACS+=( "$orig_mac" )
e8a0f5
+    fi
e8a0f5
 done < $ORIG_MACS_FILE
e8a0f5
 
e8a0f5
-# Skip this process if all MACs and network interfacs (devices) are accounted for:
e8a0f5
-test ${#MIGRATE_MACS[@]} -eq 0 && test ${#MIGRATE_DEVNAMES[@]} -eq 0 && return 0
e8a0f5
+
e8a0f5
+if [ ${#MIGRATE_MACS[@]} -ne 0 ] ; then
e8a0f5
+    # If some MACs were not found (MIGRATE_MACS not empty) then, we need a migration
e8a0f5
+    :
e8a0f5
+elif [ -s $tmp_mac_mapping_file ] ; then
e8a0f5
+    # Else, if some devices were renamed, we also need a migration, but it will
e8a0f5
+    # be automatic thanks to the $tmp_mac_mapping_file mapping file
e8a0f5
+
e8a0f5
+    # We do not need the $MAC_MAPPING_FILE file from the user, just overwrite it
e8a0f5
+    # Later, we will remove that file to not have finalize() modify the ifcfg-*
e8a0f5
+    # files.
e8a0f5
+    mkdir -p $(dirname $MAC_MAPPING_FILE)
e8a0f5
+    cp $tmp_mac_mapping_file $MAC_MAPPING_FILE
e8a0f5
+else
e8a0f5
+    # Skip this process if all MACs and network interfaces (devices) are accounted for
e8a0f5
+    unset tmp_mac_mapping_file
e8a0f5
+    return 0
e8a0f5
+fi
e8a0f5
 
e8a0f5
 # Find the MAC addresses that are now available.
e8a0f5
 # This is an array with values of the form "$dev $mac $driver"
e8a0f5
@@ -74,7 +103,7 @@ done
e8a0f5
 # so that it is shown to the user what MAC address mappings will be done:
e8a0f5
 if read_and_strip_file $MAC_MAPPING_FILE ; then
e8a0f5
     while read orig_dev orig_mac junk ; do
e8a0f5
-        read_and_strip_file $MAC_MAPPING_FILE | grep -q "$orig_mac" && MANUAL_MAC_MAPPING=true
e8a0f5
+        read_and_strip_file $MAC_MAPPING_FILE | grep -qw "^$orig_mac" && MANUAL_MAC_MAPPING=true
e8a0f5
     done < $ORIG_MACS_FILE
e8a0f5
 fi
e8a0f5
 
e8a0f5
@@ -237,7 +266,7 @@ if is_true $reload_udev ; then
e8a0f5
     echo -n "Reloading udev ... "
e8a0f5
     # Force udev to reload rules (as they were just changed)
e8a0f5
     # Failback to "udevadm control --reload" in case of problem (as specify in udevadm manpage in SLES12)
e8a0f5
-    # If nothing work, then wait 1 seconf delay. It should let the time for udev to detect changes in the rules files.
e8a0f5
+    # If nothing work, then wait 1 second delay. It should let the time for udev to detect changes in the rules files.
e8a0f5
     udevadm control --reload-rules || udevadm control --reload || sleep 1
e8a0f5
     my_udevtrigger
e8a0f5
     sleep 1
e8a0f5
@@ -252,5 +281,13 @@ if is_true $reload_udev ; then
e8a0f5
 fi
e8a0f5
 
e8a0f5
 # A later script in finalize/* will also go over the MAC mappings file and
e8a0f5
-# apply them to the files in the recovered system.
e8a0f5
+# apply them to the files in the recovered system, unless we did the mapping
e8a0f5
+# automatically, which means some device has been renamed and will probably
e8a0f5
+# gets its name back upon reboot.
e8a0f5
+if [ -s $tmp_mac_mapping_file ] ; then
e8a0f5
+    rm $MAC_MAPPING_FILE $tmp_mac_mapping_file
e8a0f5
+fi
e8a0f5
+
e8a0f5
+unset tmp_mac_mapping_file
e8a0f5
 
e8a0f5
+# vim: set et ts=4 sw=4: