How Do You Non-Destructively Check if grub2 is Installed in The Boot Sector or is Being Chain-Loaded by grub1, Without Rebooting?
We're upgrading several systems from Debian Lenny to Squeeze and I'd like to ensure that I haven't missed any grub2 installations. By default, Squeeze chain-boot-loads from grub1 and you have to run upgrade-from-grub-legacy
to upgrade. So I'd like to be able to remotely check to see if grub2 has been installed in the disk boot sector without rebooting, and without overwriting the boot sector.
Is there anything easier than doing a hexdump of the early blocks of the hard drive and trying to identify grub2-specific bytes?
I stumbled onto the answer in the grub2 debian source package. It turns out that it does require a dump of the bootsector - so a separately packaged script might be useful. Here is a script (just a wrapper around the official function) that will tell you whether or not grub2 has been installed into the boot sector. It can be easily modified for similar uses.
#!/bin/bash
set -e
if [ "$UID" -ne "0" ]; then
echo Must be run as root
exit 99
fi
scan_grub2()
{
if ! dd if="$1" bs=512 count=1 2>/dev/null | grep -aq GRUB; then
# No version of GRUB is installed.
echo Grub could not be found
return 1
fi
# The GRUB boot sector always starts with a JMP instruction.
initial_jmp="$(dd if="$1" bs=2 count=1 2>/dev/null | od -Ax -tx1 | \
head -n1 | cut -d' ' -f2,3)"
[ "$initial_jmp" ] || return 1
initial_jmp_opcode="${initial_jmp%% *}"
[ "$initial_jmp_opcode" = eb ] || return 1
initial_jmp_operand="${initial_jmp#* }"
case $initial_jmp_operand in
47|4b|4c|63)
# I believe this covers all versions of GRUB 2 up to the package
# version where we gained a more explicit mechanism. GRUB Legacy
# always had 48 here.
return 0
;;
esac
return 1
}
if scan_grub2 "/dev/sda"; then
echo Found grub 2
else
echo Did not find grub 2
#Uncomment the next line to upgrade
#upgrade-from-grub-legacy
fi