Corrupted grub env when calling grub-mender-grubenv-set during early boot

Hi,

I have a machine which is a QEMU x86_64 with VirtIO Storage based on meta-mender kirkstone-v2022.09.

Just before starting the systemd network, I check and set if a hostname variable exist in the grub env and if not i’m writing it to keep the hostname stable between firmware upgrade.

At the boot my script is doing something like

hostname = grub-mender-grubenv-print hostname.
if -z $hostname
generate_random()
grub-mender-grubenv-set hostname “my-machineRANDOM” <==== FAIL AT FIRST BOOT !!! (computed checksum did NOT match)
fi

I suspect that my script is running at the same time as another service which is also editing this file but I don’t know which one. Also it’s seems that the grub-mender-grubenv-set is not thread safe :frowning:

This is something that I can reproduce easily.
But it seems strange

a MR to fix the bad checksum is available here: Recompute sha256 by clementperon · Pull Request #30 · mendersoftware/grub-mender-grubenv · GitHub

But it still don’t understand why the first set hostname fail :frowning:

Finally,

Adding a lock to the grubenv fix the issue.

Unfortunately I don’t understand which other program is using mender grubenv during the early boot :confused:
@kacf do you have any idea ?

1 Like

I can’t say that I do, no. Nothing official from the Mender products would do this, as far as I’m aware.

One possibility would be to go through all the active systemd units and see if any of those access it. OTOH if the fix works, then I guess we don’t need to worry about it.