mirror of
https://github.com/ItsDrike/dotfiles.git
synced 2024-11-10 02:39:40 +00:00
Update chroot scripts
This commit is contained in:
parent
b4e3bf49c8
commit
f5980c3e1a
|
@ -5,24 +5,63 @@ if [ "$EUID" -ne 0 ]; then
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Take user on the chrooted machine as 1st argument,
|
||||||
|
# this will be the logged user after chroot,
|
||||||
|
# default to root if none provided
|
||||||
|
if [ $# -ge 1 ]; then
|
||||||
|
USERNAME="$1"
|
||||||
|
else
|
||||||
|
USERNAME="root"
|
||||||
|
fi
|
||||||
|
|
||||||
|
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 /mnt/arch 2> /dev/null
|
mount "$MOUNT_POINT" 2> /dev/null
|
||||||
|
|
||||||
# Mount necessary directories for chroot to be possible
|
# Mount necessary directories for chroot to be possible
|
||||||
mount --types proc /proc /mnt/arch/proc
|
mount --types proc /proc "$MOUNT_POINT/proc"
|
||||||
mount --rbind /sys /mnt/arch/sys
|
mount --rbind /sys "$MOUNT_POINT/sys"
|
||||||
mount --make-rslave /mnt/arch/sys
|
mount --make-rslave "$MOUNT_POINT/sys"
|
||||||
mount --rbind /dev /mnt/arch/dev
|
mount --rbind /dev "$MOUNT_POINT/dev"
|
||||||
mount --make-rslave /mnt/arch/dev
|
mount --make-rslave "$MOUNT_POINT/dev"
|
||||||
|
|
||||||
# Chroot with zsh shell
|
# Chroot with custom script, if aviable
|
||||||
chroot /mnt/arch /bin/zsh --login
|
if [ -f "$MOUNT_POINT/$CLIENT_SCRIPT" ]; then
|
||||||
|
chroot "$MOUNT_POINT" "$CLIENT_SCRIPT" "$USERNAME"
|
||||||
|
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 /mnt/arch/dev{/shm,/pts,}
|
umount -l "$MOUNT_POINT/dev{/shm,/pts,}"
|
||||||
umount -R /mnt/arch
|
umount -R "$MOUNT_POINT"
|
||||||
|
|
||||||
# Remount partition accordingly to fstab
|
# Remount partition accordingly to fstab
|
||||||
# (the above umountings will unmount gentoo completely,
|
# (the above umountings will unmount gentoo completely,
|
||||||
# which means that remounting is necessary)
|
# which means that remounting is necessary)
|
||||||
mount /mnt/arch
|
mount "$MOUNT_POINT"
|
||||||
|
|
17
root/usr/local/bin/chroot-client
Executable file
17
root/usr/local/bin/chroot-client
Executable file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/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
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,67 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo "Must be ran as root"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Take user on the chrooted machine as 1st argument,
|
||||||
|
# this will be the logged user after chroot,
|
||||||
|
# default to root if none provided
|
||||||
|
if [ $# -ge 1 ]; then
|
||||||
|
USERNAME="$1"
|
||||||
|
else
|
||||||
|
USERNAME="root"
|
||||||
|
fi
|
||||||
|
|
||||||
|
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 /mnt/gentoo 2>/dev/null
|
mount "$MOUNT_POINT" 2> /dev/null
|
||||||
|
|
||||||
# Mount necessary directories for chroot to be possible
|
# Mount necessary directories for chroot to be possible
|
||||||
mount --types proc /proc /mnt/gentoo/proc
|
mount --types proc /proc "$MOUNT_POINT/proc"
|
||||||
mount --rbind /sys /mnt/gentoo/sys
|
mount --rbind /sys "$MOUNT_POINT/sys"
|
||||||
mount --make-rslave /mnt/gentoo/sys
|
mount --make-rslave "$MOUNT_POINT/sys"
|
||||||
mount --rbind /dev /mnt/gentoo/dev
|
mount --rbind /dev "$MOUNT_POINT/dev"
|
||||||
mount --make-rslave /mnt/gentoo/dev
|
mount --make-rslave "$MOUNT_POINT/dev"
|
||||||
|
|
||||||
# Chroot with zsh shell
|
# Chroot with custom script, if aviable
|
||||||
chroot /mnt/gentoo /bin/zsh --login
|
if [ -f "$MOUNT_POINT/$CLIENT_SCRIPT" ]; then
|
||||||
|
chroot "$MOUNT_POINT" "$CLIENT_SCRIPT" "$USERNAME"
|
||||||
|
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 /mnt/gentoo/dev{/shm,/pts,}
|
umount -l "$MOUNT_POINT/dev{/shm,/pts,}"
|
||||||
umount -R /mnt/gentoo
|
umount -R "$MOUNT_POINT"
|
||||||
|
|
||||||
# Remount partition accordingly to fstab
|
# Remount partition accordingly to fstab
|
||||||
# (the above umountings will unmount gentoo completely,
|
# (the above umountings will unmount gentoo completely,
|
||||||
# which means that remounting is necessary)
|
# which means that remounting is necessary)
|
||||||
mount /mnt/gentoo
|
mount "$MOUNT_POINT"
|
||||||
|
|
Loading…
Reference in a new issue