Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Effectively restarting kvm/libvirt network
#!/bin/bash
# Author: Aysad Kozanoglu
# Effectively restarting kvm/libvirt network
set -e
set -u
NET_NAME=default
# NET_HOOK=/etc/libvirt/hooks/qemu
virsh net-destroy $NET_NAME
virsh net-start $NET_NAME
VMS=$( virsh list | tail -n +3 | head -n -1 | awk '{ print $2; }' )
for m in $VMS ; do
echo "$m"
MAC_ADDR=$(virsh domiflist "$m" |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})")
NET_MODEL=$(virsh domiflist "$m" | tail -n +3 | head -n -1 | awk '{ print $4; }')
set +e
virsh detach-interface "$m" network --mac "$MAC_ADDR" && sleep 3
virsh attach-interface "$m" network $NET_NAME --mac "$MAC_ADDR" --model "$NET_MODEL"
set -e
# $NET_HOOK "$m" stopped && sleep 3
# $NET_HOOK "$m" start
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.