Ubuntu10.04下kvm网桥中提到从网上找到一个方法,修改
/etc/network/interfaces然后重启networking服务实现。不过这样有一个弊端,右下脚托盘里的“网络管理器小程序”不可用了,以前点击这个图标,可以方便的切换不同的网络,可以使用无线,现在不能用了还真不习惯。网络上的文章也是说“采用修改
/etc/network/interfaces的方法之后,需要卸载networkmanager
”
。难道真的没有办法吗?
办法当然是有的,而且更简单。
我看了红旗Asianux3中/etc/init.d/kvm和/etc/init.d/libvirt两个脚本,基本就明白了。其中除了加载kvm-intel核心模块之外,最大的作用是对网络的处理。是通过brctl命令添加网桥,简单之极。后来发现已经有做虚拟化的同事整理出来一个脚本,测试对ubuntu直接生效。
脚本贴出如下:
#!/bin/bash
# filename : crbr.sh
# version : 1.0
# description: create bridge shell scripts
# variable define
ifnum=${ifnum:-$(echo $1 | awk '/^sw/ { print $NF }' | sed 's/^sw//' )}
if [ ${ifnum}"0" = "0" ]; then
echo "argument fortmat is sw[0-9]"
fi
# function define
# crbr.sh use method
method() {
echo "usage: $0 arguments"
echo "argument must be sw+number"
echo "example: $0 sw0 add/del/help"
echo "create switch device name sw0 default use eth0 "
}
create_switch() {
local switch=$1
if [ ! -e "/sys/class/net/${switch}/bridge" ]; then
brctl addbr ${switch} >/dev/null 2>&1
brctl stp ${switch} off >/dev/null 2>&1
brctl setfd ${switch} 0.1 >/dev/null 2>&1
fi
ip link set ${switch} up >/dev/null 2>&1
}
add_to_switch () {
local switch=$1
local dev=$2
if [ ! -e "/sys/class/net/${switch}/brif/${dev}" ]; then
brctl addif ${switch} ${dev} >/dev/null 2>&1
fi
ip link set ${dev} up >/dev/null 2>&1
}
add() {
echo "adding!"
switch=sw${ifnum}
pif=eth${ifnum}
create_switch ${switch}
add_to_switch ${switch} ${pif}
change_ips ${pif} ${switch}
kmod=`lsmod | grep kvm`
if [ "${kmod}0" = "0" ]; then
add_kvmmod
fi
}
del() {
echo "deleting!"
switch=sw${ifnum}
pif=eth${ifnum}
change_ips ${switch} ${pif}
ip link set ${switch} down
brctl delbr ${switch}
}
change_ips() {
local src=$1
local dst=$2
get_ip_info ${src}
ifconfig ${src} 0.0.0.0
do_ifup ${dst}
ip route add default via ${gateway} dev ${dst}
}
get_ip_info() {
addr=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
gateway=$(ip route list | awk '/^default / { print $3 }')
broadcast=$(/sbin/ip addr show dev $1 | grep inet | awk '/brd / { print $4 }')
}
do_ifup() {
if [ ${addr} ] ; then
ip addr flush $1
ip addr add ${addr} broadcast ${broadcast} dev $1
ip link set dev $1 up
fi
}
add_kvmmod() {
grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe kvm-intel
grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe kvm-amd
}
# main scripts
if [ $# != 2 ];then
method
fi
case "$2" in
add)
echo -n $"Add swicth $1"
add
echo
;;
del)
echo -n $"Delete swicth $1"
del
echo
;;
*)
echo -n $"Manual list"
method
echo
;;
esac
|
用法:
root@fan3838:~# /home/fan3838/bin/crbr.sh sw0 add
Add swicth
|
当然KVM中添加网卡时,需要添加一个到sw0的网桥。每次启动KVM中虚拟机时,先执行上面的命令即可。
阅读(865) | 评论(0) | 转发(0) |