Linux boot process and repair MBR

Source: Internet
Author: User

Grub INTRODUCTION

stored in the first 446 bytes of the MBR, grub is divided into stage1/stage1.5/stage2.

    1. Stage1: Mainly responsible for the handover between the BIOS and grub, loading the boot files stored in each partition
    2. stage1.5: Allow the bootloader in the Stage1 to identify the filesystem on the partition where the stage2 resides;
    3. Stage2: Configuration parameters, load kernel, Ramfs

Common operating instructions in grub mode
E: Edit mode, for editing menus;
C: Command mode, interactive interface; User view commands Get help

Set Grub

1, set the temporary root after the file lookup is under the temporary root can be automatically complete (this is why boot to partition the reason, simple file system)

把/boot当作临时根(hd#,#)    hd#: 磁盘编号,用数字表示;从0开始编号    #: 分区编号,用数字表示; 从0开始编号

2. Set the kernel file
Set the kernel file used in this boot, and add many cmdline parameters that can be used by the kernel for additional support;
Most commonly used parameters
Read Only: RO
Root: Root=/device (this root address is the real root-pointing partition)
One-user mode 1, s, s or single can be used to forget password change password
And a lot of writing init=/sbin/init.
Kernel/path/to/kernel_file ro Root=/dev .... quiet

3. Set Initramfs

The above configuration is read from/boot/grub/grub.conf here.

[[email protected] grub]# cat/boot/grub/grub.conf# grub.conf generated by anaconda##  Run grub after making changes to this file# notice:you has a/boot partition. This means that# all kernel and initrd paths is relative to/boot/, eg.# root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/vg0-logvol01# Initrd/initrd-[generic-]version.img#boot=/dev/sdadefault=0tim Eout=5splashimage= (hd0,0)/grub/splash.xpm.gzhiddenmenutitle CentOS (2.6.32-431.el6.x86_64) root (hd0,0) Kernel/vmli nuz-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# himself add 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 #密码必须加在 Above, or else go in grub can be deleted, no meaning
Custom Grub

For experimental purposes, a new hard drive can load the kernel, 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 the new hard drive

[[email protected] boot]# pwd/mnt/boot[[email protected] mnt]# mount /dev/sdb1 /mnt/boot/

3. Create grub under the new hard drive

[[email protected] 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

[[email protected] boot]# lsgrub  lost+found    [[email protected] grub]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64  /mnt/boot/[[email protected] boot]# mkinitrd /mnt/boot/initramfs-$(uname -r).img   $(uname -r)

5. Configuration Files

[[email protected] grub]# vim /mnt/boot/grub/grub.conf[[email protected] 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/sdadefault=0timeout=5splashimage=(hd0,0)/grub/splash.xpm.gzhiddenmenutitle 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 Root, copy a bash script

 [[email protected] grub]# mkdir/mnt/sysroot[[email protected] grub]# mount/dev/sdb3/mnt/sysroot/[[ Email protected] sysroot]# Mkdir-pv etc bin sbin lib lib64 dev proc tmp var home root mnt media[[email protecte D] sysroot]# cp/bin/bash/mnt/sysroot/bin/[[email protected] sysroot]# ldd/bin/bash linux-vdso.so.1 = (0x00 007fffd31ff000) libtinfo.so.5 =/lib64/libtinfo.so.5 (0x00007f33c03fb000) libdl.so.2 =/lib64/libdl.so.2 (0x 00007f33c01f7000) libc.so.6 =/lib64/libc.so.6 (0x00007f33bfe62000)/lib64/ld-linux-x86-64.so.2 (0x00007f33c0626 [[email protected] sysroot]# cp/lib64/libtinfo.so.5/mnt/sysroot/lib64/[[email protected] sysroot]# Cp/lib64/libdl.so.2/mnt/sysroot/lib64/[[email protected] sysroot]# cp/lib64/libc.so.6/mnt/sysroot/lib64/[[ Email protected] sysroot]# cp/lib64/ld-linux-x86-64.so.2/mnt/sysroot/lib64/#测试当前 [[email protected]1 sysroot]# chroot/mnt/sysroot/bash-4.1# 

7. Use this hard drive to boot in a new virtual machine

Bootloader Repair

Experimental purpose only destroys the front 446 bytes, repairs grub, and the rear partition does not move
Destroying the native bootloader

[[email protected] mnt]# dd if=/dev/zero of=/dev/sda bs=200 count=1[[email protected] grub]# sync

There are several ways to fix the boot state
1. Using Grub-install Tools

 [[email protected] mnt]# grub-install --root-directory=/   /dev/sda  

2. Grub Tool

 grub> root (hd0,0)root (hd0,0) Filesystem type is ext2fs, partition type 0x83grub> 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

The rescue mode of the disc is required to repair the restart state.
1. Power-on in the State of destruction

2. Loading disc

3. Select Rescue mode


Linux boot process and repair MBR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.