歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux操作系統啟動管理器-GRUB

Linux操作系統啟動管理器-GRUB

日期:2017/2/28 14:01:44   编辑:Linux教程

【GRUB是什麼】
GNU GRUB(簡稱“GRUB”)是一個來自GNU項目的啟動引導程序。GRUB是多啟動規范的實現,它允許用戶可以在計算機內同時擁有多個操作系統,並在計算機啟動時選擇希望運行的操作系統。GRUB可用於選擇操作系統分區上的不同內核,也可用於向這些內核傳遞啟動參數。GRUB是在計算機啟動後運行的第一個程序,他是用來負責加載、傳輸控制到操作系統的內核,一旦把內核掛載,系統引導管理器的任務就算完成退出,系統引導的其它部份,比如系統的初始化及啟動過程則完全由內核來控制完成。

【GRUB的功能】
1、提供菜單,並提供交互式接口;啟動界面敲e鍵: 進入編輯模式;
2、選擇要啟動的內核或系統(允許傳遞引導參數給內核、選擇界面可隱藏);
3、為編輯功能提供保護機制。
啟用內核文件保護:選擇運行指定的內核得先輸入密碼
傳遞參數保護:使用e命令得先輸入密碼
【GRUB的組成】
[root@CentOS6 grub]# ll
total 275
-rw-r--r--. 1 root root 63 Mar 16 14:12 device.map #grub的對應設備文件;
-rw-r--r--. 1 root root 13392 Mar 16 14:12 e2fs_stage1_5 #e2fs文件系統定義文件;
-rw-r--r--. 1 root root 12632 Mar 16 14:12 fat_stage1_5
-rw-r--r--. 1 root root 11760 Mar 16 14:12 ffs_stage1_5
-rw-------. 1 root root 1137 May 6 20:15 grub.conf #grub的配置文件;
-rw-r--r--. 1 root root 11768 Mar 16 14:12 iso9660_stage1_5
-rw-r--r--. 1 root root 13280 Mar 16 14:12 jfs_stage1_5
lrwxrwxrwx. 1 root root 11 Mar 16 14:12 menu.lst -> ./grub.conf
-rw-r--r--. 1 root root 11968 Mar 16 14:12 minix_stage1_5
-rw-r--r--. 1 root root 14424 Mar 16 14:12 reiserfs_stage1_5
-rw-r--r--. 1 root root 1341 Nov 14 2010 splash.xpm.gz #開機時grub界面的背景圖片;
-rw-r--r--. 1 root root 512 Mar 16 14:12 stage1
-rw-r--r--. 1 root root 126108 Mar 16 14:12 stage2
-rw-r--r--. 1 root root 12036 Mar 16 14:12 ufs2_stage1_5
-rw-r--r--. 1 root root 11376 Mar 16 14:12 vstafs_stage1_5
-rw-r--r--. 1 root root 13976 Mar 16 14:12 xfs_stage1_5

stage1: 這是一個用來啟動GRUB的映像文件(必須要有)。通常,這個文件是被裝載到MBR或者啟動扇區所在的分區.由於PC的啟動扇區的大小為512字節,所以這個映像文件編譯以後也必須為512字節。Stage1的全部的工作是從本地磁盤把Stage 2或者Stage 1.5裝載進來。由於對stage1大小的限制,它通過分程序表的形式來編碼Stage 2或者Stage 1.5的位置,所以在stage1是不能識別任何文件系統
stage1_5: MBR隨後的扇區,其作為stage1與stage2之間的橋梁,也就是說,stage1載入stage1.5,然後stage1.5載入stage2。 stage1與stage1.5之間的區別是,前者是不識別任何文件系統的但後者識別文件系統(例如 'e2fs_stage1_5'識別ext2fs)。所以你可以安全的移動stage2位置,即使是GRUB安裝完以後。
stage2: 用於讀取grub.conf配置文件,並實現引導功能的擴展。

【GRUB配置文件grub.conf】
[root@CentOS6 grub]# cat 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/mapper/vg_centos6-lv_root
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=1 #選擇第幾個title配置的內核或系統,各title從0開始編號;
timeout=5 #菜單顯示的超時時長;
splashimage=(hd0,0)/grub/splash.xpm.gz #指定菜單的背景圖片;此圖片只能為14bits色,xpm格式,
gzip壓縮;
hiddenmenu #隱藏菜單,在timout之前按下任意鍵才會顯示菜單,反之則不顯示菜單;
title CentOS (3.19.6) #顯示於菜單中的標題;
root (hd0,0) #指定boot分區所在磁盤及分區
kernel /vmlinuz-3.19.6 ro root=/dev/mapper/vg_centos6-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_centos6/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos6/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet #指定內核的絕對路徑、以只讀方式掛載根文件系統、root分區所在位置、等相關參數;
initrd /initramfs-3.19.6.img #指定initramfs文件的絕對路徑;

注意:
1、在grub中所有硬盤都識別為hd,不同的硬盤基於數字標識:如hd0(表示第一塊硬盤), hd1(第二塊硬盤),同一個硬盤上的不同分區,也使用數字標識,如hd0,0(第一塊硬盤上的第一個分區);
2、因為grub不能識別物理卷(PV),當然root不能放在邏輯卷(lv)上,所以root一般單獨分區且一定為基本磁盤分區。

【grub命令行接口】
啟動方式: 啟動界面->敲c進入
root(DEVICE):指定哪個分區為接下來要啟動的系統或內核文件所在的分區
例:root (hd0,0)
kernel: 指定要運行的內核文件
initrd: 為要運行的內核指定其可用的ramdisk文件
boot: 啟動此前配置好的內核或系統
find (DEVICE)/path/to/file 查找指定文件是否在分區上,支持命令補全
例:find (hd0,0)/vmlinuz-2.6.32-431.el6.i686
注:grub命令行接口支持命令行補全,在此模式下要啟動系統必須預先知道根所在的設備。

【grub保護機制配置】
途徑:通過修改grub的配置文件,一般為/boot/grub/grub.conf

1、生成密碼:
[root@CentOS6 ~]# grub-md5-crypt #通過md5算法來生成密碼;
Password: #鍵入預要設置的密碼;
Retype password: #確認密碼;
$1$J99TE$c7VWrcDAfB1GrVqI5.E0L. #用md5算法生成的密碼;

2、保護編輯功能,則需要在title之外添加:password --md5 密碼串;
# ----略---
#boot=/dev/sda
default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$J99TE$c7VWrcDAfB1GrVqI5.E0L. #設置編輯功能保護
title CentOS (3.19.6)
root (hd0,0)
#----略---

設置後的效果如上圖所示:提示我們想要進入編輯界面,必須先按'p'鍵再鍵入正確的密碼。

3、保護使用某內核,則需要內核對應的title之下添加:password --md5 密碼串
# ----略---
#boot=/dev/sda
default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (3.19.6)
password --md5 $1$J99TE$c7VWrcDAfB1GrVqI5.E0L. #設置保護CentOS (3.19.6)這內核;
root (hd0,0)
#----略---

【安裝grub的方式】:

1、使用grub-install命令
# grub-install [--root-directory=DIR] DEVICE
--root-directory=指定的路徑必須是內核及initrd文件所在的分區的掛載點的父目錄,且此掛載點必須叫boot
例如:在/dev/sdb上的/dev/sdb1分區含有內核及initrd文件,現需要在sdb上安裝grub。
[root@CentOS6 ~]#mount /dev/sdb1 /mnt/boot
[root@CentOS6 ~]#grub-install --root-directory=/mnt /dev/sdb

如何在Ubuntu12.04/12.10中重裝或修復Grub2引導 http://www.linuxidc.com/Linux/2012-11/74901.htm

Linux啟動引導過程 grub和mbr http://www.linuxidc.com/Linux/2013-07/87923.htm

grub 的安裝與使用 http://www.linuxidc.com/Linux/2013-07/87682.htm

grub引導程序配置文件分析 http://www.linuxidc.com/Linux/2013-07/87547.htm

CentOS 6.4 grub加密碼 http://www.linuxidc.com/Linux/2013-07/87124.htm

CentOS GRUB引導錯誤無法進入系統解決辦法 http://www.linuxidc.com/Linux/2014-11/108835.htm

Copyright © Linux教程網 All Rights Reserved