grub-install fails because of efibootmgr : Could not prepare Boot variable: Invalid argument : HELP

5,083

Make sure you're booted in UEFI mode and disable secure boot in your bios

after mounting your partitions and chroot to the system try

grub-install --target=x86_64-efi --bootloader-id=GRUB --efi-directory=/boot/efi --no-nvram --removable
Share:
5,083

Related videos on Youtube

SebMa
Author by

SebMa

Updated on September 18, 2022

Comments

  • SebMa
    SebMa over 1 year

    After a sudo apt upgrade, grub-install failed so I had to reboot on a Live Ubuntu 18.04 USB.

    After mounting /proc, /dev, /dev/pts, /run, /sys, /sys/firmware/efi/efivars into /mnt/destinationVGDir/ I chroot to my ubuntu on /dev/sda and re-run grub-install :

    ubuntu@ubuntu:~$ sudo chroot /mnt/destinationVGDir/
    root@ubuntu:/# mount -a # Mounts all other filesystems including /boot/efi
    root@ubuntu:/# grub-install 
    Installing for x86_64-efi platform.
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 18151: grub-install
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 18151: grub-install
    Could not prepare Boot variable: Invalid argument
    grub-install: error: efibootmgr failed to register the boot entry: Input/output error.
    

    If do a grub-install -v, one can see that efibootmgr is the one failing here :

    root@ubuntu:/# grub-install -v 2>&1 | uniq | tail
    grub-install: info: copying `/usr/lib/shim/BOOTX64.CSV' -> `/boot/efi/EFI/ubuntu/BOOTX64.CSV'.
    grub-install: info: copying `/boot/grub/x86_64-efi/load.cfg' -> `/boot/efi/EFI/ubuntu/grub.cfg'.
    grub-install: info: copying `/usr/lib/shim/shimx64.efi.signed' -> `/boot/efi/EFI/Boot/bootx64.efi'.
    grub-install: info: copying `/usr/lib/shim/fbx64.efi' -> `/boot/efi/EFI/Boot/fbx64.efi'.
    grub-install: info: Registering with EFI: distributor = `ubuntu', path = `\EFI\ubuntu\shimx64.efi', ESP at hostdisk//dev/sda,gpt1.
    grub-install: info: executing efibootmgr --version </dev/null >/dev/null.
    grub-install: info: executing modprobe -q efivars.
    grub-install: info: executing efibootmgr -c -d /dev/sda -p 1 -w -L ubuntu -l \EFI\ubuntu\shimx64.efi.
    Could not prepare Boot variable: Invalid argument
    grub-install: error: efibootmgr failed to register the boot entry: Input/output error.
    root@ubuntu:/# efibootmgr -c -d /dev/sda -p 1 -w -L ubuntu -l \EFI\ubuntu\shimx64.efi
    Could not prepare Boot variable: Invalid argument
    root@ubuntu:/# efibootmgr -v -v -c -d /dev/sda -p 1 -w -L ubuntu -l \EFI\ubuntu\shimx64.efi
    Could not prepare Boot variable: Invalid argument
    error trace:
     efivarfs.c:351 efivarfs_set_variable(): write failed: Invalid argument
     lib.c:81 _efi_set_variable_mode(): ops->set_variable() failed: Invalid argument
     efibootmgr.c:323 make_var(): efi_set_variable failed: Invalid argument
     efibootmgr.c:333 make_var(): Could not set variable Boot0000: Invalid argument
    root@ubuntu:/# 
    

    My SSD is fine :

    ubuntu@ubuntu:~$ sudo smartctl -A /dev/sda
    smartctl 6.6 2016-05-31 r4324 [x86_64-linux-5.4.0-42-generic] (local build)
    Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org
    
    === START OF READ SMART DATA SECTION ===
    SMART Attributes Data Structure revision number: 1
    Vendor Specific SMART Attributes with Thresholds:
    ID# ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE
      5 Reallocated_Sector_Ct   0x0033   100   100   010    Pre-fail  Always       -       0
      9 Power_On_Hours          0x0032   099   099   000    Old_age   Always       -       1947
     12 Power_Cycle_Count       0x0032   099   099   000    Old_age   Always       -       673
    177 Wear_Leveling_Count     0x0013   099   099   000    Pre-fail  Always       -       1
    179 Used_Rsvd_Blk_Cnt_Tot   0x0013   100   100   010    Pre-fail  Always       -       0
    181 Program_Fail_Cnt_Total  0x0032   100   100   010    Old_age   Always       -       0
    182 Erase_Fail_Count_Total  0x0032   100   100   010    Old_age   Always       -       0
    183 Runtime_Bad_Block       0x0013   100   100   010    Pre-fail  Always       -       0
    187 Uncorrectable_Error_Cnt 0x0032   100   100   000    Old_age   Always       -       0
    190 Airflow_Temperature_Cel 0x0032   069   049   000    Old_age   Always       -       31
    195 ECC_Error_Rate          0x001a   200   200   000    Old_age   Always       -       0
    199 CRC_Error_Count         0x003e   100   100   000    Old_age   Always       -       0
    235 POR_Recovery_Count      0x0012   099   099   000    Old_age   Always       -       30
    241 Total_LBAs_Written      0x0032   099   099   000    Old_age   Always       -       1179332232
    

    EDIT0 : Here are the special filesystems I mounted prior to the chroot :

    root@ubuntu:/# mount | grep -v ext4
    proc on /proc type proc (rw,relatime)
    tmpfs on /run type tmpfs (rw,relatime)
    udev on /dev type devtmpfs (rw,nosuid,relatime,size=2947064k,nr_inodes=736766,mode=755)
    devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
    sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
    efivarfs on /sys/firmware/efi/efivars type efivarfs (rw,nosuid,nodev,noexec,relatime)
    /dev/sda1 on /boot/efi type vfat (rw,noatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
    root@ubuntu:/# 
    

    EDIT1 : Thanks to @SamyAbdellatif, this error does not appear by adding --no-nvram option to the grub-install command :

    root@ubuntu:~# grub-install --no-nvram 
    Installing for x86_64-efi platform.
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 6065: grub-install
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 6065: grub-install
    Installation finished. No error reported.
    root@ubuntu:~# 
    

    Now If I re-install the grub-efi-amd64-signed to simulate an update, the --no-nvram option of grub-install is not used and the pb. re-appears :

    root@ubuntu:~# apt install --reinstall grub-efi-amd64-signed
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following NEW packages will be installed:
      grub-efi-amd64-signed
    0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
    Need to get 0 B/300 kB of archives.
    After this operation, 3459 kB of additional disk space will be used.
    Selecting previously unselected package grub-efi-amd64-signed.
    (Reading database ... 475217 files and directories currently installed.)
    Preparing to unpack .../grub-efi-amd64-signed_1.93.22+2.02-2ubuntu8.20_amd64.deb ...
    Unpacking grub-efi-amd64-signed (1.93.22+2.02-2ubuntu8.20) ...
    Setting up grub-efi-amd64-signed (1.93.22+2.02-2ubuntu8.20) ...
    Installing for x86_64-efi platform.
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 6191: grub-install
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 6191: grub-install
    Could not prepare Boot variable: Invalid argument
    grub-install: error: efibootmgr failed to register the boot entry: Input/output error.
    dpkg: error processing package grub-efi-amd64-signed (--configure):
     installed grub-efi-amd64-signed package post-installation script subprocess returned error exit status 1
    Errors were encountered while processing:
     grub-efi-amd64-signed
    E: Sub-process /usr/bin/dpkg returned an error code (1)
    

    EDIT2: When I rebooted, there was just a black screen. After re-checking with the Live USB, I can see no EFI boot entry for Ubuntu :

    root@ubuntu:~# efibootmgr | grep -i ubuntu
    root@ubuntu:~# 
    

    EDIT3 : I removed the grub-efi-amd64-signed package because mokutil --sb-state says This system doesn't support Secure Boot and reinstalled grub-efi-amd64 :

    root@ubuntu:~# apt install --reinstall -V grub-efi-amd64
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
    Need to get 0 B/47.9 kB of archives.
    After this operation, 0 B of additional disk space will be used.
    Preconfiguring packages ...
    (Reading database ... 475223 files and directories currently installed.)
    Preparing to unpack .../grub-efi-amd64_2.02-2ubuntu8.20_amd64.deb ...
    Unpacking grub-efi-amd64 (2.02-2ubuntu8.20) over (2.02-2ubuntu8.20) ...
    Setting up grub-efi-amd64 (2.02-2ubuntu8.20) ...
    Installing for x86_64-efi platform.
    File descriptor 3 (pipe:[189443]) leaked on vgs invocation. Parent PID 16137: grub-
    install
    File descriptor 5 (/dev/sda1) leaked on vgs invocation. Parent PID 16137: grub-inst
    all
    File descriptor 3 (pipe:[189443]) leaked on vgs invocation. Parent PID 16137: grub-
    install
    File descriptor 5 (/dev/sda1) leaked on vgs invocation. Parent PID 16137: grub-inst
    all
    Could not prepare Boot variable: Invalid argument
    grub-install: error: efibootmgr failed to register the boot entry: Input/output err
    or.
    Failed: grub-install --target=x86_64-efi --no-extra-removable 
    WARNING: Bootloader is not properly installed, system may not be bootable
    Sourcing file `/etc/default/grub'
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-5.4.0-42-generic
    Found initrd image: /boot/initrd.img-5.4.0-42-generic
    Found linux image: /boot/vmlinuz-4.15.0-128-generic
    Found initrd image: /boot/initrd.img-4.15.0-128-generic
    Found linux image: /boot/vmlinuz-4.15.0-122-generic
    Found initrd image: /boot/initrd.img-4.15.0-122-generic
    done
    

    There is still a WARNING: Bootloader is not properly installed, system may not be bootable error so I run grub-install manually with the --no-nvram option :

    root@ubuntu:~# grub-install --no-nvram
    Installing for x86_64-efi platform.
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 20234: grub-install
    File descriptor 4 (/dev/sda1) leaked on vgs invocation. Parent PID 20234: grub-install
    Installation finished. No error reported
    

    grub-install --no-nvram reports no error but does NOT create any ubuntu EFI boot entry :

    root@ubuntu:~# efibootmgr | grep -i ubuntu
    root@ubuntu:~# 
    

    EDIT3.1 : grub-install --removable reports no error but does NOT create any ubuntu EFI boot entry either.

    EDIT4 : My /etc/fstab :

    root@ubuntu:~# grep -v ^# /etc/fstab
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/rootFS /               ext4    errors=remount-ro 0       1
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/boot /boot           ext4    defaults,noatime        0       2
    UUID=9ADF-05E5  /boot/efi       vfat    defaults,noatime        0       1
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/home /home           ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/opt /opt            ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/tmp /tmp            ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/usr /usr            ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/usr_local /usr/local      ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/var /var            ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/var_cache /var/cache      ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/var_lib_snapd /var/lib/snapd           ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/var_mail /var/mail      ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/var_tmp /var/tmp        ext4    defaults,noatime        0       2
    /dev/VG_ALL_Samsung_SSD_850_EVO_500GB/photos /photos        ext4    defaults,noatime        0       2
    root@ubuntu:~# 
    

    And the output of lsblk :

    root@ubuntu:~# lsblk 
    NAME                                               MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    loop0                                                7:0    0     2G  1 loop 
    loop1                                                7:1    0  29.9M  1 loop 
    loop2                                                7:2    0    55M  1 loop 
    loop3                                                7:3    0 255.6M  1 loop 
    loop4                                                7:4    0  62.1M  1 loop 
    loop5                                                7:5    0  49.8M  1 loop 
    sda                                                  8:0    0 465.8G  0 disk 
    ├─sda1                                               8:1    0   256M  0 part /boot/efi
    └─sda2                                               8:2    0 465.5G  0 part 
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-boot          253:0    0     1G  0 lvm  /boot
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-opt           253:1    0     7G  0 lvm  /opt
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-rootFS        253:2    0     2G  0 lvm  /
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-var_lib_snapd 253:3    0    10G  0 lvm  /var/lib/snapd
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-tmp           253:4    0    10G  0 lvm  /tmp
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-usr_local     253:5    0     7G  0 lvm  /usr/local
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-usr           253:6    0    20G  0 lvm  /usr
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-var_cache     253:7    0     5G  0 lvm  /var/cache
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-var_mail      253:8    0    20G  0 lvm  /var/mail
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-var_tmp       253:9    0     2G  0 lvm  /var/tmp
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-var           253:10   0     6G  0 lvm  /var
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-VMs           253:11   0  75.5G  0 lvm  
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-.q            253:12   0    90G  0 lvm  
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-home          253:13   0   100G  0 lvm  /home
      ├─VG_ALL_Samsung_SSD_850_EVO_500GB-datas         253:14   0    60G  0 lvm  
      └─VG_ALL_Samsung_SSD_850_EVO_500GB-photos        253:15   0    50G  0 lvm  
    sdc                                                  8:32   1  28.9G  0 disk 
    ├─sdc1                                               8:33   1  28.9G  0 part 
    │ └─ventoy                                         253:16   0   2.6G  1 dm   
    └─sdc2                                               8:34   1    32M  0 part 
    sr0                                                 11:0    1  1024M  0 rom  
    root@ubuntu:~# 
    

    To re-install grub on my system, do I need to use a newer grub2-common package or a newer efibootmgr package ?

    • oldfred
      oldfred over 3 years
      Are you mounting all the required partitions? UEFI chroot, must include ESP - efi system partition askubuntu.com/questions/53578/…
    • SebMa
      SebMa over 3 years
      @oldfred Yes I did :). See my EDIT0
  • SebMa
    SebMa over 3 years
    --no-vram option seems to be the solution. Now how can I have ubuntu automatically add the --no-vram option to grub-install each times it re-installs grub ?
  • SamyAbdellatif
    SamyAbdellatif over 3 years
    I'm not sure what are you trying to achieve there but in such case you might consider building grub from the source check here
  • SebMa
    SebMa over 3 years
    @SamyAbedellatif Hope my EDIT1 will answer what I'm trying to achieve.
  • SamyAbdellatif
    SamyAbdellatif over 3 years
    the package grub-efi-amd64-signed doesn't have the --no-vram option , but there is a package grub-efi-amd64 that can handle it, refer to this answer
  • SebMa
    SebMa over 3 years
    OK I see. Can u please take a look at my EDIT2 ?
  • SebMa
    SebMa over 3 years
    grub-install --no-nvram reports no error but does NOT create an ubuntu EFI boot entry. See my EDIT3.
  • SamyAbdellatif
    SamyAbdellatif over 3 years
    What’s the contents of the /etc/fstab? And what’s the output of the lsblk command
  • SebMa
    SebMa over 3 years
    See my EDIT4 for that information.
  • SamyAbdellatif
    SamyAbdellatif over 3 years
    Can you run the lsblk -f flag and match the UUID of the boot partition with the one in the fstab >>> UUID=9ADF-05E5 .. also make sure you're in UEFI mode buy checking ls /sys/firmware/efi/efivars if it runs without errors. from that point i'm not sure what could be causing this problem , maybe check the Compatibility Support Mode (CSM) in your BIOS if so disable it.
  • SamyAbdellatif
    SamyAbdellatif over 3 years
    you can also try to turning off Fast Boot UEFI settings . and to edit the GRUB parameter in /etc/default/grub by uncommenting this line #GRUB_DISABLE_LINUX_UUID=true then 'sudo update-grub'
  • SebMa
    SebMa over 3 years
    lsblk -n -f /dev/sda1 -o uuid give 9ADF-05E5 which is the same UUID has that in my /etc/fstab. /sys/firmware/efi/efivars/ contains 51 files. I'm not sure there is a CSM setting in my Bios. Going to check...