Changing Libvirt bridge attachment in a running domain aka on-the-fly
At work I always prefer KVM hosts for reasons such as flexible, free and GUI-less. Yet I never bothered to go deeper into the networking features of Libvirt, so I only connect VMs to the host networks via Linux Bridges or OvS. Far far away from fancy virtual libvirt networks.
Even with this simple networking approach I recently faced a tedious task of reconnecting VMs to different bridges on-the-fly.
My use case came from a need to connect a single traffic generator VM to the different access ports of virtual CPEs. Essentially this meant that I need to reconnect my traffic generator interfaces to different bridges back and forth:
Apparently there is no such
virsh command that will allow you to change bridge attachments for networking devices, so a bit of bash-ing came just handy.
You know network interface device definition grepped from Libvirt XML format holds bridge association:
<!-- OMITTED --> <devices> <!-- OMITTED --> <interface type='bridge'> <mac address='52:54:00:cd:75:4f'/> <source bridge='br12'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </interface> <!-- OMITTED -->
The brute force approach would be to change the bridge identified in the domain definition, restart the domain and be with it. Not sporty at all!
Instead I decided that it would be good to have a script that for starters will take
new_bridge_id and do the rest. So
vifmove.sh (virsh interface move) was born.
This is a tiny bash script which does the job for me just fine:
Underneath its all simple, I leveraged
virsh update-device command and just templated the interface definition XML file:
If you find this one useful, feel free to add your ideas in the gist comments.