Friday, September 23, 2016

Manually installing UEFI and grub2 on USB already have Ubuntu

You might have installed Ubuntu on USB drive ( using steps similar to my other blog)  but UEFI and grub is not installed on USB drive hence you can not use same USB drive to boot Ubuntu on other system.

A Little Theory


1-UEFI industry standard boot procedure for personal computers from 2006. Firmware of your computer hardware knows that it need to look far 'Unifed Extensible Firmware Interface' install on available storage.

2-System native firmware scans all storage devices for ESP ( EFI System Partition - a variant of vfat32 partition with boot flag enabled). gparted shows boot and esp flags for this partition. Each storage device must have only on ESP.

3-Firmware mounts this (a variant of vfat32) partition to /boot/efi and locates /boot/efi/EFI/

/*..efi file and execute it. ( remember - filename on vfat32 partition is case insensitive).  It triggers grub. This file has location of grub. See this for more detail. 

4-UEFI does not rely on boot sector. Instead it invoke boot manager like - grub. In turn, grub menu will appear. Grub configuration have /boot partition information from where  kernel and initrd will be  loaded, uncopressed and executed. they will mount root flesystem and trigger user space process init ( pid=1 ).


Installing UEFI and grub2 on a same USB from which Ubuntu is booted


You are able to boot Ubuntu installed on an USB via a chainloder boot loader and want to install EFI and grub2 on this. Follow below seps to install EFI and grub.

1-Ensure you have an ESP partition of USB Flash drive - sdX and it is mount on /boot/efi. Use gparted to check flag of this partition - boot, esp
     df -PhalT /boot/efi Filesystem Type Size Used Avail Use% Mounted on /dev/sdb2 vfat 286M 132K 286M 1% /boot/efi


2-Move old stuff somewhere and install EFI and grub

mkdir /var/tmp/boot_efi_backup
mv /boot/efi/* /var/tmp/boot_efi_backup
apt-get install --reinstall grub-efi-amd64
grub-install --removable  --boot-directory=/boot --efi-directory=/boot/efi /dev/sdX
( above command use whole disk as last argument - do not partition /dev/sdX1 )
find /boot/efi/ /boot/efi/ /boot/efi/EFI /boot/efi/EFI/BOOT /boot/efi/EFI/BOOT/BOOTX64.EFI

3-Look for some interesting thing it has.

strings /boot/efi/EFI/BOOT/BOOTX64.EFI |grep -Ei "grub|hd|setuuid|root|boot|set"|less 
 ghex /boot/efi/EFI/BOOT/BOOTX64.EFI

4-Reboot with UEFI ready USB
 


Installing EFI and grub2 on a USB having Ubuntu while booted from other Ubuntu


If you want to install EFI and grub2 on a USB having an Ubuntu by mounting it on another instance of Ubuntu, Installer Media OS, Rescue CD, Live DVD etc., follow below steps. Creates ESp partition on USB if it dows not have. If you can not create it, you can not install UEFI !

1-Mount root, boot and EFI partition
   mount /dev/sdYn  /mnt
   mount /dev/sdYn  /mnt/boot
   mount /dev/sdYp  /mnt/boot/efi

2-Take backup, chroot, install EFI and grub2

  mkdir /mnt/boot_efi_backup
  mv /mnt/boot/efi/* /mnt/boot_efi_backup/
  chroot /mnt
apt-get install --reinstall grub-efi-amd64
grub-install --removable  --target=x86_64-efi --boot-directory=/mnt/boot --efi-directory=/mnt/boot/efi /dev/sdY

4-Reboot with UEFI ready USB

Installing grub2 in BIOS mode.

If your system do not support UEFI mode, you need to install grub2 in legacy BIOS mode. It will install grub on MBR of disk sdX

apt-get install grub-pc-bin grub-install --removable --target=i386-pc --debug --boot-directory=/boot /dev/sdX


  

No comments:

Post a Comment