This is a weird one...
I'm working on some software. It has a function to make a USB device bootable. It does this by running a long, complex sequence of actions to copy a minimal Linux OS onto the device and then install GRUB2 as the bootloader. The trouble is, at the end of this procedure, the device won't boot. GRUB2 loads, but drops into a rescue shell and whines that it can't find [whatever the UUID of the boot partition currently is]. The UUID it's whining about is definitely correct.
The actual command that my software is running is this:
grub2-install --boot-directory=/mnt/boot /dev/sdb
The command exits with code 0, and says on stdout "installation finished, no errors reported". And yet, GRUB won't boot.
Here's the excruciatingly weird part: If I manually type in the exact command above, then the device becomes bootable. Everything works perfectly. But when the software runs this command automatically, something goes wrong... What the hell??
Does anybody have the vaguest idea why this behaviour might occur? Are there any environment variables the installation script depends on? What kinds of disk modifications would upset GRUB? Is there some way I can see what gets changed the second time I run the script?
Edit: OpenSUSE 12.2, everything is running as
root. (Is any other user even allowed to fiddle with the MBR of a disk?)
env returns nearly identical results both from the software and from a terminal. (A few variables such as
WINDOWID were different, but I can't see how that's relevant.)
Edit: I tried having my software just run the command twice. That made no difference. So it really is an environment difference of some kind...