Skip to content

Commit f137573

Browse files
committed
Devsetup: Remove dhcp entry even without VM
When using the `attach_default_interface_cleanup` target of devsetup on a system where the CRC VM is no longer present it will fail to remove the static DHCP entry from the libvirt network. This patch makes the `interfaces-setup-cleanup.sh` script more robust as it will now remove the static entry even when the VM is gone (using the static IP address assigned).
1 parent 547f194 commit f137573

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

devsetup/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ attach_default_interface_cleanup: export BGP_NIC_2_MAC=${CRC_BGP_NIC_2_MAC}
341341
endif
342342
attach_default_interface_cleanup: export INSTANCE_NAME=${NETWORK_ISOLATION_INSTANCE_NAME}
343343
attach_default_interface_cleanup: export NETWORK_NAME=${NETWORK_ISOLATION_NET_NAME}
344+
attach_default_interface_cleanup: export IP_ADDRESS=${NETWORK_ISOLATION_IP_ADDRESS}
344345
attach_default_interface_cleanup: ## Detach default libvirt network from CRC
345346
bash scripts/interfaces-setup-cleanup.sh
346347

devsetup/scripts/interfaces-setup-cleanup.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ if [ "$EUID" -eq 0 ]; then
66
exit
77
fi
88

9-
MAC_ADDRESS=$(virsh --connect=qemu:///system dumpxml $INSTANCE_NAME | xmllint --xpath "string(/domain/devices/interface/source[@network=\"$NETWORK_NAME\"]/../mac/@address)" -)
9+
MAC_ADDRESS=$(virsh --connect=qemu:///system dumpxml $INSTANCE_NAME | xmllint --xpath "string(/domain/devices/interface/source[@network=\"$NETWORK_NAME\"]/../mac/@address)" - 2>/dev/null)
1010
if [ -n "${MAC_ADDRESS}" ]; then
1111
virsh --connect=qemu:///system detach-interface $INSTANCE_NAME network --mac $MAC_ADDRESS
1212
# First try to remove the DHCP static IP entry by MAC, if it fails try by hostname
13-
if ! virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host mac='$MAC_ADDRESS'/>" --config --live; then
14-
virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host name='$INSTANCE_NAME'/>" --config --live
13+
if virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host mac='$MAC_ADDRESS'/>" --config --live 2>/dev/null; then
14+
DHCP_REMOVED=true
1515
fi
16-
sleep 5
1716
fi
1817

18+
# Without MAC we try to remove it using the host name or IP address
19+
if [ -z "${DHCP_REMOVED}" ]; then
20+
if ! virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host name='$INSTANCE_NAME'/>" --config --live 2>/dev/null; then
21+
virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host ip='$IP_ADDRESS'/>" --config --live 2>/dev/null
22+
fi
23+
fi
24+
25+
sleep 5
26+
1927
if [ -n "$BGP" ]; then
2028
# We don't destroy the PCI devices here but before adding them, to avoid having to restart the CRC VM twice
2129

0 commit comments

Comments
 (0)