Getting a taste of libvirt hooks
I recently had the pleasure of learning how DNS systems work behind the scenes through an itsy-bitsy academic project. I learned how to set one (bind9) up from scratch and stumbled my way through it until I eventually got the gist of it.
The DNS system in question is installed on a virtual machine (we'll refer to it as "dumbvm") that I only boot up when I attend certain classes. Consequently, there are times where the virtual machine is powered off so I would like it if my host reverted back to the default nameserver set by our home router.
Fortunately, libvirt has a hook system in place for us to plug our own programs into, so let's make libvirt smarter!
#!/bin/sh
VM_NAME=$1
OPER=$2
SUB_OPER=$3
if [ "$VM_NAME" = "dumbvm" ]; then
if [ "$OPER" = "started" ]; then
if [ "$SUB_OPER" = "begin" ]; then
systemd-resolve --interface=<wlan0> dns --set-dns=<nameserver-ip-addr> --set-domain=<ns.example.com>
fi
elif [ "$OPER" = "stopped" ]; then
if [ "$SUB_OPER" = "end" ]; then
systemd-resolve --interface=<wlan0> dns --revert
fi
fi
fi
Pop that in /etc/libvirt/hooks/qemu/
and you're set.
For more information on libvirt hooks, visit the manual.