From d80d5abfab3338a98a48475e6ab3eae92b6c160f Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Fri, 16 Jul 2021 00:43:01 +0200 Subject: [PATCH] Merge chroot script into single dynamic one --- root/usr/local/bin/arch-chroot | 39 --------------- root/usr/local/bin/auto-chroot | 81 ++++++++++++++++++++++++++++++++ root/usr/local/bin/gentoo-chroot | 39 --------------- 3 files changed, 81 insertions(+), 78 deletions(-) delete mode 100755 root/usr/local/bin/arch-chroot create mode 100755 root/usr/local/bin/auto-chroot delete mode 100755 root/usr/local/bin/gentoo-chroot diff --git a/root/usr/local/bin/arch-chroot b/root/usr/local/bin/arch-chroot deleted file mode 100755 index 4e33a26..0000000 --- a/root/usr/local/bin/arch-chroot +++ /dev/null @@ -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" diff --git a/root/usr/local/bin/auto-chroot b/root/usr/local/bin/auto-chroot new file mode 100755 index 0000000..0e875ae --- /dev/null +++ b/root/usr/local/bin/auto-chroot @@ -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 diff --git a/root/usr/local/bin/gentoo-chroot b/root/usr/local/bin/gentoo-chroot deleted file mode 100755 index ab3b766..0000000 --- a/root/usr/local/bin/gentoo-chroot +++ /dev/null @@ -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"