-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetcontainer.sh
More file actions
executable file
·211 lines (183 loc) · 3.99 KB
/
Copy pathnetcontainer.sh
File metadata and controls
executable file
·211 lines (183 loc) · 3.99 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
#set -x
# Default parameters
# Prefix of container network
NET_PREFIX=24
# Name of bridge. Script expect that only one bridge will be used
BRIDGE='cnt_bridge'
function is_iface_exist {
ip link show $1 > /dev/null 2>&1
return $?
}
function create_bridge {
BR_NAME=$1
BR_IP=$2
BR_PREFIX=$3
if is_iface_exist $BR_NAME
then
echo "Interface $BR_NAME exist"
return
fi
ip link add $BR_NAME type bridge
ip addr add $BR_IP/$BR_PREFIX dev $BR_NAME
ip link set $BR_NAME up
}
function delete_bridge {
BR_NAME=$1
if is_iface_exist $BR_NAME
then
ip link del $BR_NAME
fi
}
function create_container {
CNT_NAME=$1
CNT_IP=$2
CNT_PREFIX=$3
BR_NAME=$4
HOST_IFACE="${CNT_NAME}_host"
CNT_IFACE="${CNT_NAME}_cnt"
# Create veth iface pair and connect host part to bridge
ip netns add $CNT_NAME
ip link add name $HOST_IFACE type veth peer name $CNT_IFACE
ip link set $CNT_IFACE netns $CNT_NAME
ip link set $HOST_IFACE master $BR_NAME
ip link set $HOST_IFACE up
# Condifure network in the container
ip netns exec $CNT_NAME ip addr add 127.0.0.1/8 dev lo
ip netns exec $CNT_NAME ip link set lo up
ip netns exec $CNT_NAME ip addr add $CNT_IP/$CNT_PREFIX dev $CNT_IFACE
ip netns exec $CNT_NAME ip link set $CNT_IFACE up
}
function list_containers {
CNTS=$(ip netns list | awk '{ print $1 }')
for CNT in $CNTS
do
IP=$(ip netns exec $CNT ip addr show ${CNT}_cnt 2>/dev/null | awk '/inet / { print $2 }')
if [[ "x$IP" != "x" ]]
then
echo $CNT $IP
fi
done
}
function is_container {
CNT_NAME=$1
for CNT in $(ip netns list | awk '{ print $1 }')
do
if [[ $CNT == $CNT_NAME ]]
then
return 0
fi
done
return 1
}
function delete_container {
CNT_NAME=$1
ip netns delete $CNT_NAME
}
function run_command {
CNT_NAME=$1
shift
ip netns exec $CNT_NAME "$@"
}
function print_usage {
echo "netcontaner.sh <command> [args]"
echo "Possible commands are:"
echo " create_bridge IPADDR"
echo " delete_bridge"
echo " create_container NAME IPADDR"
echo " delete_container NAME"
echo " list"
echo " show"
echo " run CONTAINER_NAME COMMAND"
echo " help"
}
# Main script
# Check for root
ID=$(id -u)
if [[ "$ID" != "0" ]]
then
echo "Root privilages needed for managing containers!"
exit 1
fi
COMMAND=$1
shift
case $COMMAND in
create_bridge)
if [[ $# -ne 1 ]]
then
echo "IP address needed for bridge creation"
exit 1
fi
create_bridge $BRIDGE $1 $NET_PREFIX
;;
delete_bridge)
NUM_C=$(list_containers | wc -l)
if [[ $NUM_C -ne 0 ]]
then
echo "Delete these containers first:"
list_containers
exit 1
fi
delete_bridge $BRIDGE
;;
create_container)
if [[ $# -lt 2 ]]
then
echo "Need to provide name and IP of container"
exit 1
fi
create_container $1 $2 $NET_PREFIX $BRIDGE
;;
list)
list_containers
;;
show)
echo "Bridge:"
if ip addr show $BRIDGE 1>/dev/null 2>&1
then
echo "$BRIDGE $(ip addr show $BRIDGE 2>/dev/null | awk '/inet / { print $2 }')"
else
echo "Not configured"
fi
echo "Containers:"
list_containers
;;
delete_container)
if [[ $# -ne 1 ]]
then
echo "You need to provide container name only"
exit 1
fi
if ! is_container $1
then
echo "Container $1 not exist"
exit 1
fi
N_PIDS=$(ip netns pids $1| wc -l)
if [[ $N_PIDS -ne 0 ]]
then
echo "Terminate this processes first:"
ip netns pids $1
exit 1
fi
delete_container $1
;;
run)
if [[ $# -lt 2 ]]
then
echo "Need to provide name of container and command"
exit 1
fi
CNT_NAME=$1
shift
run_command $CNT_NAME "$@"
;;
help)
print_usage
;;
*)
echo "Unknown command: $COMMAND"
print_usage
exit 1
;;
esac