Grub應(yīng)用

GRUB(Boot Loader):

grub: GRand Unified Bootloader
grub 0.x: grub legacy
grub 1.x: grub2

grub legacy:

stage1: mbr
stage1_5: mbr之后的扇區(qū),讓stage1中的bootloader能識別stage2所在的分區(qū)上的文件系統(tǒng);
stage2:磁盤分區(qū)(/boot/grub/)
stage2及內(nèi)核等通常放置于一個(gè)基本磁盤分區(qū);

配置文件:/boot/grub/grub.conf <-- /etc/grub.conf

功用:

(1) 提供菜單、并提供交互式接口
e: 編輯模式,用于編輯菜單;
c: 命令模式,交互式接口;
(2) 加載用戶選擇的內(nèi)核或操作系統(tǒng)
允許傳遞參數(shù)給內(nèi)核
可隱藏此菜單
(3) 為菜單提供了保護(hù)機(jī)制
為編輯菜單進(jìn)行認(rèn)證
為啟用內(nèi)核或操作系統(tǒng)進(jìn)行認(rèn)證

grub如何識別設(shè)備:

(hd#,#)
hd#: 磁盤編號,用數(shù)字表示;從0開始編號
#: 分區(qū)編號,用數(shù)字表示; 從0開始編號

grub的命令行接口

help: 獲取幫助列表
help KEYWORD: 詳細(xì)幫助信息

find (hd#,#)/PATH/TO/SOMEFILE

root (hd#,#)

kernel /PATH/TO/KERNEL_FILE: 設(shè)定本次啟動(dòng)時(shí)用到的內(nèi)核文件;額外還可以添加許多內(nèi)核支持使用的cmdline參數(shù)

例如:init=/path/to/init, selinux=0

initrd /PATH/TO/INITRAMFS_FILE: 設(shè)定為選定的內(nèi)核提供額外文件的ramdisk

boot: 引導(dǎo)啟動(dòng)選定的內(nèi)核

手動(dòng)在grub命令行接口啟動(dòng)系統(tǒng):
grub> root (hd#,#)
grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE
grub> initrd /initramfs-VERSION-RELEASE.img
grub> boot
配置文件:/boot/grub/grub.conf
[root@promote ~]# cat /etc/grub/grub.conf
cat: /etc/grub/grub.conf: No such file or directory
[root@promote ~]# cat /boot/grub/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/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-573.el6.x86_64)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-573.el6.x86_64.img

配置項(xiàng):

default=#: 設(shè)定默認(rèn)啟動(dòng)的菜單項(xiàng);落單項(xiàng)(title)編號從0開始;
timeout=#:指定菜單項(xiàng)等待選項(xiàng)選擇的時(shí)長;
splashimage=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜單背景圖片文件路徑;
hiddenmenu:隱藏菜單;
password [--md5] STRING: 菜單編輯認(rèn)證;
title TITLE:定義菜單項(xiàng)“標(biāo)題”, 可出現(xiàn)多次;
root (hd#,#):grub查找stage2及kernel文件所在設(shè)備分區(qū);為grub的“根”;
kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:啟動(dòng)的內(nèi)核
initrd /PATH/TO/INITRAMFS_FILE: 內(nèi)核匹配的ramfs文件;
password [--md5] STRING: 啟動(dòng)選定的內(nèi)核或操作系統(tǒng)時(shí)進(jìn)行認(rèn)證;

grub-md5-crypt命令

[root@promote ~]# grub-md5-crypt 
Password: 
Retype password: 
$1$H0XYM0$3ASdzIM3vfC3gh/ywCDkz1
進(jìn)入單用戶模式:

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

安裝grub:

(1) grub-install
grub-install --root-directory=ROOT /dev/DISK

(2) grub命令
grub> root (hd#,#)
grub> setup (hd#)

ldd命令:

print shared library dependencies 打印應(yīng)用程序依賴的庫文件
ldd [OPTION]... FILE...

[root@promote ~]# ldd /bin/ls
    linux-vdso.so.1 =>  (0x00007ffdecb65000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003456600000)
    librt.so.1 => /lib64/librt.so.1 (0x0000003455600000)
    libcap.so.2 => /lib64/libcap.so.2 (0x0000003461200000)
    libacl.so.1 => /lib64/libacl.so.1 (0x000000345fe00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003454e00000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003454a00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003454600000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003455200000)
    libattr.so.1 => /lib64/libattr.so.1 (0x0000003464a00000)

應(yīng)用

1.在虛擬機(jī)中添加一塊20G硬盤,裝入grub

添加一塊20G的硬盤

2.可以看出新硬盤sdb沒有分區(qū),先對其進(jìn)行分區(qū),一個(gè)操作系統(tǒng)正常來說應(yīng)該有三個(gè)分區(qū),即boot,swap和根

[root@promote ~]# fdisk -l /dev/sd[a-z]

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00090dc2

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       26109   209202176   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@promote ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x966f2c82.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +100M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-2610, default 15): 
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-2610, default 2610): +2G

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (277-2610, default 277): 
Using default value 277
Last cylinder, +cylinders or +size{K,M,G} (277-2610, default 2610): +5G

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x966f2c82

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         276     2104515   82  Linux swap / Solaris
/dev/sdb3             277         930     5253255   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@promote ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@promote ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  209715200 sda
   8        1     512000 sda1
   8        2  209202176 sda2
   8       16   20971520 sdb
   8       17     112423 sdb1
   8       18    2104515 sdb2
   8       19    5253255 sdb3
 253        0   52428800 dm-0
 253        1    2031616 dm-1
 253        2  154738688 dm-2
[root@promote ~]# mke2fs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
28112 inodes, 112420 blocks
5621 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@promote ~]# mke2fs -t ext4 /dev/sdb3
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313313 blocks
65665 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@promote ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
no label, UUID=c5312464-50ea-45d6-8ecc-493c346c1e49

3.在/mnt下創(chuàng)建一個(gè)boot目錄,把新硬盤的第一個(gè)分區(qū),掛載到剛創(chuàng)建的目錄

[root@promote ~]# mkdir /mnt/boot
[root@promote ~]# mount /dev/sdb1 /mnt/boot/
[root@promote ~]# ls /mnt/boot
lost+found

4.安裝grub并驗(yàn)證

[root@promote ~]# grub-install --root-directory=/mnt /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/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'.

(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb
[root@promote ~]# ls /mnt/boot/
grub  lost+found
[root@promote ~]# ls /mnt/boot/grub/
device.map     fat_stage1_5  iso9660_stage1_5  minix_stage1_5     stage1  ufs2_stage1_5    xfs_stage1_5
e2fs_stage1_5  ffs_stage1_5  jfs_stage1_5      reiserfs_stage1_5  stage2  vstafs_stage1_5

5.沒有配置文件,所以要自己寫一個(gè),也沒有內(nèi)核和initrd,所以需要復(fù)制本機(jī)的內(nèi)核和initrd到新硬盤下

[root@promote ~]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz
[root@promote ~]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img
[root@promote ~]# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title CentOS (Exprrss)
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/sda3
        initrd /initramfs.img

6.我們把根文件系統(tǒng)指向了/dev/sdb3,但是里面沒有文件包,所以我們要把根文件系統(tǒng)里的文件在/dev/sdb3中創(chuàng)建

[root@promote ~]# mkdir /mnt/sysroot
[root@promote ~]# mount /dev/sdb3 /mnt/sysroot
[root@promote ~]# cd /mnt/sysroot/
[root@promote sysroot]# ls
lost+found
[root@promote sysroot]# mkdir -pv bin sbin lib lib64 dev proc sys tmp var usr home root mnt media
mkdir: created directory `bin'
mkdir: created directory `sbin'
mkdir: created directory `lib'
mkdir: created directory `lib64'
mkdir: created directory `dev'
mkdir: created directory `proc'
mkdir: created directory `sys'
mkdir: created directory `tmp'
mkdir: created directory `var'
mkdir: created directory `usr'
mkdir: created directory `home'
mkdir: created directory `root'
mkdir: created directory `mnt'
mkdir: created directory `media'

7.這里面并沒有文件,我們復(fù)制一個(gè)bash到新硬盤下,并且要把依賴到的文件也復(fù)制過去,等下開機(jī)時(shí),可以直接啟動(dòng)bash
注:任何命令或程序在運(yùn)行時(shí)如果基于動(dòng)態(tài)編譯是要以來共享庫的,通過ldd查看依賴文件

[root@promote sysroot]# cp /bin/bash /mnt/sysroot/bin/
[root@promote sysroot]# ldd /bin/bash
    linux-vdso.so.1 =>  (0x00007ffcfbf32000)
    libtinfo.so.5 => /lib64/libtinfo.so.5 (0x000000345d600000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003454a00000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003454e00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003454600000)
[root@promote sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/
[root@promote sysroot]# cp /lib64/libdl.so.2 //mnt/sysroot/lib64/
[root@promote sysroot]# cp /lib64/libc.so.6  /mnt/sysroot/lib64/
[root@promote sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/

切換根,測試是否能啟動(dòng)

[root@promote sysroot]# cd
[root@promote ~]# chroot /mnt/sysroot/
bash-4.1# 
bash-4.1# 

8.限定系統(tǒng)的init程序,如果不指定,就會自動(dòng)啟動(dòng)/sbin/init,因?yàn)槲覀冞@個(gè)硬盤里沒有

default=0
timeout=5
title CentOS (Exprrss)
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/sda3 init=/bin/bash
        initrd /initramfs.img
[root@promote ~]# sync

9.使用vm創(chuàng)建一個(gè)新虛擬機(jī),其他都一樣,然后在選擇磁盤的時(shí)候,選擇使用現(xiàn)有的磁盤位置


10.直接啟動(dòng),出現(xiàn)grub菜單,然后使用‘e’鍵,然后在kernel這一行,加入?yún)?shù)selinux=0,這個(gè)參數(shù)要加在init前面,然后回車回到kernel這一行,然后使用‘b’啟動(dòng)就可以了


11.把自己系統(tǒng)的mbr弄壞,然后使用光盤的grup進(jìn)行引導(dǎo)修復(fù)

[root@promote ~]# dd if=/dev/sda of=/root/mbr.bak count=1 bs=512
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00324258 s, 158 kB/s
[root@promote ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1
1+0 records in
1+0 records out
200 bytes (200 B) copied, 0.000763293 s, 262 kB/s
[root@promote ~]# sync

此時(shí)grub的第一階段給弄壞了,sync用來同步磁盤
如果這個(gè)時(shí)候你的系統(tǒng)是開機(jī)狀態(tài),直接使用命令grub-install --root-directory=/ /dev/sda就可以直接修復(fù)了

[root@promote ~]# grub-install --root-directory=/ /dev/sda
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
[root@promote ~]# sync

然后sync同步以后,在重啟就沒有問題了
也可以使用grup然后回車進(jìn)入命令行模式進(jìn)行修復(fù)

[root@promote ~]# 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.

如果你的系統(tǒng)是關(guān)機(jī),再啟動(dòng)就不行了,只能使用緊急救援模式進(jìn)行修復(fù)
再把mbr弄壞,然后使用救援模式進(jìn)行修復(fù)
弄壞之后進(jìn)行重啟,然后把光盤鏡像掛載上,然后重啟



然后選擇第三項(xiàng),即Rescue installed system 然后回車
或者摁‘ESC’輸入linux rescus回車就可以了




安裝grub

然后exit回到救援模式的bash下重啟就行了(光盤卸掉)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容