abstrakt:什么是GRUB GNU GRUB 是一個(gè)多重操作系統(tǒng)啟動(dòng)管理器。GNU GRUB 是由GRUB(GRand Unified Bootloader) 派生而來。GRUB 最初由Erich Stefan Boleyn 設(shè)計(jì)和應(yīng)用; 系統(tǒng)啟動(dòng)引導(dǎo)管理器,是在計(jì)算機(jī)啟動(dòng)后運(yùn)行的第一個(gè)程序,他是用來負(fù)責(zé)加載、傳輸控制到操作系統(tǒng)的內(nèi)核,一旦把內(nèi)核掛載,系統(tǒng)引導(dǎo)管理器的任務(wù)就
什么是GRUB
GNU GRUB 是一個(gè)多重操作系統(tǒng)啟動(dòng)管理器。GNU GRUB 是由GRUB(GRand Unified Bootloader) 派生而來。GRUB 最初由Erich Stefan Boleyn 設(shè)計(jì)和應(yīng)用;
系統(tǒng)啟動(dòng)引導(dǎo)管理器,是在計(jì)算機(jī)啟動(dòng)后運(yùn)行的第一個(gè)程序,他是用來負(fù)責(zé)加載、傳輸控制到操作系統(tǒng)的內(nèi)核,一旦把內(nèi)核掛載,系統(tǒng)引導(dǎo)管理器的任務(wù)就算完成退出,系統(tǒng)引導(dǎo)的其它部份,比如系統(tǒng)的初始化及啟動(dòng)過程則完全由內(nèi)核來控制完成。
Linux中GRUB的配置文件
[root@localhost ~]#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、破壞grub前400b: (grub占用446b,只破壞部分grub)
[root@localhost ~]# dd if=/dev/zero of=/dev/sda count=1 bs=400
1+0 records in
1+0 records out
400 bytes (400 B) copied, 5.3e-05 seconds, 7.5 MB/s
[root@localhost ~]# sync
[root@localhost ~]# sync
2、修復(fù)grub: ( 操作第一步后未退出該Linux系統(tǒng) )
[root@localhost ~]# grub #進(jìn)入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) #第一個(gè)硬盤hd0,boot在第一個(gè)分區(qū)上0
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0) #set表示安裝grub
setup (hd0)
Checking if "/boot/grub/stage1" exists... no #exists存在,no表示該項(xiàng)被破壞了
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)"... 15 sectors are embedded.
succeeded #安裝grub成功
Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
[root@localhost ~]# vi /etc/grub.conf
...
default=0
timeout=15
splashimage=(hd0,0)/grub/splash.xpm.gz
#hiddenmenu
password --md5 $1$Zu2p2/$QAIN26LbJRJITA622oUZU.
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、安裝grub第二種方式:
可指定任意設(shè)備,安裝前做好分區(qū),有分區(qū)表,將boot目錄掛載好
# grub -install--root-directory=/path/to/boot's_parent_dir /PATH/TO/DEVICE
[root@localhost ~]# mke2fs -j /dev/sdc1 #1#格式化硬盤
[root@localhost ~]# mke2fs -j /dev/sdc2
[root@localhost ~]# mkswap /dev/sdc3
Setting up swapspace version 1, size = 139825 kB
[root@localhost ~]# mkdir /mnt/boot
[root@localhost ~]# mount /dev/sdc1 /mnt/boot/ #2#映射boot分區(qū)
[root@localhost ~]# mount
/dev/sdc1 on /mnt/boot type ext3 (rw)
[root@localhost ~]# grub-install --root-directory=/mnt /dev/sdc #3#自動(dòng)在/mnt下找boot
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. #已創(chuàng)建grub
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 #grub中硬盤的映射關(guān)系
(hd1) /dev/sdb
(hd2) /dev/sdc
[root@localhost ~]# cd /mnt/boot/
[root@localhost boot]# tree #以下為grub-install生成的文件自動(dòng)放在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, 13 files
[root@localhost boot]# vim /mnt/boot/grub/grub.conf #4#新建grub.conf文件
default=0
timeout=5
title Fake Mylinux
root (hd0,0)
kernel /vmlinuz
initrd /initrd.img
[root@localhost boot]# sync #必須多次同步
[root@localhost boot]# umount /mnt/boot/ #在/boot上無法卸載自己
umount: /mnt/boot: device is busy
umount: /mnt/boot: device is busy
[root@localhost boot]# cd
[root@localhost ~]# umount /mnt/boot/ #切換后卸載成功
新建虛擬機(jī),并連接這個(gè)IDE硬盤:(hd2) /dev/sdc,再啟動(dòng)該硬盤對(duì)應(yīng)的系統(tǒng)即可。
4、grub.conf文件丟失時(shí)進(jìn)入系統(tǒng)命令,詳如附圖
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
5、Kernel初始化的過程:
1、設(shè)備探測(cè)
2、驅(qū)動(dòng)初始化(可能會(huì)從initrd(initramfs)文件中裝載驅(qū)動(dòng)模塊)
3、以只讀掛載根文件系統(tǒng);
4、裝載第一個(gè)進(jìn)程init(PID:1)
id:runlevels:action:process
id: 標(biāo)識(shí)符
runlevels: 在哪個(gè)級(jí)別運(yùn)行此行;
action: 在什么情況下執(zhí)行此行;
process: 要運(yùn)行程序;
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
ACTION:
initdefault: 設(shè)定默認(rèn)運(yùn)行級(jí)別
sysinit: 系統(tǒng)初始化
wait: 等待級(jí)別切換至此級(jí)別時(shí)執(zhí)行
respawn: 一旦程序終止,會(huì)重新啟動(dòng)
/etc/rc.d/rc.sysinit完成的任務(wù):
1、激活udev和selinux;
2、根據(jù)/etc/sysctl.conf文件,來設(shè)定內(nèi)核參數(shù);
3、設(shè)定時(shí)鐘時(shí)鐘;
4、裝載鍵盤映射;
5、啟用交換分區(qū);
6、設(shè)置主機(jī)名;
7、根文件系統(tǒng)檢測(cè),并以讀寫方式重新掛載;
8、激活RAID和LVM設(shè)備;
9、啟用磁盤配額;
10、根據(jù)/etc/fstab,檢查并掛載其它文件系統(tǒng);
11、清理過期的鎖和PID文件;
/etc/inittab中服務(wù)控制方法:
for I in /etc/rc3.d/K*; do
$I stop
done
for I in /etc/rc3.d/S*; do
$I start
done
##: 關(guān)閉或啟動(dòng)的優(yōu)先次序,數(shù)據(jù)越小越優(yōu)先被選定
先關(guān)閉以K開頭的服務(wù),后啟動(dòng)以S開頭的服務(wù);
6、使用鎖文件,查看服務(wù)是否開起來,學(xué)習(xí)其控制方法
#!/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 $0` {start|stop|restart|status}"
}
case $1 in
start)
echo "Starting..."
touch $LOCKFILE;;
stop)
echo "Stopping..."
rm -f $LOCKFILE &> /dev/null;;
restart)
echo "Restarting..." ;;
status)
STATUS;;
*)
USAGE;;
esac
[root@localhost etc]# ./myservice.sh start
Startin...
[root@localhost etc]# ./myservice.sh status
Running...
[root@localhost etc]# ./myservice.sh stop
Stopping...
[root@localhost etc]# ./myservice.sh status
Stopping...
[root@localhost etc]# ./myservice.sh
myservice.sh {start|stop|restart|status}
[root@localhost etc]#
[root@localhost init.d]# cp myservice.sh /etc/rc.d/init.d/myservice
[root@localhost init.d]# chkconfig --list httpd #查看httpd服務(wù)
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost rc.d]# find /etc/rc.d/ -name "*myservice*" #查看無此服務(wù)
/etc/rc.d/init.d/myservice
[root@localhost rc.d]# chkconfig --add myservice #自動(dòng)創(chuàng)建服務(wù)鏈接
[root@localhost 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
[root@localhost rc.d]# chkconfig --del myservice #自動(dòng)刪除服務(wù)鏈接
[root@localhost rc.d]# find /etc/rc.d/ -name "*myservice*"
/etc/rc.d/init.d/myservice
[root@localhost rc.d]# chkconfig --list myservice
myservice 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost rc.d]# chkconfig --level 24 myservice off #修改指定哪些級(jí)別關(guān)閉
[root@localhost rc.d]# chkconfig --list myservice
myservice 0:off 1:off 2:off 3:on 4:off 5:on 6:off
[root@localhost rc.d]# cat /etc/rc.d/rc.local #S99,最后一個(gè)啟動(dòng)的程式
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
在此文件rc.local中最后一行加入命令即可執(zhí)行所需要的程式。