Linux – How should I configure grub for booting linux kernel from a USB hard drive

bootgrub2kernellinuxusb

I have a laptop hard drive in an external enclosure which I use as a large pendrive. For an added twist, I have installed Linux on it, so I can boot any machine with my distribution of choice (e.g. for data recovery or repairing a b0rked system or just using a borrowed laptop without destroying the preinstalled Windows).

The problem is that, depending on the hardware configuration, the USB hard drive may be visible under different paths. For grub configuration, I just use (hda0,0) as it is relative to the device the grub was launched from. I have UUID entries in /etc/fstab. I also specify rootwait in the kernel parameters so that it waits for the USB subsystem to settle down before trying to mount the device.

What should I pass to the kernel as root=? Currently, boot from the pendrive once, check the debug messages to see what /dev/sdX device has been assigned to the USB drive by the kernel, then reboot and edit the grub configuration. I can't change anything on the PC besides enabling Boot from USB hard drive in BIOS and setting it to a higher priority than internal hard drives.

There are various initrd generating scripts that include support for UUID in root device path, unfortunately, the Gentoo native one (genkernel) does not support rootwait and I had no luck trying to use others.

The boot process goes like this (it is quite similar in Windows):

  1. The BIOS chooses the boot device and loads whatever is its MBR (which happens to be grub stage-1).
  2. Grub loads it's configuration and stage-2 files from the device it has set as root, using (hd0) for the device it was loaded from by BIOS.
  3. Grub loads and starts a kernel (still the same numbering, so I can use (hd0,0) again ).
  4. The kernel initializes all built-in devices (rootwait does it's magic now).
  5. The kernel mounts the partition it was passed as root (this is a kernel parameter, not grub parameter).
  6. init.d starts the userland booting process, including mounting things from /etc/fstab.

Part 5 is the one giving me problems.

Best Answer

I think the answer should be to use either root=LABEL=... or root=UUID=...

But you indicate that this doesn't work for you. I would suggest looking into if there is some problem with the actions of your initrd.

Related Question