Hack 53 Repair Grub

 < Day Day Up > 

figs/moderate.gif figs/hack53.gif

Use Knoppix to restore grub to the Master Boot Record (MBR) .

There are two popular bootloaders for Linux: lilo and grub . Lilo has been around longer, and many distributions default to installing it, but most also offer grub packages. Grub has many interesting features that have made it popular, including the ability to change kernels and basically any other grub options at boot time. Grub also reads from its configuration file at boot, so you can change options in the text file and don't have to reinstall grub to the MBR to make changes. Like lilo , grub sometimes gets overwritten by a Windows install or by an accidental installation of lilo to the MBR. The procedure to restore grub to the MBR is almost identical to restoring lilo .

First, identify your root partition as in the lilo hack [Hack #52] . In this example, the root partition is /dev/hda1 .

If you want to make any changes to your grub configuration, now is the time to mount the partition read/write and edit the boot/grub/menu.lst file.


Next , mount the partition with the dev option enabled and with write permissions, so if the filesystem is not yet mounted, mount it with:

 knoppix@tty1[knoppix]$  sudo mount -o dev,rw  /mnt/hda1   

If the filesystem is already mounted, then remount it with:

 knoppix@tty1[knoppix]$  sudo mount -o remount,dev,rw  /mnt/hda1   

Once the filesystem is mounted, restore grub with the following command:

 knoppix@tty1[hda1]$  sudo chroot  /mnt/hda1    grub-install  /dev/hda   

Of course, replace /mnt/hda1 and /dev/hda with your mounted root partition and the device to which you wish to install grub , respectively.

If you use devfs , or for some reason, the chroot environment doesn't work, mount the filesystem with write permissions, then run sudo grub-install -root-directory=/mnt /hda1 /dev/hda .


6.3.1 Install grub on a System Without grub

Once grub is installed on an MBR, all it needs is the contents of the /boot/grub/ directory to boot your system. That means that you can actually use Knoppix to install grub to a system that doesn't actually have grub binaries on it. Obviously, once you have grub set up, you want to find and install the grub packages on your system, but with this next series of commands, you can use the grub tools from Knoppix to set up grub on your MBR.

To install grub directly from Knoppix, mount your root partition read/write and create a /boot/grub/menu.lst file. If you are comfortable with grub , you can do this by hand with a text editor. You can also copy over /usr/share/doc/grub/examples/menu.lst from your Knoppix CD, and comment out the different operating systems it has already configured and use it as a reference. However, there is an easier way to configure grub : use the update-grub tool. This tool scans the /boot partition for usable kernels and automatically creates a menu.lst file based on what it finds. This tool must be run from a chroot environment on the root partition, so assuming the root partition is mounted at /mnt/hda1 , you would run:

 knoppix@tty1[knoppix]$  cd  /mnt/hda1   knoppix@tty1[hda1]$  sudo mkdir boot/grub  knoppix@tty1[hda1]$  sudo cp /sbin/update-grub ./  knoppix@tty1[hda1]$  sudo chroot  /mnt/hda1    /update-grub  

The final command prompts you to create a menu.lst file. You want to do this, so answer "yes." It then scans your hard drive and adds new entries for all of the kernels it finds. The first time update-grub is run, it might not detect the correct root device to use. Grub uses special notation to define partitions, such as (hd0,0) , which describes the first partition on the first hard drive in the system or translated to Linux /dev/hda1 . If your root partition is not at /dev/hda1 , but perhaps at /dev/hda5 , you must edit the boot/grub/menu.lst file that was created, and find the following commented line:

 # groot=(hd0,0) 

Do not uncomment this line. This is a hint for the update-grub tool only, not for grub itself. Replace (hd0,0) with the correct root device for your Linux system. For example, if your root Linux partition is /dev/hda5 , change the line to:

 # groot=(hd0,4) 

Notice that grub counts partitions from zero instead of one. Rerun update-grub to update menu.lst with the correct values:

 knoppix@tty1[knoppix]$  cd  /mnt/hda5   knoppix@tty1[hda5]$  sudo chroot  /mnt/hda5    /update-grub  

This example uses /mnt/hda5 . You should of course change this, as well as the grub commands, to match your root partition.

Once the program has finished, you are ready to install grub to the boot sector. This doesn't require a chroot environment, but you must tell the grub-install program to use /mnt/hda1 as your root directory by typing the following command:

 knoppix@tty1[knoppix]$  sudo grub-install --root-directory  =/mnt/hda1 /dev/hda   

Once again, change /mnt/hda1 and /dev/hda to match your root partition and MBR. Now, you should be able to restart the computer and should be presented with your new grub prompt.

 < Day Day Up > 


Knoppix Hacks. 100 Tips and Tricks
Knoppix Hacks. 100 Tips and Tricks
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net