歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> CentOS Grub、BASH 故障、解決方法

CentOS Grub、BASH 故障、解決方法

日期:2017/2/28 13:51:22   编辑:Linux教程

簡介:

Grub 常見的兩種故障:Grub.conf 文件丟失、MBR 損壞 ( 不管恢復怎麼樣,還是先備份好吧 )

一、Grub.conf 文件丟失

shell > rm -rf /boot/grub/grub.conf ## 刪除文件、模擬故障
shell > reboot

## 故障現象如下:

GNU GRUB verstion 0.97 (634k lower / 1046400k 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>

## 就這個樣子,無法登陸系統,下面是解決方法

grub> root (hd0,) # 輸入 root (hd0,) 這時按 TAB 鍵會打印出系統上所有的分區 ( 注意在急救模式下不支持 TAB , 很傷~ )
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

## 0, 1, 2 代表分區編號,上面顯示有文件系統,分區類型,像 83 就是 Linux 普通分區,82 是虛擬分區 SWAP ( 硬盤分區時的知識 )
## 這裡不分 IDE / SATA 什麼的,都用 hd 代表,hd0 代表第一塊硬盤,hd0,0 代表第一塊硬盤的第一個分區,系統中是用 sda1 代表第一塊硬盤的第一個分區

grub> root (hd0,0)/ # 輸入 root (hd0,0)/ 這裡按 TAB 鍵搜索內核在不在此分區上 ( boot 分區 ),如果有會輸出下面信息 ( 試 83 的分區,82 是交換分區就沒必要試了 )
Possible files are: lost+found grub efi .VMlinuz-2.6.32-504.e16.x86_64.hmac System.map-2.6.32-504.e16.x86_64 config-2.6.32-504.x86_64 symvers-2.6.32-504.x86_64.gz VMlinuz-2.6.32-504.e16.x86_64 initramfs-2.6.32-504.e16.x86_64.img

## 如果可以看到 vmlinuz- 跟 initramfs- 字樣,說明這就是我們想要的東西

grub> root (hd0,2)/ # 下面是搜不到的顯示, 仔細看看 ( 發現這是根分區哎~~ hd0,2 是根分區,我們用的是 SATA , 所以根分區是 /dev/sda3 很有用 !)
Possible files are: lost+found boot dev proc sys var tmp etc root selinux lib64 usr bin home lib media mnt opt sbin srv .autorelabel

grub> root (hd0,0) # 綁定啟動分區
Filesystem type is ext2fs, partition type 0x83

grub> kernel /VMlinuz-2.6.32-504.e16.x86_64 ro root=/dev/sda3 # 指定啟動內核,ro 只讀模式,root= 指定根分區, 一定要指對 ( 可能如果 boot 沒有單獨分區不指定也是可以的 )

grub> initrd /initramfs-2.6.32-504.e16.x86_64.img # 這是內核映像吧 ( kernel 、initrd 都是上面搜出來的,輸入一點點按 TAB 可以補全 )

grub> boot # 啟動系統,手動編寫 grub.conf 或恢復備份

shell > vim /boot/grub/grub.conf

default=0 # 啟動哪個系統
timeout=5 # 等待時間
title CentOS 6.6 # 描述信息
kernel /VMlinuz-2.6.32-504.e16.x86_64 ro root=/dev/sda3 # 內核
initrd /initramfs-2.6.32-504.e16.x86_64.img # 啟動映像

## 這裡有個 vim 的用法,直接主機上 vim 是不可以用鼠標復制命令行內容的,那 kernel 跟 initrd 又那麼長,不好記
## 方法:末行模式下輸入 .! ls /boot/vmlinuz- 按 TAB 就可以補全了,這時按 Enter 鍵就可以將此條信息放到當前文件中

shell > reboot

## 這樣就恢復成功了!還是記得備份吧

## 如果 kernel 沒有指對根分區就會這樣~ 還要重啟系統,重新來過

dracut Warning: Signal caught!
?dracut Warning: Boot has faild. To debug this issue add "rdshell" to the kernel command line.
?Kernel panic - not syncing: Attempted to kill init!
?Pid: 1, comm: init Not tainted 2.6.32-504.e16.x86_64 #1
?Call Trace:
[<ffffffff815292bc] ? panic+0xa7/0x16f
? [<ffffffff815292bc] ? do_exit+0xa7/0x16f
? [<ffffffff815292bc] ? fput+0xa7/0x16f
? [<ffffffff815292bc] ? system_call_fastpath+0xa7/0x16f?
?

二、MBR 損壞

shell > dd if=/dev/zero of=/dev/sda count=1 bs=200 # 覆蓋 MBR , 模擬故障 ( /dev/sda 前 512 個字節是 MBR )
shell > reboot

## 故障現象如下:

Operating System not found # 基本就這樣,會出現本地硬盤找不到系統、從 PXE 獲取,DHCP xxxxxxxxxxxxx

## 解決方法:使用緊急救援模式修復

> 插入系統光盤,進入急救模式 ( 這個都會進吧,CentOS 直接選擇,RedHat boot:linux rescue )

shell > grub # 進入 grub 模式

grub> root (hd0,0) # 直接指定內核所在分區吧,沒法 TAB ( 一定要想辦法找到它, 例如:先 chroot /mnt/sysimage , 然後 df -HT 可以看到所有分區,假如 sda1 是啟動分區,那麼這裡對應就是 hd0,0 了 )

grub> setup (hd0) # 重新安裝 MBR 到該磁盤,實際上就是 /dev/sda

grub> quit

shell > reboot

## 現在就可以成功進入系統了,MBR 已經修復

附加:BASH 故障的解決方法

shell > rm -rf /bin/bash # 刪除 bash ,模擬故障
shell > reboot

## 故障現象如為:系統無法初始化,init 命令不存在,其實什麼指令都不能執行
## 進入急救模式重新安裝 bash

shell > mkdir /media # 直接在光盤系統中操作,因為這時無法 chroot 到 /mnt/sysimage
shell > mount /dev/sr0 /media # 掛載光盤鏡像,/dev/sr0 代表光驅,如果是 IDE 接口應該是 /dev/hdc
shell > cd /media/Packages
shell > rpm -ivh --replacepkgs --root /mnt/sysimage bash-4.1.2-29.e16.x86_64.rpm # 將 bash 安裝到 --root 指定根目錄中 --replacepkgs 是替換原有包
shell > reboot

## OK ,故障已經修復!

Copyright © Linux教程網 All Rights Reserved