Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Release all unused addresses of lxd-bridge DHCP
#!/bin/sh
set -eu
# Set value of LXD_BRIDGE
. /etc/default/lxd-bridge
addr_list_file=/tmp/lxd-addr-list.`date +%Y-%m-%dT%H:%M:%S`
lxc list | awk '$4=="RUNNING"{print $6}' > $addr_list_file
cleanup() {
rm $addr_list_file
}
trap cleanup EXIT
awk -v addr_list_file=$addr_list_file -v interface=$LXD_BRIDGE '{
mac_addr = $2
addr = $3
ret = system(sprintf("awk -v addr=%s '\''BEGIN{rc=1} $1==addr{rc=0} END{exit rc}'\'' %s", addr, addr_list_file))
if (ret == 1) {
system(sprintf("sudo dhcp_release %s %s %s", interface, addr, mac_addr))
}
}' /var/lib/lxd-bridge/dnsmasq.$LXD_BRIDGE.leases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment