Linux GRUB legacy


Linux GRUB

GRUB簡介

瞭解grub之前,需要理解linux的啓動流程,如果之前有對linux啓動流程不瞭解的可以看:http://xinzong.blog.51cto.com/10018904/1757337

GRUB(GRand Unified Bootloader)統一引導加載器,即啓動加載器。centos上的grub分爲兩個版本,分別是: 
centos 7默認採用grub2

GRUB legacy(GRUB 0.1一般稱爲grub1) 
GRUB 2(grub1.0一般稱爲grub2) 
本文主要以grub legacy展開介紹

GRUB 菜單組成

GRUB菜單

介紹菜單之前,先帶大家瞭解一下grub legacy 
grub legacy分爲三個階段:

stage 1:mbr 
stage1_5:mbr之後的扇區,作用是讓stage1中的bootloader能識別stage2所在分區上的文件系統 
stage2:磁盤分區()/boot/grub/)

stage2及內核等通常放置於一個基本磁盤分區。 
stage2的功用:

1、提供菜單、並提供交互式接口 
e:編輯模式,用於編輯菜單 
c:命令模式,交互式接口 
2、加載用戶選擇的內核或操作系統 
允許傳遞參數給內核 
可隱藏此菜單 
3、爲菜單提供了保護機制 
爲編輯菜單進行認證 
未啓用內核或操作系統進行認證

GRUB命令行接口

wKioL1cDoInCsDbBAAA2pumlWso381.png



wKiom1cDn-_iJ5InAABKwuTw-sg827.png

grub的命令行接口:

help: 獲取幫助列表 
help KEYWORD: 詳細幫助信息 
find (hd#,#)/PATH/TO/SOMEFILE:查找文件 
root (hd#,#) 
kernel /PATH/TO/KERNEL_FILE:設定本次啓動時用到的內核文件;額外還可以添加許多內核支持使用的cmdline參數; 
例如:init=/path/to/init, selinux=0 
initrd /PATH/TO/INITRAMFS_FILE: 設定爲選定內核提供額外文件的ramdisk; 
boot: 引導啓動選定的內核;


手動在grub命令行接口啓動系統:
grub> root (hd#,#)
grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE  
grub> initrd /initramfs-VERSION-RELEASE.img
grub> boot

wKioL1cDoOfw9jfhAAZb1R6BgMM023.gif

GRUB配置文件

配置文件:/boot/grub/grub.conf 
配置項:

default=#: 設定默認啓動的菜單項;菜單項(title)編號從0開始; 
timeout=#:指定菜單項等待選項選擇的時長; 
splashimage=指明菜單背景圖片路徑(hd#,#)/PATH/TO/XPM_PIC_FILE) 
hiddenmenu:隱藏菜單; 
password [–md5] STRING: 菜單編輯認證; 
title TITLE:定義菜單項“標題”, 可出現多次;

root (hd#,#):grub查找stage2及kernel文件所在設備分區;爲grub的“根”; 
kernel:啓動的內核 
initrd 內核匹配的ramfs文件; 
password [–md5] STRING: 啓動選定的內核或操作系統時進行認證; 
加密密碼可以使用: 
grub-md5-crypt命令

GRUB安裝及修復

  • 安裝修復:grub-install

實例

注意:–root-directory必須指定boot所在的上一級目錄

#mount /dev/sdb1 /mnt/boot
#grub-install --root-directory=/mnt /dev/sdb2
Probing devices to guess BIOS drives. This may take a long time.
/dev/mapper/vg_centos6-lv_root does not have any corresponding BIOS drive.
# ls /mnt/boot/
grub
# ls /mnt/boot/grub/
device.map     ffs_stage1_5      minix_stage1_5     stage2           xfs_stage1_5
e2fs_stage1_5  iso9660_stage1_5  reiserfs_stage1_5  ufs2_stage1_5
fat_stage1_5   jfs_stage1_5      stage1             vstafs_stage1_5
  • 安裝修復:grub命令

# 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 (hd1,0)
root (hd1,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd1)
setup (hd1)
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 (hd1)"...  27 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd1) (hd1)1+27 p (hd1,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.

GRUB單用戶及救援模式


單用戶模式(密碼破解)


進入單用戶模式:
(1) 編輯grub菜單(選定要編輯的title,而後使用e命令);  
(2) 在選定的kernel後附加
1, s, S或single都可以;
(3) 在kernel所在行,鍵入“b”命令;

wKioL1cDoSiT73xYAANZ-SKbn-Q406.gif

救援模式


grub修復
1、模擬破壞:(破壞前先備份)

dd if=/dev/sda of=/root/mbr.bak count=1 bs=512
dd if=/dev/zero of=/dev/sda count=1 bs=200

2、進入救援模式

wKiom1cDoLqQgn33AAZ7L6xCErQ037.png1、插入光盤進入救援模式
-方式1:點擊Rescue installed system
方式2:按esc進入命令行
輸入 linux rescue
2、選擇語言,鍵盤類型
3、選擇是否使用語言
4、是否將當前系統的跟掛載至/mnt/sysimage下
Continue:繼續
5、選擇OK # 可以使用chroot /mnt/sysimage
6、選擇shell start shell
7、切換跟,修復grub

# chroot /mnt/sysimage
# grub-install --root-directory=/ /dev/sda
# exit
# reboot

8、重啓前拆除光盤
圖解:

wKiom1cDoOSz-gL_AAUFNyL1MZM269.gif


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章