Ubuntu

如何在不重新啟動的情況下無損檢查 grub2 是否安裝在引導扇區中或被 grub1 鍊式載入?

  • April 20, 2011

我們正在將幾個系統從 Debian Lenny 升級到 Squeeze,我想確保我沒有錯過任何 grub2 安裝。預設情況下,Squeeze chain-boot-loads 從 grub1 載入,您必須執行upgrade-from-grub-legacy才能升級。因此,我希望能夠遠端檢查 grub2 是否已安裝在磁碟引導扇區中,而無需重新啟動,也不會覆蓋引導扇區。

有什麼比對硬碟驅動器的早期塊進行 hexdump 並嘗試辨識 grub2 特定字節更容易的事情嗎?

我偶然發現了 grub2 debian 源包中的答案。事實證明,它確實需要引導扇區的轉儲——因此單獨打包的腳本可能很有用。這是一個腳本(只是官方函式的包裝),它會告訴您 grub2 是否已安裝到引導扇區中。它可以很容易地修改用於類似用途。

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

引用自:https://serverfault.com/questions/259086