Saturday, June 10, 2017

UEFI system boot up and GPT partitioning system explained - with an example using parted

I am writing this post in an effort to explain the new UEFI booting system, because for some reason I have found very difficult to have all this simple concepts explained in a concise way out there.
So I will attempt to explain them here in a very high level and simple way.

I will be using the parted program as a way to explicitly show how the partitions are created in an empty hard drive. Parted only runs on Linux, but it can be used from a Linux bootable cd/dvd/usb. Nowadays most Linux distributions have a usable system that runs with the installation cd/dvd/usb.
This part is more directed to Linux users who are usually more used to the command line, but the concepts are operating system independent.

I prefer to use parted in the command line to be able to leave the concepts clearer by following the steps one by one, and not use gparted or some other graphical frontend that does a lot of "magic" by itself.

To run the parted program, go into the command line and type:
> sudo parted /dev/sdX 
(being X the hard drive letter, usually 'a')

You can see the parted reference with the 'help' command:
parted> help 

And the parted reference for a specific command with:
parted> help [command]

Regarding hard drive partition organization and booting, there are 3 main new concepts, all named after their initials:

GPT (Guid Partition Table)

GPT is the new partition table system. Before it, "msdos" was usually used on desktop computers, and all the information regarding partitions was stored in the MBR (Master Boot Record).

Using the parted program, in an empty unpartitioned hard drive, the command "mklabel" creates this new partition table system in the hard drive like this:
parted> mklabel gpt
Please take into account that if the hard drive is not empty this will erase all the information in it!!


ESP (Efi System Partition)

ESP is a special partition formatted with a fat32 filesystem with the "esp" flag added to it that usually goes at the beginning of the disk and has a size of around 100~512 MB.
This partition will have (after operating system installation) the bootloaders for the different operating systems.
With the old system, the bootloader program needed to be in a specific small part of the MBR with very limited space, making it hard for bootloader programmers to be able to fit the program in this small space.

This partition can be created with parted like this:
parted> mkpart primary fat32 1049KB 525MB
This basically tells parted to create a fat32 partition that goes from the 1049KB to 525MB (around 524 MB size) part of the disk.
I used this arbitrary numbers because they were the numbers that were on my laptop disk, but any numbers can be used as long as the partition has a minimun of 100 MB.

This command only creates the partition but does not create the actual filesystem, to do that under Linux, one can use the "mkfs.vfat" command like this:
> sudo mkfs.vfat -F32 /dev/sdX1
(note that you have to quit parted first)

Then the disk can continue to be partitioned...


UEFI (Unified Extensible Firmware Interface)

UEFI is the new standard for booting which uses the ESP partition described (and created) above. It came to replace the old MBR booting.
The UEFI system is available in newer computers (usually it can be enabled or disabled in the system setup, if disabled it goes back to MBR booting), when the computer boots, the UEFI system (built into the motherboard) looks for an ESP partition, search for the bootloaders there, and transfer the control to them. The bootloader is then in charge of booting the correspondent operating system.

Once the operating system is installed, one can browse this partition with a file explorer, and see that the installer automatically put the bootloaders there. For example after the installation of Ubuntu Linux, the grub bootloader is on: "/EFI/ubuntu/grubx64.efi"

Usually on UEFI systems, the bootloader to use can be selected from the system setup, this makes possible (although not very convenient) in the case of having multiple operating system to boot any of them by just selecting it in the system setup.
Taking this into account, for example when you install Windows on a machine that already has Linux, Windows puts it bootloader to boot first, disabling the GRUB bootloader which is the standard bootloader in Linux. But thanks to this feature of selecting the bootloader from the system setup, one could re enable GRUB to have priority over the windows boot up.


These are the very basic concepts that are necessary to understand at a VERY high level how the Operating system booting works under UEFI.
Please let me know in the comments if there is a technical or concept error.