1

I am making a multi-boot hybrid EFI+legacy USB stick where Linux images will be stored as .iso files. I would like to avoid manually adding the systems in grub.cfg or re-running something every time I add an image, so I am trying loop through files on the USB drive, for each generating a menuentry that will (hopefully) load it. The following is a bash equivalent of what I am trying to do with my grub.cfg:

for iso in "/boot/iso/*.iso"
do
  menuentry "$iso" --class os {
    loopback loop $iso
    linux (loop)/casper/vmlinuz
    initrd (loop)/casper/initrd.lz
  }
done

However, the GRUB shell does not seem to do filename expansion (globbing), which this relies on. I have also tried capturing the output of something like ls (hd0,gpt3)/boot/iso, but apparently there is neither support nor plans to add it.

Perhaps I am completely out of my mind trying to generate menuenries in a for loop, but I am hoping there is some module that makes this feasible. I also noticed that although for iso in a.iso b.iso does generate multiple menuentry blocks as expected, the $iso variable in their bodies is not substituted.

My question is, how do I create a menuentry per .iso file?

3

As it turns out, GRUB shell does support wildcard expansion, but only if you load GRUB's regexp module first. It is also important to pay attention to the quotation marks.

It is further possible to pass variables into menuentry by specifying them as arguments (treating menuentry as a function). They are then converted into a setparams call, setting them as $1, $2, $3 and so on. The following successfully boots a Linux Mint image without hardcoding the image's filename:

insmod regexp

for iso in /boot/iso/*.iso
do
  menuentry $iso $iso {
    echo "Booting from $2"
    loopback loop $2
    linux (loop)/casper/vmlinuz file=/cdrom/preseed/linuxmint.seed boot=casper iso-scan/filename=$2
    initrd (loop)/casper/initrd.lz
  }
done

I have also discovered the multibootusb project, which does precisely what I was trying to achieve, rendering my efforts obsolete.

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.