grub升級到grub2的操作 由於公司大多數系統用的還是傳統GRUB(0.97版),後階段需要升級為GRUB2,該版本為1.96或者更新版本. 先來看下grub2與傳統的grub相比之下主要增加的功能包含: 1.支援指令碼,包括條件文法和函數 2.動態載入模組 3.救援模式 4.可定製菜單,主題 5.支援圖形化啟動菜單以及增強引導畫面功能 6.新的設定檔結構,全面支援UUID 雖然grub2預設菜單對使用過grub的人來說看起來很熟悉,但是裡面還是有很大不同. 1.沒有/boot/grub/menu.1st。已被/boo/grub/grub.cfg取代 2.在grub提示符下沒有find命令,已變成search. 3.grub2已經模組化,不再需要'stage1.5'.所以引導器可以變得很小. 4.磁碟分割編號變更。現在第一個磁碟分割是 1 而不是 0。然而第一個裝置仍然是 0(沒有改變) 5./etc/grub/grub.cfg,不應在被手動編輯,主要用來編輯的設定檔是/etc/default/grub,還有所在/etc/grub.d/目錄下的檔案,在update-grub命令執行前,所有設定檔的修改都不會生效. 查看grub版本,升級至grub2 1root@10.1.1.45:~# grub-install -v2grub-install (GNU GRUB 0.97)01root@10.1.1.45:~# dpkg -i grub-common_1.96+20080724-16_amd64.deb02(Reading database ... 35648 files and directories currently installed.)03Preparing to replace grub-common 1.96+20080724-16 (using grub-common_1.96+20080724-16_amd64.deb) ...04Unpacking replacement grub-common ...05Setting up grub-common (1.96+20080724-16) ...06root@10.1.1.45:~# dpkg -i grub-pc_1.96+20080724-16_amd64.deb07Selecting previously deselected package grub-pc.08dpkg: considering removing grub in favour of grub-pc ...09dpkg: yes, will remove grub in favour of grub-pc.10(Reading database ... 35648 files and directories currently installed.)11Unpacking grub-pc (from grub-pc_1.96+20080724-16_amd64.deb) ...12Setting up grub-pc (1.96+20080724-16) ...13Generating core.img14Saving menu.lst backup in /boot/grub/menu.lst_backup_by_grub2_postinst15Running update-grub Legacy to hook our core.img in it16 Searching for GRUB installation directory ... found: /boot/grub17 Searching for default file ... found: /boot/grub/default18 Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst19 Searching for splash image ... none found, skipping ...20 Found GRUB 2: /boot/grub/core.img21 Found kernel: /boot/vmlinuz-2.6.26-2-amd6422 Updating /boot/grub/menu.lst ... done23 24Updating /boot/grub/grub.cfg ...25Found linux image: /boot/vmlinuz-2.6.26-2-amd6426Found initrd image: /boot/initrd.img-2.6.26-2-amd6427done28Processing triggers for man-db ...1.期間會有提示資訊,在選擇「OK」後,下一個選項允許使用者在正常的 GRUB 菜單中通過新增一個功能表項目來測試GRUB2.選擇「Yes」將在 GRUB 菜單中增加一個 Chainload 項.當下次使用 GRUB 啟動時,使用者可以選擇正常的 GRUB 功能表項目或是將控制權通過 Chainload 功能表項目轉到 GRUB 2。 2.下一步將會把來自 GRUB 的 menu.lst 中的內容匯入到[Linux command line](如),或者匯入[Linux default command line]。這些項目允許使用者輸入任意的特殊命令,例如 quiet, splash,noapic 或是其他同樣性質的命令。GRUB 2 將會自動匯入這些配置。通常使用者不需要修改並選擇「OK」。若有需要,選項也可以在以後再加入。 當系統已安裝完成grub2,還需要更改幾個參數 01root@10.1.1.45:grub# cat /etc/default/grub02# This file is sourced by update-grub, and its variables are propagated03# to its children in /etc/grub.d/04GRUB_DEFAULT=005GRUB_TIMEOUT=506GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`07GRUB_CMDLINE_LINUX="console=tty0 console=ttyS1,115200" #在核心引導加入console串連的相關參數,保證可以通過ipmi串連該伺服器.由於該機器型號是R710故用115200.08 09# Uncomment to disable graphical terminal (grub-pc only)10GRUB_TERMINAL=console #選擇console作為終端裝置,保證ipmi串連顯示正色.11# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux12#GRUB_DISABLE_LINUX_UUID=true1root@10.1.1.45:# vi /etc/inittab2T1:23:respawn:/sbin/getty -L ttyS1 115200 vt100 保證登入不顯示亂碼1root@10.1.1.45:grub# update-grub 生效設定檔2Updating /boot/grub/grub.cfg ...3Found linux image: /boot/vmlinuz-2.6.26-2-amd644Found initrd image: /boot/initrd.img-2.6.26-2-amd645done重啟機器後可以看到更新的grub傳統菜單歡迎畫面,也會看到一個「Chainload into GRUB 2」功能表項目,用來測試 GRUB 2 安裝。選擇 Chainload 選項將會把控制權轉到 GRUB 2 並顯示GRUB2菜單。 使用以下命令可以完全升至grub2. 01root@10.1.1.45:~# upgrade-from-grub-legacy02 03Installing GRUB to Master Boot Record of your first hard drive ...04 05Installation finished. No error reported.06This is the contents of the device map /boot/grub/device.map.07Check if this is correct or not. If any of the lines is incorrect,08fix it and re-run the script `grub-install'.09 10(hd0) /dev/hda11 12GRUB Legacy has been removed, but its configuration files have been preserved,13since this script cannot determine if they contain valuable information. If14you would like to remove the configuration files as well, use the following15command:16 17 rm -f /boot/grub/menu.lst* 下面介紹grub2使用的主要配置目錄和檔案: 1./boot/grub/grub.cfg:類似grub下/boot/grub/menu.1st的檔案.包含grub2菜單資訊.不應該手動編輯.應該由update-grub來更新,在/boot/grub目錄小會看到許多*.mod檔案,這表明grub2的模組化本質.grub2啟動時按需要載入不同的模組. 01root@10.1.1.45:grub# ls02acorn.mod _bsd.mod device.map gzio.mod lnxboot.img multiboot.mod reiserfs.mod vbe.mod03affs.mod bsd.mod diskboot.img halt.mod loadenv.mod normal.mod search.mod vbetest.mod04afs.mod cat.mod echo.mod hello.mod loopback.mod ntfscomp.mod serial.mod vga.mod05amiga.mod cdboot.img elf.mod help.mod ls.mod ntfs.mod sfs.mod video.mod06aout.mod _chain.mod ext2.mod hexdump.mod lspci.mod partmap.lst sleep.mod videotest.mod07apple.mod chain.mod fat.mod hfs.mod lvm.mod pci.mod sun.mod xfs.mod08ascii.pff cmp.mod font.mod hfsplus.mod memdisk.mod pc.mod terminal.mod09ata.mod command.lst fshelp.mod iso9660.mod menu.lst play.mod terminfo.mod10biosdisk.mod configfile.mod fs.lst jfs.mod menu.lst~ png.mod test.mod11bitmap.mod core.img fs_uuid.mod jpeg.mod menu.lst_backup_by_grub2_postinst pxeboot.img tga.mod12blocklist.mod cpio.mod gfxterm.mod kernel.img minix.mod raid.mod udf.mod13boot.img cpuid.mod gpt.mod _linux.mod moddep.lst read.mod ufs.mod14boot.mod default grub.cfg linux.mod _multiboot.mod reboot.mod vbeinfo.mod 2./etc/default/grub:包含以前在 GRUB 傳統的 menu.lst 前半部配置內容;以及每行核心配置後面的追加配置。 3./etc/grub.d/:在此目錄中的檔案將會在執行 update-grub 命令的時候讀取,並將其中的命令合并至 /boot/grub/grub.cfg 中。 在 grub.cfg 菜單中的功能表項目順序是依照在此目錄中檔案的順序來決定。 具有較小數字首碼檔案名稱的檔案將會先執行.在update-grub 執行過程中只有可執行檔會輸出至 grub.cfg。 在此目錄中的預設檔案為: 00_header 配置初始顯示項目,例形模式、預設選項、時間限制等等。這些配置資訊一般由/etc/default/grub 中匯入。使用者一般不需要對此檔案做任何更動。 05_debian_theme 此檔案中的配置用來配置引導畫面、文字顏色、高亮度選項及主題。再沒有引導畫面的情況下,此檔案會配置一個單色的主題來顯示初始菜單畫面。如何配置字型顯示及引導畫面將會在引導畫面和主題小節中討論。 10_hurd 定位 Hurd 核心位置。目前未使用。 10_linux 定位當前作業系統使用中的 root 裝置核心位置。此資訊將會用來建立這些核心顯示在菜單中的名稱。 30_os-prober 此檔案和 os-prober,用來搜尋 Linux 和其他動作系統。結果將會依據此檔案中的內容而放至 grub.cfg 中。此檔案被劃分成四個段落,代表由該指令碼所處理的四種作業系統類型:Windows, Linux, OSX 和 Hurd。此指令碼中的變數用來指定在 /boot/grub/grub.cfg 和 GRUB 2 菜單中的名稱顯示格式。熟悉基本指令碼的使用者可以修改這些變數來改變菜單選項的顯示格式。此外,使用者可以修改 /etc/default/grub 的一個選項來關閉這個指令碼。40_custom 用來加入自訂功能表項目的樣板,將會在執行 update-grub 命令時嵌入至 grub.cfg 中。此檔案中的內容,在「exec tail -n +3 $0」這一行和預設的注釋後,會毫無改變地直接匯入至/etc/default/grub.cfg. 1root@10.1.1.45:grub.d# ll2total 283-rwxr-xr-x 1 root root 1897 2009-02-11 09:58 00_header4-rwxr-xr-x 1 root root 1143 2009-02-11 09:52 05_debian_theme5-rwxr-xr-x 1 root root 2369 2009-02-11 09:58 10_hurd6-rwxr-xr-x 1 root root 4050 2009-02-11 09:58 10_linux7-rwxr-xr-x 1 root root 2567 2009-02-11 09:58 30_os-prober8-rwxr-xr-x 1 root root 84 2009-02-11 09:58 40_custom9-rw-r--r-- 1 root root 483 2009-02-11 09:58 README 在升級其它debian機器時,其中重啟機器過了grub遇到error: unknown command 'initrd'錯誤,後來Google一番,發現出問題的command list一般是如下形式: 1menuentry "Debian GNU/Linux, linux 2.6.32-2-amd64" {2 set root=(hd0,1)3 search --fs-uuid --set c618b7d1-4769-4dfe-8169-66e9f8aace434 linux /boot/vmlinuz-2.6.32-2-amd64 root=UUID=c618b7d1-4769-4dfe-8169-66e9f8aace43 ro console=tty0 console=ttyS1,1152005 initrd /boot/initrd.img-2.6.32-2-amd646}解決辦法是刪掉search那一行.