2024-04-09 09:41:04 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
help () {
|
|
|
|
echo 'dchr-run <chroot-dir> <command>'
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
runcmd () {
|
|
|
|
echo -e '-- \e[1;32mOK\e[0m -- running chroot'
|
|
|
|
chroot "$1" "$2"
|
|
|
|
}
|
|
|
|
|
|
|
|
main () {
|
|
|
|
if [[ "$1" == "--help" ]] || [[ "$1" == "" ]]
|
|
|
|
then
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
chrdir="$(readlink -f "$1")"
|
|
|
|
chrcmd="${2:-/bin/bash}"
|
|
|
|
|
|
|
|
trap "clean $chrdir" EXIT
|
|
|
|
|
|
|
|
setup "$chrdir"
|
|
|
|
runcmd "$chrdir" "$chrcmd"
|
|
|
|
exit $?
|
|
|
|
}
|
|
|
|
|
|
|
|
sourced () {
|
2024-04-09 09:47:13 +03:00
|
|
|
[ -n "$ZSH_VERSION" ] && [[ "$ZSH_EVAL_CONTEXT" =~ :file ]] && return 0
|
|
|
|
[ -n "$BASH_VERSION" ] && ! [ "$0" = "$BASH_SOURCE" ] && return 0
|
2024-04-09 09:41:04 +03:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
sourced || main $@
|