I got a bit of tired to do:
mkdir /media/disk
mount /dev/sdXXX /media/disk
mount -o bind dev /media/disk/dev
mount -t proc /proc /media/disk/proc
mount -o bind sys /media/disk/sys
chroot
exit
umount /media/disk/{sys,proc,dev}
umount /media/disk
Especially if repairing grub fails and i do it lots of times in a row.
I made a scipt to do that (like usually with the stuff i am learning at the moment, not the best tools perhaps).
Have a look at it if you like and let me know what you think
(i made some tests and ran it three times in real life. From what i see it seems to work)
pastebin with highlighting; or here:
Code: Select all
#!/usr/bin/env bash
set -e
clear; date
echo
############
# Variables
############
MOUNT_POINT="/media/disk"
MOUNT_DEV="$1"
#############
# FUNctions
# in case you got them in your roots ~/bin/my_functions
# coment out the source-lines and comment the function lines
#############################################################
#source ~/bin/my_functions/check_root
#source ~/bin/my_functions/ask_for
function check_root {
if [[ $(id -u) -ne 0 ]]
then
echo "You need to rerun as root"
exit 1
fi
}
function ask_for {
printf "\n\t You want to go on? \n\t Say yes or no: "
read answer
case $answer in
[Yy]es | [Yy] ) echo "ok: ";;
*) printf "\n\t You didn't answer yes. \n\t The script will die \n"
umount "$MOUNT_POINT"
exit 1 ;;
esac
}
#####################################################
# Check for root, check if device is given and if its a block device
#####################################################
check_root
[[ $# = 1 ]] || { echo "no device added. Rerun" ; exit 1 ; }
[[ -b $1 ]] || { echo "not a valid block device. Rerun " ; exit 2 ; }
[[ -d "$MOUNT_POINT" ]] || { echo "creating $MOUNT_POINT" && mkdir "$MOUNT_POINT" ; }
echo
############
# Mount it
############
{ mount "$MOUNT_DEV" "$MOUNT_POINT" && echo "successfully mounted" ; } || { echo "mount failed. Exit" ; exit 3 ; }
##############################
# list it and ask for going on
##############################
echo "here is the actual content of the mounted device $MOUNT_DEV on $MOUNT_POINT"
ls "$MOUNT_POINT"
ask_for
echo "we will mount /dev /proc and /sys on it"
################################
# mounting /dev /proc and /sys
################################
{ mount -o bind /dev "$MOUNT_POINT"/dev && echo "/dev mounted" ; } || { echo "mounting of dev failed" ; exit 4 ; }
{ mount -t proc /proc "$MOUNT_POINT"/proc && echo "/proc mounted" ; } || { echo "mounting of proc failed" ; exit 5 ; }
{ mount -o bind /sys "$MOUNT_POINT"/sys && echo "/sys mounted" ; } || { echo "mounting of sys failed" ; exit 6 ; }
#################################
# chroot
################################
chroot "$MOUNT_POINT"
######################################
# After all is done,
# We will exit chroot-env
# and need to umount the whole shebang
#######################################
{ umount "$MOUNT_POINT"/{sys,proc,dev} && echo "sys proc and dev umounted again" ; } || { echo "umounting of sys,proc or dev failed. Check whats wrong. " ; exit 7 ; }
{ umount "$MOUNT_POINT" && echo "$MOUNT_POINT umounted again. Looks like all is fine. " ; } || { "umounting of $MOUNT_POINT failed. Check whats wrong " ; exit 8 ; }
printf "\n\n\t Looks like all went fine. \n\t Bye. \n\n "
exit 0
Obviously i like it chatty. No need to point that out, cause i need/want it that way (for troubleshooting). If someone wants to i will rewrite it without all those: "lala succeed" and "lala failed" so it is more obvious whats going on. Then we can talk about that.
Thanks.