This post is aimed at everyone who has a USB stick that’s over 3 MB in size. If you do not have such a thing, skip to the grub rescue prompt stuff where you’ll have some information on how to boot into your usual kernel from one.
The worst thing that can happen to you, especially when you’re playing around with your partitions or upgrading. And sometimes, you don’t even get to goto the grub rescue prompt when your MBR gets corrupted. So you end up searching through your cd/dvd pouch for a live cd and realise – damn! I gave it all out to my friends. I recently figured you can have a handy USB stick to avoid this. There a a couple of ways to do it, one is just to have a partition which has the data of a “live” pen-drive, which can be created from any existing live cd around. The instructions can be found here, believe there is an application installed in ubuntu which does exactly this as well, but then this means you’ll have to sacrifice a significant amount of your disk space for just contingency situations. There is another method, which involves you creating a small partition of about 3-5 MB on your USB stick. Which IMHO is not a lot of space in the modern day USB sticks. So, here goes the method:
1) Choose your favourite partition editing method and resize and existing partition to 5 MB smaller. I usually use “partitioneditior” or “GParted”.
2) Create a new ext3 or ext4 partition using the 5MB generated above.
3) Mount the new partition, lets say its in /mnt/usb
4) Goto a shell, and
mkdir -p /mnt/usb/boot/grub
cp /usr/lib/grub/i386-pc/* /mnt/usb/boot/grub/
echo ‘(hdY) /dev/sdX ‘ /mnt/usb/boot/grub/device.map
sudo grub-install –root-directory=/mnt/usb /dev/sdX
Note, the X and Y above should be the device letter/number for the USB stick.
That’s it. Next time you boot with this USB stick, you’ll get a grub prompt which you can use to boot from.
From the grub prompt, booting is in 3 steps.
1) Specify the root partition
2) Specify the kernel image
3) Specify the corresponding initrd.
So, the commands for those are:
[If you just have one hard disk, X is probably 0, and Y is the root partition. Don't worry if you're not sure, there is tab completion and the next step will make things clear]
kernel (hdX,Y)/boot/vmlinuz-foo-bar [Where foo bar can be tab completed to the latest kernel that you have]
initrd (hdX,Y)/boot/initrd-foor-bar [Again, -foo-bar should correspond to the kernel chosen above]
If you’re unable to tab complete and locate the kernel, you’ve chosen the wrong values for X and Y. Try with something else, one hint is the partition type will be some ext*.
Now, just type boot. And you’ll be booted into your above specified kernel. You can then run grub install and restore things back to normal working conditions :)
So, at the cost of 5 MB ( You’ll only need 3 if you think 5 is too much) on your USB stick, and some ten minutes of your time, you have a way to boot when your grub fails – yes, no more running around in search of a live cd. And oh, netbook users – you don’t really have a choice do you? ;)