The script is meant to be run from the Live CD. It will ask you to choose your Linux partition and the hard drive you want to install Grub to. If there is only one partition or drive, it will automatically select it for you (Asking for confirmation before installing Grub, of course). The script also backs up your MBR before installing, so you can restore it if something goes wrong.
This has only been tested with Grub2 and Linux Mint 8 Main edition, but I think it should work with Grub legacy and other versions of Mint (other distros, too).
This script requires gtkdialog
Any feedback is welcome.
v3
Code: Select all
#!/bin/bash
# Grub-install GUI
# Many thanks to Zigbert and others at the Puppy Linux forums for their gtkdialog tips
# http://www.murga-linux.com/puppy/viewtopic.php?t=38608
[ "$(whoami)" != "root" ] && exec gksu bash $0
FDISK="$(fdisk -l 2>/dev/null)"
probedisks() {
local DISKS="$(echo "$FDISK" | grep '^Disk /dev/sd' | tr ' ' : | cut -d: -f2 | tr '\n' ' ')"
for disk in $DISKS; do
local disk_BSIZE="$(echo "$FDISK" | grep "^Disk $disk" | cut -d' ' -f5)"
local disk_SIZE="$(( $disk_BSIZE / 1024 / 1024 / 1024 ))GB"
echo "$disk|$disk_SIZE"
done
}
probeparts() {
local PARTS="$(echo "$FDISK" | grep '^/dev/sd' | cut -d' ' -f1 | tr '\n' ' ')"
for part in $PARTS; do
local part_ID="$(echo "$FDISK" | grep "^$part" | tr -s ' ' | cut -d' ' -f5)"
if [ "$part_ID" = "83" ]; then
local part_FS="$(mount -f --guess-fstype $part)"
local part_BLKSIZE="$(echo "$FDISK" | grep "^$part" | tr -s ' ' | cut -d' ' -f4)"
local part_SIZE="$(( $part_BLKSIZE * 1024 / 1024 / 1024 / 1024 ))GB"
echo "$part|$part_FS|$part_SIZE"
fi
done
}
DISKS="$(probedisks)"
PARTS="$(probeparts)"
export MAIN_DIALOG="
<window title=\"$(basename $0)\">
<vbox>
<text>
<label>Grub-Install GUI</label>
</text>
<frame Select your Linux partition>
<tree headers-clickable=\"false\">
<label>Device | Filesystem | Size</label>
<variable>LNXPART</variable>
$(for i in $PARTS; do echo "<item>$i</item>"; done)
</tree>
<hbox>
<checkbox>
<label>Or select a mountpoint</label>
<variable>CHECKBOX</variable>
<default>false</default>
<action>if false Clear:MNTPT</action>
<action>if false Disable:MNTPT</action>
<action>if false Disable:BUTTON</action>
<action>if false Enable:LNXPART</action>
<action>if true Enable:MNTPT</action>
<action>if true Enable:BUTTON</action>
<action>if true Disable:LNXPART</action>
</checkbox>
<entry accept=\"directory\" sensitive=\"false\">
<variable>MNTPT</variable>
</entry>
<button sensitive=\"false\">
<input file stock=\"gtk-open\"></input>
<action>Fileselect:MNTPT</action>
<variable>BUTTON</variable>
</button>
</hbox>
</frame>
<frame Where do you want to install Grub?>
<tree headers_visible=\"false\">
<label>||</label>
<variable>GRUB_TARGET</variable>
$(for i in $DISKS; do echo "<item>$i</item>" | sed -e 's/|/||/'; done)
$(for i in $PARTS; do echo "<item>$i</item>"; done)
</tree>
</frame>
<hbox>
<button>
<label>Install Grub</label>
<input file stock=\"gtk-harddisk\"></input>
<action>EXIT:install</action>
</button>
<button cancel></button>
<button help>
<action>Launch:HELP_WINDOW</action>
</button>
</hbox>
</vbox>
</window>"
export HELP_WINDOW="
<window title=\"Help\">
<vbox>
<frame Select your Linux partition>
<text>
<label>Select your Linux / partition here.</label>
</text>
</frame>
<frame Select a mountpoint>
<text>
<label>This is mostly for people who have /boot on a separate partition. Premount your partitions and select the mountpoint</label>
</text>
</frame>
<frame Where do you want to install Grub?>
<text>
<label>Grub is usually installed to the MBR of the first hard drive (/dev/sda), but can also be installed to the superblock of your Linux / partition. If unsure, you probably want /dev/sda</label>
</text>
</frame>
<hbox>
<button>
<label>Close</label>
<action>Closewindow:HELP_WINDOW</action>
</button>
</hbox>
</vbox>
</window>"
I="$IFS"; IFS=""
for statements in $(gtkdialog -p MAIN_DIALOG); do
eval $statements
done
IFS="$I"
[ "$EXIT" != "install" ] && exit 1
if [ ! -d "$MNTPT" -a ! -b "$LNXPART" ]; then
echo "No Linux partition or mountpoint selected" 1>&2
exit 1
fi
if [ ! -b "$GRUB_TARGET" ]; then
echo "No Grub target selected" 1>&2
exit 1
fi
export TMPFILE="$(mktemp /tmp/grub-install.log.XXXXXX)"
if [ ! -d "$MNTPT" ]; then
export MNTPT="$(mktemp -d /mnt/mount.XXXXXX)"
mount $LNXPART $MNTPT || exit 1
fi
cleanup() {
umount $MNTPT 2>/dev/null
rmdir $MNTPT 2>/dev/null
rm -f $TMPFILE
}
trap "cleanup" 0 1 2 15
grub_progress() {
echo "Backing up MBR..." && \
dd if=$GRUB_TARGET of=mbr.$$ bs=512 count=1 2>$TMPFILE &&
sleep 1 && \
echo 33 && \
echo "Installing Grub..." && \
grub-install --root-directory=$MNTPT $GRUB_TARGET 1>>$TMPFILE 2>>&1 &&
sleep 1 && \
echo 66 && \
echo "Finished" && \
echo 100
RET="$?"
if [ "$RET" != "0" ]; then
echo "Failed! Check Log"
echo 100
fi
}
export -f grub_progress
export GRUB_PB="
<window title=\"grub-install\">
<notebook labels=\"Progress|Log\">
<vbox>
<frame Installing Grub... Please wait>
<progressbar>
<input>grub_progress</input>
<action>Enable:CLOSE</action>
<action>Refresh:LOG</action>
</progressbar>
</frame>
<hbox>
<button sensitive=\"false\">
<label>Close</label>
<variable>CLOSE</variable>
<action>Closewindow:GRUB_PB</action>
</button>
</hbox>
</vbox>
<vbox>
<frame Logfile>
<text selectable=\"true\">
<input file>$TMPFILE</input>
<variable>LOG</variable>
</text>
</frame>
</vbox>
</notebook>
</window>"
gtkdialog -p GRUB_PB
exit 0
#The end