-
Notifications
You must be signed in to change notification settings - Fork 4
/
r00_emptyNet.sh
executable file
·68 lines (55 loc) · 1.52 KB
/
r00_emptyNet.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
#!/bin/bash -x
source 00cfg
CONT_NAME=winpe
BR_NAME=brhost
IPP=$IP_DNSMASQ/24
NIC_H=wpe_host
NIC_C=wpe_cont
rin() {
ip netns exec ${CONT_NAME} "$@"
}
# b: elevate privileges
# requires NOPASS sudo
echo "UID=$UID"
if [ "$UID" -ne 0 ]; then
echo "Switching to root"
exec sudo "$0" "$@"
fi
# e: elevate privileges
docker kill winpe-dnsmasq
docker kill winpe-php-fpm
docker kill winpe-nginx
# start container
docker run -d --rm \
--network none \
--name ${CONT_NAME} \
--dns 127.0.0.1 \
--dns-search demo.iot \
--hostname router \
nicolaka/netshoot sleep infinity
brctl addbr $BR_NAME || true
ip li set dev $BR_NAME up
ip link add $NIC_H type veth peer name $NIC_C
ip link set $NIC_H up
brctl addif $BR_NAME $NIC_H
echo "Inspect .State.Running"
docker inspect -f {{.State.Running}} ${CONT_NAME}
echo "Inspect .State.Pid"
docker inspect -f {{.State.Pid}} ${CONT_NAME}
# make sure the container is running
until [ "x`docker inspect -f {{.State.Running}} ${CONT_NAME} || true`" == "xtrue" ]; do
echo "wait for container/state.running"
sleep 0.1
done
# wait for pid
until [ "x`docker inspect -f {{.State.Pid}} ${CONT_NAME} || true`" != "x" ]; do
echo "wait for container/state.pid"
sleep 0.1
done
# create namespace link
[ -d /var/run/netns ] || mkdir -p /var/run/netns
DPID=$(docker inspect --format '{{ .State.Pid }}' "${CONT_NAME}")
ln -sfT "/proc/$DPID/ns/net" "/var/run/netns/${CONT_NAME}"
ip link set netns ${CONT_NAME} dev $NIC_C
rin ip link set $NIC_C up
rin ip address add $IPP dev $NIC_C