Bootloader 的作用是加載內(nèi)核到內(nèi)存,使內(nèi)核開始執(zhí)行,Grub 是 linux 上面一個功能強大的 bootloader,當我們登陸系統(tǒng)就會看到如下界面,它就是 Grub 的 menu.lst,通過它我們可以選擇不同的系統(tǒng)(多操作系統(tǒng)時)
(這里介紹的是 grub,ubantu 使用的是 grub2,兩者存在很多差異)
http://wiki.jikexueyuan.com/project/learn-linux-step-by-step/images/1.bmp" alt="" />
menu.lst 是 Grub 的開機菜單,里面的配置決定了我們?nèi)ツ睦镒x取內(nèi)核與 initrd
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-371.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-371.el5 ro root=LABEL=/ rhgb quiet rgb=0x317
initrd /initrd-2.6.18-371.el5.img
default: 默認啟動項這個與 title 對照,menu 中配置了幾個 title,啟動菜單就有幾個選擇,0代表使用第一個 title 內(nèi)容
timeout:啟動是的倒數(shù)讀秒操作,-1代表不進行倒數(shù)讀秒
splashimage:menu.lst 的背景圖片
hiddenmenu:隱藏菜單
root:代表內(nèi)核文件放置那個分區(qū),不是根目錄的意思
kernel:后面接內(nèi)核文件名,在后面指定根目錄掛載到那個分區(qū)
initrd:后面接虛擬文件系統(tǒng)文件名(其實就是指定它的位置)
(hd num1,num2): hd 代表在 grub 中硬盤與分區(qū)的代號,num1 代表硬盤代號(0開啟)。Num2 代表分區(qū)號(0開始).比如:內(nèi)核文件存儲在第一塊硬盤的 D 分區(qū)(第2個分區(qū)),可以表示為(hd 0,1)
舉例:menu.lst 配置說明
[root@localhost ~]# find / -name vmlinuz-2.6.1*;df
/boot/vmlinuz-2.6.18-371.el5
文件系統(tǒng) 1K-塊 已用 可用 已用% 掛載點
/dev/sda1 101086 11727 84140 13% /boot
通過上面我們可以看到內(nèi)核文件存在/boot/vmlinuz-2.6.18-371.el5下,同時/boot 掛載到硬盤的第一分區(qū),因此內(nèi)核文件存儲位置可以寫成(hd 0,0)
root (hd0,0)
kernel /vmlinuz-2.6.18-371.el5 ro root=LABEL=/ rhgb quiet rgb=0x317
initrd /initrd-2.6.18-371.el5.img
由于前面指定了 root 了因此后面的 kernel,initrd 只需寫接下來的路徑就可以了如:/vmlinuz-2.6.18-371.el5,接下來為根據(jù) LABEL 掛載根目錄到分區(qū) root=LABEL=/
同樣上面配置也可以寫成這樣
kernel (hd0,0)/vmlinuz-2.6.18-371.el5 ro root=LABEL=/ rhgb quiet rgb=0x317
initrd (hd0,0)/initrd-2.6.18-371.el5.img
我們知道 boot loader 裝在 MBR 或者分區(qū)的第1扇區(qū)中,chain loader 功能就是將控制權交給指定分區(qū)的 bootloader 讓其進行加載相應的內(nèi)核文件
title /dev/sda1 boot sector
root (hd0,0)
chainloader +1
比如我們的 LINUX 系統(tǒng)的 bootloader 裝在了第1個硬盤第1個分區(qū),那 bootloader 的位置就是第1塊硬盤的第一個分區(qū)的第一扇區(qū),因此
root (hd0,0)指定分區(qū)與磁盤,這里是第一個磁盤的第一個分區(qū)
chainloader +1 指定為第一扇區(qū)
同樣假如我們 LINUX 系統(tǒng)的 bootloader 再在整個硬盤的 MBR 中,那可以這么指定
title MBR loader
root (hd0)
chainloader +1
由于 MBR 位置為硬盤的一個扇區(qū),因此
root (hd0)指定第一個硬盤
chainloader +1指定為第一扇區(qū)
如果想讓一臺機器上存在多個操作系統(tǒng)可以通過控制權轉移將控制權交給指定分區(qū)的 loader 進行加載相應的操作系統(tǒng)
假如,我的機器只有一個硬盤,我想在第1分區(qū)裝 WINXP,第2個分區(qū)裝 linux,那個就可以在 menu.list 中設置2個選項,第1個選項為 winxp,第2個選項為 linux,當選擇第一個時控制權交給第1分區(qū)的 bootloader,當選擇第2分區(qū)時將控制權交給第2個分區(qū)的 bootloader 即 linux 的 loader
但是這里需要先安裝 WINXP 在安裝 LINUX 因為 window 不具有控制權轉移功能
Grub 安裝分為3個步驟
步驟1:grub 配置文件安裝
語法:grub-install[--root-directory=DIR] 設備代號
選項與參數(shù)
--root-directory:當指定 DIR 是,grub 配置文件安裝在 DIR/boot/grub
如不指定此屬性,此默認安裝在/boot/grub
[root@localhost ~]# grub-install /dev/sda
Installation finished. No error reported.
This is the contents of the device map /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'.
# this device map was generated by anaconda
(hd0) /dev/sda
[root@localhost lib]# ll /boot/grub/
-rw-r--r-- 1 root root 7584 03-31 10:52 e2fs_stage1_5
-rw-r--r-- 1 root root 7456 03-31 10:52 fat_stage1_5
步驟2:編寫 menu.list
[root@localhost lib]# vim /boot/grub/menu.lst
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-371.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-371.el5 ro roo
initrd /initrd-2.6.18-371.el5.img
步驟3:grup 主程序安裝到 MBR 或分區(qū)第一扇區(qū)
Grubshell 的簡單語法
root(hdx,x):選擇含有 grub 目錄的那個分區(qū)
find 文件路徑,
find 路徑/stage1 查找是否有安裝信息
find 路徑/vmlinuz…. 查找內(nèi)核文件
setup(hdx,x) 安裝 grub 到分區(qū)的第1扇區(qū)
setup(hd 0) 安裝 grub 到 MBR 中
[root@localhost /]# grub =>進入grub shell
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)
Filesystem type is ext2fs, partition type 0x83
grub> find /vmlinuz-2.6.18-371.el5
(hd0,0)
grub> setup (hd0) => 安裝到MBR中
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)"... 15 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> setup (hd0,0) 安裝到sector中
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,0)"... failed (this is not fatal)
Running "embed /grub/e2fs_stage1_5 (hd0,0)"... failed (this is not fatal)
Running "install /grub/stage1 (hd0,0) /grub/stage2 p /grub/grub.conf "... succeeded
Done.
grub> quit
http://wiki.jikexueyuan.com/project/learn-linux-step-by-step/images/2.bmp" alt="" />
開機后按下 e 進入 menu.lst 編輯模式
http://wiki.jikexueyuan.com/project/learn-linux-step-by-step/images/3.bmp" alt="" />
編輯 Kernel 信息。指定為單用戶模式
http://wiki.jikexueyuan.com/project/learn-linux-step-by-step/images/4.bmp" alt="" />
按 Esc 返回剛才那個頁面,按下 b, 此時系統(tǒng)會給你個 root 權限的 shell.使用 passwd 命令修改密碼即可