标签:grub
什么是GRUB
GRUB 是一个多重操作系统启动管理器。既可以用来引导window也可以引导linux,是一个非常强大的bootloader,linux运维必用神器。
GRUB版本
grub 0.x grub legacy Centos 5,6中广泛使用
stage1 位于mbr的前446字节,其主要作用是定位stage2,由于不能识别文件系统,则需要位于紧跟mbr之后的stage1_5帮助其识别文件系统。
stage2 位于磁盘分区上,为内核提供菜单,交互式接口,以及菜单保护机制
grub 1.x grub2 Centos 7中使用
GRUB修复
当系统的grub损坏时,可使用grub-install 或者grub命令
grub-install --root-directory=ROOT /dev/DISK
或者 grub
grub> root (hd#,#)
grub> setup (hd#)
实验:通过dd破坏grub,然后修复
[root@test-1 ~]# dd if=/dev/zero of=/dev/sda count=1 bs=200 1+0 records in 1+0 records out 200 bytes (200 B) copied, 0.00882837 s, 22.7 kB/s [root@test-1 ~]# reboot
重启后,由于没有grub引导,自动转由pxe启动
挂载镜像,进入救援模式修复grub
一路默认,到救援模式下的shell
通过grub命令修复
然后,quit,reboot系统。重启后,系统OK
或者在救援模式shell命令行使用grub-install --root-directory=ROOT /dev/DISK也可以修复
在GRUB命令行接口手动启动系统
grub> root (hd#,#)
grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE
grub> initrd /initramfs-VERSION-RELEASE.img
grub> boot
GRUB配置文件
配置文件为:/boot/grub/grub.conf
参数配置项:
default=# 设定默认启动的菜单项;标题项(title)编号从0开始;
timeout=# 设定菜单等待时长
splashimage=(hd#,#)/PATH/TO/FILE:指明菜单背景图片文件路径
hiddenmenu 隐藏菜单
[password [--md5]] STRING: 设置菜单编辑密码 (可有grub-md5-crypt生成md5密码串)
title String 定义菜单项标题,有多个内核时会有多个
root (hd#,#) grub查找stage2及kernel文件所在设备分区;为grub的“根”;
kernel /PATH/TO/VMLINUZ_FILE ro root=/dev/xxx 开机启动的内核
initrd /PATH/TO/INITRAMFS_FILE 内核匹配的ramfs文件;
[password [--md5] STRING] 启动选定的内核或操作系统时进行认证;
实验题:在先有虚拟机上新增一块硬盘,为其提供单独运行的bash系统,然后新建虚拟机挂载新硬盘启动并测试其功能。
[root@test-1 ~]# fdisk /dev/sdb #新硬盘,创建3个分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4312b6bb.
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): +200M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (27-2610, default 27):
Using default value 27
Last cylinder, +cylinders or +size{K,M,G} (27-2610, default 2610): +2G
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (289-2610, default 289):
Using default value 289
Last cylinder, +cylinders or +size{K,M,G} (289-2610, default 2610):
Using default value 2610
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): 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: 0x4312b6bb
Device Boot Start End Blocks Id System
/dev/sdb1 1 26 208813+ 83 Linux
/dev/sdb2 27 288 2104515 82 Linux swap / Solaris
/dev/sdb3 289 2610 18651465 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@test-1 ~]# 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@test-1 ~]# 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@test-1 ~]# cat /proc/partitions
major minor #blocks name
8 0 125829120 sda
8 1 204800 sda1
8 2 62914560 sda2
8 16 20971520 sdb
8 17 208813 sdb1
8 18 2104515 sdb2
8 19 18651465 sdb3
253 0 20971520 dm-0
253 1 2097152 dm-1
253 2 10485760 dm-2
253 3 20971520 dm-3
[root@test-1 ~]# mkfs -t ext4 -L "boot" /dev/sdb1 #创建文件系统
mke2fs 1.41.12 (17-May-2010)
Filesystem label=boot
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
52208 inodes, 208812 blocks
10440 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
26 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@test-1 ~]# mkfs -t ext4 -L "root" /dev/sdb3
mke2fs 1.41.12 (17-May-2010)
Filesystem label=root
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1166880 inodes, 4662866 blocks
233143 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
143 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@test-1 ~]# mkswap -L "swap" /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
LABEL=swap, UUID=91554fc3-8fbf-4927-944b-2a0c28cad1f5
[root@test-1 ~]# mkdir -p /mnt/{boot,sysroot} #创建2个挂载点
[root@test-1 ~]# cd /mnt/
[root@test-1 mnt]# ls
boot sysroot
[root@test-1 mnt]# mount /dev/sdb1 /mnt/boot/ #挂载新硬盘的分区到挂载点
[root@test-1 mnt]# mount /dev/sdb3 /mnt/sysroot/
[root@test-1 mnt]# mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /mnt/boot type ext4 (rw)
/dev/sdb3 on /mnt/sysroot type ext4 (rw)
[root@test-1 mnt]# cd sysroot/
[root@test-1 sysroot]# mkdir bin boot dev etc home lib lib64 media misc mnt net opt proc root sbin selinux srv sys tmp usr var
[root@test-1 sysroot]# ls
bin boot dev etc home lib lib64 lost+found media misc mnt net opt proc root sbin selinux srv sys tmp usr var
[root@test-1 sysroot]# cp /bin/bash /bin/ls bin/ #把bash和ls复制到bin/下
[root@test-1 sysroot]# ls bin/
bash ls
[root@test-1 sysroot]# ldd /bin/bash /bin/ls
/bin/bash:
linux-vdso.so.1 => (0x00007fffd6fff000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003c51c00000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003c47000000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c47400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c46c00000)
/bin/ls:
linux-vdso.so.1 => (0x00007fff78bff000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003c48c00000)
librt.so.1 => /lib64/librt.so.1 (0x0000003c47c00000)
libcap.so.2 => /lib64/libcap.so.2 (0x0000003c4a800000)
libacl.so.1 => /lib64/libacl.so.1 (0x0000003c52000000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c47400000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003c47000000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c46c00000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c47800000)
libattr.so.1 => /lib64/libattr.so.1 (0x0000003c52800000)
[root@test-1 sysroot]# cp /lib64/libtinfo.so.5 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 /lib64/libselinux.so.1 /lib64/librt.so.1 /lib64/libcap.so.2 /lib64/libacl.so.1 /lib64/libpthread.so.0 /lib64/libattr.so.1 lib64/
[root@test-1 sysroot]# chroot /mnt/sysroot/ #chroot测试bash是否可用
bash-4.1# ls
bin boot dev etc home lib lib64 lost+found media misc mnt net opt proc root sbin selinux srv sys tmp usr var
bash-4.1# exit
exit
###经测试可以在新硬盘的的根下可以使用
[root@test-1 sysroot]# cd /mnt/boot/
[root@test-1 boot]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 /boot/initramfs-2.6.32-504.el6.x86_64.img . #复制内核
[root@test-1 boot]# ls
initramfs-2.6.32-504.el6.x86_64.img lost+found vmlinuz-2.6.32-504.el6.x86_64
[root@test-1 boot]# grub-install --root-directory=/mnt /dev/sdb #制作grub
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@test-1 boot]# ls
grub initramfs-2.6.32-504.el6.x86_64.img lost+found vmlinuz-2.6.32-504.el6.x86_64
[root@test-1 boot]# cd grub/
[root@test-1 grub]# vi grub.conf ##制作grub配置文件
default=0
timeout=5
title Min Linux
root (hd0,0)
kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/sda3 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-504.el6.x86_64.img
[root@test-1 grub]# sync
[root@test-1 grub]# sync然后创建新的虚拟机,挂载新的硬盘,测试。
最后进入bash命令,测试一下ls是否可用。
这样完成了一个有简单bash命令的linux。
标签:grub
原文地址:http://iznowow.blog.51cto.com/6232028/1691563