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

**URL:** <https://hub.mender.io/t/corrupted-grub-env-when-calling-grub-mender-grubenv-set-during-early-boot/5516>\
**Category:** General Discussions\
**Tags:** x86-grub, grub, qemux86-64\
**Created:** [December 30, 2022, 2:27pm UTC](https://hub.mender.io/t/corrupted-grub-env-when-calling-grub-mender-grubenv-set-during-early-boot/5516 "2022-12-30T14:27:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![clementp](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/clementp/32/1392_2.png) [@clementp](https://hub.mender.io/u/clementp)\
**Post date:** [December 30, 2022, 2:27pm UTC](https://hub.mender.io/t/corrupted-grub-env-when-calling-grub-mender-grubenv-set-during-early-boot/5516/1 "2022-12-30T14:27:43Z")

</div>

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 ☹

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

---

<div class="post-metadata">

**Author:** ![clementp](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/clementp/32/1392_2.png) [@clementp](https://hub.mender.io/u/clementp)\
**Post date:** [January 10, 2023, 11:14am UTC](https://hub.mender.io/t/corrupted-grub-env-when-calling-grub-mender-grubenv-set-during-early-boot/5516/2 "2023-01-10T11:14:02Z")

</div>

a MR to fix the bad checksum is available here: [Recompute sha256 by clementperon · Pull Request #30 · mendersoftware/grub-mender-grubenv · GitHub](https://github.com/mendersoftware/grub-mender-grubenv/pull/30)

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

---

<div class="post-metadata">

**Author:** ![clementp](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/clementp/32/1392_2.png) [@clementp](https://hub.mender.io/u/clementp)\
**Post date:** [January 19, 2023, 1:29pm UTC](https://hub.mender.io/t/corrupted-grub-env-when-calling-grub-mender-grubenv-set-during-early-boot/5516/3 "2023-01-19T13:29:30Z")

</div>

Finally,

Adding a lock to the grubenv fix the issue.

> <https://github.com/mendersoftware/grub-mender-grubenv/pull/28>
>
> Doing concurrent acces on mender grub environement utility will trig the followi…ng error:
> /usr/bin/grub-editenv: error: invalid environment block.
> 
> This append when a process is editing an env so the other is trying to restore it.
> 
> Fix this by running critical section under a flock
> 
> Signed-off-by: Clément Péron \<peron.clem@gmail.com\>

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

---

<div class="post-metadata">

**Author:** ![kacf](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/kacf/32/146_2.png) [@kacf](https://hub.mender.io/u/kacf)\
**Post date:** [January 20, 2023, 7:32am UTC](https://hub.mender.io/t/corrupted-grub-env-when-calling-grub-mender-grubenv-set-during-early-boot/5516/4 "2023-01-20T07:32:29Z")

</div>

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.
