Introduction to grub
Stored in the first 446 bytes of MBR, grub is divided into stage1/stage1.5/stage2.
- stage1: Mainly responsible for the handover between BIOS and GRUB, loading the boot files stored in each partition
- stage1.5: Let the bootloader in stage1 recognize the file system on the partition where stage2 is located;
- stage2: configuration parameters, load kernel, ramfs
Common operation instructions in grub mode
e: edit mode, used to edit menus;
c: command mode, interactive interface; users can view commands for help
setup grub
1. The file search after setting the temporary root can be automatically completed under the temporary root (this is why the boot is partitioned, a simple file system)
把/boot当作临时根
(hd#,#)
hd#: 磁盘编号,用数字表示;从0开始编号
#: 分区编号,用数字表示; 从0开始编号
2. Set the kernel file to
set the kernel file used in this startup; additionally, you can add many cmdline parameters supported by the kernel;
the most commonly used parameters
read-only : ro
root: root=/device (this root address is the real root The partition pointed to)
single-user mode 1, s, S or single can be used to forget the password, change the password
, and write init=/sbin/init and many other
kernels /PATH/TO/KERNEL_FILE ro root=/dev... .quiet
3. Set up initramfs
The above configuration is read from /boot/grub/grub.conf
[root@node1 grub]# 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/vg0-LogVol01
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg0-LogVol01 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg0/LogVol01 rd_LVM_LV=vg0/LogVol00 crashkernel=auto SYSFONT=latarcyrheb-sun16 rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
#自己添加
title fortest-CentOS (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg0-LogVol01 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg0/LogVol01 rd_LVM_LV=vg0/LogVol00 crashkernel=auto SYSFONT=latarcyrheb-sun16 rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
password --md5 $1$fQ1Hq/$otpD/NAH5fB/bCgmcqfl41 #密码必须加在上面,不然进去grub可以删除,没有意义
custom grub
For experimental purposes, a new hard disk can load the kernel and boot
1. Prepare the new hard drive
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 942 5253255 83 Linux
2. Mount a new hard drive
[root@node1 boot]# pwd
/mnt/boot
[root@node1 mnt]# mount /dev/sdb1 /mnt/boot/
3. Create grub under the new hard disk
[root@node1 mnt]# grub-install --root-directory=/mnt /dev/sdb #指定boot所在的目录, 安装到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
4. Copy the kernel, ramfs
[root@node1 boot]# ls
grub lost+found
[root@node1 grub]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/
[root@node1 boot]# mkinitrd /mnt/boot/initramfs-$(uname -r).img $(uname -r)
5. Configuration file
[root@node1 grub]# vim /mnt/boot/grub/grub.conf
[root@node1 grub]# cat 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/vg0-LogVol01
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (test1)
root (hd0,0)
# root=/dev/sda3 这块硬盘分区3是根
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda3 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-431.el6.x86_64.img
6. Mount the root and copy a bash script
[root@node1 grub]# mkdir /mnt/sysroot
[root@node1 grub]# mount /dev/sdb3 /mnt/sysroot/
[root@node1 sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc tmp var home root mnt media
[root@node1 sysroot]# cp /bin/bash /mnt/sysroot/bin/
[root@node1 sysroot]# ldd /bin/bash
linux-vdso.so.1 => (0x00007fffd31ff000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f33c03fb000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f33c01f7000)
libc.so.6 => /lib64/libc.so.6 (0x00007f33bfe62000)
/lib64/ld-linux-x86-64.so.2 (0x00007f33c0626000)
[root@node1 sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/
[root@node1 sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/
[root@node1 sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/
[root@node1 sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/
#测试当前
[root@node1 sysroot]# chroot /mnt/sysroot/
bash-4.1#
7. Boot with this hard disk in the new virtual machine
bootloader repair
The purpose of the experiment is to only destroy the first 446 bytes, repair the grub, and destroy the local bootloader without leaving the rear partition .
[root@node1 mnt]# dd if=/dev/zero of=/dev/sda bs=200 count=1
[root@node1 grub]# sync
There are various repair methods in the boot state
1. Use the grub-install tool
[root@node1 mnt]# grub-install --root-directory=/ /dev/sda
2. grub tool
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
Repair in the restart state, need the rescue mode of the disc
1. Boot in the damaged state
2. Load the CD
3. Select rescue mode