grub: GRand Unified Bootloader是一個多重操作系統啓動管理器,用來引導不同的操作系統。
grub legacy
stage1 : mbr
stage1_5: mbr之後的扇區,讓stage1中的bootloader能識別stage2所在的分區上的文件系統;提供grub文件系統驅動和基本磁盤分區
stage2:磁盤分區(/boot/grub/)
配置文件:/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: 設定本次啓動時用到的內核文件; 額外還可以添加許多內核支持使用的cmdline參數; 例如:init=/path/to/init, selinux=0 initrd /PATH/TO/INITRAMFS_FILE: 設定爲選定的內核提供額外的ramdisk;必須與內核版本號完全匹配,否則無法被內核裝載; boot:引導啓動選定的內核;
手動在grub命令行接口啓動系統:
配置文件:/boot/grub/grub.conf
配置項:
default=#:設定默認啓動的菜單項;落單項(title)編號從0開始
timeout=#:指定菜單項等待選項選擇的時長;
splashp_w_picpath=(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:啓動選定的內核或操作系統時進行認證;
grub-md5-crypt命令 :生成對應要保護的認證密碼
進入單用戶模式:
(1) 編輯grub菜單(選定要編輯的title,而後使用e命令);
(2) 在選定的kernel後附加
1,s,S或single都可以;
(3) 在kernel所在行,鍵入“b”命令;
安裝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@shakura ~]# mkdir /mnt/boot [root@shakura ~]# mount /dev/sdb1 /mnt/boot/ [root@shakura ~]# grub-install --root-directory=/mnt /dev/sdb
●複製當前系統的內核和initrd文件至/mnt/boot/目錄下,編輯grub.conf文件
[root@shakura ~]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz [root@shakura ~]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img [root@shakura ~]# 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@shakura ~]# mkdir /mnt/sysroot [root@shakura sysroot]# mount /dev/sdb3 /mnt/sysroot/ [root@shakura ~]# cd /mnt/sysroot [root@shakura ~]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media
●複製當前系統下的/bin/bash至/mnt/sysroot/目錄下,並複製bash所用的庫文件
[root@shakura sysroot]# cp /bin/bash /mnt/sysroot/bin/ [root@shakura 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@shakura sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/ [root@shakura sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/ [root@shakura sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/ [root@shakura sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/ [root@shakura sysroot]# chroot /mnt/sysroot/ # 切換至根目錄,查看bash是否可用 bash-4.1# exit
●接下來新建一個虛擬機使用現有的這塊sdb硬盤,啓動此虛擬機,可以發現自己DIY的linux;
(2) grub
grub> root(hd#,#)
現在我們手動破壞當前磁盤上的mbr,再安裝grub。在破壞之前先備份一下,注意:別破壞分區表,只破壞前446個字節。
[root@shakura ~]# 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@shakura ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 記錄了1+0 的讀入 記錄了1+0 的寫出 200字節(200 B)已複製,0.000220939 秒,905 kB/秒 [root@shakura ~]# 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@shakura ~]# 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(破壞後重啓,使用緊急救援模式)
設置爲光盤啓動
選擇語言,這裏我直接選擇默認了
選擇鍵盤模式: