Skip to content

Commit

Permalink
Devsetup: Remove dhcp entry even without VM
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Akrog committed Nov 29, 2024
1 parent 547f194 commit f137573
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions devsetup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ attach_default_interface_cleanup: export BGP_NIC_2_MAC=${CRC_BGP_NIC_2_MAC}
endif
attach_default_interface_cleanup: export INSTANCE_NAME=${NETWORK_ISOLATION_INSTANCE_NAME}
attach_default_interface_cleanup: export NETWORK_NAME=${NETWORK_ISOLATION_NET_NAME}
attach_default_interface_cleanup: export IP_ADDRESS=${NETWORK_ISOLATION_IP_ADDRESS}
attach_default_interface_cleanup: ## Detach default libvirt network from CRC
bash scripts/interfaces-setup-cleanup.sh

Expand Down
16 changes: 12 additions & 4 deletions devsetup/scripts/interfaces-setup-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ if [ "$EUID" -eq 0 ]; then
exit
fi

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

# Without MAC we try to remove it using the host name or IP address
if [ -z "${DHCP_REMOVED}" ]; then
if ! virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host name='$INSTANCE_NAME'/>" --config --live 2>/dev/null; then
virsh --connect=qemu:///system net-update $NETWORK_NAME delete ip-dhcp-host "<host ip='$IP_ADDRESS'/>" --config --live 2>/dev/null
fi
fi

sleep 5

if [ -n "$BGP" ]; then
# We don't destroy the PCI devices here but before adding them, to avoid having to restart the CRC VM twice

Expand Down

0 comments on commit f137573

Please sign in to comment.