xref: /DragonOS/tools/qemu/ifup-nat (revision cde5492f725681ed89abe1e6eb088e05d943d793)
1*cde5492fSlogin#!/bin/bash
2*cde5492fSlogin# 设置 bridge 名称
3*cde5492fSloginBRIDGE=dragonos-bridge
4*cde5492fSlogin# 设置网络信息
5*cde5492fSloginNETWORK=192.168.137.0
6*cde5492fSloginNETMASK=255.255.255.0
7*cde5492fSloginGATEWAY=192.168.137.1
8*cde5492fSloginDHCPRANGE=192.168.137.100,192.168.137.200
9*cde5492fSlogin# 启用PXE支持的可选参数
10*cde5492fSloginTFTPROOT=
11*cde5492fSloginBOOTP=
12*cde5492fSlogin
13*cde5492fSloginfunction check_bridge()
14*cde5492fSlogin{
15*cde5492fSlogin        if brctl show | grep "^$BRIDGE" &> /dev/null; then
16*cde5492fSlogin                return 1
17*cde5492fSlogin        else
18*cde5492fSlogin                return 0
19*cde5492fSlogin        fi
20*cde5492fSlogin}
21*cde5492fSlogin
22*cde5492fSloginfunction create_bridge()
23*cde5492fSlogin{
24*cde5492fSlogin        brctl addbr "$BRIDGE"
25*cde5492fSlogin        brctl stp "$BRIDGE" on
26*cde5492fSlogin        brctl setfd "$BRIDGE" 0
27*cde5492fSlogin        ifconfig "$BRIDGE" "$GATEWAY" netmask "$NETMASK" up
28*cde5492fSlogin}
29*cde5492fSlogin
30*cde5492fSloginfunction enable_ip_forward()
31*cde5492fSlogin{
32*cde5492fSlogin        echo 1 > /proc/sys/net/ipv4/ip_forward
33*cde5492fSlogin}
34*cde5492fSlogin
35*cde5492fSloginfunction add_filter_rules()
36*cde5492fSlogin{
37*cde5492fSlogin        iptables -t nat -A POSTROUTING -s "$NETWORK"/"$NETMASK" \
38*cde5492fSlogin                ! -d "$NETWORK"/"$NETMASK" -j MASQUERADE
39*cde5492fSlogin}
40*cde5492fSlogin
41*cde5492fSloginfunction start_dnsmasq()
42*cde5492fSlogin{
43*cde5492fSlogin# 禁止重复运行dnsmasq
44*cde5492fSlogin        ps -ef | grep "dnsmasq" | grep -v "grep" &> /dev/null
45*cde5492fSlogin        if [ $? -eq 0 ]; then
46*cde5492fSlogin                echo "dnsmasq 已经在运行"
47*cde5492fSlogin                return 1
48*cde5492fSlogin        fi
49*cde5492fSlogin        dnsmasq \
50*cde5492fSlogin                --strict-order \
51*cde5492fSlogin                --except-interface=lo \
52*cde5492fSlogin                --interface=$BRIDGE \
53*cde5492fSlogin                --listen-address=$GATEWAY \
54*cde5492fSlogin                --bind-interfaces \
55*cde5492fSlogin                --dhcp-range=$DHCPRANGE \
56*cde5492fSlogin                --conf-file="" \
57*cde5492fSlogin                --pid-file=/var/run/qemu-dhcp-$BRIDGE.pid \
58*cde5492fSlogin                --dhcp-leasefile=/var/run/qemu-dhcp-$BRIDGE.leases \
59*cde5492fSlogin                --dhcp-no-override \
60*cde5492fSlogin                ${TFTPROOT:+"--enable-tftp"} \
61*cde5492fSlogin                ${TFTPROOT:+"--tftp-root=$TFTPROOT"} \
62*cde5492fSlogin                ${BOOTP:+"--dhcp-boot=$BOOTP"}
63*cde5492fSlogin}
64*cde5492fSlogin
65*cde5492fSloginfunction setup_bridge_nat()
66*cde5492fSlogin{
67*cde5492fSlogin        check_bridge "$BRIDGE"
68*cde5492fSlogin        if [ $? -eq 0 ]; then
69*cde5492fSlogin                create_bridge
70*cde5492fSlogin        fi
71*cde5492fSlogin        enable_ip_forward
72*cde5492fSlogin        add_filter_rules "$BRIDGE"
73*cde5492fSlogin        start_dnsmasq "$BRIDGE"
74*cde5492fSlogin}
75*cde5492fSlogin
76*cde5492fSlogin# 安装前需要检查$1参数
77*cde5492fSloginif [ -n "$1" ]; then
78*cde5492fSlogin        setup_bridge_nat
79*cde5492fSlogin        brctl addif "$BRIDGE" "$1"
80*cde5492fSlogin        ifconfig "$1" 0.0.0.0 up
81*cde5492fSlogin        exit 0
82*cde5492fSloginelse
83*cde5492fSlogin        echo "发现错误:没有指定接口"
84*cde5492fSlogin        exit 1
85*cde5492fSloginfi
86