mtsc/matter-chroot/main.sh
2023-12-22 20:44:46 +03:00

85 lines
2.1 KiB
Bash

#!/bin/bash
# matter-chroot | Matterlinux Chroot Script
# Copyright (C) 2023 Matterlinux
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
############################
## logging functions/vars ##
############################
RED="\e[31m"
BOLD="\e[1m"
RESET="\e[0m"
error() {
echo -e "$BOLD$RED>>>$RESET$BOLD $1$RESET"
exit 1
}
####################
## util functions ##
####################
chrt() {
mount --bind /dev $TARGET/dev
mount --bind /dev/pts $TARGET/dev/pts
mount -t proc proc $TARGET/proc
mount -t sysfs sysfs $TARGET/sys
mount -t tmpfs tmpfs $TARGET/run
if [ -h $TARGET/dev/shm ]; then
mkdir -p $TARGET/$(readlink $TARGET/dev/shm)
else
mount -t tmpfs -o nosuid,nodev tmpfs $TARGET/dev/shm
fi
local prompt='\e[1m\e[31m(chroot)\e[0m\e[1m \u@\h:\w#\e[0m '
chroot $TARGET /usr/bin/env -i \
HOME=/root \
TERM="$TERM" \
PS1="$prompt" \
PATH=/usr/bin:/usr/sbin \
$@
umount $TARGET/dev/pts
mountpoint -q $TARGET/dev/shm && umount $TARGET/dev/shm
umount $TARGET/dev
umount $TARGET/run
umount $TARGET/proc
umount $TARGET/sys
}
#################
## main script ##
#################
if [ $# -eq 0 ]; then
error "Please specify a directory"
fi
TARGET=$(realpath $1)
if [ -f $TARGET ]; then
error "$TARGET is a file"
fi
if [ ! -d $TARGET ]; then
error "$TARGET does not exist"
fi
if [ $# -gt 1 ]; then
chrt ${@:2}
exit 0
fi
chrt bash --login