lyr/lyr.sh

81 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -e
export LYRROOT="$(dirname "$(readlink -f "$0")")"
export LYRREPO="${LYRREPO:-/usr/var/lyr}"
[ -n "$1" ] || { \
echo "
usage: $(basename $0) <command> <layer name>
commands:
workdir
returns the work directory path for the layer.
upperdir
returns the upper directory path for the layer.
mountdir
returns the mount directory path for the layer.
mk
create a new layer with the given name.
rm
unmount and delete the layer with the given name.
up [<lower path>[:<lower path>]]
mount the given layer with the given lower paths. print the mount path.
dn
unmount the given layer.
" && exit 0; }
# if this is running on an interactive
# terminal (as opposed to in a script), if tput is installed, and if tput
# knows some color codes, then set the color values.
if [ -t 1 ] && [ "$(tput colors 2>/dev/null || echo 0)" -ge 8 ]; then
_clr="$(tput sgr0)"
_blu="$(tput setaf 6)"
_ylw="$(tput setaf 3)"
_red="$(tput setaf 1)"
fi
log() { echo "$_blu[LOG]$_clr $@"; }
wrn() { echo "$_ylw[WRN]$_clr $@" >&2; }
err() { echo "$_red[ERR]$_clr $@" >&2; exit 1; }
case "$1" in
workdir) echo "$LYRREPO/work/$2";;
upperdir) echo "$LYRREPO/upper/$2";;
mountdir) echo "$LYRREPO/mount/$2";;
mk) mkdir -p "$LYRREPO/upper/$2";;
rm)
{ ! mountpoint -q "$LYRREPO/mount/$2" || umount "$LYRREPO/mount/$2"; } \
&& rm -fr "$LYRREPO/upper/$2" "$LYRREPO/mount/$2" "$LYRREPO/work/$2"
;;
up)
[ -n "$3" ] && { layer="$3" && lower="$2"; } || layer="$2"
work="$LYRREPO/work/$layer"
upper="$LYRREPO/upper/$layer"
mnt="$LYRREPO/mount/$layer"
[ -d "$upper" ] || err "layer '$layer' not recognized!"
mkdir -p "$work" "$mnt"
if [ -n "$lower" ]; then
mount -t overlay -o lowerdir=$lower,upperdir=$upper,workdir=$work \
none "$mnt"
else
mount -o bind "$upper" "$mnt"
fi
echo "$mnt"
;;
dn) umount "$LYRREPO/mount/$2" && rm -r "$LYRREPO/mount/$2";;
*) err "unrecognized command '$1'";;
esac