github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/linux_backend/bin/overlay.sh (about) 1 #!/bin/bash 2 3 set -e 4 5 action=$1 6 container_path=$2 7 overlay_path=$2/overlay 8 rootfs_path=$2/rootfs 9 base_path=$3 10 11 function get_mountpoint() { 12 df -P $1 | tail -1 | awk '{print $NF}' 13 } 14 15 function current_fs() { 16 mountpoint=$(get_mountpoint $1) 17 18 local mp 19 local fs 20 21 while read _ mp fs _; do 22 if [ "$fs" = "rootfs" ]; then 23 continue 24 fi 25 26 if [ "$mp" = "$mountpoint" ]; then 27 echo $fs 28 fi 29 done < /proc/mounts 30 } 31 32 function should_use_overlayfs() { 33 # load it so it's in /proc/filesystems 34 modprobe -q overlayfs >/dev/null 2>&1 || true 35 36 # cannot mount overlayfs in aufs 37 if [ "$(current_fs $overlay_path)" == "aufs" ]; then 38 return 1 39 fi 40 41 # cannot mount overlayfs in overlayfs; whiteout not supported 42 if [ "$(current_fs $overlay_path)" == "overlayfs" ]; then 43 return 1 44 fi 45 46 # check if it's a known filesystem 47 grep -q overlayfs /proc/filesystems 48 } 49 50 function should_use_aufs() { 51 # load it so it's in /proc/filesystems 52 modprobe -q aufs >/dev/null 2>&1 || true 53 54 # cannot mount aufs in aufs 55 if [ "$(current_fs $overlay_path)" == "aufs" ]; then 56 return 1 57 fi 58 59 # cannot mount aufs in overlayfs 60 if [ "$(current_fs $overlay_path)" == "overlayfs" ]; then 61 return 1 62 fi 63 64 # check if it's a known filesystem 65 grep -q aufs /proc/filesystems 66 } 67 68 function setup_fs() { 69 mkdir -p $overlay_path 70 mkdir -p $rootfs_path 71 72 if should_use_aufs; then 73 mount -n -t aufs -o br:$overlay_path=rw:$base_path=ro+wh none $rootfs_path 74 elif should_use_overlayfs; then 75 mount -n -t overlayfs -o rw,upperdir=$overlay_path,lowerdir=$base_path none $rootfs_path 76 else 77 # aufs and overlayfs are the only supported mount types. 78 # aufs and overlayfs can be used in nested containers by mounting 79 # the overlay directories on e.g. tmpfs 80 echo "the directories that contain the depot and rootfs must be mounted on a filesystem type that supports aufs or overlayfs" >&2 81 exit 222 82 fi 83 } 84 85 function rootfs_mountpoints() { 86 cat /proc/mounts | grep $rootfs_path | awk '{print $2}' 87 } 88 89 function teardown_fs() { 90 for i in $(seq 480); do 91 local mountpoints=$(rootfs_mountpoints) 92 if [ -z "$mountpoints" ] || umount $mountpoints; then 93 if rm -rf $container_path; then 94 return 0 95 fi 96 fi 97 98 sleep 0.5 99 done 100 101 return 1 102 } 103 104 if [ "$action" = "create" ]; then 105 setup_fs 106 else 107 teardown_fs 108 fi