-
Notifications
You must be signed in to change notification settings - Fork 14
/
fast-vm-net-cleanup
executable file
·34 lines (28 loc) · 1.52 KB
/
fast-vm-net-cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
FASTVM_HELPER=/usr/libexec/fast-vm-helper.sh
if [ ! -f '/etc/fast-vm.conf' ]; then
echo "[err] global configuration file /etc/fast-vm.conf not found"
exit 1
fi
# load global configuration file
. /etc/fast-vm.conf
echo "[inf] fixing fast-vm network $LIBVIRT_NETWORK"
virsh --connect qemu:///system net-dumpxml "$LIBVIRT_NETWORK"|grep 'host mac'|awk '{print $2,$3}' | while read -r mac ip; do
mac=$(echo "$mac"|cut -d\' -f2)
ip=$(echo "$ip"|cut -d\' -f2)
vm_number=$(echo "$ip"|cut -d. -f4)
vm_name=$(virsh --connect qemu:///system list --all|awk '{print $2}'|grep -E "^${VM_PREFIX}.*-$vm_number"|head -1)
if [ -z "$vm_name" ] && [ "$vm_number" -gt 2 ]; then
echo "[inf] deleting reservation for non-existing VM number $vm_number"
echo "dhcp_release" "$LIBVIRT_NETWORK" "$vm_number" "$mac" | sudo -n $FASTVM_HELPER
virsh --connect qemu:///system net-update "$LIBVIRT_NETWORK" delete ip-dhcp-host --xml "<host mac='$mac' ip='$ip'/>" --live --config
fi
done
echo "[inf] check for dummy DHCP reservation"
if [ "$(virsh --connect qemu:///system net-dumpxml "$LIBVIRT_NETWORK"|grep -c '52:54:00:99:ff:99')" -eq 0 ]; then
echo "[inf] adding missing dummy DHCP reservation"
virsh --connect qemu:///system net-update "$LIBVIRT_NETWORK" add-last ip-dhcp-host --xml "<host mac='52:54:00:99:ff:99' ip='192.168.${SUBNET_NUMBER}.2'/>" --live --config
fi
# TODO
# - regenerate missing DHCP reservations for existing fastvm VMs
# - delete wrong DHCP leases where existing VM MAC is having non-assigned address