What is Grub
GNU GRUB is a multi-os boot manager. GNU grub is derived from grub (GRand Unified Bootloader). GRUB was originally designed and applied by Erich Stefan Boleyn;
The system Boot Manager, is the first program to run after the computer starts, he is responsible for loading, transfer control to the operating system of the kernel, once the kernel is mounted, the system Boot Manager's task to complete the exit, the other parts of the system boot, such as the system initialization and startup process is completely controlled by the kernel to complete.
configuration files for grub in Linux
[[Email protected] ~] #vim/boot/grub/grub.conf
Default=0
Timeout=5
Splashimage= (hd0,0)/grub/splash.xpm.gz
Hiddenmenu
Title Red Hat Enterprise Linux Server (2.6.18-164.EL5)
Root (hd0,0)
Kernel/vmlinuz-2.6.18-164.el5 ro root=label=/rhgb quiet
Initrd/initrd-2.6.18-164.el5.img
1, Destroy Grub 400b: (Grub occupies 446b, only partial grub is destroyed)
[[email protected] ~]# dd if=/dev/zero of=/dev/sda count=1 bs=400
1+0 Records in
1+0 Records out
Bytes (+ B) copied, 5.3e-05 seconds, 7.5 MB/s
[[Email protected] ~]# Sync
[[Email protected] ~]# Sync
2. Fix Grub: (The Linux system was not exited after the first step of operation)
[Email protected] ~]#Grub#进入Grub Mode
Probing devices to guess BIOS drives. This could 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)#第一个硬盘hd0, boot on the first partition0
Root (hd0,0)
Filesystem type is EXT2FS, partition type 0x83
Grub>Setup (hd0)#set表示Installing Grub
Setup (hd0)
Checking if "/boot/grub/stage1" exists ... no#exists exists, no means the item is destroyed.
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)" ... Sectors is embedded.
Succeeded #Installation Grub succeeded
Running "Install/grub/stage1 (hd0) (hd0) 1+15 P (hd0,0)/grub/stage2/grub/grub.conf" ... succeeded
Done.
Grub> quit
Quit
[Email protected] ~]# vi/etc/grub.conf
...
Default=0
Timeout=15
Splashimage= (hd0,0)/grub/splash.xpm.gz
#hiddenmenu
Password--md5 $1$zu2p2/$QAIN 26lbjrjita622ouzu.
Title My Linux
Root (hd0,0)
Kernel/vmlinuz-2.6.18-164.el5 ro root=label=/rhgb quiet
Initrd/initrd-2.6.18-164.el5.img
3. Install Grub the second way:
You can specify any device , partition before installation, partition table , mount boot directory.
# Grub -install--root-directory=/path/to/boot ' S_parent_dir /path/to/device
[Email protected] ~]#mke2fs-j/dev/sdc1 #1 # Format the hard drive
[Email protected] ~]# mke2fs-j/DEV/SDC2
[Email protected] ~]# MKSWAP/DEV/SDC3
Setting up Swapspace version 1, size = 139825 KB
[Email protected] ~]#mkdir/mnt/boot
[Email protected] ~]#Mount/dev/sdc1/mnt/boot/ #2 # Mapping Boot partitions
[email protected] ~]# mount
/DEV/SDC1 on/mnt/boot type ext3 (rw)
[Email protected] ~]#Grub-install--root-directory=/mnt/dev/sdc#3 #Automatically find boot under/mnt
Probing devices to guess BIOS drives. This could take a long time.
Installation finished. No Error reported.
This is the contents of the device map/mnt/boot/grub/Device.map.#Grub has been created
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 #Mapping relationships for hard disks in grub
(HD1)/DEV/SDb
(HD2)/DEV/SDC
[Email protected] ~]# cd/mnt/boot/
[Email protected] boot]#Tree #The following files generated by Grub-install are automatically placed under boot
.
|--Grub
| |--Device.map
| |--e2fs_stage1_5
| |--fat_stage1_5
| |--ffs_stage1_5
| |--iso9660_stage1_5
| |--jfs_stage1_5
| |--minix_stage1_5
| |--reiserfs_stage1_5
| |--Stage1
| |--Stage2
| |--ufs2_stage1_5
| |--vstafs_stage1_5
| '--xfs_stage1_5
'--Lost+found
2 directories, files
[Email protected] boot]#Vim/mnt/boot/grub/grub.conf#4 #New grub.conf File
Default=0
Timeout=5
Title Fake Mylinux
Root (hd0,0)
Kernel/vmlinuz
Initrd/initrd.img
[[email protected] boot]# sync # must be synchronized multiple times
[[email protected] boot]# umount/mnt/boot/ # cannot uninstall itself on/boot
Umount:/mnt/boot:device is busy
Umount:/mnt/boot:device is busy
[[Email protected] boot]# CD
[[email protected] ~]# umount/mnt/boot/ # switch after uninstall succeeded
Create a new virtual machine and connect to the IDE hard drive : (HD2)/dev/sdC, then start the corresponding system of the hard drive.
4, grub.conf file lost when entering the system command, detailed as attached to the diagram
Grub> Find
grub> Root (Hd#,n)
grub> Kernel/vmlinuz-2.6.18-308.el5 ro root=label=/
Grub> initrd/initrd-2.6.18-308.el5.img
grub> Boot
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/8A/5F/wKiom1gurLnzbuviAAKkL6ZhJf4454.png-wh_500x0-wm_3 -wmp_4-s_612661821.png "title=" Grub.conf-bad.png "alt=" Wkiom1gurlnzbuviaakkl6zhjf4454.png-wh_50 "/>
5, the process of kernel initialization:
1, equipment detection
2. Driver initialization (May load driver module from INITRD (INITRAMFS) file)
3. Mount the root file system with read-only loading;
4. Loading the first process init (pid:1)
Id:runlevels:action:process
ID: identifier
Runlevels: At which level this line runs;
Action: Under what circumstances the line is executed;
Process: to run the program;
Id:3:initdefault:
Si::sysinit:/etc/rc.d/rc.sysinit
ACTION:
Initdefault: Setting the default Run level
Sysinit: System Initialization
Wait: Execution when the waiting level switches to this level
Respawn: Once the program is terminated, it restarts
Tasks completed by/etc/rc.d/rc.sysinit:
1. Activate Udev and SELinux;
2, according to the/etc/sysctl.conf file, to set the kernel parameters;
3, set the clock clock;
4, loading keyboard mapping;
5, enable the swap partition;
6, set the host name;
7, the root file system detection, and read-write mode to re-mount;
8. Activate RAID and LVM devices;
9, enable disk quotas;
10, according to/etc/fstab, check and mount other file system;
11, clean up the expired lock and PID file;
Service control methods in/etc/inittab:
For I in/etc/rc3.d/k*; Do
$I stop
Done
For I in/etc/rc3.d/s*; Do
$I start
Done
# #: The priority of closing or starting, the smaller the data the higher the priority is selected
First, the service starting with K is closed and the service starts with S.
6. Use the lock file to see if the service is open and learn how to control it .
#!/bin/bash
#
# Chkconfig: 2345 77 22
# Description: Test Service
Lockfile=/var/lock/subsys/myservice
STATUS (){
If [-e $LOCKFILE];then
echo "Running ..."
Else
echo "Stopping ..."
Fi
}
USAGE (){
echo "' basename $ ' {start|stop|restart|status}"
}
Case $ in
Start
echo "Starting ..."
Touch $LOCKFILE;;
Stop
echo "Stopping ..."
Rm-f $LOCKFILE &>/dev/null;
Restart
echo "Restarting ...";;
Status
STATUS;;
*)
USAGE;;
Esac
[[email protected] etc]#./myservice.sh start
Startin ...
[Email protected] etc]#./myservice.sh status
Running ...
[[email protected] etc]#./myservice.sh stop
Stopping ...
[Email protected] etc]#./myservice.sh status
Stopping ...
[Email protected] etc]#./myservice.sh
myservice.sh {Start|stop|restart|status}
[Email protected] etc]#
[Email protected] init.d]# CP Myservice.sh/etc/rc.d/init.d/myservice
[[email protected] init.d]# chkconfig --list httpd # View httpd Services
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[[email protected] rc.d]# find /etc/rc.d/-name "*myservice*" # View no this service
/etc/rc.d/init.d/myservice
[[email protected] rc.d]# chkconfig--add myservice # auto Create service link
[[email protected] rc.d]# find /etc/rc.d/-name "*myservice*"
/etc/rc.d/init.d/myservice
/etc/rc.d/rc6.d/k22myservice
/etc/rc.d/rc0.d/k22myservice
/etc/rc.d/rc5.d/s77myservice
/etc/rc.d/rc1.d/k22myservice
/etc/rc.d/rc3.d/s77myservice
/etc/rc.d/rc2.d/s77myservice
/etc/rc.d/rc4.d/s77myservice
[Email protected] rc.d]#chkconfig--delMyService#Automatically Delete Service links
[Email protected] rc.d]# find/etc/rc.d/-name "*myservice*"
/etc/rc.d/init.d/myservice
[Email protected] rc.d]# chkconfig--list myservice
MyService 0:off 1:off2:on 3:on 4:on 5:on6:off
[Email protected] rc.d]# Chkconfig--levelMyService off#Modify to specify which levels are closed
[Email protected] rc.d]# chkconfig--list myservice
MyService 0:off 1:off2:off3:on 4:off 5:on6:off
[[email protected] rc.d]# cat/etc/rc.d/rc.local #S99, the last program to start
#!/bin/sh
#
# This script is executed *after* all and the other init scripts.
# can put your own initialization stuff in here if you don ' t
# want to does the full Sys V style init stuff.
Touch/var/lock/subsys/local
The last line in this file rc.local the command to execute the required program.
Script Learning Example : Cat/etc/rc.d/rc.sysinit cat/etc/inittab
---end---
Grub of Linux system startup process