grub2-editenv: error: environment block too small | Simple Solution

Introduction

In todays quick guide, let’s focus on how to resolve the grub2-editenv: error: environment block too small error.

This error occurs when we have a corrupted grub environment file. The expected size of /boot/grub2/grubenv or /boot/efi/EFI/redhat/grubenv should not exceed exactly more than 1024 bytes. In most case, this could happen during some grub customization or during OS Update/Upgrade and reboot. Additionally, we may be required to reinstall the current kernel version.

How can be rectified from this error? Using grub2-editenv command we can regenerate the /boot/grub2/grubenv or /boot/efi/EFI/redhat/grubenv files. I have faced this issue on an RHEL 8.2 Linux server, however, the same fix can be applied for RHEL 7/8 versions and CentOS Linux 7/8 versions.

Another grub solution you may be interested are below.

The Actual Error

I’m trying to set the default kernel using grubby command and getting error as “grub2-editenv: error: environment block too small“.

[root@gruberror ~]# grubby --set-default-index=0
 grub2-editenv: error: environment block too small.
 The default is /boot/loader/entries/9cadf8d3a8ce4efcae3f028610e0e89f-4.18.0-240.el8.x86_64.conf with index 0 and kernel /boot/vmlinuz-4.18.0-240.el8.x86_64
 [root@gruberror ~]# 

Listing Grub environment (grub2-editenv)

Let’s start now. List the the grub environment files.

[root@gruberror ~]# grub2-editenv list
 saved_entry=9cadf8d3a8ce4efcae3f028610e0e89f-4.18.0-240.el8.x86_64
 kernelopts=root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet 
 boot_success=0
 [root@gruberror ~]# 

The actual file size should be 1024 bytes as we said before, the actual file is linked under /boot/grub2/grubenv

[root@gruberror ~]# ls -ltr /boot/grub2/grubenv 
 lrwxrwxrwx. 1 root root 25 Aug 31 14:50 /boot/grub2/grubenv -> ../efi/EFI/redhat/grubenv
 [root@gruberror ~]# 
 [root@gruberror ~]# ls -ltr /boot/efi/EFI/redhat/grubenv 
 -rwx------. 1 root root 1024 Dec 12 00:54 /boot/efi/EFI/redhat/grubenv
 [root@gruberror ~]# 

Regenerate the issue

Let’s start to regenerate the issue and fix it by editing the environment block.

For demonstration purpose just i’m going to increase the file size more than 1024 bytes using fallocate command.

[root@gruberror ~]# 
[root@gruberror ~]# fallocate -l 2024 /boot/efi/EFI/redhat/grubenv 
[root@gruberror ~]#

Now the size is different than 1024 bytes.

[root@gruberror ~]# ls -ltr /boot/efi/EFI/redhat/grubenv 
 -rwx------. 1 root root 2024 Dec 12 01:03 /boot/efi/EFI/redhat/grubenv
 [root@gruberror ~]#

In case, if I have installed with multiple kernels and I need to set the latest kernel as default. I may run the grubby command. But, when we try with the grubby command it will throw some error, the cause of the error is due to the wrong file size of grubenv file.

[root@gruberror ~]# grubby --set-default-index=0
grub2-editenv: error: environment block too small.
The default is /boot/loader/entries/9cadf8d3a8ce4efcae3f028610e0e89f-4.18.0-240.el8.x86_64.conf with index 0 and kernel /boot/vmlinuz-4.18.0-240.el8.x86_64
[root@gruberror ~]#

So, before setting the default kernel using grubby command i need to fix this error.

The Actual Fix

Run the command # grub2-editenv create to generate a new grubenv file, this will restore the size with appropriate content in it.

[root@gruberror ~]# grub2-editenv create
[root@gruberror ~]# 
[root@gruberror ~]# ls -ltr /boot/efi/EFI/redhat/grubenv 
-rwx------. 1 root root 1024 Dec 12 01:09 /boot/efi/EFI/redhat/grubenv
[root@gruberror ~]#

That’s it we have done with resolving the issue.

Conclusion

When the grubenv file size changes it will throw the above error, once we regenerate the file the size will be back to normal and which will resolve the issue. Subscribe to our newsletter for more troubleshooting guides in future.