引導加載程序grub詳解

上一章講到,常見的boot loader(引導加載程序)有LILO和grub,LILO有一大缺點,它只能識別0-1023範圍內的柱面構成的分區的內核文件,故已逐漸被grub取代。node

Grub(GRand Unified Bootloader)是一個來自GNU項目的多操做系統引導程序,它負責加載內核並移交控制權linux


一、Grub版本:shell

   CentOS 五、6  Grub 0.97vim

   CentOS 7    Grub2 1.96bash


二、grub程序由三段組成網絡

   stage1: MBR(0柱面 0磁道 1扇區)中ide

   stage1_5: MBR隨後的扇區ui

   stage2: 讀取grub.conf配置文件,並實現引導功能的擴展this

  MBR所給予的空間畢竟過小,容不下較大的引導程序,所以grub程序被分爲三段:"stage 1" 被裝入磁盤的MBR中;特殊的"stage 1.5"被裝入MBR隨後的扇區,它可以識別內核和"stage 2"所在分區的文件系統格式並幫助引導"stage 2",它是"stage 1"和"stage 2"之間的紐帶;"stage 2"位於文件系統上。stage 2程序和grub.conf能夠與內核文件處於不一樣的分區上(但必須位於同一磁盤),只要"stage 1.5"可以驅動它們各自所在的分區。spa

  在linux中,與啓動相關的文件(grub、vmlinuz、initramfs)均位於boot目錄下

wKiom1Y97cezUjDlAAMPMpfmEjg976.jpg


三、grub的功能

  ①提供菜單,並提供交互式接口

     e: 進入編輯模式

  ②選擇要啓動的內核或系統

     容許傳遞引導參數給內核

     選擇界面可隱藏

  ③爲編輯功能提供保護機制

     啓用內核文件:

       選擇運行指定的內核得先輸入密碼

     傳遞參數:

       使用e命令得先輸入密碼


開機後有三秒種的過渡頁面,按任意鍵可進入菜單頁面。在菜單頁面可用上下鍵選擇所要啓動的內核或內核,按e鍵進入內核編輯模式,按c鍵進入grub命令行模式

wKiom1Y93ECDZICpAAXuijAZyK8123.jpg


四、grub命令行接口:

    help:查看幫助,即列出命令列表

    root (DEVICE):指定哪一個分區爲接下來要啓動的系統或內核文件所在的分區,例如root (hd0,0)

      全部硬盤都被識別爲hd

      不一樣的硬盤基於數字標識,從0開始:如hd0, hd1等

      同一個硬盤上的不一樣分區,也使用數字標識,從0開始:如hd0,0  hd1,5

    find (DEVICE)/path/to/file查找文件。當咱們不肯定內核文件位於哪一個分區上時,可以使用該命令;提示:grub支持tab補全

    kernel /path/to/kernel_file:指定要運行的內核文件,如kernel /vmlinuz-2.6.32-431.el6.x86_64。

     【注】:這裏的文件路徑是相對於內核文件所在分區而言,其開頭的「/」不是指操做系統的根,而是指內核所在的分區,由於grub是直接以該分區爲入口找尋內核的。如有單獨的boot分區,則不能寫成kernel /boot/vmlinuz-2.6.32-431.el6.x86_64;在指定的內核位置後一般還指明要掛載的根分區

    initrd /path/to/kernel_file:爲要運行的內核指定其可用的ramdisk文件,其版本必須與內核版本保持一致

     【注】:grub沒法識別邏輯卷,所以kernel與initramfs必定不能放於邏輯捲上

    boot: 啓動此前配置好的內核或系統


五、grub.conf

  經過grub命令行接口接入系統比較麻煩,爲此,grub提供了一個配置文件/boot/grub/grub.conf(它有一個軟連接/etc/grub.conf),grub會讀取這個配置文件並按配置參數引導系統

  參數:

   default=:默認選擇第幾個title配置的內核或系統,各title從0開始編號

   timeout=#:過渡頁面顯示的超時時長;

   splashp_w_picpath=/path/to/some_p_w_picpath_file:指定菜單的背景圖片;此圖片只能爲14bits色,xpm格式,gzip壓縮;

   hiddenmenu:隱藏菜單

   title TILTE STRING:顯示於菜單中的標題

      root

      kernel

      initrd

  ■若因爲grub.conf文件錯誤或丟失而沒法自動進入系統,可經過grub命令行引導系統後再手動更改或建立這個文件

[root@node2 ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
	root (hd0,0)
	kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=aa0330af-3681-428c-98e2-ccf2e6f0f686 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
	initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@node2 ~]# ll /etc/grub/conf
ls: cannot access /etc/grub/conf: No such file or directory
[root@node2 ~]# ll /etc/grub.conf
lrwxrwxrwx. 1 root root 22 Aug  6 04:44 /etc/grub.conf -> ../boot/grub/grub.conf
[root@node2 ~]# cp /boot/{vmlinuz-2.6.32-431.el6.x86_64,initramfs-2.6.32-431.el6.x86_64.img} /    #將boot目錄下的內核和臨時根文件複製一份到根分區下
[root@node2 ~]# ls /
bin   dev  home                                 lib    lost+found  misc  net  proc  sbin     srv  tmp  var
boot  etc  initramfs-2.6.32-431.el6.x86_64.img  lib64  media       mnt   opt  root  selinux  sys  usr  vmlinuz-2.6.32-431.el6.x86_64
[root@node2 ~]# vim /etc/grub.conf
...
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=aa0330af-3681-428c-98e2-ccf2e6f0f686 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.el6.x86_64.img
#添加以下幾行,構成一個新的啓動項
title testCentos  #新的標題
        root (hd0,1)  #如今根分區下也有內核和臨時根文件,根分區爲sda2,故寫成(hd0,1)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda2
        initrd /initramfs-2.6.32-431.el6.x86_64.img

wKiom1Y98ETSejkjAAFWhHyrKD0221.jpg


六、grub保護機制:可防止惡意用戶隨意進入單用戶模式修改root密碼

  ①生成密碼:

   grub-md5-crypt

  ②保護全部內核,須要在title以外添加

   password --md5 密碼串

  ③保護使用某內核,則須要在內核對應的title之下添加

   password --md5 密碼串

[root@node2 ~]# grub-md5-crypt   #生成密碼
Password: 
Retype password: 
$1$oWUbV$Sb/PsrhmkE5bUJPMJGn871
[root@node2 ~]# vim /etc/grub.conf
...
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$oWUbV$Sb/PsrhmkE5bUJPMJGn871   #添加於title以外可保護全部內核
title CentOS (2.6.32-431.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=aa0330af-3681-428c-98e2-ccf2e6f0f686 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.el6.x86_64.img
title testCentos
password --md5 $1$oWUbV$Sb/PsrhmkE5bUJPMJGn871   #可保護使用指定內核
        root (hd0,1)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=aa0330af-3681-428c-98e2-ccf2e6f0f686
        initrd /initramfs-2.6.32-431.el6.x86_64.img

wKiom1ZDPuiTisr7AADVUM40CRc925.png


七、安裝grub的方式:

  方法1:使用grub-install命令

    grub-install [--root-directory=DIR] DEVICE 

      DEVICE:針對哪一個磁盤安裝

      --root-directory=DIR:指定grub映像文件(主要是「stage 2」)的存放位置,默認爲當前系統根目錄。grub-install會在指定的目錄下建立boot/grub/的層級目錄,並將生成的「stage 2」置於DIR/boot/grub/下。由於內核與initramfs文件一般位於boot目錄下,故通常將DIR指定爲boot目錄的父目錄;固然,咱們也能夠指定爲其它目錄,但要手動編輯grub.conf文件從新指明內核文件的位置。

    例如  mount /dev/sdb1 /mnt/boot

         grub-install --root-directory=/mnt/ /dev/sdb

 

  方法2:輸入grub命令進入grub命令行

    root (hd0,0):至關於上面的--root-directory=DIR

    setup (hd0):爲哪一個磁盤安裝

    quit:退出


  情景1:grub損壞但系統還未關閉或重啓,此時可直接使用上面的兩種方法修復

[root@node2 ~]# dd if=/dev/zero of=/dev/sda bs=400 count=1   #將MBR中的引導加載程序沖刷掉,注意不能殃及分區表
1+0 records in
1+0 records out
400 bytes (400 B) copied, 0.000324129 s, 1.2 MB/s
[root@node2 ~]# rm -f /boot/grub/stage2   #將"stage 2"也刪除
[root@node2 ~]# grub-install /dev/sda      #使用grub-install的方式安裝grub
Installation finished. No error reported.
This is the contents of the device map /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'.

# this device map was generated by anaconda
(hd0)     /dev/sda
[root@node2 ~]# ll /boot/grub/stage2   #已從新生成"stage 2"
-rw-r--r-- 1 root root 126100 Oct 28 22:19 /boot/grub/stage2

[root@node2 ~]# dd if=/dev/zero of=/dev/sda bs=400 count=1
1+0 records in
1+0 records out
400 bytes (400 B) copied, 0.000368562 s, 1.1 MB/s
[root@node2 ~]# grub
Probing devices to guess BIOS drives. This may take a long time.


    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> root (hd0,0)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)   #使用方法二安裝grub
setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit


  情景2:grub損壞且系統已關閉,這時候可用以下兩種方式修復:

  方式一:將故障磁盤掛載於其它正常主機上重裝grub

   ①手動將node1的磁盤的grub損毀並關機

[root@node1 ~]# dd if=/dev/zero of=/dev/sda bs=400 count=1
1+0 records in
1+0 records out
400 bytes (400 B) copied, 0.000164892 s, 2.4 MB/s
[root@node1 ~]# shutdown -h now

   ②將node1的磁盤移除並添加到node2上(關於 如何在不重啓的狀況下識別新增硬盤 見末尾補充部分)

   ③掛載node1的分區並安裝grub,安裝完後卸載

[root@node2 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   30G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0   10G  0 part /
└─sda3   8:3    0    2G  0 part [SWAP]
sdb      8:16   0   30G  0 disk   #node1的硬盤在node2上被識別爲sdb
├─sdb1   8:17   0  200M  0 part 
├─sdb2   8:18   0   10G  0 part 
└─sdb3   8:19   0    2G  0 part 
sr0     11:0    1  4.2G  0 rom  
[root@node2 ~]# mkdir /mnt/boot
[root@node2 ~]# mount /dev/sdb1 /mnt/boot
[root@node2 ~]# grub-install --root-directory=/mnt /dev/sdb
/dev/sdb does not have any corresponding BIOS drive.
# 注意:若這裏提示找不到相應的BIOS驅動,可以使用選項--recheck從新檢查磁盤,以下:
[root@node2 ~]# grub-install --root-directory=/mnt --recheck /dev/sdb
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 /mnt/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/sda
(hd1)	/dev/sdb
[root@node2 ~]# umount /mnt/boot

   ④從node2上移除node1的硬盤後再裝回node1啓動便可


  方式二:插入系統光盤進入救援模式修復

   ①將Linux安裝光盤放入光驅。硬盤的MBR損壞後,BIOS會自動從光盤引導,進入菜單頁面後,選擇進入「救援模式」

wKiom1ZDZfyy5vJDAAFtEXTEpes354.png

   ②系統會檢測硬件,引導光盤上的Linux環境,依次提示選擇救援模式下使用的語言、鍵盤以及是否須要設置網絡,可按需選擇

   ③接下來系統會查找根分區,出現掛載提示,硬盤的根分區將被掛載到光盤Linux環境的/mnt/sysp_w_picpath目錄下;默認選項「continue」表示掛載權限爲讀寫,「Read-only」爲只讀,如。此處,由於要對系統進行修復,故須要有讀寫權限,選擇「continue」。

wKioL1ZDZnazrUsdAAAlR6OD-5Y673.png

   ④按提示執行「chroot /mnt/sysp_w_picpath」切換根環境並安裝grub,而後退出shell並重啓便可

     chroot:切換根環境,即以指定的目錄做爲根

     用法:chroot /path/to/directory [SHELL]

     例如 chroot /mnt/sysp_w_picpath /bin/tcsh   #將sysp_w_picpath做爲根目錄,並運行其中的tcsh

wKioL1ZDZteTcFkMAAAWGvrAjm8343.png

wKioL1ZDZtegVqcCAAAbyy8vN_w752.png


補充:如何實現不重啓linux而識別新增硬盤?

 新增硬盤後,使用以下命令識別:

    echo "scsi add-single-device w x y z" > /proc/scsi/scsi

    其中:

       w 是主機適配器標識,第一個適配器爲零(0)
       x 是主機適配器上的SCSI通道,第一個通道爲零(0)
       y 是設備的SCSI標識
       z 是LUN號,第一個LUN爲零(0)

 執行上述命令前,要先查看/proc/scsi/scsi,肯定新增硬盤的ID


 相反,在不重啓系統的狀況下將硬盤從系統中移除的命令爲:

    echo "scsi remove-single-device w x y z" > /proc/scsi/scsi

 使用該命令前需確保該硬盤已卸載

[root@node2 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   30G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0   10G  0 part /
└─sda3   8:3    0    2G  0 part [SWAP]
sr0     11:0    1  4.2G  0 rom
[root@node2 ~]# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: VMware,  Model: VMware Virtual S Rev: 1.0 
  Type:   Direct-Access                    ANSI  SCSI revision: 02
Host: scsi2 Channel: 00 Id: 00 Lun: 00
  Vendor: NECVMWar Model: VMware IDE CDR10 Rev: 1.00
  Type:   CD-ROM                           ANSI  SCSI revision: 05
#能夠看到,當前最大ID號爲00,所以新增硬盤的ID號應爲01
[root@node2 ~]# echo "scsi add-single-device 0 0 1 0" > /proc/scsi/scsi
[root@node2 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   30G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0   10G  0 part /
└─sda3   8:3    0    2G  0 part [SWAP]
sdb      8:16   0   30G  0 disk   #新增硬盤已被識別出來
├─sdb1   8:17   0  200M  0 part 
├─sdb2   8:18   0   10G  0 part 
└─sdb3   8:19   0    2G  0 part 
sr0     11:0    1  4.2G  0 rom
[root@node2 ~]# echo "scsi remove-single-device 0 0 1 0" > /proc/scsi/scsi   #移去該硬盤
[root@node2 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   30G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0   10G  0 part /
└─sda3   8:3    0    2G  0 part [SWAP]
sr0     11:0    1  4.2G  0 rom
相關文章
相關標籤/搜索