-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathkvm-network-restart
79 lines (68 loc) · 2.64 KB
/
kvm-network-restart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
#
# Yury V. Zaytsev <yury@shurup.com> (C) 2011
#
# Modified by Erico Mendonca (erico.mendonca@suse.com) to add the following:
#
# - SUSE paths
# - progress messags
# - tighter scope (only change the VMs that use the supplied network name)
#
# (dec 2018)
#
# 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
if [ -z $1 ]; then
echo "Usage: $0 <network name>"
exit 1
fi
command -v xpath >/dev/null 2>&1 || { echo >&2 "I require xpath, but it's not installed. Try apt install libxml-xapth-perl (Debian/Ubuntu). Aborting."; exit 1; }
command -v virsh >/dev/null 2>&1 || { echo >&2 "I require virsh, but it's not installed?. Aborting."; exit 1; }
NETWORK_NAME=$1
NETWORK_HOOK=/etc/libvirt/hooks/qemu
echo "--> restarting network $NETWORK_NAME"
virsh net-define /etc/libvirt/qemu/networks/$NETWORK_NAME.xml
virsh net-destroy $NETWORK_NAME
virsh net-start $NETWORK_NAME
echo "--> network restarted"
MACHINES=$( virsh list | tail -n +3 | head -n -1 | awk '{ print $2; }' )
for m in $MACHINES ; do
echo "---> processing VM: $m"
MACHINE_INFO=$( virsh dumpxml "$m" | xpath -e /domain/devices/interface[1] 2> /dev/null )
MACHINE_MAC=$( echo "$MACHINE_INFO" | grep "mac address" | cut -d '"' -f 2 )
MACHINE_MOD=$( echo "$MACHINE_INFO" | grep "model type" | cut -d '"' -f 2 )
MACHINE_NET=$( echo "$MACHINE_INFO" | grep "source network" | cut -d '"' -f 2 )
if [ "$MACHINE_NET" == "$NETWORK_NAME" ]; then
echo "--> detaching and attaching interfaces for mac address $MACHINE_MAC ($m)"
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
echo "--> calling network hook script"
$NETWORK_HOOK "$m" stopped && sleep 3
$NETWORK_HOOK "$m" start
else
echo "--> machine $m is not attached to network $NETWORK_NAME, skipping"
fi
echo "--> done"
done