Installing GRUB using grub-install

缪修德
2023-12-01

 

平时总是弄得grub出错进不来系统,给我来个grub-rescue,我用livecd的Ubuntu进入系统后,重新安装grub到mbr,方法如下:

grub-install --boot-directory=/mnt/boot /dev/sda

其中的boot目录就是挂载的boot分区,如果没有boot分区,那就是根目录

下面是安装文档的原文:

3.1 Installing GRUB using grub-install

For information on where GRUB should be installed on PC BIOS platforms,see BIOS installation.

In order to install GRUB under a UNIX-like OS (suchas gnu), invoke the programgrub-install (seeInvoking grub-install) as the superuser (root).

The usage is basically very simple. You only need to specify oneargument to the program, namely, where to install the boot loader. Theargument has to be either a device file (like ‘/dev/hda’). For example, under Linux the following will install GRUB into the MBRof the first IDE disk:

# grub-install /dev/hda

Likewise, under GNU/Hurd, this has the same effect:

# grub-install /dev/hd0

But all the above examples assume that GRUB should put images underthe /boot directory. If you want GRUB to put images under a directoryother than/boot, you need to specify the option--boot-directory. The typical usage is that you create a GRUBboot floppy with a filesystem. Here is an example:

     # mke2fs /dev/fd0
     # mount -t ext2 /dev/fd0 /mnt
     # mkdir /mnt/boot
     # grub-install --boot-directory=/mnt/boot /dev/fd0
     # umount /mnt

Some BIOSes have a bug of exposing the first partition of a USB drive as afloppy instead of exposing the USB drive as a hard disk (they call it“USB-FDD” boot). In such cases, you need to install like this:

# losetup /dev/loop0 /dev/sdb1
     # mount /dev/loop0 /mnt/usb
     # grub-install --boot-directory=/mnt/usb/bugbios --force --allow-floppy /dev/loop0

This install doesn't conflict with standard install as long as they are inseparate directories.

Note that grub-install is actually just a shell script and thereal task is done bygrub-mkimage andgrub-setup. Therefore, you may run those commands directly to install GRUB, withoutusinggrub-install. Don't do that, however, unless you are veryfamiliar with the internals of GRUB. Installing a boot loader on a runningOS may be extremely dangerous.


 类似资料: