grub: GRand Unified Bootloader
- grub 0.x: grub legacy(centos5，6)
- grub 1.x: grub2(centos7)
grub legacy(grub 0.x)
The sector after stage 1_5: mbr enables the bootloader in stage 1 to identify the file system on the partition where stage 2 is located
stage2: partition (/ boot/grub /)
# ls /boot/grub/ device.map fat_stage1_5 grub.conf jfs_stage1_5 minix_stage1_5 splash.xpm.gz stage2 vstafs_stage1_5 e2fs_stage1_5 ffs_stage1_5 iso9660_stage1_5 menu.lst reiserfs_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
Stage 2 and kernel are usually placed on basic disk partition, not lvm and soft raid. To read lvm and soft raid partition, the driver in the root file system is required. At this time, the root file system has not been loaded.
The root file system is generally a logical partition to facilitate the expansion and management. If / boot is also in the same partition as the root file system, the grub program cannot load the files in the logical partition, so it cannot access the / boot directory, or load the kernel, and the system cannot start. So that's why / boot is independent.
If the root file system does not use logical volumes, / boot can not be partitioned independently.
When installing CentOS 6 operating system, if it is not manually partitioned, it will automatically separate / boot into a partition, and make / and swap into logical partitions
First of all, with fdisk, you can see that / dev/sda2 is Linux LVM (logical partition), while / dev/sda1 is a normal partition.
Using pvs, we can see that the name of vg in / dev/sda2 is vg Luo c610
Using vgs, we can see that there are two LV S in VG Lau c610
Using lvs, we can see that the two lvs in vg-c610 are LV-root and LV-swap, respectively
By using mount, we can see that the mount point of lv_root is /; / dev/sda1 is mounted to / boot
# fdisk -l /dev/sda 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 1045 7875584 8e Linux LVM # pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_c610 lvm2 a--u 7.51g 0 # vgs VG #PV #LV #SN Attr VSize VFree vg_c610 1 2 0 wz--n- 7.51g 0 # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv_root vg_c610 -wi-ao---- 6.71g lv_swap vg_c610 -wi-ao---- 816.00m # mount | grep "^/dev" /dev/mapper/vg_c610-lv_root on / type ext4 (rw) /dev/sda1 on /boot type ext4 (rw)
The role of stage 2:
1. Provide menu and interactive interface
- Press e: enter the edit mode, which is used to edit the menu
- Press c: command mode to enter the interactive interface.
2. Load the kernel or operating system selected by the user
- Allow parameters to be passed to the kernel
- You can hide this menu
3. Provide protection mechanism for menu
- Password protection for edit menu
- Password protection for boot kernel
grub command uses:
Help get help; help KEYWORD details of a command
Identification device: root (hd × x, x): hd and the outer bracket are fixed. The first digital ID is the number of disks; the second is the number of partitions. The number starts at 0. Starting from grub2 is starting from 1.
(hd0,0): the first partition of the first disk.
Find command: find
When searching, you must indicate which partition of which disk you are on.
Find the kernel, know the name of the kernel file, but don't know which partition is on the disk, but you must also indicate the disk and partition when you search. It's too pit. You can only try it in vain. A kernel file was found in (hd0,0).
grub> find (hdx,y)/vmlinuz-3.10.0-957.el7.x86_64
The corresponding relationship between the disk found on the Internet and the hd(ාාා) in grub command is unknown
- sda1 means the first primary partition of the first hard disk = hd(0,0)
- sda2 means the second primary partition of the first hard disk = hd(0,1)
- sdb1 means the first primary partition of the second hard disk = hd(1,0)
- sdb2 indicates the second primary partition of the second hard disk = hd(1,1)
Enter the selected disk: root
grub> root hd(0,0) grub> find /vmlinuz-2.6.32-754.el6.x86_64 hd(0,0)
After entering the disk, you do not need to add the disk when using the find command.
Note: if / boot is on the same file system as the root file system, add / boot to the path. grub> find /boot/vmlinuz-2.6.32-754.el6.x86_64
Select the kernel to start: kernel /PATH/TO/KERNEL_FILE
File name of kernel: vmlinuz*
In addition, many kernel parameters can be specified.
- selinux=0 (selinux feature is not enabled)
- init=/sbin/init (first user space program)
Select the ramdisk file to be used by the kernel: initrd / path / to / ramdisk? Fle
Note: the version number of the ramdisk file should be exactly the same as that of the kernel
Start the selected kernel: boot
Inside of operating system startup:
1. Display the list of bootable operating systems.
2. After selecting the system to be started, press e to enter the following screen.
First line (root (hd0,0)): Specifies the disk and partition where the kernel vmlinuz-2.6.32-754.el6.x86_resides
The second line: the location of vmlinuz-2.6.32-754.el6.x86_, and the parameters of kernel startup. ro: read only. Root is the partition where the root file system is located.
Line 3: the location of the ramdisk file used when the kernel starts
When you click enter in the interface of selecting the operating system to start, these three lines of instructions are actually allowed.
Manual start system
The configuration file that controls the content shown in the figure above is / etc/grub.conf. When it is damaged, you don't know which kernel and ramdisk you want to use to start the system. But after starting, you can enter the grub command line interface. At this time, you can manually specify the kernel, and after ramdisk, start the system.
1. Select the kernel to start
grub> root (hd0,0) grub> kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/mapper/vg_c610-lv_root
2. Select the ramdisk file to be used by the kernel
grub> initrd /initramfs-2.6.32-754.el6.x86_64.img
3. Start the selected kernel
grub profile description
/Contents of etc/grub.conf file:
- Default: when there are multiple operating systems, which one is selected by default.
- timeout: wait for 5 seconds for the user. If the user does not operate, start the operating system on the default option.
- Splash image: select the image of the operating system interface.
- title: content displayed
- root: Specifies the disk and partition where the kernel vmlinuz-2.6.32-754.el6.x86 ʄ resides
- Kernel: location of vmlinuz-2.6.32-754.el6.x86_, and parameters of kernel startup.
- initrd: the location of the ramdisk file used when the kernel starts
default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-754.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/mapper/vg_c610-lv_root rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us LANG=\ en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg_c610/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_c610/lv_root rd_NO_DM rh\ gb quiet initrd /initramfs-2.6.32-754.el6.x86_64.img
Add an operating system manually: add title and root below it, etc.
When you press e, you need to enter the password: password -- MD5 $1 $jxrwo0 $yf3q8 / fnpt7r8xkgkkhj0. Write it on the hidden menu.
When you press enter, you need to enter the password: password -- MD5 $1 $jxrwo0 $yf3q8 / fnpt7r8xkgkkhj0 to start the system. Write it under initrd.
Command to generate MD5 password: grub-md5-crypt
default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz password --md5 $1$jXrWo0$YF3q8/FNPT7rR8xkgKKhj0 hiddenmenu title CentOS 6 (2.6.32-754.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/mapper/vg_c610-lv_root rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us LANG=\ en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg_c610/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_c610/lv_root rd_NO_DM rh\ gb quiet initrd /initramfs-2.6.32-754.el6.x86_64.img title CentOS 6 (my test) root (hd0,0) kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/mapper/vg_c610-lv_root rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us LANG=\ en_US.UTF-8 rd_NO_MD rd_LVM_LV=vg_c610/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_c610/lv_root rd_NO_DM rh\ gb quiet initrd /initramfs-2.6.32-754.el6.x86_64.img password --md5 $1$jXrWo0$YF3q8/FNPT7rR8xkgKKhj0
menu diagram at startup:
How to enter single user mode
1. On the selected worry system, press e
2. On the selected kernel, press e, then after the displayed kernel parameters, press space, press 1 or s or s, then enter, and finally press b.
Grub is broken, fix grub
Damage 1: the contents of the partition where the boot is located have been deleted by mistake and have been shut down. How to fix it?
Next, we will simulate this kind of damage, remove the damaged grub disk and connect it to another host. There is a disk on this host that is / dev/sda. After connecting the damaged disk, the damaged disk is / dev/sdb. After the repair, remove the original / dev/sda, so the repaired disk becomes / dev/sda.
1. Find a new disk and simulate it as a grub damaged disk, which is divided into three sections, / dev/sdb1 for / boot, / dev/sdb3 for root file system, / dev/sdb2 for swap
# fdisk -l /dev/sdb Device Boot Start End Blocks Id System /dev/sdb1 1 14 112423+ 83 Linux /dev/sdb2 15 40 208845 82 Linux swap / Solaris /dev/sdb3 41 105 522112+ 83 Linux # mke2fs -t ext4 /dev/sdb1 # mke2fs -t ext4 /dev/sdb3 # mkswap /dev/sdb2
2. Mount partition
# mount /dev/sdb1 /mnt/boot/ # ls /mnt/boot/ lost+found
3. Use grub install to install grub
Options -- root directory: Tell grub install which directory to look for / boot; the next partition tells grub install which partition to look for / boot.
After successful execution, the stage file is created in the directory / mnt/boot/grub.
# grub-install --root-directory=/mnt /dev/sdb1 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 # ls /mnt/boot/grub/ device.map grub.conf minix_stage1_5 ufs2_stage1_5 e2fs_stage1_5 grub.conf~ reiserfs_stage1_5 vstafs_stage1_5 fat_stage1_5 iso9660_stage1_5 stage1 xfs_stage1_5 ffs_stage1_5 jfs_stage1_5 stage2
4. After grub is installed, there is no kernel and ramdisk in the / boot directory, so copy the kernel and ramdisk from other places
# cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /mnt/boot/ # cp /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot # ls /mnt/boot/ grub initramfs-2.6.32-754.el6.x86_64.img lost+found vmlinuz-2.6.32-754.el6.x86_64
5. Create the / boot/grub/grub.conf file
Note: the kernel and initrd are in (hd0,0); and the root file system is assumed to be on / dev/sda3.
default=0 timeout=5 title CentOS 6 (test) root (hd0,0) kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/sda3 initrd /initramfs-2.6.32-754.el6.x86_64.img
6. Since there is no root file system in / dev/sda3, create it manually
# mkdir /mnt/sysroot [root@c610 ~]# mount /dev/sdb3 /mnt/sysroot/ [root@c610 ~]# cd /mnt/sysroot/ [root@c610 sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr bome root mnt media
7. After the kernel starts, to start the init program, it is too complex to create an environment that can start init. Simply, let the kernel start the bash program instead of init.
Copy the bash executable and the dependent libraries to / dev/sda3
With the ldd command, you can know which library files the executable depends on.
Test whether Bash is available, and use chroot command to switch the root file system. After switching, Bash is directly entered, indicating that bash can operate normally. Try to run ls command, ls command did not, because the executable file and Library of ls were not copied. cd and pwd are built-in commands of bash, so they can be used normally.
[root@c610 sysroot]# cp /bin/bash /mnt/sysroot/bin [root@c610 sysroot]# ldd /bin/bash linux-vdso.so.1 => (0x00007ffeb5542000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00000030ab800000) libdl.so.2 => /lib64/libdl.so.2 (0x00000030a9000000) libc.so.6 => /lib64/libc.so.6 (0x00000030a9400000) /lib64/ld-linux-x86-64.so.2 (0x000056492e521000) [root@c610 sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/ [root@c610 sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/ [root@c610 sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/ [root@c610 sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/ [root@c610 sysroot]# chroot /mnt/sysroot/ bash-4.1# ls bash: ls: command not found bash-4.1# cd /tmp bash-4.1# pwd /tmp bash-4.1# exit exit [root@c610 sysroot]#
8. Modify the / boot/grub/grub.conf file
Since the init program is started by default after the kernel is started, the program started by default must be modified to bash
Also indicate the parameter: selinux=0, before the init parameter
default=0 timeout=5 title CentOS 6 (test) root (hd0,0) kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/sda3 selinux=0 init=/bin/bash initrd /initramfs-2.6.32-754.el6.x86_64.img
9. Remove the original / dev/sda, so the repaired disk becomes / dev/sda, using the modified disk system.
Damage 2: the contents of the partition where the boot is located have been deleted by mistake. Fortunately, it has not been shut down. How to repair it?
Do not need to remove the hard disk, directly in the current system, the recovery is good. The repair method is similar to the above.
Use CD to enter rescue mode
The above damage condition 1 can also be repaired.
The principle of the CD-ROM rescue mode is that there is a bash program that can run in the CD-ROM. through the bash program, the grub install command is called to complete the repair of grub. If the kernel file in / boot is also damaged, there is a kernel in the CD-ROM, which can be copied. But if the ramdisk is broken, it can't be fixed. Ramdisk is generated according to the operating system, so there is no disk. But you can install it again on the same machine and regenerate it into a ramdisk file.
QQ group of mutual learning in c/c + +: 877684253