github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/linux_backend/skeleton/destroy.sh (about)

     1  #!/bin/bash
     2  
     3  [ -n "$DEBUG" ] && set -o xtrace
     4  set -o nounset
     5  set -o errexit
     6  shopt -s nullglob
     7  
     8  cd $(dirname $0)
     9  
    10  source ./etc/config
    11  
    12  ./net.sh teardown
    13  
    14  cgroup_path="${GARDEN_CGROUP_PATH}"
    15  
    16  if [ -f ./run/wshd.pid ]
    17  then
    18    pid=$(cat ./run/wshd.pid)
    19  
    20    # Arbitrarily pick the cpu substem to check for live tasks.
    21    path=${cgroup_path}/cpu/instance-$id
    22    tasks=$path/tasks
    23  
    24    if [ -d $path ]
    25    then
    26      # Kill the container's init pid; the kernel will reap all tasks.
    27      kill -9 $pid
    28  
    29      # Wait while there are tasks in one of the instance's cgroups.
    30      #
    31      # Even though we've technically killed the root of the pid namespace,
    32      # it can take a brief period of time for the kernel to reap.
    33      while [ -f $tasks ] && [ -n "$(cat $tasks)" ]; do
    34        sleep 0.1
    35      done
    36    fi
    37  
    38    # Done, remove pid
    39    rm -f ./run/wshd.pid
    40  
    41    # Remove cgroups
    42    for system_path in ${cgroup_path}/*
    43    do
    44      path=$system_path/instance-$id
    45  
    46      if [ -d $path ]
    47      then
    48        # Recursively remove all cgroup trees under (and including) the instance.
    49        #
    50        # Running another containerization tool in the container may create these,
    51        # and the parent cannot be removed until they're removed first.
    52        #
    53        # find .. -delete ensures that it processes them depth-first.
    54        find $path -type d -delete
    55      fi
    56    done
    57  
    58    umount -l $rootfs_path/dev/pts || true
    59  
    60    exit 0
    61  fi
    62