Thursday, May 31, 2012

Toying Around With GRUB 2

I ran into a problem today: when trying to select my Windows 7 from the grub list, I accidentally selected the one right above it: "Windows Recovery Environment..." Unfortunately, this opens up a the Samsung Recovery 4 Solution software which overwrites something on the MBR, so that the next time you boot up, grub no longer loads. Luckily, I was able to solve the problem by installing Ubuntu 12.04 LTS (which I had downloaded recently and was needing to install anyway).

Putting GRUB on a thumbdrive

While it was installing, though, I did some research because it got me thinking: I should either remove that option from the list altogether, or have a quicker way to boot into one of my existing linux distros to quickly rewrite grub2 to the MBR. I liked the idea of the latter, so I attempted writing grub2 to a flash drive which will then be used to load an OS already on my hard drive.

And you know what? SUCCESS... was what followed. Well, not right away, but after some research and a few attempts and some hard core thinking-on-my-own (I know, that can be dangerous sometimes), I was successful (I need to credit here the GRUB Manual and a post I found via google):
  1. First, I'm assuming you have a working OS with a working grub bootloader: boot up that OS.
  2. Plug in your flash drive and run fdisk -l to find your flash drive; it will most likely be listed as /dev/sdx1, where x is usually a letter > 'a'.
  3. Mount your flash drive to a temporary folder. See below:
    $ sudo mkdir /mnt/USB
    $ sudo mount /dev/sdx1 /mnt/USB
    $ ls /mnt/USB                    #check you mounted the right flash drive
  4. This step may not be necessary, but as I didn't test it without this step, I'll include it anyway: bind your OS's dev folder so when you install grub it can search it:
    $ sudo mkdir /mnt/USB/dev
    $ sudo mount --bind /mnt/USB/dev /dev
  5. Install grub to thumbdrive's MBR:
    $ grub-install --force --no-floppy --root-directory=/mnt/USB /dev/sdx
  6. Next, make your grub.cfg file on your OS and copy it to your flash drive:
    $ sudo grub-mkconfig -o /boot/grub/grub.cfg
    $ cp /boot/grub/grub.cfg /mnt/USB/boot/grub.cfg
  7. Unmount everything that doesn't need to be mounted, and then reboot your computer with your flash drive plugged in to see if it works.
If you want, and are familiar with how, you can edit the grub.cfg on your flash drive to only include specific OS's; remember, though, this is a safety key, essentially, so that if anything goes wrong with your MBR you can still easily boot your preferred OS by simply booting up with your thumbdrive plugged in. Then, you can go about fixing your MBR to boot the local grub as usual (which can usually be done with a simple 'grub-install /dev/sda', assuming your MBR is on sda and you booted into the linux distro hosting your grub).

Some notes of caution

  • I also backed up all my files on the flash drive before performing this grub install; however, I don't appear to have damaged those files at all, and I think (in my un-expert opinion) it should be safe to continue to use it as a regular thumbdrive.
  • I'm not an expert at this, so if I've done anything risky, I'd love to know, and I'd advise you to do some of your own research as well.
  • My thumbdrive was already formatted as a FAT32.

What I might do next

GRUB 2 can also run iso's live from the thumbdrive, so I might look into that. I already have a YUMI flash drive that has a Windows 7 Recovery iso on it, just in case, but it might be kind of fun to play around with... after some more research.

No comments:

Post a Comment

Please keep your comments respectful and in the spirit of constructive criticism.