dotfiles/root/usr/local/bin/arch-chroot

40 lines
968 B
Plaintext
Raw Normal View History

2021-05-05 22:58:39 +00:00
#!/bin/sh
2021-07-15 21:38:34 +00:00
# Ensure we run as root
2021-05-05 22:58:39 +00:00
if [ "$EUID" -ne 0 ]; then
echo "Must be ran as root"
2021-07-15 21:38:34 +00:00
exit 1
2021-05-05 22:58:39 +00:00
fi
2021-07-15 21:38:34 +00:00
# Take chroot user as 1st argument, default to root
2021-07-15 14:15:45 +00:00
if [ $# -ge 1 ]; then
USERNAME="$1"
else
USERNAME="root"
fi
MOUNT_POINT="/mnt/arch"
2021-05-05 22:58:39 +00:00
# Make sure the partition is mounted according to fstab
2021-07-15 14:15:45 +00:00
mount "$MOUNT_POINT" 2> /dev/null
2021-05-05 22:58:39 +00:00
# Mount necessary directories for chroot to be possible
2021-07-15 14:15:45 +00:00
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"
2021-07-15 21:38:34 +00:00
# Use /bin/su for chrooting with --login to also run
# /etc/profile and ~/.profile or ~/.zprofile
chroot "$MOUNT_POINT" "/bin/su" "$USERNAME" --login
2021-05-05 22:58:39 +00:00
# Unmount recursively mounted directories
2021-07-15 21:38:34 +00:00
umount -l "$MOUNT_POINT/dev"
umount -l "$MOUNT_POINT/sys"
umount -l "$MOUNT_POINT/proc"
2021-07-15 14:15:45 +00:00
umount -R "$MOUNT_POINT"
2021-05-05 22:58:39 +00:00
2021-07-15 21:38:34 +00:00
# Remount partition according to fstab
2021-07-15 14:15:45 +00:00
mount "$MOUNT_POINT"