在linux系統(tǒng)中可以執(zhí)行grub命令。進入grub的命令界面,在這里面可以使用一些常用的命令。也可以對不太了解的命令進行幫助查看。可以使用TAB鍵對命令進行補全操作,系統(tǒng)會給予提示信息。如下:
[root@RHEL5 ~]# 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> help
blocklist FILE?????????????????????????boot
cat FILE???????????????????????????????chainloader [--force] FILE
clear??????????????????????????????????color?NORMAL?[HIGHLIGHT]
configfile FILE????????????????????????device DRIVE DEVICE
displayapm?????????????????????????????displaymem
find FILENAME??????????????????????????geometry DRIVE [CYLINDER HEAD SECTOR [
halt [--no-apm]????????????????????????help [--all] [PATTERN ...]
hide PARTITION?????????????????????????initrd FILE [ARG ...]
kernel [--no-mem-option] [--type=TYPE] makeactive
map TO_DRIVE FROM_DRIVE????????????????md5crypt
module FILE [ARG ...]??????????????????modulenounzip FILE [ARG ...]
pager [FLAG]???????????????????????????partnew PART TYPE START LEN
parttype PART TYPE?????????????????????quit
reboot?????????????????????????????????root [DEVICE [HDBIAS]]
rootnoverify [DEVICE [HDBIAS]]?????????serial [--unit=UNIT] [--port=PORT] [--
setkey [TO_KEY FROM_KEY]???????????????setup [--prefix=DIR] [--stage2=STAGE2_
terminal [--dumb] [--no-echo] [--no-ed terminfo [--name=NAME --cursor-address
testvbe MODE???????????????????????????unhide PARTITION
uppermem KBYTES????????????????????????vbeprobe [MODE]
grub> help root
root: root [DEVICE [HDBIAS]]
Set the current "root device" to the device DEVICE, then attempt
to mount it to get the partition size (for passing the partition
descriptor in `ES:ESI', used by some chain-loaded bootloaders),
the BSD drive-type (for booting BSD kernels using their native
boot format), and correctly determine??the PC partition where a
BSD sub-partition is located. The optional HDBIAS parameter is a
number to tell a BSD kernel how many BIOS drive numbers are on
controllers before the current one. For example, if there is an
IDE disk and a SCSI disk, and your FreeBSD root partition is on
the SCSI disk, then use a `1' for HDBIAS.
rootnoverify: rootnoverify [DEVICE [HDBIAS]]
Similar to `root', but don't attempt to mount the partition. This
is useful for when an OS is outside of the area of the disk that
GRUB can read, but setting the correct root device is still
desired. Note that the items mentioned in `root' which derived
from attempting the mount will NOT work correctly.
grub>
命令格式:
setup [--prefix=DIR] [--stage2=STAGE2_FILE] [--force-lba] INSTALL_DEVICE [IMAGE_DEVICE]
INSTALL_DEVICE:指定安裝grub的位置。
IMAGE_DEVICE:指定要安裝的鏡象文件。
例如:
[root@RHEL5 ~]# 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,)
Possible partitions are:
Partition num: 0,??Filesystem type is ext2fs, partition type 0x83
Partition num: 1,??Filesystem type unknown, partition type 0x82
Partition num: 2,??Filesystem type is ext2fs, partition type 0x83
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> 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)"...??15 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub>
?3.grub-install命令
grub-install拷貝grub鏡象文件到DIR/boot目錄中(可以通過參數(shù)--root-directory指定目錄),并用grub shell安裝grub到MBR中。
命令格式:
grub-install [OPTION] install_device
例如:
[root@RHEL5 ~]# grub-install hd0
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@RHEL5 ~]#
首先使用grub-install命令對已安裝好的系統(tǒng)進行重新安裝grub(因為系統(tǒng)默認的安裝方式是用install安裝的)。[root@RHEL5 ~]# grub-install hd0Installation 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@RHEL5 ~]#執(zhí)行完成之后,使用rm命令刪除/boot/grub下的stage2文件,重啟系統(tǒng)。會看到有如下的錯誤出現(xiàn):?? 這個時候我們只能進入linux rescue模式對系統(tǒng)進行修復了,而且只可以使用grub-install命令對系統(tǒng)進行修復。因為/boot/grub下的stage2文件已經(jīng)被我們刪掉了。原因是的安裝源已被破壞。這個時候插入光盤,設置為cdrom啟動,輸入linux rescue回車進入rescue模式中。掛起你的系統(tǒng)到/mnt/sysimage目錄下。執(zhí)行命令grub-install hd0命令來重新安裝grub系統(tǒng)可以正常啟動。具體操作如下: ?回車??? 選配置繼續(xù)。(幾步的語言選擇和網(wǎng)絡設置省略)?? 選擇ok后,出現(xiàn)sh-3.2#后,執(zhí)行以下操作。?? 完成后重新啟動系統(tǒng)。可以看到系統(tǒng)正常啟動成功了。?備注:那么在什么情況下使用grub-install命令來重新安裝grub呢?1:grub被擦掉的時候。2:/boot/grub目錄下的所有鏡像文件都給刪除時。?2:使用grub-install命令安裝的grub且boot/grub目錄下stage2文件被誤刪除時(有人會考慮到把/usr/share/grub/i386-redhat/下的stage2拷貝到/boot/grub目錄中,此方法是不成功的)。
?2)?setup命令測試
使用setup命令重新安裝grub到MBR中。[root@RHEL5 ~]# grubProbing 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)?Filesystem type is ext2fs, partition type 0x83grub> 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)"...??15 sectors are embedded.succeeded?Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeededDone.grub> quit[root@RHEL5 ~]#使用rm命令刪除/boot/grub/目錄中的stage2文件重啟系統(tǒng)。系統(tǒng)仍然可以啟動,而且重新生成了stage2文件。如下:[root@RHEL5 grub]# cd[root@RHEL5 ~]# cd /boot//grub/[root@RHEL5 grub]# ls stage2stage2[root@RHEL5 grub]#現(xiàn)在使用shred命令刪除stage2文件,再使用rm命令把stage2文件刪除。重新啟動系統(tǒng)。[root@RHEL5 grub]# shred -v stage2shred: stage2: pass 1/25 (random)...shred: stage2: pass 2/25 (ffffff)...shred: stage2: pass 3/25 (492492)...shred: stage2: pass 4/25 (888888)...shred: stage2: pass 5/25 (924924)...shred: stage2: pass 6/25 (249249)...shred: stage2: pass 7/25 (cccccc)...shred: stage2: pass 8/25 (222222)...shred: stage2: pass 9/25 (db6db6)...shred: stage2: pass 10/25 (777777)...shred: stage2: pass 11/25 (999999)...shred: stage2: pass 12/25 (eeeeee)...shred: stage2: pass 13/25 (random)...shred: stage2: pass 14/25 (dddddd)...shred: stage2: pass 15/25 (000000)...shred: stage2: pass 16/25 (555555)...shred: stage2: pass 17/25 (111111)...shred: stage2: pass 18/25 (444444)...shred: stage2: pass 19/25 (aaaaaa)...shred: stage2: pass 20/25 (333333)...shred: stage2: pass 21/25 (bbbbbb)...shred: stage2: pass 22/25 (b6db6d)...shred: stage2: pass 23/25 (666666)...shred: stage2: pass 24/25 (6db6db)...shred: stage2: pass 25/25 (random)...[root@RHEL5 grub]#rm –rf stage2[root@RHEL5 grub]#reboot這個時候系統(tǒng)不能再啟動了,出現(xiàn)如下顯示:現(xiàn)在要恢復系統(tǒng)的話,只能是使用grub-install命令對系統(tǒng)進行恢復。修復方法與上面相同。
?3)?install命令測試
使用install命令重新寫MBR。如下:[root@RHEL5 ~]# grubProbing 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)?Filesystem type is ext2fs, partition type 0x83grub> install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.confgrub> quit[root@RHEL5 ~]#使用rm命令刪除/boot/grub/目錄中的stage2文件重啟系統(tǒng)。系統(tǒng)仍然可以啟動,而且重新生成了stage2文件。如下:[root@RHEL5 grub]# cd[root@RHEL5 ~]# cd /boot//grub/[root@RHEL5 grub]# ls stage2stage2[root@RHEL5 grub]#現(xiàn)在使用shred命令刪除stage2文件,再使用rm命令把stage2文件刪除。重新啟動系統(tǒng)。[root@RHEL5 grub]# shred -v stage2shred: stage2: pass 1/25 (random)...shred: stage2: pass 2/25 (ffffff)...shred: stage2: pass 3/25 (492492)...shred: stage2: pass 4/25 (888888)...shred: stage2: pass 5/25 (924924)...shred: stage2: pass 6/25 (249249)...shred: stage2: pass 7/25 (cccccc)...shred: stage2: pass 8/25 (222222)...shred: stage2: pass 9/25 (db6db6)...shred: stage2: pass 10/25 (777777)...shred: stage2: pass 11/25 (999999)...shred: stage2: pass 12/25 (eeeeee)...shred: stage2: pass 13/25 (random)...shred: stage2: pass 14/25 (dddddd)...shred: stage2: pass 15/25 (000000)...shred: stage2: pass 16/25 (555555)...shred: stage2: pass 17/25 (111111)...shred: stage2: pass 18/25 (444444)...shred: stage2: pass 19/25 (aaaaaa)...shred: stage2: pass 20/25 (333333)...shred: stage2: pass 21/25 (bbbbbb)...shred: stage2: pass 22/25 (b6db6d)...shred: stage2: pass 23/25 (666666)...shred: stage2: pass 24/25 (6db6db)...shred: stage2: pass 25/25 (random)...[root@RHEL5 grub]#rm –rf stage2[root@RHEL5 grub]#reboot這個時候系統(tǒng)不能再啟動了,出現(xiàn)如下顯示:
?2.boot為非獨立分區(qū)
該測試系統(tǒng)分區(qū)如下:[root@rhel5 grub]# Last login: Tue Apr 28 08:02:03 2009 from 192.168.88.81[root@rhel5 ~]# fdisk -lDisk /dev/sda: 21.4 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes???Device Boot??????Start?????????End??????Blocks???Id??System/dev/sda1???*???????????1????????2544????20434648+??83??Linux/dev/sda2????????????2545????????2609??????522112+??82??Linux swap / Solaris[root@rhel5 ~]#boot分區(qū)與根在同一分區(qū)中。grub-install命令與setup命令與boot為獨立分區(qū)時使用方法一樣。而install命令略有不同。就是在指定路徑的時候不能省略boot目錄。如下:grub>root (hd0,0)grub>install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/grub.conf?以下為測試結果:使用install命令重新寫MBR。如下:[root@RHEL5 ~]# grubProbing 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)?Filesystem type is ext2fs, partition type 0x83grub>install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/grub.confgrub> quit[root@RHEL5 ~]#使用rm命令刪除/boot/grub/目錄中的stage2文件重啟系統(tǒng)。系統(tǒng)仍然可以啟動。現(xiàn)在使用shred命令刪除stage2文件,再使用rm命令把stage2文件刪除。重新啟動系統(tǒng)。[root@RHEL5 grub]# shred -v stage2shred: stage2: pass 1/25 (random)...shred: stage2: pass 2/25 (ffffff)...shred: stage2: pass 3/25 (492492)...shred: stage2: pass 4/25 (888888)...shred: stage2: pass 5/25 (924924)...shred: stage2: pass 6/25 (249249)...shred: stage2: pass 7/25 (cccccc)...shred: stage2: pass 8/25 (222222)...shred: stage2: pass 9/25 (db6db6)...shred: stage2: pass 10/25 (777777)...shred: stage2: pass 11/25 (999999)...shred: stage2: pass 12/25 (eeeeee)...shred: stage2: pass 13/25 (random)...shred: stage2: pass 14/25 (dddddd)...shred: stage2: pass 15/25 (000000)...shred: stage2: pass 16/25 (555555)...shred: stage2: pass 17/25 (111111)...shred: stage2: pass 18/25 (444444)...shred: stage2: pass 19/25 (aaaaaa)...shred: stage2: pass 20/25 (333333)...shred: stage2: pass 21/25 (bbbbbb)...shred: stage2: pass 22/25 (b6db6d)...shred: stage2: pass 23/25 (666666)...shred: stage2: pass 24/25 (6db6db)...shred: stage2: pass 25/25 (random)...[root@RHEL5 grub]#rm –rf stage2[root@RHEL5 grub]#reboot這個時候系統(tǒng)不能再啟動了,出現(xiàn)如下顯示:同樣現(xiàn)在要恢復系統(tǒng)的話,只能是使用grub-install命令對系統(tǒng)進行恢復。恢復后系統(tǒng)就可以正常啟動了。
七、修復grub.conf
當系統(tǒng)中的grub.conf文件被刪除掉后。在沒有備份的情況下我們只能用grub交互模式來寫grub.conf文件讓系統(tǒng)重新啟動起來。正如下圖所示:這臺測試機的分區(qū)如下:?[root@rhel5 ~]# fdisk -l?Disk /dev/sda: 21.4 GB, 21474836480 bytes?255 heads, 63 sectors/track, 2610 cylinders?Units = cylinders of 16065 * 512 = 8225280 bytes???Device Boot??????Start?????????End??????Blocks???Id??System?/dev/sda1???*???????????1????????2544????20434648+??83??Linux?/dev/sda2????????????2545????????2609??????522112+??82??Linux???? swap / Solaris[root@rhel5 ~]#boot分區(qū)為非獨立分區(qū)。grub.conf文件內容如下:[root@rhel5 grub]# cat grub.confdefault=0timeout=5splashimage=(hd0,0)/boot/grub/splash.xpm.gzhiddenmenutitle Red Hat Enterprise Linux Server (2.6.18-92.el5)????????root (hd0,0)????????kernel /boot/vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet????????initrd /boot/initrd-2.6.18-92.el5.img現(xiàn)在嘗試用如下操作修復grub.conf文件來啟動系統(tǒng)。輸入完成后按回車開始正常啟動系統(tǒng)。也可以寫成下面這樣使系統(tǒng)可以正常啟動。不使用卷標指定根,直接使用設備名。正常進入系統(tǒng)之后在重新編寫grub.conf文件。