mtsc/matter-chroot/main.sh

119 lines
3.0 KiB
Bash
Raw Normal View History

2023-12-22 17:44:46 +00:00
#!/bin/bash
2024-08-11 14:45:54 +00:00
# matter-chroot | MatterLinux chroot script
2024-05-01 20:56:27 +00:00
# MatterLinux 2023-2024 (https://matterlinux.xyz)
2023-12-22 17:44:46 +00:00
# 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/>.
2024-08-11 14:45:54 +00:00
#############################
## import common functions ##
#############################
location="$(dirname "${0}")"
location="$(realpath "${location}")"
commonsh="$(echo "${location}" | sed 's/\/bin/\/lib/g')/mtsc-common.sh"
2023-12-22 17:44:46 +00:00
2024-08-11 14:45:54 +00:00
source "${commonsh}"
if [ "${?}" != "0" ]; then
echo "Failed to import mtsc-common"
2023-12-22 17:44:46 +00:00
exit 1
2024-08-11 14:45:54 +00:00
fi
2023-12-22 17:44:46 +00:00
####################
## util functions ##
####################
linkresolv(){
2024-08-11 14:45:54 +00:00
if [ ! -f "${target}/etc/resolv.conf" ]; then
ln -sf /run/systemd/resolve/resolv.conf "${target}/etc/resolv.conf"
fi
}
2023-12-22 17:44:46 +00:00
chrt() {
2024-08-11 14:45:54 +00:00
mount -t proc proc "${target}/proc"
mount -t sysfs sysfs "${target}/sys"
if [[ -d "${target}/sys/firmware/efi/efivars" ]]; then
mount -t efivarfs efivarfs "${target}/sys/firmware/efi/efivars"
fi
2024-08-11 14:45:54 +00:00
mount -o bind /dev "${target}/dev"
mount -t devpts none "${target}/dev/pts"
mount --bind /run "${target}/run"
linkresolv
2023-12-22 17:44:46 +00:00
2024-08-11 14:45:54 +00:00
if [ -h "${target}/dev/shm" ]; then
mkdir -p "${target}/$(readlink "${target}/dev/shm")"
2023-12-22 17:44:46 +00:00
else
2024-08-11 14:45:54 +00:00
mount -t tmpfs -o nosuid,nodev tmpfs "${target}/dev/shm"
2023-12-22 17:44:46 +00:00
fi
2024-01-07 11:19:11 +00:00
local prompt='\['$BOLD'\['$RED'(chroot)\['$RESET'\['$BOLD' \u@\h:\w#\['$RESET' '
2024-08-11 14:45:54 +00:00
chroot "${target}" /usr/bin/env -i \
HOME=/root \
TERM="${TERM}" \
PS1="${prompt}" \
PATH=/usr/bin:/usr/sbin \
"$@"
2024-08-11 14:45:54 +00:00
local ret_code=$?
# kill procs that may prevent umount
killall -9 keyboxd 2> /dev/null
killall -9 dirmngr 2> /dev/null
killall -9 gpg-agent 2> /dev/null
2024-08-11 14:45:54 +00:00
umount "${target}/proc"
mountpoint -q "${target}/sys/firmware/efi/efivars" && umount "${target}/sys/firmware/efi/efivars"
umount "${target}/sys"
2024-08-11 14:45:54 +00:00
umount "${target}/dev/pts"
mountpoint -q "${target}/dev/shm" && umount "${target}/dev/shm"
umount "${target}/dev"
umount "${target}/run"
return $ret_code
2023-12-22 17:44:46 +00:00
}
#################
## main script ##
#################
if [ "$EUID" -ne 0 ]; then
error "Cannot chroot without root"
2024-08-11 14:45:54 +00:00
exit 1
fi
2023-12-22 17:44:46 +00:00
if [ $# -eq 0 ]; then
error "Please specify a directory"
2024-08-11 14:45:54 +00:00
exit 1
2023-12-22 17:44:46 +00:00
fi
2024-08-11 14:45:54 +00:00
target="$(realpath "${1}")"
if [ -f "${target}" ]; then
error "${target} is a file"
exit 1
2023-12-22 17:44:46 +00:00
fi
2024-08-11 14:45:54 +00:00
if [ ! -d "${target}" ]; then
error "${target} does not exist"
exit 1
2023-12-22 17:44:46 +00:00
fi
if [ $# -gt 1 ]; then
chrt ${@:2}
2024-08-11 14:45:54 +00:00
exit $?
2023-12-22 17:44:46 +00:00
fi
2024-01-18 15:02:56 +00:00
chrt bash --noprofile --login
exit $?