Merge chroot script into single dynamic one

This commit is contained in:
ItsDrike 2021-07-16 00:43:01 +02:00
parent 037aeeac14
commit d80d5abfab
No known key found for this signature in database
GPG key ID: B5F6B41F708C3ADD
3 changed files with 81 additions and 78 deletions

View file

@ -1,39 +0,0 @@
#!/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"

81
root/usr/local/bin/auto-chroot Executable file
View file

@ -0,0 +1,81 @@
#!/bin/sh
yes_no() {
while true; do
printf "$1 (y/n): "
read -r yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no";;
esac
done
}
# Ensure we run as root
if [ "$EUID" -ne 0 ]; then
echo "Must be ran as root"
exit 1
fi
# Take NEWROOT as 1st argument
if [ $# -ge 1 ]; then
NEWROOT="$1"
else
echo "Provide newroot directory"
exit 1
fi
# Take chroot user as 2nd argument, default to root
if [ $# -ge 2 ]; then
USERNAME="$2"
else
USERNAME="root"
fi
# Check if given NEWROOT is already mounted, if it is
# set REMOUNT to the mount source, so that we can remount
# it once we're done.
df_out=$(df --output=source,target | grep -w "$NEWROOT")
if [ -n "$df_out" ]; then
REMOUNT="$(echo $df_out | awk '{print $1}')"
else
# If the target isn't mounted already, check
# if user gave $3 (mount location)
if [ $# -ge 3 ]; then
mount "$3" "$NEWROOT"
else
# If user didn't give mount location, try to
# mount according to fstab
if [ -n "$(grep -w "$NEWROOT" /etc/fstab)" ]; then
mount "$NEWROOT"
else
# Ask for user confirmation to ensure that filesystem
# is ready for chroot in given NEWROOT, exit if not
yes_no "$NEWROOT wasn't mounted, is your filesystem in place?" || exit 1
fi
fi
fi
# Mount necessary directories for chroot to be possible
mount --types proc /proc "$NEWROOT/proc"
mount --rbind /sys "$NEWROOT/sys"
mount --make-rslave "$NEWROOT/sys"
mount --rbind /dev "$NEWROOT/dev"
mount --make-rslave "$NEWROOT/dev"
# Use /bin/su for chrooting with --login to also run
# /etc/profile and ~/.profile or ~/.zprofile
chroot "$NEWROOT" "/bin/su" "$USERNAME" --login
# Unmount recursively mounted directories
umount -l "$NEWROOT/dev"
umount -l "$NEWROOT/sys"
umount -l "$NEWROOT/proc"
umount -R "$NEWROOT"
# Remount partition according to fstab if REMOUT is set
# in order to leave the filesystem in the state it was
if [ -n "$REMOUNT" ]; then
mount "$REMOUNT" "$NEWROOT"
fi

View file

@ -1,39 +0,0 @@
#!/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/gentoo"
# 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"