一、grub基本功能
GNU GRUB 和GRUB是GRand Unified Bootloader的縮寫,它是一個多重操作系統啓動管理器。用來引導不同系統,如windows,linux。
在X86架構的機器中,Linux、BSD 或其它Unix類的操作系統中GRUB、LILO 是大家最爲常用,應該說是主流。
Windows也有類似的工具NTLOADER;比如我們在機器中安裝了Windows 98後,我們再安裝一個Windows XP ,在機器啓動的會有一個菜單讓我們選擇進入是進入Windows 98 還是進入Windows XP。NTLOADER就是一個多系統啓動引導管理器,NTLOADER 同樣也能引導Linux,只是極爲麻煩罷了。
在PowerPC 架構的機器中,如果安裝了Linux的Powerpc 版本,大多是用yaboot 多重引導管理器,比如Apple機用的是IBMPowerPC處理器,所以在如果想在Apple機上,安裝Macos 和Linux Powerpc 版本,大多是用yaboot來引導多個操作系統。
因爲X86架構的機器仍是主流, 所以目前GRUB和LILO 仍然是我們最常用的多重操作系統引導管理器
二、grub階段
1、grub legacy
stage1 : mbr
stage1_5: mbr之後的扇區,讓stage1中的bootloader能識別stage2所在的分區上的文件系統;提供grub文件系統驅動和基本磁盤分區
stage2:磁盤分區(/boot/grub/)
2、配置文件:/boot/grub/grub.conf <-- /etc/grub.conf (符號鏈接文件)
stage2及內核等通常放置於一個基本磁盤分區:
功用:
(1) 提供菜單、並提供交互式接口
e:編輯模式,用於編輯菜單;
c:命令模式,用於交互式接口;
(2) 加載用戶選擇的內核或操作系統
允許傳遞參數給內核
可隱藏此菜單
(3) 爲菜單提供了保護機制
如何識別設備:
(hd#,#) 第幾個磁盤的第幾個分區;
hd#:磁盤編號,用數字表示;從0開始編號
# :分區標號,用數字表示;從0開始編號
(hd0,0)
三、 grub的命令行接口:
help :獲取幫助列表
help KEYWORD:詳細幫助信息
find(hd#,#)/PATH/TO/SOMEFILE
root (hd#,#)
kernel /PATH/TO/KERNEL_FILE:設定本次啓動時用到的內核文件;額外還可以添加許多內核支持使用的cmdine參數
例如:init=/path/to/init,selinux=0
initrd /PATH/TO/INITRAMFS_FILE:設定爲選定的內核提供額外文件的ramdisk
boot:引導啓動選定的內核
3.1手動在grub命令行接口啓動系統
3.2 配置文件:/boot/grub/grub.conf
配置項:
default=#:設定默認啓動的菜單項;落單項(title)編號從0開始
timeout=#:指定菜單項等待選項選擇的時長;
splashimage=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜單背景圖片文件路徑;gimp 640x480
hiddenmenu:隱藏菜單;
password [--md5] STRING:菜單編輯認證;
title TITLE:定義菜單項“標題”,可出現多次
root(hd#,#):grub查找stage2及kernel文件所在設備分區;爲grub的“根”;
kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:啓動的內核
initd /PATH/TO/INITRAMFS_FILE:內核匹配的ramfs文件;
password [--md5] STRING:啓動選定的內核或操作系統時進行認證;
3.3 grub-md5-crypt命令生成對應要保護的認證密碼
3.4 進入單用戶模式:
(1) 編輯grub菜單(選定要編輯的title,而後使用e命令);
(2) 在選定的kernel後附加
1,s,S或single都可以;
(3) 在kernel所在行,鍵入“b”命令;
3.5 安裝grub:
(1) grub-install
grub-install - - root-directory=ROOT /dev/DISK
示例:
當前虛擬機上重新添加一塊硬盤
將該硬盤進行分區,sbd1爲boot分區(200MB),sdb2爲交換分區(2GB),sdb3爲根分區(5GB),分區完成後使用cat命令查看配置文件如下;
在/mnt下創建boot目錄,並把sdb1掛載至/mnt/boot目錄下,安裝grub程序:
[root@localhost ~]# mkdir /mnt/boot [root@localhost ~]# mount /dev/sdb1 /mnt/boot/ [root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb
複製當前系統的內核和initrd文件至/mnt/boot/目錄下,編輯grub.conf文件
[root@localhost ~]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz [root@localhost ~]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img [root@localhost ~]# vim /mnt/boot/grub/grub.conf default=0 timeout=5 title CentOS DIY root (hd0,0) kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash initrd /initramfs.img
在mnt目錄下創建sysroot目錄,並創建系統常用文件夾
[root@localhost ~]# mkdir /mnt/sysroot [root@localhost sysroot]# mount /dev/sdb3 /mnt/sysroot/ [root@localhost ~]# cd /mnt/sysroot [root@localhost ~]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media
複製當前系統下的/bin/bash至/mnt/sysroot/目錄下,並複製bash所用的庫文件
[root@localhost sysroot]# cp /bin/bash /mnt/sysroot/bin/ [root@localhost sysroot]# ldd /bin/bash # 查看bash所需要的庫文件 linux-vdso.so.1 => (0x00007ffc97596000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x000000355b600000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003553200000) libc.so.6 => /lib64/libc.so.6 (0x0000003553600000) /lib64/ld-linux-x86-64.so.2 (0x0000003552e00000) [root@localhost sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/ [root@localhost sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/ [root@localhost sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/ [root@localhost sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/ [root@localhost sysroot]# chroot /mnt/sysroot/ # 切換至根目錄,查看bash是否可用 bash-4.1# exit
接下來新建一個虛擬機使用現有的這塊sdb硬盤,啓動此虛擬機,可以發現自己DIY的linux;
(2) grub
grub> root(hd#,#)
現在我們手動破壞當前磁盤上的mbr,再安裝grub。在破壞之前先備份一下,注意:別破壞分區表,只破壞前446個字節。
[root@localhost ~]# dd if=/dev/sda of=/root/mbr.bak count=1 bs=512 #備份mbr信息 記錄了1+0 的讀入 記錄了1+0 的寫出 512字節(512 B)已複製,0.000241106 秒,2.1 MB/秒 [root@localhost ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 記錄了1+0 的讀入 記錄了1+0 的寫出 200字節(200 B)已複製,0.000220939 秒,905 kB/秒 [root@localhost ~]# grub-install --root-directory=/ /dev/sda #手動安裝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
至此,修復成功。
grub> setup (hd#)
[root@localhost ~]# 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) 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.
esc /boot: linux rescue(破壞後重啓,使用緊急救援模式)
設置爲光盤啓動
進入修復模式
選擇語言,這裏我直接選擇默認
選擇鍵盤模式:
最後,重啓系統,卸載光盤,系統修復成功。