forked from alagalah/gbpsfc-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovswork.sh
executable file
·84 lines (73 loc) · 1.99 KB
/
ovswork.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
#!/usr/bin/env bash
set -e
BRIDGE=$1
GUEST_ID=$2
IPADDR=$3
BROADCAST=$4
GWADDR=$5
MAC=$6
OF_PORT=$7
GUESTNAME=$8
VLANTAG=$9
[ "$IPADDR" ] || {
echo "Syntax:"
echo "pipework <hostinterface> <guest> <ipaddr>/<subnet> <broadcast> <gateway> [vlan tag]"
exit 1
}
# Step 1: Find the guest (for now, we only support LXC containers)
while read dev mnt fstype options dump fsck
do
[ "$fstype" != "cgroup" ] && continue
echo $options | grep -qw devices || continue
CGROUPMNT=$mnt
done < /proc/mounts
[ "$CGROUPMNT" ] || {
echo "Could not locate cgroup mount point."
exit 1
}
N=$(find "$CGROUPMNT" -name "$GUEST_ID*" | wc -l)
case "$N" in
0)
echo "Could not find any container matching $GUEST_ID"
exit 1
;;
1)
true
;;
*)
echo "Found more than one container matching $GUEST_ID"
exit 1
;;
esac
NSPID=$(head -n 1 $(find "$CGROUPMNT" -name "$GUEST_ID*" | head -n 1)/tasks)
[ "$NSPID" ] || {
echo "Could not find a process inside container $GUEST_ID"
exit 1
}
# Step 2: Prepare the working directory
mkdir -p /var/run/netns
rm -f /var/run/netns/$NSPID
ln -s /proc/$NSPID/ns/net /var/run/netns/$NSPID
# Step 3: Creating virtual interfaces
LOCAL_IFNAME=vethl-$GUESTNAME #$NSPID
GUEST_IFNAME=vethg-$GUESTNAME #$NSPID
ip link add name $LOCAL_IFNAME type veth peer name $GUEST_IFNAME
ip link set $LOCAL_IFNAME up
# Step 4: Adding the virtual interface to the bridge
ip link set $GUEST_IFNAME netns $NSPID
if [ "$VLANTAG" ]
then
ovs-vsctl add-port $BRIDGE $LOCAL_IFNAME tag=$VLANTAG
echo $LOCAL_IFNAME
else
ovs-vsctl add-port $BRIDGE $LOCAL_IFNAME
echo $LOCAL_IFNAME
fi
# Step 5: Configure netwroking within the container
ip netns exec $NSPID ip link set $GUEST_IFNAME name eth0
ip netns exec $NSPID ip addr add $IPADDR broadcast $BROADCAST dev eth0
ip netns exec $NSPID ifconfig eth0 hw ether $MAC
ip netns exec $NSPID ip addr add 127.0.0.1 dev lo
ip netns exec $NSPID ip link set eth0 up
ip netns exec $NSPID ip link set lo up
ip netns exec $NSPID ip route add default via $GWADDR