Debian Codegraph @

dev-utils/fai-mk-network

raw
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # fai-mk-network -- setup a test network for FAI
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2015-2022 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12
13 PATH=/sbin:/bin:/usr/sbin:/usr/bin
14
15 tapnum=9 # how many tap devies will be created
16 brip=192.168.33.250 # IP of bridge device
17 brname=br0
18
19 # default network device used for routing to the outside internet
20 netdev=$(ip route | awk '/^default/ {print $5}'|head -1)
21
22 usage() {
23
24 cat <<EOF
25 fai-mk-network, setup a test network for FAI
26
27 Copyright (C) 2015-2022 by Thomas Lange
28
29 Usage: fai-mk-network {OPTIONS} USER
30
31 -r Remove the network setup.
32 -P Do not put the bridge into a private network
33 -i <NIC> Use NIC instead of default network device
34
35 DESCRIPTION
36 fai-mk-network creates $tapnum tap devices which are added to a software bridge.
37 The tap devices will belong to the user specified. This bridge will be on a
38 private subnet ($brip/24) unless -P is specified. Also a NATing is enabled for
39 the private subnet. Using -P the bridge will use the network of $netdev (or -i) instead
40 of a private subnet. Do not call this command in a network mounted directory
41 when using -P. You can configure different settings by modifying some variables
42 in the script itself.
43
44 Using fai-kvm(1) you can start virtual machines which are connected
45 to one of the tap devices.
46 EOF
47 exit 0
48 }
49
50
51 mk_tap_devices() {
52
53 for i in $(eval echo {1..$tapnum}); do
54 [ -f /sys/class/net/tap$i/address ] && continue
55 ip tuntap add dev tap$i mode tap user $user
56 done
57 }
58
59 mk_bridge() {
60
61 if [ -d /sys/class/net/$brname ]; then
62 echo "Bridge $brname already exists. Aborting."
63 exit 4
64 fi
65
66 if [ $private -eq 0 ]; then
67 dhcpcd -k $netdev
68 ip addr flush $netdev # remove IP address of real nic
69 ip link set $netdev down
70 ip link set $netdev name r$netdev
71 brname=$netdev # use name of NIC for bridge
72 fi
73
74 ip link add name $brname type bridge
75 ip link set $brname up
76 if [ $private -eq 0 ]; then
77 ip link set r$netdev master $brname
78 ip link set r$netdev up
79 dhcpcd $netdev
80 else
81 ip addr add $brip/24 brd + dev $brname
82 fi
83
84 for i in $(eval echo {1..$tapnum}); do
85 [ -f /sys/class/net/tap$i/address ] || continue
86 ip link set tap$i master $brname # add device to the bridge
87 ip link set tap$i up
88 done
89 }
90
91 rm_bridge() {
92
93 for i in $(eval echo {1..$tapnum}); do
94 [ -f /sys/class/net/tap$i/address ] || continue
95 ip link set tap$i nomaster
96 ip link set tap$i down
97 ip tuntap del dev tap$i mode tap
98 done
99
100 if [ $private -eq 0 ]; then
101 brname=$netdev # use name of NIC for bridge
102 dhcpcd -k $netdev
103 ip link set r$netdev nomaster
104 fi
105
106
107 ip link set $brname down
108 ip addr flush $brname
109 ip link delete $brname type bridge
110
111 if [ $private -eq 1 ]; then
112 iptables -t nat -D POSTROUTING -o $netdev -j MASQUERADE
113 sysctl -w net.ipv4.ip_forward=0
114 else
115 ip link set r$netdev down
116 ip link set r$netdev name $netdev
117 ip link set $netdev up
118 dhcpcd $netdev
119 fi
120 exit 0
121 }
122
123
124 remove=0
125 private=1 # private network by default
126
127 while getopts Phri: opt ; do
128 case "$opt" in
129 r) remove=1;;
130 P) private=0 ;;
131 h) usage ;;
132 i) netdev=$OPTARG ;;
133 esac
134 done
135 shift $((OPTIND - 1))
136
137 if [ $(id -u) != "0" ]; then
138 echo "ERROR: You must run this program as root."
139 echo
140 usage
141 fi
142
143 if [ $remove -eq 1 ]; then
144 rm_bridge
145 fi
146
147 if [ -z "$1" ]; then
148 echo "ERROR: Please add a user name which will own the tap devices."
149 echo ""
150 usage
151 fi
152 user=$1
153
154
155 mk_tap_devices
156 mk_bridge
157
158 # enable forward IP and do NATing
159 if [ $private -eq 1 ]; then
160 sysctl -w net.ipv4.ip_forward=1
161 iptables -t nat -I POSTROUTING -o $netdev -j MASQUERADE
162 fi
163
164
165 # entry for /etc/network/interfaces:
166 # auto br0
167 # iface br0 inet static
168 # address 192.168.33.1
169 # netmask 255.255.255.0
170 # bridge_ports regex (tap).*