Skip to content

Instantly share code, notes, and snippets.

Last active Jul 21, 2016
What would you like to do?
Test dnsmasq with LXC containers
# test-dnsmasq
release_lease() {
local name=$1
# release dnsmasq lease, if any before creating new container
container_mac=$(grep "\s${name}\s" ${LEASES} | awk '{print $2}')
container_ip=$( grep "\s${name}\s" ${LEASES} | awk '{print $3}')
dhcp_release natbr0 ${container_ip} ${container_mac}
wait_for_host() {
# wait for host name resolution
# it does not mean that host is up
local hostname="$1"
local retries="${2:-10}"
local delay="${3:-3}"
for (( i=1; i<=${retries}; i++ )); do
host ${hostname} > /dev/null && return 0;
sleep ${delay}
return 1;
# main
echo "Starting Test: $(host ${container})"
lxc-create -q -n ${container} -t turnkey -- ${app} -i ~/inithooks.conf
echo "Container created: $(host ${container})"
lxc-start -d -n ${container}
wait_for_host ${container}
echo "Container started: $(host -W30 ${container})"
sleep 5;
echo "Container started+5: $(host ${container})"
sleep 5;
echo "Container started+10: $(host ${container})"
lxc-stop -n ${container}
echo "Container stopped: $(host ${container})"
lxc-destroy -n ${container}
echo "Container destroyed: $(host ${container})"
release_lease ${container}
echo "dnsmasq lease released: $(host ${container})"
service dnsmasq reload
echo "dnsmasq reloaded: $(host ${container})"
# end
Copy link

Dude4Linux commented Dec 13, 2015

This script was used to demonstrate the interaction between dnsmasq and LXC when a container is created, started and then destroyed. Uncomment the subroutine calls to observe the effect of wait_for_host and release_lease. These subroutines were incorporated into nginx-proxy and lxc-turnkey template. Since there is not yet a hook for lxc-destroy, we do the next best thing and release any old leases before re-creating a container using the same name.

Copy link

Dude4Linux commented Dec 15, 2015

Corrected a mistake pointed out by Anton and clarified that we're using the default inithooks.conf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment