-
-
Save penglei/7b682283575297be69e9b4c6c5ed1fda to your computer and use it in GitHub Desktop.
Cleanly restart the default libvirt network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Yury V. Zaytsev <[email protected]> (C) 2011 | |
# | |
# This work is herewith placed in public domain. | |
# | |
# Use this script to cleanly restart the default libvirt network after its | |
# definition have been changed (e.g. added new static MAC+IP mappings) in order | |
# for the changes to take effect. Restarting the network alone, however, causes | |
# the guests to lose connectivity with the host until their network interfaces | |
# are re-attached. | |
# | |
# The script re-attaches the interfaces by obtaining the information about them | |
# from the current libvirt definitions. It has the following dependencies: | |
# | |
# - virsh (obviously) | |
# - tail / head / grep / awk / cut | |
# - XML::XPath (e.g. perl-XML-XPath package) | |
# | |
# Note that it assumes that the guests have exactly 1 NAC each attached to the | |
# given network! Extensions to account for more (or none) interfaces etc. are, | |
# of course, most welcome. | |
# | |
# ZYV | |
# | |
set -e | |
set -u | |
NETWORK_NAME=default | |
NETWORK_HOOK=/etc/libvirt/hooks/qemu | |
#virsh net-define /opt/config/libvirt/network-$NETWORK_NAME.xml | |
virsh net-destroy $NETWORK_NAME | |
virsh net-start $NETWORK_NAME | |
MACHINES=$( virsh list | tail -n +3 | head -n -1 | awk '{ print $2; }' ) | |
for m in $MACHINES ; do | |
echo "$m" | |
MACHINE_INFO=$( virsh dumpxml "$m" | xpath -e /domain/devices/interface[1] 2> /dev/null ) | |
echo "$MACHINE_INFO" | |
MACHINE_MAC=$( echo "$MACHINE_INFO" | grep "mac address" | cut -d '"' -f 2 ) | |
echo "$MACHINE_MAC" | |
MACHINE_MOD=$( echo "$MACHINE_INFO" | grep "model type" | cut -d '"' -f 2 ) | |
set +e | |
virsh detach-interface "$m" network --mac "$MACHINE_MAC" && sleep 3 | |
virsh attach-interface "$m" network $NETWORK_NAME --mac "$MACHINE_MAC" --model "$MACHINE_MOD" | |
set -e | |
#$NETWORK_HOOK "$m" stopped && sleep 3 | |
#$NETWORK_HOOK "$m" start | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe you need to restart vm to make it effective.