kvm: qemu: provide a reset method for virtio
[kvm-userspace.git] / scripts / kvm
blobcddc931fd3b289f3c325e23b55f261e996328bd6
1 #!/bin/sh
2 # kvm init script Takes care for all VMM tasks
4 # chkconfig: - 99 01
5 # description: The KVM is a kernel level Virtual Machine Monitor. \
6 # Currently it starts a bridge and attached eth0 for it
8 dir=$(dirname "$0")
10 ifnum=${ifnum:-$(ip route list | awk '/^default / { print $NF }' | sed 's/^[^0-9]*//')}
11 ifnum=${ifnum:-0}
12 switch=${sw0:-sw${ifnum}}
13 pif=${pif:-eth${ifnum}}
14 antispoof=${antispoof:-no}
15 command=$1
17 if [ -f /etc/sysconfig/network-scripts/network-functions ]; then
18 . /etc/sysconfig/network-scripts/network-functions
21 #check for bonding link aggregation
22 bond_int=$(awk < /etc/sysconfig/network-scripts/ifcfg-${pif} '/^MASTER=/ { print $BF }' | sed 's/MASTER=//')
23 if [ ${bond_int}"0" != "0" ]; then
24 pif=${bond_int}
27 if [ -f /etc/sysconfig/network-scripts/ifcfg-${pif} ]; then
28 . /etc/sysconfig/network-scripts/ifcfg-${pif}
31 get_ip_info() {
32 addr=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
33 gateway=$(ip route list | awk '/^default / { print $3 }')
34 broadcast=$(/sbin/ip addr show dev $1 | grep inet | awk '/brd / { print $4 }')
37 #When a bonding device link goes down, its slave interfaces
38 #are getting detached so they should be re-added
39 bond_link_up () {
40 dev=$1
41 is_bonding=$(echo ${dev} | awk '/^bond/ { print $NF }')
42 if [ ${is_bonding}"0" != "0" ]; then
43 for slave in `awk < /proc/net/bonding/bond0 '/Slave Interface: / {print $3 }'`; do
44 ifenslave $dev $slave
45 done
50 do_ifup() {
51 if [ ${addr} ] ; then
52 ip addr flush $1
53 bond_link_up $1
54 ip addr add ${addr} broadcast ${broadcast} dev $1
55 ip link set dev $1 up
59 link_exists()
61 if ip link show "$1" >/dev/null 2>/dev/null
62 then
63 return 0
64 else
65 return 1
69 create_switch () {
70 local switch=$1
72 if [ ! -e "/sys/class/net/${switch}/bridge" ]; then
73 brctl addbr ${switch} >/dev/null 2>&1
74 brctl stp ${switch} off >/dev/null 2>&1
75 brctl setfd ${switch} 0.1 >/dev/null 2>&1
77 ip link set ${switch} up >/dev/null 2>&1
81 add_to_switch () {
82 local switch=$1
83 local dev=$2
85 if [ ! -e "/sys/class/net/${switch}/brif/${dev}" ]; then
86 brctl addif ${switch} ${dev} >/dev/null 2>&1
89 ip link set ${dev} up >/dev/null 2>&1
92 #taken from Xen
93 transfer_routes () {
94 local src=$1
95 local dst=$2
96 # List all routes and grep the ones with $src in.
97 # Stick 'ip route del' on the front to delete.
98 # Change $src to $dst and use 'ip route add' to add.
99 ip route list | sed -ne "
100 /dev ${src}\( \|$\)/ {
102 s/^/ip route del /
105 s/${src}/${dst}/
106 s/^/ip route add /
109 }" | sh -e
113 change_ips() {
114 local src=$1
115 local dst=$2
117 #take care also for case we do not have /etc/sysconfig data (the switch as a src case)
118 if [ -x $BOOTPROTO ]; then
119 if [ -x $(pgrep dhclient) ];then
120 BOOTPROTO="null"
121 else
122 BOOTPROTO="dhcp"
126 if [ $BOOTPROTO = "dhcp" ]; then
127 ifdown ${src} >/dev/null 2>&1 || true
128 ip link set ${src} up >/dev/null 2>&1
129 bond_link_up ${src}
130 pkill dhclient >/dev/null 2>&1
131 for ((i=0;i<3;i++)); do
132 pgrep dhclient >/dev/null 2>&1 || i=4
133 sleep 1
134 done
135 dhclient ${dst} >/dev/null 2>&1
136 else
137 get_ip_info ${src}
138 ifconfig ${src} 0.0.0.0
139 do_ifup ${dst}
140 transfer_routes ${src} ${dst}
141 ip route add default via ${gateway} dev ${dst}
145 antispoofing () {
146 iptables -P FORWARD DROP >/dev/null 2>&1
147 iptables -F FORWARD >/dev/null 2>&1
148 iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT >/dev/null 2>&1
151 status () {
152 local dev=$1
153 local sw=$2
155 echo '============================================================'
156 ip addr show ${dev}
157 ip addr show ${sw}
158 echo ' '
159 brctl show ${sw}
160 echo ' '
161 ip route list
162 echo ' '
163 route -n
164 echo '============================================================'
165 gateway=$(ip route list | awk '/^default / { print $3 }')
166 ping -c 1 ${gateway} || true
167 echo '============================================================'
170 start () {
171 if [ "${switch}" = "null" ] ; then
172 return
175 create_switch ${switch}
176 add_to_switch ${switch} ${pif}
177 change_ips ${pif} ${switch}
179 if [ ${antispoof} = 'yes' ] ; then
180 antispoofing
183 grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe kvm-intel
184 grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe kvm-amd
187 stop () {
188 if [ "${switch}" = "null" ]; then
189 return
191 if ! link_exists "$switch"; then
192 return
195 change_ips ${switch} ${pif}
196 ip link set ${switch} down
197 brctl delbr ${switch}
199 grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe -r kvm-intel
200 grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe -r kvm-amd
201 /sbin/modprobe -r kvm
205 case "$command" in
206 start)
207 echo -n $"Starting KVM: "
208 start
209 echo
212 stop)
213 echo -n $"Shutting down KVM: "
214 stop
215 echo
218 status)
219 status ${pif} ${switch}
223 echo "Unknown command: $command" >&2
224 echo 'Valid commands are: start, stop, status' >&2
225 exit 1
226 esac