From 350ef0cb51e131443fa68d1c20ae3980680ca356 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Tue, 9 Apr 2024 10:41:04 +0400 Subject: [PATCH] dchr-run v0.1.0 --- run.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 run.sh diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..388bfa7 --- /dev/null +++ b/run.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +help () { + echo 'dchr-run ' + echo 'https://dchr.dc09.ru' +} + +clean () { + echo '-- Cleaning up' + umount "$1/proc" + umount "$1/sys" + umount "$1/dev" + umount "$1/run" +} + +error () { + echo -e '-- \e[1;31mError\e[0m -- see output above' + exit 1 # `trap` in main() calls cleanup function +} + +setup () { + echo '-- Mounting' + mount -t proc /proc "$1/proc" || error + mount -t sysfs /sys "$1/sys" || error + mount -o bind /dev "$1/dev" || error + mount -o bind /run "$1/run" +} + +cphosts () { + if ! [ -e "$1/etc/hosts" ] + then + echo '-- Copying hosts' + cp /etc/hosts "$1/etc/hosts" + fi + if ! [ -e "$1/etc/resolv.conf" ] + then + echo '-- Copying resolv.conf' + cp /etc/resolv.conf "$1/etc/resolv.conf" + fi +} + +runcmd () { + echo -e '-- \e[1;32mOK\e[0m -- running chroot' + chroot "$1" "$2" + return $? +} + +main () { + if [[ "$1" == "--help" ]] || [[ "$1" == "" ]] + then + help + exit 0 + fi + + chrdir="$(readlink -f "$1")" + chrcmd="${2:-/bin/bash}" + + trap "clean $chrdir" EXIT + + setup "$chrdir" + cphosts "$chrdir" + runcmd "$chrdir" "$chrcmd" + exit $? +} + +sourced () { + if [ -n "$ZSH_VERSION" ] + then + case $ZSH_EVAL_CONTEXT in + *:file*) return 0;; + esac + elif [ -n "$BASH_VERSION" ] + then + ! [ "$0" = "$BASH_SOURCE" ] && return 0 + fi + return 1 +} + +sourced || main $@