qemu-kvm 网络 nat模式 (四)

qemu-ifup-NAT

#!/bin/bash
BRIDGE=virbr0
NETWORK=192.168.122.0
GATEWAY=192.168.122.1
NETMASK=255.255.255.0
DHCPRANGE=192.168.122.2,192.168.122.254
TFTPROOT=
BOOTP=
function check_bridge()
{
if brctl show | grep "^BRIDGE" &> /dev/null;then
return 1
else
return 0
fi
}
function create_bridge()
{
brctl addbr "BRIDGE"
brctl stp "BRIDGE" on
brctl setfd "BRIDGE" 0
ifconfig "$BRIDGE" "GATEWAY" netmask "$NETMASK" up
}
function enable_ip_forward()
{
echo 1 > /proc/sys/net/ipv4/ip_forward
}
function add_filter_rules()
{
iptables -t nat -A POSTROUTING -s "$NETWORK"/"$NETMASK" ! -d "$NETWORK"/"$NETMASK" -j MASQUERADE
}
function start_dnsmasq()
{
ps -ef | grep "dnsmasq" |grep -v "grep" &> /dev/null
if [ $? -eq 0 ];then
echo "warning:dnsmasq is already running"
return 1
fi
dnsmasq --strict-order --except-interface=lo --interface=$BRIDGE --listen-address=$GATEWAY --bind-interfaces --dhcp-range=$DHCPRANGE --conf-file="" --pid-file=/var/run/qemu-dhcp-$BRIDGE.pid --dhcp-leasefile=/var/run/qemu-dhcp-$BRIDGE.leases --dhcp-no-override ${TFTPROOT:+"--enable-tftp"} ${TFTPROOT:+"--tftp-root=$TFTPROOT"} ${BOOTP:+"--dhcp-boot=$BOOTP"}
}
function setup_bridge_nat()
{
check_bridge "$BRIDGE"
if [ $? -eq 0 ];then
create_bridge
fi
enable_ip_forward
add_filter_rules "$BRIDGE"
start_dnsmasq "$BRIDGE"
}
if [ -n "$1"];then
setup_bridge_nat
ifconfig "$1" 0.0.0.0 up
brctl addif "$BRIDGE" "$1"
exit 0
else
echo "Error:no interface specified"
exit 1
fi



qemu-ifdown-NAT

#!/bin/bash
BRIDGE="virbr0"
if [ -n "$1" ];then
ip link set $1 down
brctl delif "$BRIDGE" $1
ip link set "$BRIDGE" down
brctl delbr "$BRIDGE"
iptables -t nat -F
exit 0
else
echo "Error: no interface specified"
exit 1
fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值