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/)配置文件:/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命令行接口启动系统:
grub> root (hd#,#) grub> kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE grub> initrd /initramfs-VERSION-RELEASE.img grub> boot
-
配置文件:/boot/grub/grub.conf
配置项:
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命令
-
进入单用户模式:
- 编辑grub菜单(选定要编辑的title,而后使用e命令);
- 在选定的kernel后附加
1, s, S或single都可以; - 在kernel所在行,键入“b”命令;
-
安装grub:
-
grub-install
grub-install --root-directory=ROOT /dev/DISK -
grub
grub> root (hd#,#)
grub> setup (hd#)
-
一些问题
partx -u <disk>
更行设备分区表
-n M:N (M-N)
指定分区可以使用-1(最后一块)、-2(最后第二块)
练习:
1、新加硬盘,提供直接单独运行bash系统;
加一块20G硬盘,创建3个分区
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +100M
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (206848-41943039, default 206848):
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-41943039, default 41943039): +2G
Partition 2 of type Linux and of size 2 GiB is set
Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (4401152-41943039, default 4401152):
Using default value 4401152
Last sector, +sectors or +size{K,M,G} (4401152-41943039, default 41943039): +5G
Partition 3 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partx -u /dev/sdb
格式化分区
[root@localhost ~]# mke2fs -t ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mke2fs -t ext4 /dev/sdb3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=cdf5d128-072d-4e89-a77f-c46553748f40
[root@localhost ~]# mkdir /mnt/boot
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb
-bash: grub-install: command not found
[root@localhost ~]# yum install grub2
[root@localhost ~]# grub2-install --boot-directory=/mnt/boot /dev/sdb
Installing for i386-pc platform.
Installation finished. No error reported.
复制内核文件和initramfs文件到boot目录下,并编辑grub.cfg
[root@localhost ~]# cp /boot/vmlinuz-3.10.0-957.el7.x86_64 /mnt/boot/
[root@localhost ~]# cp /boot/initramfs-3.10.0-957.el7.x86_64.img /mnt/boot/
[root@localhost ~]# cp /boot/grub2/grub.cfg /mnt/boot/grub2/
创建根目录
[root@localhost ~]# mkdir /mnt/sysroot
[root@localhost ~]# mount /dev/sdb3 /mnt/sysroot
[root@localhost ~]# mkdir /mnt/sysroot/{etc,bin,sbin,lib,lib64,dev,proc,sys,tmp,var,usr,home,root,mnt,media}
[root@localhost ~]# ls /mnt/sysroot
bin dev etc home lib lib64 lost+found media mnt proc root sbin sys tmp usr var
拷贝bash程序以及依赖库文件
[root@localhost ~]# cp /bin/bash /mnt/sysroot/bin/
[root@localhost ~]# ldd /bin/bash
linux-vdso.so.1 => (0x00007fffa3bbe000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f376f703000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f376f4ff000)
libc.so.6 => /lib64/libc.so.6 (0x00007f376f132000)
/lib64/ld-linux-x86-64.so.2 (0x00007f376f92d000)
[root@localhost ~]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/
[root@localhost ~]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/
[root@localhost ~]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/
[root@localhost ~]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/
检测bash
[root@localhost ~]# chroot /mnt/sysroot
bash-4.2# who
bash: who: command not found
bash-4.2# exit
exit
做不下去了, grub2不会配置
centos6.10
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 16 20971520 sdb
8 0 20971520 sda
8 1 512000 sda1
8 2 20458496 sda2
253 0 18423808 dm-0
253 1 2031616 dm-1
[root@localhost ~]# fdisk /dev/sdb
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): 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): 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): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# 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 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# 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 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
[root@localhost ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
no label, UUID=a6752240-ddec-4b85-938b-456478100451
[root@localhost ~]# mount /dev/sdb1 /tmp/boot
[root@localhost ~]# mount /dev/sdb3 /tmp/sysroot
[root@localhost ~]# ls /tmp/boot
lost+found
[root@localhost ~]# ls /tmp/sysroot
lost+found
[root@localhost ~]# grub-install --root-directory=/tmp /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 /tmp/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@localhost ~]# cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /tmp/boot/vmlinuz
[root@localhost ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img /tmp/boot/initramfs.img
[root@localhost ~]# vi /tmp/boot/grub/grub.conf
[root@localhost ~]# mkdir /tmp/sysroot/{etc,bin,sbin,lib,lib64,dev,proc,sys,tmp,var,usr,home,root,mnt,media}
[root@localhost ~]# ldd /bin/bash
linux-vdso.so.1 => (0x00007ffd1d7fc000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f3a9cdda000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f3a9cbd6000)
libc.so.6 => /lib64/libc.so.6 (0x00007f3a9c841000)
/lib64/ld-linux-x86-64.so.2 (0x0000558397d36000)
[root@localhost ~]# cp /bin/bash /tmp/sysroot/bin/
[root@localhost ~]# cp /lib64/libtinfo.so.5 /tmp/sysroot/lib64/
[root@localhost ~]# cp /lib64/libdl.so.2 /tmp/sysroot/lib64/
[root@localhost ~]# cp /lib64/libc.so.6 /tmp/sysroot/lib64/
[root@localhost ~]# cp /lib64/ld-linux-x86-64.so.2 /tmp/sysroot/lib64/
[root@localhost ~]# sync
报错
2、破坏本机grub stage1,而后在救援模式下修复之;
备份并破坏mbr扇区
[root@localhost ~]# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000185385 s, 2.8 MB/s
[root@localhost ~]# 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.000198694 s, 1.0 MB/s
[root@localhost ~]# sync
及时补救
[root@localhost ~]# 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@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. grub> quit quit
挂载iso镜像
启动rescue模式
启动shell
grub-install
3、为grub设置保护功能;
[root@localhost ~]# grub-md5-crypt
Password:
Retype password:
$1$QOKhH0$RbVw/n2FkwY/ZgUUBJ.ZW0
[root@localhost ~]# vi /tmp/boot/grub/grub.conf
[root@localhost ~]# cat /tmp/boot/grub/grub.conf
default=0
timeout=5
password --md5 $1$QOKhH0$RbVw/n2FkwY/ZgUUBJ.ZW0
title CentOS6
password --md5 $1$QOKhH0$RbVw/n2FkwY/ZgUUBJ.ZW0
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash
initrd /initramfs.img
网友评论