I can't get GRUB_SAVEDEFAULT=true to work

grub2-mkconfig after setting that option puts the savedefault command (which works) into grub.cfg for each of the non-Fedora entries (which are directly in grub.cfg).

But the Fedora entries are all in /boot/loader/entries and I can’t find any way to get the savedefault behavior for those.

When I select one of the Fedora kernels from the grub2 menu, I want it to become the default for future boots, until I select another.

I like the concept of individual files in /boot/loader/entries but I don’t understand details about how those become entries in the menu and I don’t know whether/how I can influence that process.

An ordinary entry can included execution of grub2 commands (such as the function savedefault). Is there any way to insert command execution into the menu entries created from loader/entries ?

=================

Solved:

What I needed was the line
save_default=true
in the file /boot/grub2/grubenv

I’d still appreciate any information on more generally customizing the menu entries the come from /boot/loader/entries

The original problem was my fault. There were things I needed to prevent grub2-mkconfig from breaking, and the way I did that was flawed. I was unhappy to be using it at all. But I could not find any documentation on enabling this feature other than starting with GRUB_SAVEDEFAULT=true in the file that is input to grub2-mkconfig. What I wanted to know was what really drives the feature (what grub2-mkconfig changes in response to that setting).

Since you aren’t supposed to edit /boot/grub2/grubenv, I tried grub-editenv and was annoyed to discover the man page for it is on Fedora 35, but not the program itself and I couldn’t find a way to install it, so I rebooted into Ubuntu just to run that program. I could have also done it typing commands directly into grub2.

1 Like

To make permanent changes to the default kernel for booting see the docs at
https://docs.fedoraproject.org/en-US/fedora/latest/system-administrators-guide/kernel-module-driver-configuration/Working_with_the_GRUB_2_Boot_Loader/#sec-Making_Persistent_Changes_to_a_GRUB_2_Menu_Using_the_grubby_Tool

The same doc shows how to make temporary changes as well.

You can use the man page for grubby to see what all is available with that tool.

Are you on btrfs? What you’d want does not work with btrfs.

Thankyou, but I can’t find anything there nor in other grubby documentation that answered my question.

I knew (but still haven’t tried) that I could change the current value of the default with grubby from inside Linux. But that isn’t what I was trying to do. I was trying to enable the feature that grub2 itself would change the default each time I selected a kernel to boot.

I also searched that documentation for an alternative to grub-editenv once I realized I needed to change the contents of /boot/grub2/grubenv. I couldn’t find that either, though I expect it is there. grubby does some of what grub-editenv does.

My Fedora is on btrfs, but that shouldn’t and doesn’t matter.

My grub2 partition is not on btrfs. I didn’t think it could be.

Each time the system is a new kernel is installed or grub2-mkconfig is run the /boot/grub2/grubenv file is rewritten. Each boot updates it. The value in the saved-entry line there is used to select which kernel to boot, and AFAIK that value is set when the kernel is updated and normally is the newest kernel installed.

Changing the value of GRUB_DEFAULT in /etc/default/grub will rewrite /boot/grub2/grubenv to select the chosen kernel when grub2-mkconfig is run. The allowed values only seem to be ‘saved’, the index number [0-2], or the kernel name. The kernel that boots with the first 2 options depends on which kernels are currently installed and the kernel name would lock that one kernel to always default boot.

How to make it dynamic so the default follows the selected kernel chosen from the grub menu is beyond my skills at this point. I perused the entire document I linked above as well as the man page for grubby and cannot see any way to make selection of the default kernel dynamic.

I can see that it may be possible to write a script that would alter the content of /boot/grub2/grubenv after the boot has completed to set it for automatically booting the then running kernel in subsequent boots, but it seems that feature is not available with grub as it exists today.

You should have been able to use grub2-editenv?
Or am I misunderstanding.

1 Like

I think grub2-editenv may allow editing or viewing the content of grubenv, but the use case for the OP seems something that is beyond the bounds of what grub or grubby is capable of. He is asking to make the default boot kernel dynamic so it follows whichever kernel he last selected for booting – and that kernel becomes the default going forward.

I can find no way to do that within the current grub2 configs.

Thanks. I never saw that in any of pages I found with google in trying to deal with these grub2 issues. Even though the pages were about grub2, they all said grub-editenv, just like the file is grubenv, not grub2env.

I just now checked the man page for grub2-editenv and it matches the man page for grub-editenv including the name of the command grub-editenv

So until I saw your post, I never thought to try with the 2.

First attempt to use it, I trashed the whole grubenv file, still don’t understand what I did wrong. But the set command works, so I easily put back the save_default=true. I hope that’s all I really need.

1 Like

I don’t know whether we are misunderstanding each other or what. But try looking for
GRUB_SAVEDEFAULT=true
It is described in many many places including grub2 documentation and Fedora documentation and described as doing exactly what I asked about and now have working.

My issue is that setting does not directly establish that feature. It only tells the next use of grub2-mkconfig to enable that feature, and I have problems with grub2-mkconfig, so I needed to know (and still have found nowhere documented) what really enables that feature.

I now know it is save_default=true in the file /boot/grub2/grubenv

I found that with more controlled misuse of grub2-mkconfig, rather than from google and/or grub2 documentation.

Ok, I had not found that, so thanks for the info.
I added GRUB_SAVEDEFAULT=true into /etc/default/grub (GRUB_DEFAULT=saved was already there) then ran grub2-mkconfig -o /boot/grub2/grub.cfg and it rebuilt the grub.cfg file as well as adding the save_default=true into the /boot/grub2/grubenv file.

I am rebooting now to test it.

Update:
I rebooted twice and the kernel booted each time was the last one selected and successfully booted.
Thus it seems that for your option to remain active with kernel updates the only thing required is to add GRUB_SAVEDEFAULT=true into /etc/default/grub. Each time grub.cfg is rebuilt after that the /boot/grub2/grubenv file should contain the option save_default=true and the last kernel selection will remain active until the user changes it while booting.

Again, thank you for guiding me to locate that particular option for the /etc/default/grub file.

Then it should work: you need two entries in /etc/default/grub:

# Remember the last choice
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true

and regenerate grub with grub2-mkconfig.

It works on my installation, but, as I told you, it does not work if the booted OS is on BTRFS (I’m talking about the OS you’re booting, not the partition where grub is installed)

I thought that was only if BTRFS was used for /boot, 2095363 – Grub2 GRUB_SAVEDEFAULT fails with btrfs boot partition with the error "sparse file not allowed"

I think I now understand the miscommunication there:
My actual OS image (vmlinuz-6.0.7-100.fc35.x86_64) is in the boot partition, next to the grub2 directory (that holds grub.cfg and grubenv). That partition is not BTRFS.

The / partition (everything other than /boot) is BTRFS

I didn’t know (before reading the above link) that grub2 could work at all with BTRFS for /boot

I interpreted the original “are you on BTRFS” question as meaning /
But what matters for that issue seems to be where grub2/grubenv is located, which is not BTRFS