initial commit.

This commit is contained in:
yafox 2020-11-25 06:31:08 +00:00
commit 16a2351d89
No known key found for this signature in database
GPG Key ID: B501C30B37F4806C
5 changed files with 108 additions and 0 deletions

20
LICENSE Normal file
View File

@ -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.

15
README Normal file
View File

@ -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 <chroot target> <commands>

52
chin.sh Executable file
View File

@ -0,0 +1,52 @@
#!/bin/sh
set -e
if [ ! -n "$1" ]; then
echo "usage: $(basename $0) <chroot target> <commands>"
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

9
makefile Normal file
View File

@ -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

12
sloc.sh Executable file
View File

@ -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.