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?

1 Answer 1


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.

set -e

if [ "$UID" -ne "0" ]; then
  echo Must be run as root
  exit 99

  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

  # 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
      # 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

  return 1

if scan_grub2 "/dev/sda"; then
  echo Found grub 2
  echo Did not find grub 2
  #Uncomment the next line to upgrade
  • It says "Grub could not be found, Did not find grub 2" but I guess I'm using at least one of them... Apr 3, 2022 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.