~ubuntu-branches/ubuntu/wily/qemu-kvm-spice/wily

« back to all changes in this revision

Viewing changes to kvm/scripts/kvm

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-10-19 10:44:56 UTC
  • Revision ID: james.westby@ubuntu.com-20111019104456-xgvskumk3sxi97f4
Tags: upstream-0.15.0+noroms
ImportĀ upstreamĀ versionĀ 0.15.0+noroms

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# kvm init script              Takes care for all VMM tasks
 
3
#
 
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
 
7
 
 
8
dir=$(dirname "$0")
 
9
 
 
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
 
16
 
 
17
if [ -f /etc/sysconfig/network-scripts/network-functions ]; then
 
18
    . /etc/sysconfig/network-scripts/network-functions
 
19
fi
 
20
 
 
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}
 
25
fi
 
26
 
 
27
if [ -f /etc/sysconfig/network-scripts/ifcfg-${pif} ]; then
 
28
    . /etc/sysconfig/network-scripts/ifcfg-${pif}
 
29
fi
 
30
 
 
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 }')
 
35
}
 
36
 
 
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
 
46
    fi
 
47
}
 
48
 
 
49
 
 
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
 
56
     fi
 
57
}
 
58
 
 
59
link_exists()
 
60
{
 
61
    if ip link show "$1" >/dev/null 2>/dev/null
 
62
    then
 
63
        return 0
 
64
    else
 
65
        return 1
 
66
    fi
 
67
}
 
68
 
 
69
create_switch () {
 
70
    local switch=$1
 
71
 
 
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
 
76
    fi
 
77
    ip link set ${switch} up >/dev/null 2>&1
 
78
}
 
79
 
 
80
 
 
81
add_to_switch () {
 
82
    local switch=$1
 
83
    local dev=$2
 
84
 
 
85
    if [ ! -e "/sys/class/net/${switch}/brif/${dev}" ]; then
 
86
        brctl addif ${switch} ${dev} >/dev/null 2>&1
 
87
    fi
 
88
 
 
89
    ip link set ${dev} up >/dev/null 2>&1
 
90
}
 
91
 
 
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}\( \|$\)/ {
 
101
  h
 
102
  s/^/ip route del /
 
103
  P
 
104
  g
 
105
  s/${src}/${dst}/
 
106
  s/^/ip route add /
 
107
  P
 
108
  d
 
109
}" | sh -e
 
110
}
 
111
 
 
112
 
 
113
change_ips() {
 
114
    local src=$1
 
115
    local dst=$2
 
116
 
 
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"
 
123
        fi
 
124
    fi
 
125
 
 
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}
 
142
    fi
 
143
}
 
144
               
 
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
 
149
}
 
150
 
 
151
status () {
 
152
    local dev=$1
 
153
    local sw=$2
 
154
    
 
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 '============================================================'
 
168
}
 
169
 
 
170
start () {
 
171
    if [ "${switch}" = "null" ] ; then
 
172
        return
 
173
    fi
 
174
 
 
175
    create_switch ${switch}
 
176
    add_to_switch ${switch} ${pif}
 
177
    change_ips ${pif} ${switch}
 
178
 
 
179
    if [ ${antispoof} = 'yes' ] ; then
 
180
        antispoofing
 
181
    fi
 
182
 
 
183
    grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe kvm-intel
 
184
    grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe kvm-amd
 
185
}
 
186
 
 
187
stop () {
 
188
    if [ "${switch}" = "null" ]; then
 
189
        return
 
190
    fi
 
191
    if ! link_exists "$switch"; then
 
192
        return
 
193
    fi
 
194
 
 
195
    change_ips ${switch} ${pif}
 
196
    ip link set ${switch} down
 
197
    brctl delbr ${switch}
 
198
 
 
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
 
202
}
 
203
 
 
204
 
 
205
case "$command" in
 
206
    start)
 
207
        echo -n $"Starting KVM: "
 
208
        start
 
209
        echo
 
210
        ;;
 
211
    
 
212
    stop)
 
213
        echo -n $"Shutting down KVM: "
 
214
        stop
 
215
        echo
 
216
        ;;
 
217
 
 
218
    status)
 
219
        status ${pif} ${switch}
 
220
        ;;
 
221
 
 
222
    *)
 
223
        echo "Unknown command: $command" >&2
 
224
        echo 'Valid commands are: start, stop, status' >&2
 
225
        exit 1
 
226
esac