Boot Loader
Bootloader的作用是加載內核到內存,使內核開始執行,Grub是linux上面一個功能強大的bootloader,當我們登陸系統就會看到如下界面,它就是Grub的menu.lst,通過它我們可以選擇不同的系統(多操作系統時)
(這裏介紹的是grub,ubantu使用的是grub2,兩者存在很多差異)
功能介紹
menu .list
menu.lst是Grub的開機菜單,裏面的配置決定了我們去哪裏讀取內核與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內容
timeout:啓動是的倒數讀秒操作,-1代表不進行倒數讀秒
splashimage:menu.lst的背景圖片
hiddenmenu:隱藏菜單
root:代表內核文件放置那個分區,不是根目錄的意思
kernel:後面接內核文件名,在後面指定根目錄掛載到那個分區
initrd:後面接虛擬文件系統文件名(其實就是指定它的位置)
(hd num1,num2): hd代表在grub中硬盤與分區的代號,num1代表硬盤代號(0開啓)。Num2 代表分區號(0開始).比如:內核文件存儲在第一塊硬盤的D分區(第2個分區),可以表示爲(hd 0,1)
舉例:menu.lst配置說明
[root@localhost ~]# find / -name vmlinuz-2.6.1*;df /boot/vmlinuz-2.6.18-371.el5 文件系統 1K-塊 已用 可用 已用% 掛載點 /dev/sda1 101086 11727 84140 13% /boot
通過上面我們可以看到內核文件存在/boot/vmlinuz-2.6.18-371.el5下,同時/boot掛載到硬盤的第一分區,因此內核文件存儲位置可以寫成(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,接下來爲根據LABEL掛載根目錄到分區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
chain loader控制權轉移
我們知道boot loader裝在MBR或者分區的第1扇區中,chain loader功能就是將控制權交給指定分區的bootloader 讓其進行加載相應的內核文件
title /dev/sda1 boot sector root (hd0,0) chainloader +1
比如我們的LINUX系統的bootloader裝在了第1個硬盤第1個分區,那bootloader的位置就是第1塊硬盤的第一個分區的第一扇區,因此
root (hd0,0)指定分區與磁盤,這裏是第一個磁盤的第一個分區
chainloader +1 指定爲第一扇區
同樣假如我們LINUX系統的bootloader再在整個硬盤的MBR中,那可以這麼指定
title MBR loader root (hd0) chainloader +1
由於MBR位置爲硬盤的一個扇區,因此
root (hd0)指定第一個硬盤
chainloader +1指定爲第一扇區
多系統並存環境
如果想讓一臺機器上存在多個操作系統可以通過控制權轉移將控制權交給指定分區的loader進行加載相應的操作系統
假如,我的機器只有一個硬盤,我想在第1分區裝WINXP,第2個分區裝linux,那個就可以在menu.list中設置2個選項,第1個選項爲winxp,第2個選項爲linux,當選擇第一個時控制權交給第1分區的bootloader,當選擇第2分區時將控制權交給第2個分區的bootloader 即linux的loader
但是這裏需要先安裝WINXP在安裝LINUX因爲window不具有控制權轉移功能
Grub安裝
Grub安裝分爲3個步驟
1. grup配置文件安裝
2. menu.list文件編輯
3. grup 主程序安裝到MBR或分區第一扇區
步驟1:grub配置文件安裝
語法:grub-install[--root-directory=DIR] 設備代號
選項與參數
--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或分區第一扇區
Grubshell的簡單語法
root(hdx,x):選擇含有grub目錄的那個分區
find 文件路徑,
find 路徑/stage1 查找是否有安裝信息
find 路徑/vmlinuz…. 查找內核文件
setup(hdx,x) 安裝grub到分區的第1扇區
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
忘記root密碼解決
開機後按下e進入menu.lst編輯模式
編輯Kernel信息。指定爲單用戶模式
按Esc返回剛纔那個頁面,按下b, 此時系統會給你個root 權限的shell.使用passwd命令修改密碼即可