Linux之作業系統啟動管理器-GRUB

來源:互聯網
上載者:User

標籤:linux   geub   啟動管理器   

【GRUB是什麼】

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

【GRUB的功能】

    1、提供菜單,並提供互動式介面;啟動介面敲e鍵: 進入編輯模式;

    2、選擇要啟動的核心或系統(允許傳遞引導參數給核心、選擇介面可隱藏);

    3、為編輯功能提供保護機制。

   啟用核心檔案保護:選擇運行指定的核心得先輸入密碼

   傳遞參數保護:使用e命令得先輸入密碼

【GRUB的組成】

[[email protected] 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_5lrwxrwxrwx. 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】

[[email protected] 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/sdadefault=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命令列介面支援命令列補全,在此模式下要啟動系統必須預Crowdsourced Security Testing道根所在的裝置。

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/85/wKioL1VLLd-hDbKjAAF2HWjaAeI712.jpg" title="grub.png" alt="wKioL1VLLd-hDbKjAAF2HWjaAeI712.jpg" />

【grub保護機制配置】

  途徑:通過修改grub的設定檔,一般為/boot/grub/grub.conf

   1、產生密碼:  

[[email protected] ~]# grub-md5-crypt #通過md5演算法來產生密碼;Password:   #鍵入預要設定的密碼;Retype password: #確認密碼;$1$J99TE$c7VWrcDAfB1GrVqI5.E0L. #用md5演算法產生的密碼;

   2、保護編輯功能,則需要在title之外添加:password --md5 密碼串;

# ----略---#boot=/dev/sdadefault=1timeout=5splashimage=(hd0,0)/grub/splash.xpm.gzhiddenmenupassword --md5 $1$J99TE$c7VWrcDAfB1GrVqI5.E0L. #設定編輯功能保護title CentOS (3.19.6)        root (hd0,0)#----略---

     650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6C/85/wKioL1VLMi_Bi5GkAAEe6t_aRfQ004.jpg" title="11.png" alt="wKioL1VLMi_Bi5GkAAEe6t_aRfQ004.jpg" />

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

   3、保護使用某核心,則需要核心對應的title之下添加:password --md5 密碼串

# ----略---#boot=/dev/sdadefault=1timeout=5splashimage=(hd0,0)/grub/splash.xpm.gzhiddenmenutitle 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。

[[email protected] ~]#mount /dev/sdb1 /mnt/boot[[email protected] ~]#grub-install --root-directory=/mnt   /dev/sdb


本文出自 “飛奔的蝸牛” 部落格,請務必保留此出處http://ljmsky.blog.51cto.com/2878/1643981

Linux之作業系統啟動管理器-GRUB

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.