I'm trying to use partition labels (or UUIDs, that does not matter) to select root partition without use of an initrd/initramfs.

With initramfs, this is fairly easy: root=LABEL=... or root=/dev/disk/by-label/... in the kernel arguments does the job. However, without initramfs the above is not possible: the LABEL= notation is decoded by initramfs script, and /dev/disk/by-label/... symlinks are created by udev running from the initramfs. Only the root=/dev/sdXY notation is understood by the kernel itself.

However, GRUB supports shell-like scripting, and I think that it is somehow possible to write a GRUB 2 scriptlet that will generate root=/dev/sdXY string from a device label or UUID.

Especially, the command search --label <label> --set ROOTDEV will set variable ROOTDEV with a string like hd0,gpt5 which points to the partition with given label. If I understand correctly (please correct me if I'm wrong) the order of devices in GRUB and kernel is the same, so hd0 is /dev/sda, hd1 is /dev/sdb and so on.

So here is a question: is there a way, from inside the GRUB 2 script, to convert hd0,gpt5 into /dev/sda5? Exactly the hd0 into sda part - the rest is easy with regexes.

  • No, the device in GRUB and kernel has no reason to be the same. – Stéphane Chazelas Jan 11 '14 at 21:07
  • 1
    You cannot make a purely textual translation: GRUB has to follow BIOS device numbering, and the kernel doesn't, so there is no intrinsic connection between the a in Linux's sda and the 0 in Grub's hd0. You can't do the conversion inside Grub as far as I know, but you can do it from /boot/grub/device.map when generating the Grub configuration file. – Gilles Jan 11 '14 at 21:49
  • @Gilles: dammit. So is initrd unavoidable in this case?.. – intelfx Jan 12 '14 at 13:21

When Grub2 is on a USB stick (that USB stick has no Linux intalled on it, so grub.cfg mut be manually created and edited), the data partition literal changes, depending on how many internal and external devices are connected, and if hardware boots on UEFI or BIOS modes.

In some lines i need to have a parameter in the form of /dev/sd$#, not GRUB style, it must point to such partition data (the literal is not allways the same, it depends a lot on the computer i boot with such USB).

With GRUB search command i can get the literal, but in GRUB style.

I do not know how to use regular expressions inside grub.cfg file to change hd# to sd$, neither to change ',msdos#' or ',gpt#' to #.

Maybe a link to regular expresions in grub.cfg can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.