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
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
- Open the Desktop Factory configuration interface:
$ make menuconfig
Copy Code - Navigate to “Target Software->Bootloaders->GRUB2…”
- Set “grub2 Platform String” to efi. Save the configuration.
- Remove the existing grub2 build:
$ make grub2-distclean
Copy Code - Rebuild the Factory:
$ make
Copy Code
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
Copy Code
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
Copy Code
3. Format the RFS partition using mkfs.ext4:
$ sudo mkfs.ext4 -L "rfs" /dev/sdX2
Copy Code
Initialize the RFS contents
- Mount the volume:
$ sudo mount -t ext4 /dev/sdX2 /rfs
- Change to the mounted directory:
$ cd rfs
Copy Code - As root, extract the RFS output tarball into the mount point:
$ sudo tar -xf path to factory/build_* images rfs rootfs.tar.gz
Copy Code - 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
Copy Code
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
Copy Code
Using Chroot to install grub on the device.
- Enter the RFS directory:
$ cd /rfs
Copy Code - Create the EFI directory under boot:
$ sudo mkdir -p boot/efi
Copy Code - 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
Copy Code - Bind mount the EFI mount point directory as follows:
$ sudo mount --bind /efi ./boot/efi
Copy Code - Enter the chroot jail:
32-bit Target $ sudo linux32 chroot . 64-bit Target $ sudo linux64 chroot .
Copy Code - Install grub to the Linux partition:
32-bit Target
Copy Code# grub-install --target=i386-efi --efi-directory=/boot/efi --removable --boot-directory=/boot/efi/EFI --bootloader-id=grub /dev/sdX
Copy Code64-bit Target
Copy Code# grub-install --target=x86_64-efi --efi-directory= boot efi --removable --boot-directory= boot efi EFI --bootloader-id=grub dev sdX
Copy Code
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.
Copy Code
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
Copy Code
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.
- Open the grub.cfg file, which is located at /efi/EFI/grub/grub.cfg.
- Locate the line that sets the root device, which is usually in the menu entry option:
.set root='hd1,msdos2'
Copy Code - Change the line to match the second partition of the first hard drive:
set root='hd0,msdos2'
Copy Code - If we do not use an
, we must change the kernel command line arguments to boot from the correct device.initrd
Copy CodeWe 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
Copy Code
(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 ###
Copy Code
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:
- Create the startup.nsh file and open it in the preferred editor.
- 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
Copy Code - Copy the file to the EFI partition’s root:
$ sudo cp startup.nsh efi
Copy Code
Clean up
- Continue with the RFS partition:
$ cd
Copy Code - Unmount the RFS partition as follows:
$ sudo umount rfs
Copy Code - Unmount the EFI partition as follows:
$ sudo umount efi
Copy Code - 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.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments