2
# kvm init script Takes care for all VMM tasks
5
# description: The KVM is a kernel level Virtual Machine Monitor. \
6
# Currently it starts a bridge and attached eth0 for it
10
ifnum=${ifnum:-$(ip route list | awk '/^default / { print $NF }' | sed 's/^[^0-9]*//')}
12
switch=${sw0:-sw${ifnum}}
13
pif=${pif:-eth${ifnum}}
14
antispoof=${antispoof:-no}
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
27
if [ -f /etc/sysconfig/network-scripts/ifcfg-${pif} ]; then
28
. /etc/sysconfig/network-scripts/ifcfg-${pif}
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
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
54
ip addr add ${addr} broadcast ${broadcast} dev $1
61
if ip link show "$1" >/dev/null 2>/dev/null
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
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
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}\( \|$\)/ {
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
126
if [ $BOOTPROTO = "dhcp" ]; then
127
ifdown ${src} >/dev/null 2>&1 || true
128
ip link set ${src} up >/dev/null 2>&1
130
pkill dhclient >/dev/null 2>&1
131
for ((i=0;i<3;i++)); do
132
pgrep dhclient >/dev/null 2>&1 || i=4
135
dhclient ${dst} >/dev/null 2>&1
138
ifconfig ${src} 0.0.0.0
140
transfer_routes ${src} ${dst}
141
ip route add default via ${gateway} dev ${dst}
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
155
echo '============================================================'
164
echo '============================================================'
165
gateway=$(ip route list | awk '/^default / { print $3 }')
166
ping -c 1 ${gateway} || true
167
echo '============================================================'
171
if [ "${switch}" = "null" ] ; then
175
create_switch ${switch}
176
add_to_switch ${switch} ${pif}
177
change_ips ${pif} ${switch}
179
if [ ${antispoof} = 'yes' ] ; then
183
grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe kvm-intel
184
grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe kvm-amd
188
if [ "${switch}" = "null" ]; then
191
if ! link_exists "$switch"; then
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
207
echo -n $"Starting KVM: "
213
echo -n $"Shutting down KVM: "
219
status ${pif} ${switch}
223
echo "Unknown command: $command" >&2
224
echo 'Valid commands are: start, stop, status' >&2