1. 程式人生 > >謝煙客---------Linux之grub應用

謝煙客---------Linux之grub應用

linux

Grub基礎、工作流程、grub配置文件、grub功能、grub內置命令行接口

grub安裝、grub修復

雙系統安裝(先Windows後Linux,以免windows NTloader會覆蓋Linux loader)


GRUB Grand Uniform Bootloader

CentOS5,6 grub1 0系,設備或分區編號從0開始

CentOS7 grub2 1系,設備或分區編號從1開始


CentOS5,6 grub應用:


一、grub的工作流程

stage1 mbr中

stage1.5 mbr之後的扇區,存儲stage2所在分區的驅動

stage2 提供菜單,加載內核和ramdisk至內存,移交控制權


二、grub配置文件

通常boot(stage2)在一個普通的分區上:

boot為一個獨立的分區, /可以為普通分區或lvm訪問boot通過掛載在根中實現

內核: kernel /vmlinuz, initrd /initramfs


boot和根在同一個分區,/必須為普通分區

內核: kernel /boot/vmlinuz, initrd /boot/initramfs

配置路徑

/etc/grub.conf -> ../boot/grub/grub.conf

/boot/grub/menu.lst -> ./grub.conf


配置格式

default=0            ## 默認啟動菜單 
timeout=5            ## 等待用戶選擇菜單的超時時長
splashimage=(hd0,0)/meiling.xpm.gz     ## 菜單的背景圖片 640X480 14位索引色
hiddenmenu                             ## 是否隱藏菜單
password --md5 STRING                  ## 菜單加密功能,STRING由grub-md5-crypt 或 openssl passwd -1 -salt STRING 生成
title CentOS 6 (2.6.32-696.el6.i686)   ## 定義菜單項的標題
root (hd0,0)                           ## 引導設備位置 左0: 第一個磁盤設備,右0:第一個分區
    kernel /vmlinuz-2.6.32-696.el6.i686 ro root=/dev/mapper/myvg-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_LVM_LV=myvg/lv_swap  KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet rd_NO_DM rd_LVM_LV=myvg/lv_root rhgb quiet
    initrd /initramfs-2.6.32-696.el6.i686.img
    password --md5 STRING

註意: root,kernel,initd格式的縮進


三、grub的功能

提供菜單 : tile TITLE

加載用戶選擇的kernel及ramdisk : kernel path parameters , initrd path

為菜單提供保護機制(kernel和菜單編輯認證) : password --md5 STRING


四、grub的內置命令行接口

root (hd#,#) 指定引導設備在哪個設備的哪個分區上。#從0開始,表示1.


kernel /vmlinuz PARAMETERS

當stage1.5找到stage2時,內核只識別stage2所在的分區,此分區為/,所以分區的vmlinuz路徑為,/vmlinuz

PARAMETERS:

ro 以只讀方式掛載root=PATH指明的路徑的根

root=<路徑> 指明root的位置

quiet 靜默模式

selinux=0 不啟動selinux

init=/path/to/somefile 開機後,init程序為哪個。默認為/sbin/init

1,s,S,single 單用戶模式

<更多參數: google: kernel command line parameters>

initrd /initramfs 指定initramfs的路徑


五、grub的安裝

安裝grub: grub-install命令 重新安裝grub stage1,1.5,2


grub-install命令

grub-install --root-directory=DIR DEIVCE

--root-directory=DIR 指向boot目錄的上一級目錄

DEVICE 給哪個設備安裝grub


1)關機

技術分享


2)添加新的磁盤

技術分享

技術分享

技術分享

技術分享

技術分享


3)啟動虛擬機

技術分享


4)進入命令行

技術分享


5)查看磁盤

技術分享


6)分區

技術分享

技術分享

技術分享

技術分享

技術分享


7)格式化

技術分享

技術分享

技術分享

技術分享


8)掛載

技術分享

技術分享


9)安裝grub

技術分享

技術分享


10)復制bash程序及其相關的庫

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

11) 給出配置文件

技術分享

技術分享


12)同步

技術分享


13)關機

# shutdown -h now


13)查看第二個磁盤的名字

技術分享

技術分享

13)、新建虛擬機

技術分享

技術分享

技術分享


14)啟動新建的虛擬機

技術分享


15)啟動OK

技術分享



六、grub的修復

1、在命令行中: grub 只是stage1(MBR 512)被破壞時,才能修復.

2、開機失效: rescue模式


1、在命令行中: grub

1)備份mbr

[[email protected] ~]# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1


2)破壞mbr的stage1

[[email protected] ~]# dd of=/dev/sda if=/dev/zero bs=446 count=1


3)用grub命令行修復

1)、進入grub命令行

[[email protected] ~]# 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>


2)、設置引導設備位置

grub> root (hd0,0)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83


3)、給設備安裝grub

grub> setup (hd0)
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.


4)重啟驗證

grub> quit  
quit
[[email protected] ~]# reboot
[[email protected] ~]#

技術分享


2、開機失效: rescue模式

1)備份mbr

[[email protected] ~]# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1


2)破壞mbr的stage1

[[email protected] ~]# dd of=/dev/sda if=/dev/zero bs=446 count=1


3)重啟

技術分享


4)rescue模式救援

1)、掛載光盤 關機 --> 編輯虛擬主機 --> CD/DVD(IDE) --> 使用ISO鏡像文件 --> 瀏覽選擇DVD光盤


2)、開機進入界面 開啟虛擬機

技術分享

保證光盤連接


3)、選擇黃色標記的行ESC,輸入 linux rescue 後回車

技術分享

技術分享


4)、選擇語言

技術分享

OK


5)、鍵盤類型

技術分享

OK

技術分享


6)、是否需要啟動網絡

技術分享

NO


7)、臨時將/掛載在/mnt/sysimage中

技術分享

Continue

技術分享


8)、告知已經找到磁盤,需要使用root,運行chroot /mnt/sysimage即可

技術分享

OK


9)、

技術分享


10)、選擇第一個

技術分享


11)、打印一個shell提示符

技術分享


12)、切換至原root根目錄

技術分享


13)、修復

技術分享

14)、重啟測試

技術分享


技術分享




雙系統安裝

1、新建一個虛擬機

2、添加U盤安裝windows

3、安裝Linux



本文出自 “Reading” 博客,請務必保留此出處http://sonlich.blog.51cto.com/12825953/1960959

謝煙客---------Linux之grub應用