如何重新安装开机管理程序(GRUB)
   
    一、光盘重新安装开机管理程序(GRUB)
    
    
   
    
     请执行下列步骤:
     
     
     
     
    
   
- 
     
 用 CentOS 安装碟片开机(例如首张 CD 或 DVD)。
 
 
 
 
 
- 
 CentOS-4 或 CentOS-5 请在
 
 boot:
 
 提示输入
 
 linux rescue
 
 。CentOS-6 请从开机选单选择 Rescue installed system。
 
 
 
 
 
- 
     
 以读写模式挂载所有文件系统。
 
 
 
 
 
- 
 chroot 到你硬盘内的真正根目录(’/’)。
 
 
 # chroot /mnt/sysimage
 
 
 
 
 
- 
     
 重新安装开机管理程序(GRUB)。
 
 
 
 
 - 
 若果你要在 SCSI 硬盘或 SATA 硬盘的 MBR(/dev/sda)重新安装 GRUB:
 
 
 # grub-install /dev/sda
 
 
 
 
 
- 
 若果你要在 SCSI 硬盘或 SATA 硬盘的第二个分区内的 PBR(/dev/sda2)重新安装 GRUB:
 
 
 # grub-install /dev/sda2
 
 
 
 
 
- 
 若果你要在 IDE 硬盘的 MBR(/dev/hda)重新安装 GRUB:
 
 
 # grub-install /dev/hda
 
 
 
 
 
- 
 若果你要在 HP Smart Array 磁盘数组的 MBR(/dev/c0d0)重新安装 GRUB:
 
 
 # grub-install /dev/cciss/c0d0
 
- 
    二、CentOS 5 及 6 上安装 GRUB
   
   
   
   
   
    此文件解释如何在 CentOS 5 或 6 上安装或重装 Grub 开机程序。如果你的开机程序曾被毁坏或破坏 —— 譬如安装了不兼容的操作系统 —— 你必须重新安装 Grub 开机程序。
    
    
    
    
   
   
   
   
   
    
    
    
    
   
    安装
    
     Grub
    
    涉及三个步骤:
    
    
    
    
   
- 
创建 
 
 
 /boot/grub/grub.conf
 
 
 配置文件。
 
 
 
 
- 
在 
 
 
 /boot/grub
 
 
 安装 Grub 文件。
 
 
 
 
- 
安装 Grub 
 
 开机程序
 
 。
 
 
 
 
    第二及第三步可以通过
    
     
      grub-install
     
    
    单一个指令来完成。
    
    
    另外,第二步亦可通过
    
     
      grub
     
    
    这个指令来完成。
    
    
    
    
   
    
    
    
    
   
    1. Grub 惯用名称
   
   
   
   
   
   
   
    Grub 按以下方式为碟盘及分区命名:
    
    
    
    
   
- 
     不论碟盘是 IDE(PATA)、SATA 或 SCSI 碟盘,也不论它们的普遍称谓是 {
 
 {hd}} 或 {
 
 {sd}},所有碟盘一律称为 {
 
 {hd}}。
 
 
 
 
- 
     所有碟盘及分区一律以 0 而不是 1 开始编号。
 
 
 
 
- 
     不论碟盘上有多少个主分区,扩展分区一律以 4 开始编号。
 
 
 
 
- 
     第一个数字代表碟盘;第二个数字(存在的话)代表分区。
 
 
 
 
    
     样例:
    
    
    
    
    
    
    
   
| 
          | 
          | 
          | 
| 
          | 第一个碟盘 | /dev/sda(或 /dev/hda) | 
| 
          | 第二个碟盘 | /dev/sdb(或 /dev/hdb) | 
| 
          | 第一个碟盘,第一个分区 | /dev/sda1(或 /dev/hda1) | 
| 
          | 第二个碟盘,第一个分区 | /dev/sdb1(或 /dev/hdb1) | 
| 
          | 第二个碟盘,第二个分区 | /dev/sdb2(或 /dev/hdb2) | 
   
   
   
   
    
    
    
    
   
    2. 创建或编辑 /boot/grub/grub.conf 配置文件
   
   
   
   
   
   
   
    首先,你应该创建或编辑
    
     
      /boot/grub/grub.conf
     
    
    这个配置文件。
    
    
    要是你的碟盘未曾安装 Grub 开机程序,你应该先创建配置文件,然后才安装开机程序。
    
    
    
    
   
    在某些发行版本下,配置文件名为
    
     /boot/grub/menu.lst
    
    。在 CentOS 下,
    
     /boot/grub/menu.lst
    
    是条连结,它指向正式配置文件
    
     /boot/grub/grub.conf
    
    。
    
    
    
    
   
    此文件含有以下信息:
    
    
    
    
   
- 
 缺省的开机分区
 
 。
 
 
 
 
- 
 等候多久
 
 才自动引导缺省的分区。
 
 
 
 
- 
选择性地提供一个 
 
 开机划面
 
 ,在等候用户输入时显示出来。
 
 
 
 
- 
可供用户选择的各个 
 
 开机分区
 
 。
 
 
 
 
    
     样例:
    
    
    
    
    
   
    (请勿输入行首的数字。它们索引样例档后的解释。)
    
    
    
    
   
    
    
   
1 default=0 2 timeout=10 3 splashimage=(hd0,5)/boot/grub/splash.xpm.gz 4 title CentOS 5.6 i686 5 root (hd0,5) 6 kernel /boot/vmlinuz-2.6.18-238.19.1.el5.centos.plusPAE ro root=/dev/sda6 rhgb noquiet 7 initrd /boot/initrd-2.6.18-238.19.1.el5.centos.plusPAE.img 8 title Linux Mint 11 9 root (hd0,9) 10 kernel /boot/vmlinuz-2.6.32-26-generic-pae ro root=/dev/sda10 11 initrd /boot/initrd.img-2.6.32-26-generic-pae
   
   
   
   
| 
          | 
         假若你把 | |
   
   
   
   
    
     解释:
    
    
    
    
    
   
    
     1.
    
    缺省引导清单内的首个分区。请注意 Grub 的编号以 0 开始,而不是 1。
    
    
    
    
   
    
     2.
    
    停顿 10 秒等候用户输入,然后引导缺省分区。
    
    
    
    
   
    
     3.
    
    等候用户输入时显示开机划面(选择性的)。
    
    
    
    
   
    
     4.
    
    第一个可用的开机分区。在此样例中,它是缺省的分区(见行 1)。
    
    
    
    
   
    
     5.
    
    按 Grub 的命名方式指定主分区。
    
    
    
    
   
    
     6.
    
    此行包含以下 5 部份:
    
    
    
    
   
    
    
   
kernel /boot/vmlinuz-2.6.18-238.19.1.el5.centos.plusPAE
   
   
   
   
    以惯用的命名方式指出内核(
    
     vmlinuz
    
    )的位置。
    
    
    
    
   
    
    
   
ro
   
   
   
   
    首先以 ro(只读)方式挂载分区,以便在有需要时执行
    
     fsck
    
    。
    
    
    
    
   
    
    
   
root=/dev/sda6
   
   
   
   
    以惯用的命名方式指出主分区的位置。你也可通过文件系统的
    
     UUID
    
    (通用唯一标识码)来指定主分区,例如:
    
     root=UUID=134d2a24-2699-4b0c-823b-1e13633f3b07
    
    。CentOS 现在缺省采用 UUID。你可以利用这个指命来找出一个文件系统的 UUID:
    
     tune2fs -l /dev/sda6
    
    。
    
    
    
    
   
    
    
   
rhgb
   
   
   
   
    采用图像式开机(Red Hat 图像式开机)。需要文字模式开机时可删除。
    
    
    
    
   
    
    
   
noquiet
   
   
   
   
    输出详细信息。删除后可减低详细度。
    
    
    
    
   
    
     7.
    
    以惯用的命名方式指出初始化 ramdisk
    
     initrd
    
    或
    
     initramfs
    
    的位置。
    
    
    
    
   
    
     8.
    
    第二个可用的开机分区。
    
    
    
    
   
    
     9.
    
    按 Grub 的命名方式指定主分区。
    
    
    
    
   
    
     10.
    
    此行包含以下 3 部份:
    
    
    
    
   
    
    
   
kernel /boot/vmlinuz-2.6.32-26-generic-pae
   
   
   
   
    以惯用的命名方式指出内核(
    
     vmlinuz
    
    )的位置。
    
    
    
    
   
    
    
   
ro
   
   
   
   
    首先以 ro(只读)方式挂载分区,以便在有需要时执行
    
     fsck
    
    。
    
    
    
    
   
    
    
   
root=/dev/sda10
   
   
   
   
    以惯用的命名方式指出主分区的位置。另外请参阅第 6 项有关 UUID 的备注。
    
    
    
    
   
    
     11.
    
    以惯用的命名方式指出初始化 ramdisk
    
     initrd
    
    或
    
     initramfs
    
    的位置。
    
    
    
    
   
    
    
    
    
   
    3. 利用 grub-install 安装 Grub
   
   
   
   
   
   
   
    当可行时,安装 Grub 的最简易方法是利用
    
     
      grub-install
     
    
    这个指令。
    
    
    
    
   
    此指令将会:
    
    
    
    
   
- 
在现有分区的 
 
 /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 ~]#
   
   
   
   
    
    
    
    
   
    4. 替换方法:单单利用 grub 安装开机程序
   
   
   
   
   
   
   
    你也可利用
    
     
      grub
     
    
    这个指令来安装开机程序,它却不会在
    
     /boot/grub
    
    内安装 Grub 的文件。要是开机程序被盖过或损坏了,此方法可修复 Grub 的安装。
    
    
    
    
   
- 
执行 
 
 
 grub
 
 
 。
 
 
 
 
- 
你可选择执行 
 
 
 find /boot/grub/stage1
 
 
 寻找哪个分区在
 
 /boot/grub
 
 内含有 Grub 所需的文件。如果你的
 
 /boot
 
 采用独立分区,请执行
 
 
 find /grub/stage1
 
 
 。
 
 
 
 
- 
执行 
 
 
 root [按 Grub 方式命名的分区]
 
 
 来指定哪个分区的
 
 /boot/grub
 
 内含有 Grub 文件及备有
 
 /boot/grub/grub.conf
 
 。
 
 
 
 
- 
利用 
 
 
 setup [按 Grub 方式命名的开机程序位置]
 
 
 来安装开机程序。
 
 
 
 
- 
执行 
 
 
 quit
 
 
 来离开 Grub 程序。
 
 
 
 
    
     样例:
    
    
    
    
    
   
    
    
   
[root@localhost ~]# 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> find /boot/grub/stage1
 (hd0,5)
 (hd0,9)
grub> root (hd0,5)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  15 sectors are embedded.
succeeded
 Running "install /boot/grub/stage1 (hd0) (hd0)1+15 p
(hd0,5)/boot/grub/stage2
/boot/grub/grub.conf"... succeeded
Done.
grub> quit
   
   
   
   
    
    
    
    
   
    5. 备注及警告
   
    5.1. 以独立分区存放 /boot
   
   
   
   
   
    要是你的
    
     /boot
    
    放在独立的分区内,
    
     /boot/grub/grub.conf
    
    内以
    
     root
    
    、
    
     kernel
    
    及
    
     initrd
    
    起首的行必须有下列改动:
    
    
    
    
   
- 
以 
 
 
 root
 
 
 起首的那一行必须指定
 
 /boot
 
 的分区,而不是
 
 /
 
 的分区。
 
 
 
 
- 
以 
 
 
 kernel
 
 
 起首的那一行必须假设内核(
 
 vmlinuz
 
 )位于
 
 /
 
 ,而不是
 
 /boot
 
 。然而,该行中
 
 root
 
 指令必须指定
 
 /
 
 的分区,而不是
 
 /boot
 
 的分区。
 
 
 
 
- 
以 
 
 
 initrd
 
 
 起首的那一行必须假设初始化 ramdisk(
 
 initrd
 
 或
 
 initramfs
 
 )位于
 
 /
 
 ,而不是
 
 /boot
 
 。
 
 
 
 
    
     样例:
    
    
    
    
    
   
    
     
      /boot
     
     的分区
    
    是
    
     /dev/sda1
    
    ,或
    
     hd(0,0)
    
    。
    
     
      /
     
     的分区
    
    是
    
     /dev/sda2
    
    ,或
    
     hd(0,1)
    
    。
    
     /boot/grub/grub.conf
    
    档须含有下列设置:
    
    
    
    
   
    
    
   
title CentOS 5.6 i686 root (hd0,0) kernel /vmlinuz-2.6.18-238.19.1.el5.centos.plusPAE ro root=/dev/sda2 initrd /initrd-2.6.18-238.19.1.el5.centos.plusPAE.img
   
   
   
   
    
    
    
    
   
    5.2. Grub 及 ext4fs
   
   
   
   
   
   
   
    CentOS 5 的 Grub 并不兼容
    
     ext4fs
    
    。更准确来说,它不能读入 ext4fs 缺省采用的
    
     256-byte inodes
    
    。
    
    
    
    
   
    可行的解决方法有两个:
    
    
    
    
   
- 
     采用 CentOS 6 的 Grub,它能正确地支持 ext4fs。
 
 
 
 
- 
利用 
 
 
 mkfs.ext4 -I 128
 
 
 这个指令创建一个拥有 128-byte inodes 的 ext4 文件系统。这却意味某些 ext4fs 功能将不可使用。
 
 
 
 
    
    
    
    
   
    5.3. 在开机程序运用 Grub 的命令行
   
   
   
   
   
    你可在开机程序内直接输入 Grub 的指令。在开机选单按
    
     
      c
     
    
    ,然后输入那些出现在
    
     /boot/grub/grub.conf
    
    配置文件内的 Grub 指令。
    
    
    
    
   
    当配置文件出错时,这个功能很有用。
    
    
    
    
   
    
     样例:
    
    
    
    
    
   
    
    
   
grub> find /boot/grub/stage1 (hd0,5) grub> root (hd0,5) grub> kernel /boot/vmlinuz-2.6.18-238.19.1.el5.centos.plusPAE ro root=/dev/sda6 grub> initrd /boot/initrd-2.6.18-238.19.1.el5.centos.plusPAE.img grub> boot
   
   
   
   
    你也可以通过
    
     
      e
     
    
    这个指令在 Grub 开机程序内编辑
    
     /boot/grub/grub.conf
    
    内的某行。举个例说,要令 Linux 以单一用户模式(runlevel 1)引导,编辑含有
    
     kernel
    
    的那一行并在行末加入
    
     
      1
     
    
    。按着按
    
     
      b
     
    
    来引导它。
    
    
    
    
   
    
    
    
    
   
    5.4. 利用 Grub 来引导 Windows 分区
   
   
   
   
   
    要使用 Grub 来引导一个 Windows 分区,请在
    
     /boot/grub/grub.conf
    
    加入类似以下的内容:
    
    
    
    
   
    
     样例:
    
    
    
    
    
   
    
    
   
title Windows rootnoverify (hd0,0) makeactive chainloader +1
参看官方网站:
    
     http://wiki.centos.org/zh/TipsAndTricks/ReinstallGRUB
    
   
    
     http://wiki.centos.org/zh/HowTos/GrubInstallation#head-6e422a26ea86278281f109ae03b7c22159db50e6
    
    
   
 

