-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·122 lines (98 loc) · 2.43 KB
/
setup.sh
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
#!/bin/bash
create_vxlan_network() { # <vni>
if (( $# < 1 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
vni="$1"
vx="vxlan$vni"
br="br$vni"
port="${2:-4789}"
group="239.1.1.$vni"
nic="enp94s0"
## Create the bridge and vxlan interface
sudo ip link add $vx type vxlan id $vni group $group dstport $port dev $nic
sudo ip link add $br type bridge
sudo ip link set $vx master $br
sudo ip link set $vx up
sudo ip link set $br up
}
delete_vxlan_network() { # <vni>
if (( $# != 1 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
vni="$1"
vx="vxlan$vni"
br="br$vni"
sudo ip link delete $br
sudo ip link delete $vx
}
create_container() { # <id> <vni>
if (( $# != 2 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
id="$1"
ip="10.0.1.$id/24"
ns="ns$id"
veth="veth$id"
vethp="${veth}p"
vni="$2"
br="br$vni"
## Create the virtual interface connected to the overlay network
sudo ip netns add $ns
# VxLAN adds 50 bytes of headers, so MTU has to be adjusted
sudo ip link add $veth mtu 1450 type veth peer name $vethp mtu 1450
sudo ip link set $vethp master $br
sudo ip link set $veth netns $ns
sudo ip netns exec $ns ip addr add $ip dev $veth
sudo ip netns exec $ns ip link set $veth up
sudo ip netns exec $ns ip link set lo up
sudo ip link set $vethp up
}
delete_container() { # <id>
if (( $# != 1 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
id="$1"
ns="ns$id"
veth="veth$id"
vethp="${veth}p"
sudo ip link delete $vethp
sudo ip netns del $ns
}
create_containers() { # <start_id> <end_id> <vni>
if (( $# != 3 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
beg_id="$1"
end_id="$2"
vni="$3"
for id in $(seq $beg_id $end_id); do
create_container $id $vni
done
}
delete_containers() { # <start_id> <end_id>
if (( $# != 2 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
beg_id="$1"
end_id="$2"
for id in $(seq $beg_id $end_id); do
delete_container $id
done
}
nsrun() { # <id> <command...>
if (( $# < 2 )); then
echo " :: ERROR :: $0 :: bad argument"
return 1
fi
ns="ns$1"
shift
cmd="$@"
sudo ip netns exec $ns $cmd
}