Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.87 KB | None | 0 0
  1. #!/bin/sh
  2. # Description: Manage namespaces.
  3. # Depends on: sh, util-linux, iproute2, procps
  4. # Optional: iptables -t -A POSTROUTING -j MASQUERADE && echo 1 > /proc/sys/net/ipv4/ip_forward
  5.  
  6. NS=$(dirname $0)
  7. NSROOT=$NS/root/$2
  8. RUN=$NS/run/$2
  9.  
  10. test -f "$NS/rc.conf" && . $NS/rc.conf
  11.  
  12. start() {
  13.     check $2
  14.  
  15.     eval "addr=\"\$$2_addr\""
  16.     eval "onstart=\"\$$2_onstart\""
  17.  
  18.     if test -n "$addr"; then
  19.         ip link add br0 type bridge 2> /dev/null
  20.         ip link set br0 up 2> /dev/null
  21.         ip addr add 10.0.0.1/24 dev br0 2> /dev/null
  22.  
  23.         ip link add veth_$2 type veth peer name eth0
  24.         ip link set veth_$2 up
  25.         ip link set veth_$2 master br0
  26.     fi
  27.  
  28.     ip netns add $2
  29.  
  30.     if test -n "$addr"; then
  31.         ip link set eth0 netns $2
  32.     fi
  33.  
  34.     ip netns exec $2 ip link set lo up
  35.  
  36.     if test -n "$addr"; then
  37.         ip netns exec $2 ip addr add $addr/24 dev eth0
  38.         ip netns exec $2 ip link set eth0 up
  39.         ip netns exec $2 ip route add default via 10.0.0.1
  40.  
  41.         cp /etc/resolv.conf $NSROOT/etc
  42.     fi
  43.  
  44.     cp $NS/init $NSROOT
  45.    
  46.     setsid ip netns exec $2 unshare -fmuip env -i container=$2 chroot $NSROOT /init &
  47.  
  48.     PID=$!
  49.     sleep 3
  50.     echo $(pgrep -P $PID) > $RUN
  51.  
  52.     $0 run $2 "hostname $2"
  53.     $0 run $2 "mount -t proc none /proc"
  54.     $0 run $2 "mount -t devtmpfs -o nosuid,size=52k,nr_inodes=2048,mode=755 none /dev"
  55.     $0 run $2 "mount -t devpts -o nosuid,noexec,relatime,mode=600 none /dev/pts"
  56.  
  57.     test -n "$onstart" && echo "$onstart" | $0 run $2 sh -
  58.  
  59.     echo "$2: started"
  60. }
  61.  
  62. stop() {
  63.     kill -9 $(cat $RUN) || true
  64.  
  65.     eval "addr=\"\$$2_addr\""
  66.  
  67.     test -n "$addr" && ip link del veth_$2
  68.     ip netns delete $2
  69.  
  70.     echo "$2: stopped"
  71. }
  72.  
  73. restart() {
  74.     stop "null" $2
  75.     sleep 1
  76.     start "null" $2
  77. }
  78.  
  79. run() {
  80.     test -f $RUN && PID=$(cat $RUN)
  81.  
  82.     if ps -p $PID >/dev/null; then
  83.         true
  84.     else
  85.          echo "$2 is not running" && exit 1
  86.     fi
  87.  
  88.     shift
  89.     shift
  90.     nsenter -t $PID -m -u -i -n -p env -i container=$2 TERM=$TERM chroot $NSROOT "$@"
  91. }
  92.  
  93. check() {
  94.     test ! -d $NSROOT && echo $NSROOT directory does not exists. && exit 1
  95.     test -f $RUN && PID=$(cat $RUN) &&
  96.         ps -p $PID > /dev/null &&
  97.         echo $2 running with PID $PID &&
  98.         exit 1
  99. }
  100.  
  101. init() {
  102.     mkdir -p $NS/root
  103.     mkdir -p $NS/run
  104.     touch $NS/rc.conf
  105.     cat << EOF > $NS/rc.conf
  106. #!/bin/sh
  107.  
  108. # blue_addr="10.0.0.2"
  109. # blue_onstart="/etc/rc.d/nginx start"
  110. EOF
  111.  
  112.     cat << EOF > $NS/init
  113. #!/bin/sh
  114.  
  115. while :;
  116. do
  117.     sleep 86400
  118. done
  119. EOF
  120.     chmod +x $NS/init
  121. }
  122.  
  123. help() {
  124.     cat << EOF
  125. Usage: $0 [COMMAND] [NAMESPACE] [OPTION]...
  126. Manage namespaces.
  127.  
  128. Commands:
  129.   start   initialize namespace, chroot; copy and start /init in background
  130.   stop    kill /init
  131.   restart stop and start namespace
  132.   run     exec command in running namespace
  133.   check   check if namespace exists in $NSROOT directory, returns 1 on fail
  134.   init    create folders, rc.conf and init file in directory of this script
  135. EOF
  136. }
  137.  
  138. case $1 in
  139.     check|start|stop|restart|run|init) $1 $@ ;;
  140.     *) help ;;
  141. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement