commit 16a2351d89c65403f79aa7883e91d6d0d18bd8df Author: yafox Date: Wed Nov 25 06:31:08 2020 +0000 initial commit. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bd8c239 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright 2020 "yafox" + +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..72593f4 --- /dev/null +++ b/README @@ -0,0 +1,15 @@ +# chin + +abbreviation of "chroot in." + +sets up /proc, /dev, /sys, and /tmp directories if they do not already exist in +the target directory, chroots into the target directory and executes the given +command, and then tears down whatever directories it set up upon exiting. + +designed to set up build environments for code that already comes from a trusted +source. do not assume the resulting chroot conveys any sort of security! +consider this an organizational tool more than anything else. + +40 SLOC. + +usage: chin.sh diff --git a/chin.sh b/chin.sh new file mode 100755 index 0000000..46545ac --- /dev/null +++ b/chin.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +set -e + +if [ ! -n "$1" ]; then + echo "usage: $(basename $0) " + exit 0 +fi + +# 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)" + _red="$(tput setaf 1)" +fi + +err() { + echo "$_red[ERR]$_clr $@" >&2 + exit 1 +} + +tgt="$1" +shift + +[ -d "$tgt" ] || err "no such directory as '$tgt'" + +[ "$(stat -Lt "$tgt")" != "$(stat -Lt "/")" ] \ +|| err "chroot target points to /! did something go wrong? exiting." + +# if the user already did some of the necessary prep, don't mess with it. +[ -e "$tgt/proc" ] || export _proc="skip" +[ -e "$tgt/dev" ] || export _dev="skip" +[ -e "$tgt/sys" ] || export _sys="skip" +[ -e "$tgt/tmp" ] || export _tmp="skip" + +[ -z "$_proc" ] || { mkdir "$tgt/proc" && mount -n -t proc proc "$tgt/proc"; } +[ -z "$_dev" ] || { mkdir "$tgt/dev" && mount -o bind /dev "$tgt/dev"; } +[ -z "$_sys" ] || { mkdir "$tgt/sys" && mount -n -t sysfs sys "$tgt/sys"; } +[ -z "$_tmp" ] || { mkdir "$tgt/tmp" && mount -n -t tmpfs tmpfs "$tgt/tmp"; } + +cleanup() { + [ -z "$_tmp" ] || { umount "$tgt/tmp" && rmdir "$tgt/tmp"; } + [ -z "$_sys" ] || { umount "$tgt/sys" && rmdir "$tgt/sys"; } + [ -z "$_dev" ] || { umount "$tgt/dev" && rmdir "$tgt/dev"; } + [ -z "$_proc" ] || { umount "$tgt/proc" && rmdir "$tgt/proc"; } +} +trap cleanup EXIT INT HUP + +cd -- "$tgt" +chroot . sh -c "$@" +cd - >/dev/null diff --git a/makefile b/makefile new file mode 100644 index 0000000..dad9cb2 --- /dev/null +++ b/makefile @@ -0,0 +1,9 @@ +ROOTDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) +PREFIX ?= /usr +DESTDIR ?= $(PREFIX)/bin + +install: + cp -a $(ROOTDIR)/chin.sh $(DESTDIR)/chin + +uninstall: + rm $(DESTDIR)/chin 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.