| #!/bin/sh |
| |
| set -eu |
| |
| fstab_fs_mntopt() { |
| awk "\"overlay\" == \$1 && \"$1\" == \$2 { printf(\"%s\n\", \$4) }" |
| } |
| |
| fstab_option_iter() { |
| tr ',' '\n' |
| } |
| |
| fstab_option_value() { |
| cut -d= -f2 |
| } |
| |
| overlay_add() { |
| o_lowerdir="$1" |
| o_upperdir=$(mktemp -d) |
| o_workdir=$(mktemp -d) |
| chmod $(stat -c '%a' $o_lowerdir) $o_upperdir $o_workdir |
| mount -t overlay -o lowerdir="${o_lowerdir}",upperdir=${o_upperdir},workdir=${o_workdir} overlay "${o_lowerdir}" |
| } |
| |
| overlay_remove() { |
| o_lowerdir="$1" |
| o_options="$(fstab_fs_mntopt $o_lowerdir < /proc/mounts)" |
| umount $o_lowerdir |
| if [ -z "$o_options" ] |
| then |
| echo >&2 $o_lowerdir is not an overlay |
| exit 1 |
| else |
| echo "${o_options}" | |
| fstab_option_iter | |
| while read FS_MNTOPT |
| do |
| case $FS_MNTOPT in |
| upperdir=*|workdir=*) |
| rm -rf "$(echo $FS_MNTOPT | fstab_option_value)" |
| ;; |
| esac |
| done |
| fi |
| } |
| |
| if [ $# -lt 2 ] |
| then |
| cat $0 |
| exit 1 |
| fi |
| |
| subcmd=$1 |
| shift |
| |
| case $subcmd in |
| add) |
| for target in "$@" |
| do |
| overlay_add "$target" |
| done |
| ;; |
| remove) |
| for target in "$@" |
| do |
| overlay_remove "$target" |
| done |
| ;; |
| -h|--help|*) |
| cat $0 |
| ;; |
| esac |