Grub应用

GRUB(Boot Loader):

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

grub legacy:

stage1: mbr
stage1_5: mbr之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统;
stage2:磁盘分区(/boot/grub/)
stage2及内核等通常放置于一个基本磁盘分区;

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

功用:

(1) 提供菜单、并提供交互式接口
e: 编辑模式,用于编辑菜单;
c: 命令模式,交互式接口;
(2) 加载用户选择的内核或操作系统
允许传递参数给内核
可隐藏此菜单
(3) 为菜单提供了保护机制
为编辑菜单进行认证
为启用内核或操作系统进行认证

grub如何识别设备:

(hd#,#)
hd#: 磁盘编号,用数字表示;从0开始编号
#: 分区编号,用数字表示; 从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命令行接口启动系统:
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

配置项:

default=#: 设定默认启动的菜单项;落单项(title)编号从0开始;
timeout=#:指定菜单项等待选项选择的时长;
splashimage=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜单背景图片文件路径;
hiddenmenu:隐藏菜单;
password [--md5] STRING: 菜单编辑认证;
title TITLE:定义菜单项“标题”, 可出现多次;
root (hd#,#):grub查找stage2及kernel文件所在设备分区;为grub的“根”;
kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:启动的内核
initrd /PATH/TO/INITRAMFS_FILE: 内核匹配的ramfs文件;
password [--md5] STRING: 启动选定的内核或操作系统时进行认证;

grub-md5-crypt命令

[root@promote ~]# grub-md5-crypt 
Password: 
Retype password: 
$1$H0XYM0$3ASdzIM3vfC3gh/ywCDkz1
进入单用户模式:

(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 打印应用程序依赖的库文件
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)

应用

1.在虚拟机中添加一块20G硬盘,装入grub

添加一块20G的硬盘

2.可以看出新硬盘sdb没有分区,先对其进行分区,一个操作系统正常来说应该有三个分区,即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下创建一个boot目录,把新硬盘的第一个分区,挂载到刚创建的目录

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

4.安装grub并验证

[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.没有配置文件,所以要自己写一个,也没有内核和initrd,所以需要复制本机的内核和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.我们把根文件系统指向了/dev/sdb3,但是里面没有文件包,所以我们要把根文件系统里的文件在/dev/sdb3中创建

[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.这里面并没有文件,我们复制一个bash到新硬盘下,并且要把依赖到的文件也复制过去,等下开机时,可以直接启动bash
注:任何命令或程序在运行时如果基于动态编译是要以来共享库的,通过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/

切换根,测试是否能启动

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

8.限定系统的init程序,如果不指定,就会自动启动/sbin/init,因为我们这个硬盘里没有

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创建一个新虚拟机,其他都一样,然后在选择磁盘的时候,选择使用现有的磁盘位置


10.直接启动,出现grub菜单,然后使用‘e’键,然后在kernel这一行,加入参数selinux=0,这个参数要加在init前面,然后回车回到kernel这一行,然后使用‘b’启动就可以了


11.把自己系统的mbr弄坏,然后使用光盘的grup进行引导修复

[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

此时grub的第一阶段给弄坏了,sync用来同步磁盘
如果这个时候你的系统是开机状态,直接使用命令grub-install --root-directory=/ /dev/sda就可以直接修复了

[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然后回车进入命令行模式进行修复

[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.

如果你的系统是关机,再启动就不行了,只能使用紧急救援模式进行修复
再把mbr弄坏,然后使用救援模式进行修复
弄坏之后进行重启,然后把光盘镜像挂载上,然后重启



然后选择第三项,即Rescue installed system 然后回车
或者摁‘ESC’输入linux rescus回车就可以了




安装grub

然后exit回到救援模式的bash下重启就行了(光盘卸掉)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349