grubrepair

Submitted scripts and programs
Forum rules
Your own work only.
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

grubrepair

Unread post by machinebacon » Tue Jul 30, 2013 7:37 am

Written by Ikey Doherty (SolusOS)
A damned useful script!

Code: Select all

#!/bin/bash

clear 
 
GRUB_TARGET="/mnt/repairs"
  
setup () {
if [[ ! -d "$GRUB_TARGET" ]]; then
mkdir -p "$GRUB_TARGET"
fi
}
   
bind_it () {
mount --bind $1 $2
}
    
blkid -o list # | yad --text-info --title "List of all partitions" --width=650 --height=450 &
     
read -p "Please enter the device you wish to install grub to (MBR): "
DEVICE_INST="$REPLY"
read -p "Please enter the partition device node on which your installation exists (Your linux install): "
PARTITION_INST="$REPLY"
setup
      
# Attempt mount
mount "$PARTITION_INST" "$GRUB_TARGET"
if [[ $? != 0 ]]; then
echo "Could not mount the device. Aborting"
exit 1
fi
       
for mountpoint in "/dev/" "/dev/pts" "/dev/shm" "/proc" "/sys"; do
bind_it "$mountpoint" "$GRUB_TARGET$mountpoint"
done
        
# run teh commands
chroot "$GRUB_TARGET" "grub-install" "$DEVICE_INST"
chroot "$GRUB_TARGET" "update-grub"
         
for mountpoint in "/dev/pts" "/dev/shm" "/dev" "/proc" "/sys"; do
umount "$GRUB_TARGET$mountpoint"
done
          
umount "$GRUB_TARGET"
..gnutella..

User avatar
swftech
Sobe
Posts: 160
Joined: Wed Oct 17, 2012 12:40 pm
Location: S Florida

Re: grubrepair

Unread post by swftech » Tue Jul 30, 2013 1:34 pm

I see you went on a useful script spree in these here parts, thanks Master bacon :)
Speaking of Ikey, I've been lurking and following his progress for SolusOS 2 on their forums.
Totally rebuilding Solus from scratch with Consort, Pisi and all else for the next version.
That's the only stable version I like so I'm anxious to eventually be able to try it out when complete.
Thanks again, I woke up to some useful scripts this morning!

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: grubrepair

Unread post by machinebacon » Tue Jul 30, 2013 2:11 pm

Mikey,
stay tuned for some surprises ;)
..gnutella..

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: grubrepair

Unread post by xaos52 » Fri Aug 23, 2013 10:07 am

Here is what I madeof it.
Bit more generalized.
All the functions could go to a bash library, and then sourced in a small script depending on what you want to do in the chroot.

Code: Select all


#!/bin/bash

# Arguments:
#        $1: CHROOT_DEVICE
#        $2: CHROOT_MOUNTPOINT

# And replace the COMMANDS array definition in yje main section of
# this script with the series of commands you want to run in the
# chroot

# e.g. chroot.sh /dev/mapper/VG10-linuxBBQ 

# Is it possible to run more than 1 command in one run?
# YES! By putting the commands in an array and passing the array to the
# do_chroot function.

# WARNING: The scripts uses 'indirect variables', which is a bash feature only!


usage () {
    echo -e "Usage:\n \
       $0 CHROOT_DEVICE CHROOT_MOUNTPOINT COMMAND"
    exit 1
}


# as_fn_set_status STATUS
# -----------------------
# Set $? to STATUS, without forking.
as_fn_set_status ()
{
  return $1
} # as_fn_set_status

# as_fn_exit STATUS
# -----------------
# Exit the shell with STATUS, even in a "trap 0" or "set -e" context.
as_fn_exit ()
{
  set +e
  as_fn_set_status $1
  exit $1
} # as_fn_exit

setup () {
    local chroot_mountpoint="$1"
    if [[ ! -d "$chroot_mountpoint" ]]; then
        mkdir -p "chroot_mountpoint"
    fi
}

breakdown () {
    local chroot_mountpoint="$1"
    do_bind_unmounts $chroot_mountpoint
    umount "$chroot_mountpoint"
}

bind_it () {
    mount --bind $1 $2
}

do_bind_mounts () {
    local mountpoint chroot_mountpoint="$1"
    for mountpoint in "/dev/" "/dev/pts" "/dev/shm" "/proc" "/sys"; do
        bind_it "$mountpoint" "${chroot_mountpoint}${mountpoint}"
    done
}

do_bind_unmounts () {
    local mountpoint chroot_mountpoint="$1"
    for mountpoint in "/dev/pts" "/dev/shm" "/dev" "/proc" "/sys"; do
        umount "${chroot_mountpoint}${mountpoint}"
    done
}

# main program starts here
do_chroot () {
    local CHROOT_DEVICE CHROOT_MOUNTPOINT CHROOT_COMMAND
    declare -a CHROOT_COMMANDS
    local ARRAY_NAME CHROOT_COMMANDS
    
    ARRAY_NAME=$1[@]
    # Uses variable indirection, which is a bashism!
    CHROOT_COMMANDS=("${!ARRAY_NAME}")
    CHROOT_DEVICE="$2"
    CHROOT_MOUNTPOINT="$3"
    [[ x"$CHROOT_DEVICE" = "x" ]] && usage
    [[ x"$CHROOT_MOUNTPOINT" = "x" ]] && usage
    [[ $# -eq 0 ]] && usage
    [[ "$(id -u)" -eq 0 ]] || { echo -e "\tRoot permissions required...\n"; exit 1; }
    [[ -b $CHROOT_DEVICE ]] || { echo -e "\t$CHROOT_DEVICE is not a block device...\n"; exit 1; }


    # Catch error status, even normal exit. Make sure anything mounted
    # by this script is unmounted, no matter if the script exits
    # normally or not.
    trap 'exit_status=$?
    breakdown $CHROOT_MOUNTPOINT
    exit $exit_status
' 0
    for ac_signal in 1 2 13 15; do
        trap 'ac_signal='$ac_signal'; as_fn_exit 1' $ac_signal
    done
    ac_signal=0

    setup
    
# Attempt mount
    mount "$CHROOT_DEVICE" "$CHROOT_MOUNTPOINT"
    # returns status 32 when already mounted
    if [[ $? != 0 && $? != 32 ]]; then
        echo "Could not mount $CHROOT_DEVICE on $CHROOT_MOUNTPOINT"
        exit 1
    fi
    do_bind_mounts $CHROOT_MOUNTPOINT
    
    # run teh command
    for COMMAND in "${CHROOT_COMMANDS[@]}"; do
        chroot "$CHROOT_MOUNTPOINT" ${COMMAND}
    done
}

# main program starts here
# create an array with commands+arguments to run in the chroot

CHROOT_DEVICE="$1"
CHROOT_MOUNTPOINT="$2"

declare -a COMMANDS
# Change the COMMANDS definition to the series of commands you want to run

# -----------------------------------------------------------------------

# Example 1: Install grub to the MBR of your HD and run update-grub
    COMMANDS=( \ 'grub-install /dev/sda' \ 'update-grub' \ )


# Example 2:
#     run apt-get update and apt-get dist-upgrade in a system you chroot into
# COMMANDS=( \
#     'apt-get update' \
#     'apt-get dist-upgrade' \
#     )

# Example 3: install the 'lvm2' package in a system you chroot into
#     and run update-initrams
# COMMANDS=( \
#     'apt-get update' \
#     'apt-get install lvm2' \
#     '/usr/sbin/update-initramfs -u -vvv /initrd.img' \
#     )


# -----------------------------------------------------------------------

do_chroot COMMANDS $CHROOT_DEVICE $CHROOT_MOUNTPOINT


Connected. Take this REPL, brother, and may it serve you well.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: grubrepair

Unread post by machinebacon » Fri Aug 23, 2013 10:18 am

Thanks xaos, I love it! This will definitely save some serious ass :D
..gnutella..

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: grubrepair

Unread post by xaos52 » Wed Sep 18, 2013 9:20 am

New version of the script available from my github repos
Renamed the script 'grubrepair' to 'changeroot_update_grub'.
Added script changeroot_apt_get_update which lets you update and upgrade a system by chrooting into it.
Both scripts source chroot_lib.
It is easy now to make a new script to do whatever you want in a chrooted system, by launching a single script from the command line and carrying on with whatever you are doing on the system you are currently booted into.
Connected. Take this REPL, brother, and may it serve you well.

Post Reply