Update chroot scripts

This commit is contained in:
ItsDrike 2021-07-15 23:38:34 +02:00
parent 21ca7d9f09
commit 037aeeac14
No known key found for this signature in database
GPG key ID: B5F6B41F708C3ADD
3 changed files with 20 additions and 93 deletions

View file

@ -1,13 +1,12 @@
#!/bin/sh #!/bin/sh
# Ensure we run as root
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
echo "Must be ran as root" echo "Must be ran as root"
exit exit 1
fi fi
# Take user on the chrooted machine as 1st argument, # Take chroot user as 1st argument, default to root
# this will be the logged user after chroot,
# default to root if none provided
if [ $# -ge 1 ]; then if [ $# -ge 1 ]; then
USERNAME="$1" USERNAME="$1"
else else
@ -15,7 +14,6 @@ else
fi fi
MOUNT_POINT="/mnt/arch" MOUNT_POINT="/mnt/arch"
CLIENT_SCRIPT="/usr/local/bin/chroot-client"
# Make sure the partition is mounted according to fstab # Make sure the partition is mounted according to fstab
mount "$MOUNT_POINT" 2> /dev/null mount "$MOUNT_POINT" 2> /dev/null
@ -27,41 +25,15 @@ mount --make-rslave "$MOUNT_POINT/sys"
mount --rbind /dev "$MOUNT_POINT/dev" mount --rbind /dev "$MOUNT_POINT/dev"
mount --make-rslave "$MOUNT_POINT/dev" mount --make-rslave "$MOUNT_POINT/dev"
# Chroot with custom script, if aviable # Use /bin/su for chrooting with --login to also run
if [ -f "$MOUNT_POINT/$CLIENT_SCRIPT" ]; then # /etc/profile and ~/.profile or ~/.zprofile
chroot "$MOUNT_POINT" "$CLIENT_SCRIPT" "$USERNAME" chroot "$MOUNT_POINT" "/bin/su" "$USERNAME" --login
else
# If we didn't find the script in the chroot environment
# try to find it in this mahcine and copy it over
if [ -f "$CLIENT_SCRIPT" ]; then
echo "Client script not in chroot environment, copying"
mkdir -p "$MOUNT_POINT/$(dirname $CLIENT_SCRIPT)"
cp "$CLIENT_SCRIPT" "$MOUNT_POINT/$CLIENT_SCRIPTt"
chroot "$MOUNT_POINT" "$CLIENT_SCRIPT" "$USERNAME"
else
echo "Unable to run chroot client script, proceeding manually"
echo "You may need to run 'source /etc/profile' afterwards"
# Try to obtain chroot user's default shell from /etc/passwd
awkstring="BEGIN { FS=\":\" } /$USERNAME/ { print \$7 }"
shell="$(cat /etc/passwd | awk "$awkstring")"
if [ -n "$shell" ]; then
echo "Found user's shell, trying to chroot with it ($shell)"
chroot "$MOUNT_POINT" "$shell" --login
else
echo "Unable to find user in chroot's /etc/passwd, using root with bash shell"
chroot "$MOUNT_POINT" "/bin/bash" --login
fi
fi
fi
# Unmount recursively mounted directories # Unmount recursively mounted directories
umount -l "$MOUNT_POINT/dev{/shm,/pts,}" umount -l "$MOUNT_POINT/dev"
umount -l "$MOUNT_POINT/sys"
umount -l "$MOUNT_POINT/proc"
umount -R "$MOUNT_POINT" umount -R "$MOUNT_POINT"
# Remount partition accordingly to fstab # Remount partition according to fstab
# (the above umountings will unmount gentoo completely,
# which means that remounting is necessary)
mount "$MOUNT_POINT" mount "$MOUNT_POINT"

View file

@ -1,17 +0,0 @@
#!/bin/sh
if [ $# -ge 1 ]; then
if ! id "$1" &>/dev/null; then
echo "No such user, give valid username"
else
USERNAME="$1"
fi
else
USERNAME="root"
fi
source /etc/profile
exec su "$USERNAME" --login
exit

View file

@ -1,13 +1,12 @@
#!/bin/sh #!/bin/sh
# Ensure we run as root
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
echo "Must be ran as root" echo "Must be ran as root"
exit exit 1
fi fi
# Take user on the chrooted machine as 1st argument, # Take chroot user as 1st argument, default to root
# this will be the logged user after chroot,
# default to root if none provided
if [ $# -ge 1 ]; then if [ $# -ge 1 ]; then
USERNAME="$1" USERNAME="$1"
else else
@ -15,7 +14,6 @@ else
fi fi
MOUNT_POINT="/mnt/gentoo" MOUNT_POINT="/mnt/gentoo"
CLIENT_SCRIPT="/usr/local/bin/chroot-client"
# Make sure the partition is mounted according to fstab # Make sure the partition is mounted according to fstab
mount "$MOUNT_POINT" 2> /dev/null mount "$MOUNT_POINT" 2> /dev/null
@ -27,41 +25,15 @@ mount --make-rslave "$MOUNT_POINT/sys"
mount --rbind /dev "$MOUNT_POINT/dev" mount --rbind /dev "$MOUNT_POINT/dev"
mount --make-rslave "$MOUNT_POINT/dev" mount --make-rslave "$MOUNT_POINT/dev"
# Chroot with custom script, if aviable # Use /bin/su for chrooting with --login to also run
if [ -f "$MOUNT_POINT/$CLIENT_SCRIPT" ]; then # /etc/profile and ~/.profile or ~/.zprofile
chroot "$MOUNT_POINT" "$CLIENT_SCRIPT" "$USERNAME" chroot "$MOUNT_POINT" "/bin/su" "$USERNAME" --login
else
# If we didn't find the script in the chroot environment
# try to find it in this mahcine and copy it over
if [ -f "$CLIENT_SCRIPT" ]; then
echo "Client script not in chroot environment, copying"
mkdir -p "$MOUNT_POINT/$(dirname $CLIENT_SCRIPT)"
cp "$CLIENT_SCRIPT" "$MOUNT_POINT/$CLIENT_SCRIPTt"
chroot "$MOUNT_POINT" "$CLIENT_SCRIPT" "$USERNAME"
else
echo "Unable to run chroot client script, proceeding manually"
echo "You may need to run 'source /etc/profile' afterwards"
# Try to obtain chroot user's default shell from /etc/passwd
awkstring="BEGIN { FS=\":\" } /$USERNAME/ { print \$7 }"
shell="$(cat /etc/passwd | awk "$awkstring")"
if [ -n "$shell" ]; then
echo "Found user's shell, trying to chroot with it ($shell)"
chroot "$MOUNT_POINT" "$shell" --login
else
echo "Unable to find user in chroot's /etc/passwd, using root with bash shell"
chroot "$MOUNT_POINT" "/bin/bash" --login
fi
fi
fi
# Unmount recursively mounted directories # Unmount recursively mounted directories
umount -l "$MOUNT_POINT/dev{/shm,/pts,}" umount -l "$MOUNT_POINT/dev"
umount -l "$MOUNT_POINT/sys"
umount -l "$MOUNT_POINT/proc"
umount -R "$MOUNT_POINT" umount -R "$MOUNT_POINT"
# Remount partition accordingly to fstab # Remount partition according to fstab
# (the above umountings will unmount gentoo completely,
# which means that remounting is necessary)
mount "$MOUNT_POINT" mount "$MOUNT_POINT"