Bobcares

Add EFI Grub in Ubuntu: Configuration and Setup

by | Sep 28, 2022

 Let us take a closer look at how to add EFI grub in ubuntu in a few simple steps with the support of our Server management support services at Bobcares.

Requirements to Install GRUB2 with EFI Support in Ubuntu

Add EFI Grub in Ubuntu

Ubuntu

  • x86 Host Machine: Note: For 64-bit targets, we must have a 64-bit host OS.
  • x86 Target Machine.
  • Desktop Factory with the following options enabled: 1. bash (needed for chroot) 2. grep (needed for grub-install) 3. ncurses Target Software->Kernel->Include kernel image within the RFS.

Configurations to Install GRUB2 with EFI Support in Ubuntu

Build GRUB2 with EFI Support

  1. Open the Desktop Factory configuration interface:

    $ make menuconfig

  2. Navigate to “Target Software->Bootloaders->GRUB2…”
  3. Set “grub2 Platform String” to efi. Save the configuration.
  4. Remove the existing grub2 build:

    $ make grub2-distclean

  5. Rebuild the Factory:

    $ make

Attach the volume to the host machine

Connect a USB stick, Compact Flash, SD card, or hard drive to the host. Using dmesg, identify the device node and unmount any auto-mounting volumes. We’ll assume /dev/sdX is the disk’s name here.

Create a Linux partition on the device for the RFS.

1. Using fdisk, we have to create a Linux partition for the RFS.follow steps given below for configurations:

$ sudo fdisk /dev/sdX

Type o to create a new partition table.

Create the EFI Partition.

  • Type n to create a new partition.
  • To pick a primary partition type, type p.
  • Set Partition Number to 1.
  • Accept the default value for First Sector.
  • Set the Last Sector to +50M.
  • Type t to set the partition type.
  • Type c to set the partition type to W95 FAT32 (LBA).

Create the RFS Partition.

  • Type n to create a new partition.
  • To pick a primary partition type, type p.
  • Set Partition Number to 2.
  • Accept the defaults for the First and Last Sectors.
  • To create a new partition table, enter w.

2. Format the EFI partition using mkfs.vfat:

$ sudo mkfs.vfat -n "efi" /dev/sdX1

3. Format the RFS partition using mkfs.ext4:

$ sudo mkfs.ext4 -L "rfs" /dev/sdX2

Initialize the RFS contents

  1. Mount the volume:

    $ sudo mount -t ext4 /dev/sdX2 /rfs

  2. Change to the mounted directory:

    $ cd rfs

  3. As root, extract the RFS output tarball into the mount point:

    $ sudo tar -xf path to factory/build_* images rfs rootfs.tar.gz

  4. Copy the kernel to the volume and rename it to vmlinuz for autodetection:

    $ sudo mkdir -p boot $ sudo cp path/tofactory/build_* images bzImage- boot/vmlinuz-version

Set up the EFI Partition

The next step to add EFI grub in ubuntu is to set up the EFI partition. The majority of the EFI configuration will be done within the chroot. However, we must still mount the volume. Type in the following command to set up the EFI partition:

$ sudo mount /dev/sdX1 /efi

Using Chroot to install grub on the device.

  1. Enter the RFS directory:

    $ cd /rfs

  2. Create the EFI directory under boot:

    $ sudo mkdir -p boot/efi

  3. Bind mount the /dev, /sys, and /proc directories. This allows us to access the system from the chroot jail.

    $ sudo mount --bind /dev ./dev $ sudo mount --bind /sys ./sys $ sudo mount --bind /proc ./proc

  4. Bind mount the EFI mount point directory as follows:

    $ sudo mount --bind /efi ./boot/efi

  5. Enter the chroot jail:

    32-bit Target
    $ sudo linux32 chroot .
    64-bit Target
    $ sudo linux64 chroot .

  6. Install grub to the Linux partition:

    32-bit Target

    # grub-install --target=i386-efi --efi-directory=/boot/efi --removable --boot-directory=/boot/efi/EFI --bootloader-id=grub /dev/sdX

    64-bit Target

    # grub-install --target=x86_64-efi --efi-directory= boot efi --removable --boot-directory= boot efi EFI --bootloader-id=grub dev sdX

If the efibootmgr application is present in the filesystem, the —removable flag stops it from operating on the host machine. It would otherwise modify the host’s EFI partition, which is undesired.

Create a grub.cfg file under boot grub

Note that In order for grub-mkconfig to recognize the kernel image automatically, it must be called vmlinuz-version>. Alternatively, we may manually add a menu entry pointing to bzImage.

# grub-mkconfig -o /boot/efi/EFI/grub/grub.cfg.

Exit chroot and unmount dev, sys, and proc

To Exit Chroot anf unmounting the dev, sys, and porc type in the following command line:

# exit
$ sudo umount ./dev
$ sudo umount ./sys
$ sudo umount ./proc
$ sudo umount ./boot/efi

Cleanup grub.cfg

At this point, we should modify grub.cfg to correctly configure the root device to add EFI grub in ubuntu.

The produced grub.cfg will specify the incorrect root due to the bind mounted device tree.

  1. Open the grub.cfg file, which is located at /efi/EFI/grub/grub.cfg.
  2. Locate the line that sets the root device, which is usually in the menu entry option:

    set root='hd1,msdos2'.

  3. Change the line to match the second partition of the first hard drive:

    set root='hd0,msdos2'

  4. If we do not use an initrd, we must change the kernel command line arguments to boot from the correct device.

    We could also add more boot options to the kernel command line.

    linux /boot/vmlinuz-3.4-ts-i686 root=/dev/sda2 rw video=vesafb vga=0x318 vmalloc=256MB console=ttyS0,115200.

(OPTIONAL) Add a manual menu-entry to grub.cfg

If the kernel was not identified automatically, or if we just want to add another boot entry, we can manually update grub.cfg.

Enter the following between the 10 linux lines to continue the configurations for adding EFI grub in ubuntu:

### BEGIN etc grub.d 10_linux ### menuentry 'Timesys' --class gnu-linux --class gnu --class os { linux /bzImage } ### END etc grub.d10_linux ###

Create startup.nsh script

At boot time, the EFI shell executes a script called startup.nsh. This file should be placed in the root of the EFI partition:

  1. Create the startup.nsh file and open it in the preferred editor.
  2. After that, Include the following content:

    For 32-bit targets
    FS0: cd efi grub boot.efi
    For 64-bit targets
    FS0: cd efi grub x86_64-efi grub.efi

  3. Copy the file to the EFI partition’s root:

    $ sudo cp startup.nsh efi

Clean up

  1. Continue with the RFS partition:

    $ cd

  2. Unmount the RFS partition as follows:

    $ sudo umount rfs

  3. Unmount the EFI partition as follows:

    $ sudo umount efi

  4. Take the hard disk out of the system and connect it to the target system. This marks the final step in adding an EFI grub in ubuntu.

[Need assistance with similar queries? We are here to help]

Conclusion

To conclude we have now learned how to install or add EFI Grub in Ubuntu within a few simple steps along with one optional step put forward by our Server Management Support Services.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.