7

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

6

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
1
  • 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.