commit 660c5922f97ca52a6682740e054196387a916bd9 Author: yafox Date: Wed Nov 25 06:05:22 2020 +0000 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a5fe136 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright 2020 "fox" + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..e0ff434 --- /dev/null +++ b/README @@ -0,0 +1,34 @@ +lyr +==== + +an abbreviation of "layer." + +maintains a repository of overlayfs upper directories, work directories, and +mount paths at LYRREPO. simplifies mounting, unmounting, creating, and +deleting layers. + +62 ELOC. + +usage: lyr.sh [command] + +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 [[:]] + mount the given layer with the given lower paths. print the mount path. + + dn + unmount the given layer. diff --git a/lyr.sh b/lyr.sh new file mode 100755 index 0000000..660faa1 --- /dev/null +++ b/lyr.sh @@ -0,0 +1,80 @@ +#!/bin/sh + +set -e + +export LYRROOT="$(dirname "$(readlink -f "$0")")" +export LYRREPO="${LYRREPO:-/usr/var/lyr}" + +[ -n "$1" ] || { \ +echo " +usage: $(basename $0) + +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 [[:]] + 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 diff --git a/makefile b/makefile new file mode 100644 index 0000000..1681fd4 --- /dev/null +++ b/makefile @@ -0,0 +1,15 @@ +SRCDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) +PREFIX ?= /usr +DESTDIR ?= $(PREFIX)/bin +LYRREPO ?= $(PREFIX)/var/lyr +LYRROOT ?= $(PREFIX)/share/lyr + +install: + mkdir -p $(LYRREPO) + ln -sf $(SRCDIR) $(LYRROOT) + ln -sf $(LYRROOT)/lyr.sh $(DESTDIR)/lyr + +uninstall: + rm $(DESTDIR)/lyr + rm $(LYRROOT) + rm -fr $(LYRREPO) diff --git a/sloc.sh b/sloc.sh new file mode 100755 index 0000000..1ea7b20 --- /dev/null +++ b/sloc.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +# find all *.sh files not under `pkg` that are not symbolic links, strip all +# trailing whitespace, then all leading whitespace, then all lines starting +# with '#', then all empty lines. then count the remaining lines. + +find . -name "*.sh" ! -path "**/pkg/**" ! -type l \ +| xargs sed 's/[[:space:]]*$//g; s/^[[:space:]]*//g; s/^#.*$//g; /^$/d' \ +| wc -l - \ +| cut -d' ' -f1 + +# note that this script's ELOC is also included in the count.