Boot Loader: Grub


		grub		安裝 GRUB 開機管理程式。

  1. LILO 的問題
    1. 沒有提供互動介面,所以如果 partition 亂了,容易導致無法開機的問題;
      ### 維護模式的進入:
      linux –s
      linux single
      
    2. 每次安裝新核心,都需要重新安裝 lilo 一次。
  2. grub 的優點:
    1. 只需要安裝一次:只要修改 /boot/grub/menu.lst 中的設定,下次開機就生效。
    2. 認識與支援較多的檔案系統。
    3. 可以使用 grub 的主程式直接在檔案系統中搜尋核心。
    4. 有互動介面:
      1. 開機時,可以進入『自行編輯與修改開機設定項目』模式
      2. 可進入維護模式
  3. grub 硬碟代號:(hd0,0)
    1. 硬碟代號以小括號 ( ) 包起來;
    2. 硬碟以 hd 表示,後面會接一組數字;
    3. 以『搜尋順序』做為硬碟的編號,而不是依照硬碟排線的排序。
    4. 第一個搜尋到的硬碟為 0 號,第二個為 1 號,以此類推;
    5. 每顆硬碟的第一個 partition 代號為 0,依序類推。
    6. 硬碟代號表
      硬碟搜尋順序 在 Grub 當中的代號
      第一顆 (hd0) (hd0,0) (hd0,1) (hd0,4) ....
      第二顆 (hd1) (hd1,0) (hd1,1) (hd1,4) ....
      第三顆 (hd2) (hd2,0) (hd2,1) (hd2,4) ....
  4. 如果 Linux 原來使用 lilo,現在要改用 grub,必須使用 grub-install 將一些必要的檔案複製到 /boot/grub。
    [root@www ~]# grub-install [--root-directory=DIR] INSTALL_DEVICE
    選項與參數:
    --root-directory=DIR 預設 /,grub-install 將檔案複製到 /boot/grub/,
      若要複製到其他目錄與裝置去,就要用這個參數。
    INSTALL_DEVICE 安裝的裝置代號。
    
    #範例一:將 grub 安裝在目前系統的 MBR 底下,系統為 /dev/hda:
    [root@www ~]# grub-install /dev/hda
    # 因為原本 /dev/hda 就是使用 grub ,所以似乎不會出現什麼特別的訊息。
    # 如果去查閱一下 /boot/grub 的內容,會發現所有的檔案都更新了,因為重裝了!
    
    #範例二:我的 /home 為獨立的 /dev/hda3 ,如何安裝 grub 到 /dev/hda3 (boot sector)
    [root@www ~]# grub-install --root-directory=/home /dev/hda3
    Probing devices to guess BIOS drives. This may take a long time.
    Installation finished. No error reported.
    This is the contents of the device map /home/boot/grub/device.map.
    Check if this is correct or not. If any of the lines is incorrect,
    fix it and re-run the script `grub-install'.
    
    (fd0)   /dev/fd0
    (hd0)   /dev/hda   %*<==會給予裝置代號的對應表!*)
    
    [root@www ~]# ll /home/boot/grub/
    -rw-r--r-- 1 root root     30 Apr 30 11:12 device.map
    -rw-r--r-- 1 root root   7584 Apr 30 11:12 e2fs_stage1_5
    ....(底下省略)....
    # 檔案都安裝了,但並沒有設定檔,那要自己建立。
    

  5. 編輯 menu.lst
    [root @test root]# vi /boot/grub/menu.lst 
    timeout 100                      %*<==延遲時間,單位0.1秒 *)
    Color black/cyan yellow/cyan     %*<==設定顏色的參數 *)
    i18n (hd0,0)/boot/grub/messages  %*<==設定 i18n 多國語系的訊息提示 *)
    keytable (hd0,0)/boot/us.klt     %*<==設定鍵盤形式,通常使用美規的鍵盤即可 *)
    Default 0                   %*<==預設的開機選項,依照底下的排序,第一個為 0 *)
    title linux                      %*<==第一個開機設定檔,亦即排序為 0 號 *)
         kernel (hd0,0)/boot/vmlinuz root=/dev/hda1 devfs=mount 
         initrd (hd0,0)/boot/initrd.img 
                                %*<==上面兩個則是開機設定檔的基本內容,分別是 *)
                                   核心檔案所在地與 Ramdisk 的設定內容 
    title failsafe              %*<==第二個開機設定檔,亦即排序為 1 號 *)
         kernel (hd0,0)/boot/vmlinuz root=/dev/hda1 devfs=nomount failsafe 
         initrd (hd0,0)/boot/initrd.img 
    title windows 
      rootnoverify (hd0,0)  
      chainloader +1
    ## 設定 Windows OS 的方式跟 Linux 不同。
    ## rootnoverify 指示 GRUB 不需掛載 hda1 的分割區,只需知道是要啟動此分割區上的作業系統即可。 
    ## chainloader 指定 hda1 的第一個磁區做啟動磁區 (boot sector)。
    ## 兩行亦可合併成 chainloader (hd0,0) +1。
    

  6. 編輯 menu.lst
    [root @test root]# vi /boot/grub/menu.lst 
    timeout 100                      %*<==延遲時間,單位0.1秒 *)
    Color black/cyan yellow/cyan     %*<==設定顏色的參數 *)
    i18n (hd0,0)/boot/grub/messages  %*<==設定 i18n 多國語系的訊息提示 *)
    keytable (hd0,0)/boot/us.klt     %*<==設定鍵盤形式,通常使用美規的鍵盤即可 *)
    Default 0                   %*<==預設的開機選項,依照底下的排序,第一個為 0 *)
    title linux                      %*<==第一個開機設定檔,亦即排序為 0 號 *)
         kernel (hd0,0)/boot/vmlinuz root=/dev/hda1 devfs=mount 
         initrd (hd0,0)/boot/initrd.img 
                                %*<==上面兩個則是開機設定檔的基本內容,分別是 *)
                                   核心檔案所在地與 Ramdisk 的設定內容 
    title failsafe              %*<==第二個開機設定檔,亦即排序為 1 號 *)
         kernel (hd0,0)/boot/vmlinuz root=/dev/hda1 devfs=nomount failsafe 
         initrd (hd0,0)/boot/initrd.img 
    title windows 
      rootnoverify (hd0,0)  
      chainloader +1
    ## 設定 Windows OS 的方式跟 Linux 不同。
    ## rootnoverify 指示 GRUB 不需掛載 hda1 的分割區,只需知道是要啟動此分割區上的作業系統即可。 
    ## chainloader 指定 hda1 的第一個磁區做啟動磁區 (boot sector)。
    ## 兩行亦可合併成 chainloader (hd0,0) +1。
    
  7. grub:安裝 grub 開機管理程式。
    1. 使用 root [磁碟代號] 選擇開機根目錄 / 所在磁碟代號;
    2. 使用 setup [磁碟代號] 選擇 MBR 或 Super block 直接安裝;
    3. 使用 quit 離開 grub 。
    [root @test root]# grub 
    # 安裝在 Super Block : 
    grub> root (hd0,0)      %*<==這裡輸入我要的 root 硬碟磁區! *)
     Filesystem type is ext2fs, partition type 0x83 
    grub> setup (hd0,0)     %*<==實際安裝上來!使用 setup 指令! *)
     Checking if "/boot/grub/stage1" exists... yes 
     Checking if "/boot/grub/stage2" exists... yes 
     Checking if "/boot/grub/e2fs_stage1_5" exists... yes 
     Running "embed /boot/grub/e2fs_stage1_5 (hd0,0)"... failed (this is not fatal) 
     Running "embed /boot/grub/e2fs_stage1_5 (hd0,0)"... failed (this is not fatal) 
     Running "install /boot/grub/stage1 (hd0,0) /boot/grub/stage2 p /boot/grub/menu 
    .lst "... succeeded 
    Done. 
    
    # 安裝在 MBR : 
    grub> root (hd0,0) 
     Filesystem type is ext2fs, partition type 0x83 
    grub> setup (hd0) 
     Checking if "/boot/grub/stage1" exists... yes 
     Checking if "/boot/grub/stage2" exists... yes 
     Checking if "/boot/grub/e2fs_stage1_5" exists... yes 
     Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  17 sectors are embedded. 
    succeeded 
     Running "install /boot/grub/stage1 (hd0) (hd0)1+17 p (hd0,0)/boot/grub/stage2 
    /boot/grub/menu.lst"... succeeded 
    Done. 
    grub> quit             %*<==離開 grub*)
    

練習題

  1. boot loader grub 中的硬碟代號 (hd0,0),代表意義為何?
    Sol. hd 指的是 IDE 的硬碟, 第一個 0 代表第一個 IDE 的 master,第二個 0 代表第一塊 partition 的代號,故此代號為 hda1。
  2. boot loader grub 中的硬碟代號 (hd3,1),代表意義為何?
    Sol. hd 指的是 IDE 的硬碟, 3 代表第二個 IDE 的 slave,1 代表第二塊 partition 的代號,故此代號為 hdd2。
  3. 假設系統僅有一顆 SATA 硬碟,該硬碟的第一個邏輯分割槽在 Linux 與 grub 中的檔名與代號?
    Sol. Linux 中的檔名為 /dev/sda5,grub 中的磁碟代號為 (hd0,4)。
  4. boot loader grub 的設定檔為何?
    Sol. /boot/grub/menu.lst
  5. boot loader grub 的設定檔中『Default 0』代表意義為何?
    Sol. 預設的開機選項為排序第一的項目。
  6. boot loader grub 的設定檔中『Default 2』代表意義為何?
    Sol. 預設的開機選項為排序第三的項目。
  7. boot loader grub 的設定檔中『timeout 100』代表意義為何?
    Sol. 延遲時間 10 秒,若沒做選擇會以預設項目開機。
  8. boot loader grub 的設定檔中『kernel (hd0,0)/boot/vmlinuz』代表意義為何?
    Sol. 核心檔案為分割槽 hda1 下的 /boot/vmlinuz。
  9. boot loader grub 的設定檔中『initrd (hd0,0)/boot/initrd.img』代表意義為何?
    Sol. Ramdisk 的設定內容在 hda1 下的 /boot/initrd.img。
  10. boot loader grub 的設定檔中,windows 開機項目中之『rootnoverify (hd0,0)』代表意義為何?
    Sol. 指示 GRUB 不需掛載 hda1 的分割區,只需知道是要啟動此分割區上的作業系統即可。
  11. boot loader grub 的設定檔中,windows 開機項目中之『chainloader (hd0,0) +1』代表意義為何?
    Sol. 指定 hda1 的第一個磁區做啟動磁區 (boot sector)。
  12. 安裝 boot loader grub 時,執行指令 grub 後,必須做那些動作?
    Sol. 1. 使用 root 選擇開機根目錄 / 所在磁碟代號;2. 使用 setup 選擇 MBR 或 Super block 安裝;3.使用 quit 離開 grub 。
  DYWANG_HOME