Skip to content

Commit 7d1af51

Browse files
mnecasyaacov
authored andcommitted
MTV-1711 | Keep static IP on Debian based interfaces
Issue: All Debian-based operating systems can have the network configurations in the `/etc/network/interfaces`. Right now we are not fetching information from these config files when creating the udev rule to set the interface name. Fix: Add a new function to the `network_config_util.sh` script. I have decided to use the `ifquery` inside the guest. Alternatively, we would need to start parsing the config files. We can easily grep the IP address from the configfile and find the interface name but there could be multiple interface configurations inside one file. Docs: https://man.cx/interfaces(5) Signed-off-by: Martin Necas <mnecas@redhat.com>
1 parent a04c257 commit 7d1af51

File tree

12 files changed

+121
-0
lines changed

12 files changed

+121
-0
lines changed

virt-v2v/pkg/customize/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ go_library(
3939
"scripts/rhel/run/systemd-test.d/root/etc/netplan/50-netplan.yaml",
4040
"scripts/rhel/run/systemd-test.d/root/run/systemd/network/netplan-ens160.network",
4141
"scripts/rhel/run/systemd-test.d/root/tmp/macToIP",
42+
"scripts/rhel/run/ifcfg-hwaddr-test.d/expected-udev.rule",
43+
"scripts/rhel/run/ifcfg-hwaddr-test.d/root/etc/sysconfig/network-scripts/ifcfg-ens192",
44+
"scripts/rhel/run/ifcfg-hwaddr-test.d/root/etc/sysconfig/network-scripts/ifcfg-ens224",
45+
"scripts/rhel/run/ifcfg-hwaddr-test.d/root/tmp/macToIP",
46+
"scripts/rhel/run/network-interfaces-multiple-test.d/expected-udev.rule",
47+
"scripts/rhel/run/network-interfaces-multiple-test.d/root/etc/network/interfaces",
48+
"scripts/rhel/run/network-interfaces-multiple-test.d/root/etc/network/interfaces.d/if-ens192",
49+
"scripts/rhel/run/network-interfaces-multiple-test.d/root/etc/network/interfaces.d/if-eth0",
50+
"scripts/rhel/run/network-interfaces-multiple-test.d/root/tmp/macToIP",
51+
"scripts/rhel/run/network-interfaces-single-test.d/expected-udev.rule",
52+
"scripts/rhel/run/network-interfaces-single-test.d/root/etc/network/interfaces",
53+
"scripts/rhel/run/network-interfaces-single-test.d/root/tmp/macToIP",
54+
"scripts/rhel/run/test-network_config_util.sh",
4255
],
4356
importpath = "github.com/konveyor/forklift-controller/virt-v2v/pkg/customize",
4457
visibility = ["//visibility:public"],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="aa:bb:cc:dd:ee:ff",NAME="ens192"
2+
SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="aa:bb:cc:dd:ee:fe",NAME="eth0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file describes the network interfaces available on your system
2+
# and how to activate them. For more information, see interfaces(5).
3+
4+
source /etc/network/interfaces.d/*
5+
6+
# The loopback network interface
7+
auto lo
8+
iface lo inet loopback
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The primary network interface
2+
auto ens192
3+
iface ens192 inet static
4+
address 10.184.121.84/24
5+
gateway 10.184.121.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The primary network interface
2+
auto eth0
3+
iface eth0 inet static
4+
address 192.168.1.11/24
5+
gateway 192.168.1.1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aa:bb:cc:dd:ee:ff:ip:10.184.121.84,things,more
2+
aa:bb:cc:dd:ee:fe:ip:192.168.1.11,hello,world
3+
aa:bb:cc:dd:ee:fd:ip:192.168.1.12,ipv6,support
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="aa:bb:cc:dd:ee:ff",NAME="ens192"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file describes the network interfaces available on your system
2+
# and how to activate them. For more information, see interfaces(5).
3+
4+
source /etc/network/interfaces.d/*
5+
6+
# The loopback network interface
7+
auto lo
8+
iface lo inet loopback
9+
10+
# The primary network interface
11+
auto ens192
12+
iface ens192 inet static
13+
address 10.184.121.84/24
14+
gateway 10.184.121.1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aa:bb:cc:dd:ee:ff:ip:10.184.121.84,things,more
2+
aa:bb:cc:dd:ee:fe:ip:192.168.1.11,hello,world
3+
aa:bb:cc:dd:ee:fd:ip:192.168.1.12,ipv6,support

virt-v2v/pkg/customize/scripts/rhel/run/network_config_util.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
V2V_MAP_FILE="${V2V_MAP_FILE:-/tmp/macToIP}"
55
NETWORK_SCRIPTS_DIR="${NETWORK_SCRIPTS_DIR:-/etc/sysconfig/network-scripts}"
66
NETWORK_CONNECTIONS_DIR="${NETWORK_CONNECTIONS_DIR:-/etc/NetworkManager/system-connections}"
7+
NETWORK_INTERFACES_DIR="${NETWORK_INTERFACES_DIR:-/etc/network/interfaces}"
8+
IFQUERY_CMD="${IFQUERY_CMD:-ifquery}"
79
SYSTEMD_NETWORK_DIR="${SYSTEMD_NETWORK_DIR:-/run/systemd/network}"
810
UDEV_RULES_FILE="${UDEV_RULES_FILE:-/etc/udev/rules.d/70-persistent-net.rules}"
911
NETPLAN_DIR="${NETPLAN_DIR:-/}"
@@ -225,6 +227,57 @@ udev_from_netplan() {
225227
done
226228
}
227229

230+
# Create udev rules based on the macToIP mapping + output from parse_ifquery_file
231+
udev_from_ifquery() {
232+
# Check if ifquery command exist
233+
if ! ${IN_TESTING:-false} && ! command -v $IFQUERY_CMD>/dev/null 2>&1; then
234+
log "Warning: ifquery is not installed."
235+
return 0
236+
fi
237+
238+
# ifquery with interface dir
239+
ifquery_get() {
240+
$IFQUERY_CMD -i "$NETWORK_INTERFACES_DIR" "$@" 2>&3
241+
}
242+
243+
# Loop over all interface names and return the one with target_ip, or null
244+
find_interface_by_ip() {
245+
target_ip="$1"
246+
# Loop through all interfaces and check for the given IP address
247+
ifquery_get -l | while read IFNAME; do
248+
if ifquery_get $IFNAME | grep -q "$target_ip"; then
249+
echo "$IFNAME"
250+
return
251+
fi
252+
done
253+
}
254+
255+
# Read the mapping file line by line
256+
cat "$V2V_MAP_FILE" | while read line;
257+
do
258+
# Extract S_HW and S_IP from the current line in the mapping file
259+
extract_mac_ip "$line"
260+
261+
# If S_HW and S_IP were not extracted, skip the line
262+
if [ -z "$S_HW" ] || [ -z "$S_IP" ]; then
263+
log "Warning: invalid mac to ip line: $line."
264+
continue
265+
fi
266+
267+
# Search the parsed ifquery output for a matching IP address
268+
interface_name=$(find_interface_by_ip "$S_IP")
269+
270+
# If no interface is found, skip this entry
271+
if [ -z "$interface_name" ]; then
272+
log "Info: no interface name found to $S_IP."
273+
continue
274+
fi
275+
276+
# Create the udev rule based on the extracted MAC address and interface name
277+
echo "SUBSYSTEM==\"net\",ACTION==\"add\",ATTR{address}==\"$(remove_quotes "$S_HW")\",NAME=\"$(remove_quotes "$interface_name")\""
278+
done
279+
}
280+
228281
# Write to udev config
229282
# ----------------------------------------
230283

@@ -250,6 +303,7 @@ main() {
250303
udev_from_ifcfg
251304
udev_from_nm
252305
udev_from_netplan
306+
udev_from_ifquery
253307
} | check_dupe_hws > "$UDEV_RULES_FILE" 2>/dev/null
254308
echo "New udev rule:"
255309
cat $UDEV_RULES_FILE

0 commit comments

Comments
 (0)