dotfiles/root/usr/local/bin/arch-chroot
2021-07-15 23:38:34 +02:00

40 lines
968 B
Bash
Executable file

#!/bin/sh
# Ensure we run as root
if [ "$EUID" -ne 0 ]; then
echo "Must be ran as root"
exit 1
fi
# Take chroot user as 1st argument, default to root
if [ $# -ge 1 ]; then
USERNAME="$1"
else
USERNAME="root"
fi
MOUNT_POINT="/mnt/arch"
# Make sure the partition is mounted according to fstab
mount "$MOUNT_POINT" 2> /dev/null
# Mount necessary directories for chroot to be possible
mount --types proc /proc "$MOUNT_POINT/proc"
mount --rbind /sys "$MOUNT_POINT/sys"
mount --make-rslave "$MOUNT_POINT/sys"
mount --rbind /dev "$MOUNT_POINT/dev"
mount --make-rslave "$MOUNT_POINT/dev"
# Use /bin/su for chrooting with --login to also run
# /etc/profile and ~/.profile or ~/.zprofile
chroot "$MOUNT_POINT" "/bin/su" "$USERNAME" --login
# Unmount recursively mounted directories
umount -l "$MOUNT_POINT/dev"
umount -l "$MOUNT_POINT/sys"
umount -l "$MOUNT_POINT/proc"
umount -R "$MOUNT_POINT"
# Remount partition according to fstab
mount "$MOUNT_POINT"