restoring grub from live cd

categories: tutorial

The CS-club at Jacobs University recently organized a Linux install fest where we installed mostly Ubuntu 10.10 on people's machines. It turned out that in some cases (reasons yet unknown) grub failed to install correctly and the computer would still boot into Windows.

Eventually we figured out how to solve this problem and this is how.

There are two ways of doing it and they depend on whether one can chroot into the target system or not from the live CD. This again depends on the architectures being compatible with each other as using qemu user mode emulation is not an option on a live CD.

The reason one has to have the target linux installation as the root is update-grub which takes no argument to specify a differently mounted /. There is possible an environment variable somewhere that fixes this but we werent able to find one.

Method 1 (no chroot)

So in case chroot is not possible, the steps are:

  1. install grub to the MBR from the live CD
  2. manually boot into the already installed linux via the grub cli
  3. run grub-update in the installed system

After booting into the live system, do

mount /dev/sdZX /mnt

where /dev/sdZX is the /boot partition

grub-install --recheck --root-directory=/mnt /dev/sdZ

where /dev/sdZ is the primary harddisk.

While you are still in the live system, it's also easy to take some notes which partition / is on.

reboot

then in grub do the following:

set root=(hd0,X)
linux /vmlinuz... root=/dev/sdZY
initrd /initrd...
boot

The first root= specifies your /boot partition. To get an overview of the available partitions, run ls from the grub prompt. To have a look whether you selected the right partition as the grub root, check its contents with ls / . When specifying the linux kernel image and the initrd, instead of the ... press [TAB] to automatically complete the filenames. Carefully adjust the root= kernel commandline option as /dev/sdZY has to point to the partition containing /.

If everything was entered correctly, your linux will boot and after opening a terminal you can let grub autoconfigure itself and install it to the MBR once again (where /dev/sdZ is again your primary harddrive).

update-grub
grub-install --recheck /dev/sdZ

Method 2 (with chroot)

In case it is possible to chroot into the installed system from the live system the process is a bit more complex but less time consuming due to no reboots or mess ups at the grub cli wrt. partition numbers.

The steps are:

  1. mount the root file system from the harddrive somewhere
  2. bindmount all important pseudo filesystems into it
  3. chroot into the system
  4. update-grub and grub-install
  5. umount everything and reboot

After booting into the live system, do

mount /dev/sdZX /mnt

where /dev/sdZX is your / partition and possibly also do:

mount /dev/sdZY /mnt/boot

where /dev/sdZY is your /boot partition in case you have an extra /boot partition.

mount --bind /dev /mnt/dev
mount --bind /dev/pts /mnt/dev/pts
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys

grub seems to need at least /dev to be bindmounted correctly.

chroot /mnt

then, in the chroot, do:

update-grub
grub-install --recheck /dev/sdZ

where /dev/sdZ is your primary harddrive.

Exit the chroot and reboot.

Problems with update-grub not detecting windows

this problem happened a few times as well but afaik catalin is still investigating how to fix this issue. Will probably update this section later on.

View Comments
blog comments powered by Disqus