Blame SOURCES/rear-bz1659137.patch

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