Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTV-2025 | Prevent Multiple NICs wrong mapping during migration #1358

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions pkg/virt-v2v/customize/scripts/rhel/run/network_config_util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ NETWORK_CONNECTIONS_DIR="${NETWORK_CONNECTIONS_DIR:-/etc/NetworkManager/system-c
NETWORK_INTERFACES_DIR="${NETWORK_INTERFACES_DIR:-/etc/network/interfaces}"
IFQUERY_CMD="${IFQUERY_CMD:-ifquery}"
SYSTEMD_NETWORK_DIR="${SYSTEMD_NETWORK_DIR:-/run/systemd/network}"
# UDEV_RULES_FILE_TEMP comes earlier lexicographically, hence it take precedence over UDEV_RULES_FILE
# and will prevent collisions by creating temporary rules first.
UDEV_RULES_FILE_TEMP="${UDEV_RULES_FILE_TEMP:-/etc/udev/rules.d/70-persistent-net-temp.rules}"
UDEV_RULES_FILE="${UDEV_RULES_FILE:-/etc/udev/rules.d/70-persistent-net.rules}"
NETPLAN_DIR="${NETPLAN_DIR:-/}"

Expand Down Expand Up @@ -109,7 +112,7 @@ udev_from_ifcfg() {
continue
fi

echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$DEVICE")\""
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"temp_$(remove_quotes "$DEVICE")\""
done
}

Expand Down Expand Up @@ -147,7 +150,7 @@ udev_from_nm() {
continue
fi

echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$DEVICE")\""
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"temp_$(remove_quotes "$DEVICE")\""
done
}

Expand Down Expand Up @@ -223,7 +226,7 @@ udev_from_netplan() {
fi

# Create the udev rule based on the extracted MAC address and interface name
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$interface_name")\""
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"temp_$(remove_quotes "$interface_name")\""
done
}

Expand Down Expand Up @@ -274,7 +277,22 @@ udev_from_ifquery() {
fi

# Create the udev rule based on the extracted MAC address and interface name
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$interface_name")\""
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"temp_$(remove_quotes "$interface_name")\""
done
}


# Create final udev rules based on the temporary udev rules
udev_final_from_temp_rules() {
# Read the temporary udev rules file line by line
cat "$UDEV_RULES_FILE_TEMP" | while read line;
do
# Extract the temporary name and final name
TEMP_NAME=$(echo "$line" | grep -o 'NAME="temp_[^"]*"' | cut -d'"' -f2)
FINAL_NAME=$(echo "$TEMP_NAME" | sed 's/^temp_//')

# Generate final udev rule by replacing the temporary name with the final name in the original line
echo "$(echo "$line" | sed "s/temp_$FINAL_NAME/$FINAL_NAME/")"
done
}

Expand All @@ -299,14 +317,18 @@ check_dupe_hws() {

# Create udev rules check for duplicates and write them to udev file
main() {
{
{
udev_from_ifcfg
udev_from_nm
udev_from_netplan
udev_from_ifquery
} | check_dupe_hws > "$UDEV_RULES_FILE_TEMP" 2>/dev/null

{
udev_final_from_temp_rules
} | check_dupe_hws > "$UDEV_RULES_FILE" 2>/dev/null
echo "New udev rule:"

echo "New udev rules:"
cat $UDEV_RULES_FILE
}

main
2 changes: 2 additions & 0 deletions pkg/virt-v2v/customize/scripts/rhel/run/test-network_config_util.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test_dir() {
export NETWORK_SCRIPTS_DIR="$TEST_DIR/etc/sysconfig/network-scripts"
export NETWORK_CONNECTIONS_DIR="$TEST_DIR/etc/NetworkManager/system-connections"
export UDEV_RULES_FILE="$TEST_DIR/etc/udev/rules.d/70-persistent-net.rules"
export UDEV_RULES_FILE_TEMP="$TEST_DIR/etc/udev/rules.d/70-persistent-net-temp.rules"
export SYSTEMD_NETWORK_DIR="$TEST_DIR/run/systemd/network"
export NETPLAN_DIR="$TEST_DIR/"

Expand All @@ -36,6 +37,7 @@ test_dir() {
cp -a $TEST_SRC_DIR/root/* $TEST_DIR

# Clean up from previous runs
rm -f "$UDEV_RULES_FILE_TEMP"
rm -f "$UDEV_RULES_FILE"
mkdir -p $(dirname "$UDEV_RULES_FILE")

Expand Down
Empty file added tests/suit/change
Empty file.
Loading