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