scripts/private-network: Indent code

This commit is contained in:
Fabio Rehm 2014-06-09 00:25:41 -03:00
parent 0bd071f95d
commit d22d6588f8

View file

@ -6,9 +6,9 @@
set -e set -e
case "$1" in case "$1" in
--wait) --wait)
WAIT=1 WAIT=1
;; ;;
esac esac
IFNAME=$1 IFNAME=$1
@ -24,109 +24,109 @@ MACADDR=$4
[ "$WAIT" ] && { [ "$WAIT" ] && {
while ! grep -q ^1$ /sys/class/net/$CONTAINER_IFNAME/carrier 2>/dev/null while ! grep -q ^1$ /sys/class/net/$CONTAINER_IFNAME/carrier 2>/dev/null
do sleep 1 do sleep 1
done done
exit 0 exit 0
} }
[ "$IPADDR" ] || { [ "$IPADDR" ] || {
echo "Syntax:" echo "Syntax:"
echo "pipework <hostinterface> [-i containerinterface] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr]" echo "pipework <hostinterface> [-i containerinterface] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr]"
echo "pipework <hostinterface> [-i containerinterface] <guest> dhcp [macaddr]" echo "pipework <hostinterface> [-i containerinterface] <guest> dhcp [macaddr]"
echo "pipework --wait" echo "pipework --wait"
exit 1 exit 1
} }
# First step: determine type of first argument (bridge, physical interface...) # First step: determine type of first argument (bridge, physical interface...)
if [ -d /sys/class/net/$IFNAME ] if [ -d /sys/class/net/$IFNAME ]
then then
if [ -d /sys/class/net/$IFNAME/bridge ] if [ -d /sys/class/net/$IFNAME/bridge ]
then then
IFTYPE=bridge IFTYPE=bridge
BRTYPE=linux BRTYPE=linux
elif $(which ovs-vsctl >/dev/null) && $(ovs-vsctl list-br|grep -q ^$IFNAME$) elif $(which ovs-vsctl >/dev/null) && $(ovs-vsctl list-br|grep -q ^$IFNAME$)
then then
IFTYPE=bridge IFTYPE=bridge
BRTYPE=openvswitch BRTYPE=openvswitch
else IFTYPE=phys else IFTYPE=phys
fi fi
else else
case "$IFNAME" in case "$IFNAME" in
br*) br*)
IFTYPE=bridge IFTYPE=bridge
BRTYPE=linux BRTYPE=linux
;; ;;
*) *)
echo "I do not know how to setup interface $IFNAME." echo "I do not know how to setup interface $IFNAME."
exit 1 exit 1
;; ;;
esac esac
fi fi
# Second step: find the guest (for now, we only support LXC containers) # Second step: find the guest
while read dev mnt fstype options dump fsck while read dev mnt fstype options dump fsck
do do
[ "$fstype" != "cgroup" ] && continue [ "$fstype" != "cgroup" ] && continue
echo $options | grep -qw devices || continue echo $options | grep -qw devices || continue
CGROUPMNT=$mnt CGROUPMNT=$mnt
done < /proc/mounts done < /proc/mounts
[ "$CGROUPMNT" ] || { [ "$CGROUPMNT" ] || {
echo "Could not locate cgroup mount point." echo "Could not locate cgroup mount point."
exit 1 exit 1
} }
# Try to find a cgroup matching exactly the provided name. # Try to find a cgroup matching exactly the provided name.
N=$(find "$CGROUPMNT" -name "$GUESTNAME" | wc -l) N=$(find "$CGROUPMNT" -name "$GUESTNAME" | wc -l)
case "$N" in case "$N" in
0) 0)
echo "Container $GUESTNAME not found." echo "Container $GUESTNAME not found."
exit 1 exit 1
;; ;;
1) 1)
true true
;; ;;
*) *)
echo "Found more than one container matching $GUESTNAME." echo "Found more than one container matching $GUESTNAME."
exit 1 exit 1
;; ;;
esac esac
if [ "$IPADDR" = "dhcp" ] if [ "$IPADDR" = "dhcp" ]
then then
# Check for first available dhcp client # Check for first available dhcp client
DHCP_CLIENT_LIST="udhcpc dhcpcd dhclient" DHCP_CLIENT_LIST="udhcpc dhcpcd dhclient"
for CLIENT in $DHCP_CLIENT_LIST; do for CLIENT in $DHCP_CLIENT_LIST; do
which $CLIENT >/dev/null && { which $CLIENT >/dev/null && {
DHCP_CLIENT=$CLIENT DHCP_CLIENT=$CLIENT
break break
}
done
[ -z $DHCP_CLIENT ] && {
echo "You asked for DHCP; but no DHCP client could be found."
exit 1
} }
done
[ -z $DHCP_CLIENT ] && {
echo "You asked for DHCP; but no DHCP client could be found."
exit 1
}
else else
# Check if a subnet mask was provided. # Check if a subnet mask was provided.
echo $IPADDR | grep -q / || { echo $IPADDR | grep -q / || {
echo "The IP address should include a netmask." echo "The IP address should include a netmask."
echo "Maybe you meant $IPADDR/24 ?" echo "Maybe you meant $IPADDR/24 ?"
exit 1 exit 1
} }
# Check if a gateway address was provided. # Check if a gateway address was provided.
if echo $IPADDR | grep -q @ if echo $IPADDR | grep -q @
then then
GATEWAY=$(echo $IPADDR | cut -d@ -f2) GATEWAY=$(echo $IPADDR | cut -d@ -f2)
IPADDR=$(echo $IPADDR | cut -d@ -f1) IPADDR=$(echo $IPADDR | cut -d@ -f1)
else else
GATEWAY= GATEWAY=
fi fi
fi fi
NSPID=$(head -n 1 $(find "$CGROUPMNT" -name "$GUESTNAME" | head -n 1)/tasks) NSPID=$(head -n 1 $(find "$CGROUPMNT" -name "$GUESTNAME" | head -n 1)/tasks)
[ "$NSPID" ] || { [ "$NSPID" ] || {
echo "Could not find a process inside container $GUESTNAME." echo "Could not find a process inside container $GUESTNAME."
exit 1 exit 1
} }
[ ! -d /var/run/netns ] && mkdir -p /var/run/netns [ ! -d /var/run/netns ] && mkdir -p /var/run/netns
@ -136,31 +136,31 @@ ln -s /proc/$NSPID/ns/net /var/run/netns/$NSPID
# Check if we need to create a bridge. # Check if we need to create a bridge.
[ $IFTYPE = bridge ] && [ ! -d /sys/class/net/$IFNAME ] && { [ $IFTYPE = bridge ] && [ ! -d /sys/class/net/$IFNAME ] && {
(ip link set $IFNAME type bridge > /dev/null 2>&1) || (brctl addbr $IFNAME) (ip link set $IFNAME type bridge > /dev/null 2>&1) || (brctl addbr $IFNAME)
ip link set $IFNAME up ip link set $IFNAME up
} }
# If it's a bridge, we need to create a veth pair # If it's a bridge, we need to create a veth pair
[ $IFTYPE = bridge ] && { [ $IFTYPE = bridge ] && {
LOCAL_IFNAME=pl$NSPID$CONTAINER_IFNAME LOCAL_IFNAME=pl$NSPID$CONTAINER_IFNAME
GUEST_IFNAME=pg$NSPID$CONTAINER_IFNAME GUEST_IFNAME=pg$NSPID$CONTAINER_IFNAME
ip link add name $LOCAL_IFNAME type veth peer name $GUEST_IFNAME ip link add name $LOCAL_IFNAME type veth peer name $GUEST_IFNAME
case "$BRTYPE" in case "$BRTYPE" in
linux) linux)
(ip link set $LOCAL_IFNAME master $IFNAME > /dev/null 2>&1) || (brctl addif $IFNAME $LOCAL_IFNAME) (ip link set $LOCAL_IFNAME master $IFNAME > /dev/null 2>&1) || (brctl addif $IFNAME $LOCAL_IFNAME)
;; ;;
openvswitch) openvswitch)
ovs-vsctl add-port $IFNAME $LOCAL_IFNAME ovs-vsctl add-port $IFNAME $LOCAL_IFNAME
;; ;;
esac esac
ip link set $LOCAL_IFNAME up ip link set $LOCAL_IFNAME up
} }
# If it's a physical interface, create a macvlan subinterface # If it's a physical interface, create a macvlan subinterface
[ $IFTYPE = phys ] && { [ $IFTYPE = phys ] && {
GUEST_IFNAME=ph$NSPID$CONTAINER_IFNAME GUEST_IFNAME=ph$NSPID$CONTAINER_IFNAME
ip link add link $IFNAME dev $GUEST_IFNAME type macvlan mode bridge ip link add link $IFNAME dev $GUEST_IFNAME type macvlan mode bridge
ip link set $IFNAME up ip link set $IFNAME up
} }
ip link set $GUEST_IFNAME netns $NSPID ip link set $GUEST_IFNAME netns $NSPID
@ -168,26 +168,26 @@ ip netns exec $NSPID ip link set $GUEST_IFNAME name $CONTAINER_IFNAME
[ "$MACADDR" ] && ip netns exec $NSPID ip link set $CONTAINER_IFNAME address $MACADDR [ "$MACADDR" ] && ip netns exec $NSPID ip link set $CONTAINER_IFNAME address $MACADDR
if [ "$IPADDR" = "dhcp" ] if [ "$IPADDR" = "dhcp" ]
then then
[ $DHCP_CLIENT = "udhcpc" ] && ip netns exec $NSPID $DHCP_CLIENT -qi $CONTAINER_IFNAME [ $DHCP_CLIENT = "udhcpc" ] && ip netns exec $NSPID $DHCP_CLIENT -qi $CONTAINER_IFNAME
[ $DHCP_CLIENT = "dhclient" ] && ip netns exec $NSPID $DHCP_CLIENT $CONTAINER_IFNAME [ $DHCP_CLIENT = "dhclient" ] && ip netns exec $NSPID $DHCP_CLIENT $CONTAINER_IFNAME
[ $DHCP_CLIENT = "dhcpcd" ] && ip netns exec $NSPID $DHCP_CLIENT -q $CONTAINER_IFNAME [ $DHCP_CLIENT = "dhcpcd" ] && ip netns exec $NSPID $DHCP_CLIENT -q $CONTAINER_IFNAME
else else
ip netns exec $NSPID ip addr add $IPADDR dev $CONTAINER_IFNAME ip netns exec $NSPID ip addr add $IPADDR dev $CONTAINER_IFNAME
[ "$GATEWAY" ] && { [ "$GATEWAY" ] && {
ip netns exec $NSPID ip route delete default >/dev/null 2>&1 && true ip netns exec $NSPID ip route delete default >/dev/null 2>&1 && true
} }
ip netns exec $NSPID ip link set $CONTAINER_IFNAME up ip netns exec $NSPID ip link set $CONTAINER_IFNAME up
[ "$GATEWAY" ] && { [ "$GATEWAY" ] && {
ip netns exec $NSPID ip route replace default via $GATEWAY ip netns exec $NSPID ip route replace default via $GATEWAY
} }
fi fi
# Give our ARP neighbors a nudge about the new interface # Give our ARP neighbors a nudge about the new interface
if which arping > /dev/null 2>&1 if which arping > /dev/null 2>&1
then then
IPADDR=$(echo $IPADDR | cut -d/ -f1) IPADDR=$(echo $IPADDR | cut -d/ -f1)
ip netns exec $NSPID arping -c 1 -A -I $CONTAINER_IFNAME $IPADDR > /dev/null 2>&1 ip netns exec $NSPID arping -c 1 -A -I $CONTAINER_IFNAME $IPADDR > /dev/null 2>&1
else else
echo "Warning: arping not found; interface may not be immediately reachable" echo "Warning: arping not found; interface may not be immediately reachable"
fi fi
exit 0 exit 0