SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. set -e
  3.  
  4. case "$1" in
  5.     --wait)
  6.     while ! grep -q ^up$ /sys/class/net/eth1/operstate 2>/dev/null
  7.     do sleep 1
  8.     done
  9.     exit 0
  10.     ;;
  11. esac
  12.  
  13. IFNAME=$1
  14. GUESTNAME=$2
  15. IPADDR=$3
  16. MACADDR=$4
  17.  
  18. [ "$IPADDR" ] || {
  19.     echo "Syntax:"
  20.     echo "pipework <hostinterface> <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr]"
  21.     echo "pipework <hostinterface> <guest> dhcp [macaddr]"
  22.     echo "pipework --wait"
  23.     exit 1
  24. }
  25.  
  26. # First step: determine type of first argument (bridge, physical interface...)
  27. if [ -d /sys/class/net/$IFNAME ]
  28. then
  29.     if [ -d /sys/class/net/$IFNAME/bridge ]
  30.     then IFTYPE=bridge
  31.     else IFTYPE=phys
  32.     fi
  33. else
  34.     case "$IFNAME" in
  35.     br*)
  36.         IFTYPE=bridge
  37.         ;;
  38.     *)
  39.         echo "I do not know how to setup interface $IFNAME."
  40.         exit 1
  41.         ;;
  42.     esac
  43. fi
  44.  
  45. # Second step: find the guest (for now, we only support LXC containers)
  46. while read dev mnt fstype options dump fsck
  47. do
  48.     [ "$fstype" != "cgroup" ] && continue
  49.     echo $options | grep -qw devices || continue
  50.     CGROUPMNT=$mnt
  51. done < /proc/mounts
  52.  
  53. [ "$CGROUPMNT" ] || {
  54.     echo "Could not locate cgroup mount point."
  55.     exit 1
  56. }
  57.  
  58. N=$(find "$CGROUPMNT" -name "$GUESTNAME*" | wc -l)
  59. case "$N" in
  60.     0)
  61.     echo "Could not find any container matching $GUESTNAME."
  62.     exit 1
  63.     ;;
  64.     1)
  65.     true
  66.     ;;
  67.     *)
  68.     echo "Found more than one container matching $GUESTNAME."
  69.     exit 1
  70.     ;;
  71. esac
  72.  
  73. if [ "$IPADDR" = "dhcp" ]
  74. then
  75.     # We use udhcpc to obtain the DHCP lease, make sure it's installed.
  76.     which udhcpc >/dev/null || {
  77.     echo "You asked for DHCP; please install udhcpc first."
  78.     exit 1
  79.     }
  80. else
  81.     # Check if a subnet mask was provided.
  82.     echo $IPADDR | grep -q / || {
  83.     echo "The IP address should include a netmask."
  84.     echo "Maybe you meant $IPADDR/24 ?"
  85.     exit 1
  86.     }
  87.     # Check if a gateway address was provided.
  88.     if echo $IPADDR | grep -q @
  89.     then
  90.         GATEWAY=$(echo $IPADDR | cut -d@ -f2)
  91.         IPADDR=$(echo $IPADDR | cut -d@ -f1)
  92.     else
  93.         GATEWAY=
  94.     fi
  95. fi
  96.  
  97. NSPID=$(head -n 1 $(find "$CGROUPMNT" -name "$GUESTNAME*" | head -n 1)/tasks)
  98. [ "$NSPID" ] || {
  99.     echo "Could not find a process inside container $GUESTNAME."
  100.     exit 1
  101. }
  102. mkdir -p /var/run/netns
  103. rm -f /var/run/netns/$NSPID
  104. ln -s /proc/$NSPID/ns/net /var/run/netns/$NSPID
  105.  
  106.  
  107. # Check if we need to create a bridge.
  108. [ $IFTYPE = bridge ] && [ ! -d /sys/class/net/$IFNAME ] && {
  109.     ip link add $IFNAME type bridge
  110.     ip link set $IFNAME up
  111. }
  112.  
  113. # If it's a bridge, we need to create a veth pair
  114. [ $IFTYPE = bridge ] && {
  115.     LOCAL_IFNAME=vethl$NSPID
  116.     GUEST_IFNAME=vethg$NSPID
  117.     ip link add name $LOCAL_IFNAME type veth peer name $GUEST_IFNAME
  118.     ip link set $LOCAL_IFNAME master $IFNAME
  119.     ip link set $LOCAL_IFNAME up
  120. }
  121.  
  122. # If it's a physical interface, create a macvlan subinterface
  123. [ $IFTYPE = phys ] && {
  124.     GUEST_IFNAME=macvlan$NSPID
  125.     ip link add link $IFNAME dev $GUEST_IFNAME type macvlan mode bridge
  126.     ip link set $IFNAME up
  127. }
  128.  
  129. ip link set $GUEST_IFNAME netns $NSPID
  130. ip netns exec $NSPID ip link set $GUEST_IFNAME name eth1
  131. [ "$MACADDR" ] && ip netns exec $NSPID ip link set eth1 address $MACADDR
  132. if [ "$IPADDR" = "dhcp" ]
  133. then
  134.     ip netns exec $NSPID udhcpc -qi eth1
  135. else
  136.     ip netns exec $NSPID ip addr add $IPADDR dev eth1
  137.     ip netns exec $NSPID ip link set eth1 up
  138.     [ "$GATEWAY" ] && {
  139.     ip netns exec $NSPID ip route replace default via $GATEWAY
  140.     }
  141. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top