Using grub2 to recover your system

grub2 is hailed as the all new, super modular cure-all remedy for all booting problem you’ve had, have and will have. At least that’s the way the developers and some enthusiasts see it, whereas most blokes who’ve actually had to use it with more than arrow keys and enter will paint a slightly different picture.

The thing with grub2 is that even though in theory it sounds like the end of all things booting, it’s about as well-documented as the question for life, the universe, and everything.

And as I today had to try to fight my way through googling for necessary information again, I’d thought I’d create a quick step-by-step reference with all the most interesting bits you’ll ever need already there.

Thusly, the ingredients needed to resurrect your computer with grub2. The gist is that you have the goal of booting one specific operating system on your computer, from wherein which you’ll use whatever methods you deem necessary to update your grub in the “right way” – usually a downgrade to an older version and waiting for the dust to blow over.

  1. A booting grub2. If your grub2 already fails to boot because of some random error, you need to get a grub in smelling distance of your BIOS. One of the most proven methods is to
    1. Download a USB rescue image like grml (usually from Your Other Computer or that of somebody else)
    2. Put it on an USB stick (dd if=grml-variant_version.iso of=/dev/sdx in most cases, with appropriately chosen variables)
    3. (Re)boot, eventually adjusting the priority for your USB HDD/USB key

    And that’s it, you’re in a grub. Also note that it’s recommendable to have an USB stick with a rescue image lying around for the times when you can’t just easily download it.

  2. Enter the command line/shell mode by pressing ‘c‘.
  3. Do an ‘ls‘, which will give you a listing of recognized devices. Doing an ‘ls device‘, e.g. ls (hd0,1) will give you more information about that device.
  4. If the information by your ls isn’t complete, you will have to load some modules (by using insmod modulename). Here’s a checklist:
    1. If you do not see any other devices which look like your hard drive(s), e.g. you only have an (hd0) device from your USB medium, then load a device driver. They will allow you to find the actual devices. Examples include:
      • biosdisk
      • scsi
      • fs_uuid
      • pci
      • raid
      • mdraid
      • dm_nv
    2. If you have devices, but no partitions, you’ll need a partition driver. It seems the default grub config does not load any partition driver, and debugging this is just a bit annoying. But there’s two easy choices for most people:
      • Load the module “part_msdos“.
      • If this doesn’t help, try “part_gpt“.

      These are the two most common partition tables (at least for next to everyone reading this guide in need) and should help your grub find its partitions again.

    3. Eventually, you will also have to load your filesystem drivers. I presume you already know which those are, but for the sake of completion:
      • Almost all Linux use ext2
      • Most current Windows will use ntfs, but fat is also an option.
      • Mac users will use hfsplus for newer systems, hfs for older ones.
    4. The next step depends on exactly what you want to do. There’s a fork in the road – if you just want to load your previously unbootable grub, you will try to load its configuration file, else you’ll try to boot your operating system kernel.

    5. To search for a file, you use the search -f filename command, which will give you results on where files of that name are stored. Use root device to set the resultant device as the root device for your further operations. If you only want to load your old grub config, type in configfile filename, whereas filename will usually be something like /grub/grub.cfg or /boot/grub/grub.cfg.
    6. Should this fail to resolve your problem, or not be what you’re aiming for, you’ll need to find the operating system. For most Linuxens, you’ll probably have a file called /vmlinuz or /boot/vmlinuz to search for. For Windows operating systems, look for /Windows/win.ini. For Mac: no clue. When found, set your root device (with root device).
    7. Now methods will become divergent, as operating systems differ in the way of booting them.
      Linux
      1. kernel kernel_filename
      2. initrd initrd_filename [most current kernels come with an “initial ramdisk” holding modules etc.]
      3. boot – if all goes well, you’re set.
      Windows
      1. chainloader +1
      2. boot
      MacOS
      Probably the same as Windows, using the chainloader.

    And that’s it. It should cover most cases you’d need to restore your capability of booting your operating system. You’ll probably want to fix/install your bootloader after this, though.

    A helpful tool for debugging your current grub state is probe, which will allow you to check what drivers are assigned to devices.