Lilnux (debian): How to switch from pc to efi and recreate boot partition

root's picture

You need to swich from bios to efi? Read on.
DISCLAIMER: you need to be familiar with these commands and make sure you have backup because you may destroy your linux if you do not pay attention.

1. List the existing partiton

# fdisk -l /dev/sda
Output:

root@vboxbox:~# fdisk -l /dev/sda
Disk /dev/sda: 14.7 GiB, 15724707840 bytes, 30712320 sectors
Disk model: Kingston DataTra
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xfcf559cd

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1  *      2048   499711   497664  243M 83 Linux
/dev/sda2       501758 30711807 30210050 14.4G  5 Extended
/dev/sda5       501760 30711807 30210048 14.4G 8e Linux LVM

2. Must convert your partition table from MBR to GPT

The commant gdisk confirms you have MBR:

root@vboxbox:~# gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. 
***************************************************************

Disk /dev/sda: 30712320 sectors, 14.6 GiB
Model: Kingston DataTra
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): A3E95EC8-299A-4799-B553-CF4DE54438FB
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 30712286
Partitions will be aligned on 2048-sector boundaries
Total free space is 4541 sectors (2.2 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          499711   243.0 MiB   8300  Linux filesystem
   5          501760        30711807   14.4 GiB    8E00  Linux LVM

Transform the partition table from MBR to GPT:
1. run gdisk /dev/sda
2. type r
3. type f (choose Y)
4. type w (choose Y)

root@vboxbox:~# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************


Command (? for help): r

Recovery/transformation command (? for help): f
Warning! This will destroy the currently defined partitions! Proceed? (Y/N): y

Recovery/transformation command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

Now, if you run again gdisk -l, you should have GPT present:

root@vboxbox:~# gdisk -l /dev/sda
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 30712320 sectors, 14.6 GiB
Model: Kingston DataTra
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 482F1B37-C2BB-470E-A8E6-16F9AE60F9D9
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 30712286
Partitions will be aligned on 2048-sector boundaries
Total free space is 4541 sectors (2.2 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          499711   243.0 MiB   8300  Linux filesystem
   5          501760        30711807   14.4 GiB    8E00  Linux LVM

Shrink /boot filesystem

# umount /boot; e2fsck -f /dev/sda1; resize2fs /dev/sda1 200M

root@vboxbox:~# resize2fs /dev/sda1 200M
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/sda1 is mounted on /boot; on-line resizing required
resize2fs: On-line shrinking not supported
root@vboxbox:~# umount /boot
root@vboxbox:~# resize2fs /dev/sda1 200M
resize2fs 1.44.5 (15-Dec-2018)
Please run 'e2fsck -f /dev/sda1' first.

root@vboxbox:~# e2fsck -f /dev/sda1
e2fsck 1.44.5 (15-Dec-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda1: 343/62248 files (24.2% non-contiguous), 103564/248832 blocks
root@vboxbox:~# resize2fs /dev/sda1 200M
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/sda1 to 204800 (1k) blocks.
The filesystem on /dev/sda1 is now 204800 (1k) blocks long.

root@vboxbox:~# mount /boot
root@vboxbox:~# df -h /boot
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       194M   93M   91M  51% /boot

Shrink boot partition and create boot efi partition (that will become /boot/efi)

NOTE: here you may wreck your boot partition so if you want to be on a safe side, you can save it's content. If you skip this, no worries: everything in boot can be recreated and is presented in this tutorial.
Choose whatever method you want (fdisk, cfdisk, etc.).
By default, debian installation creates /boot partition with 244M in size. So in order to have an efi partition next to the existing boot, we need to reduce /boot to 200M and create another efi partition 44M in size
At the end you should have:

root@vboxbox:~# fdisk -l /dev/sda
Disk /dev/sda: 14.7 GiB, 15724707840 bytes, 30712320 sectors
Disk model: Kingston DataTra
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 482F1B37-C2BB-470E-A8E6-16F9AE60F9D9

Device      Start      End  Sectors  Size Type
/dev/sda1    2048   411647   409600  200M Linux filesystem
/dev/sda2  411648   501759    90112   44M EFI System
/dev/sda5  501760 30711807 30210048 14.4G Linux LVM

If the newly created partition is not listed with "EFI System" type, you can modify it with gdisk:
1. run gdisk /dev/sda
2. type t
3. choose partition number 2
4. choose code EF00
5 type w (choose Y)

root@vboxbox:~# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): t
Partition number (1-5): 2
Current type is 'EFI System'
Hex code or GUID (L to show codes, Enter = 8300): EF00
Changed type of partition to 'EFI System'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

Format efi partition as vfat

# mkfs.vfat /dev/sda2

root@vboxbox:~# mkfs.vfat /dev/sda2
mkfs.fat 4.1 (2017-01-24)

If you get the following error (or any other error), do this after reboot.

root@vboxbox:~# mkfs.vfat /dev/sda2
mkfs.fat 4.1 (2017-01-24)
mkfs.vfat: Attempting to create a too large filesystem

Did I say reboot? Yes, you need to do that in order to enable UEFI in BIOS.

Boot in rescue mode with linux installation disk and mount the root partition

1. Advanced options → Rescue mode
2. Configure keyboard, hostname, domain and network.
3. Chose device to use as root system, e.g. /dev/rootvg/root
4. Mount separate /boot partition? -> YES
5. Choose "Execute a shell in /dev/rootvg/root.

Update /etc/fstab to reflect the modifications above

In our example, sda1 is /boot and sda2 is new efi boot (which will be /boot/efi)
List partition UUIDs:

root@debian:/# ls -la /dev/disk/by-uuid/ | grep sd
lrwxrwxrwx 1 root root  10 Nov  7 12:16 0a277628-c287-4d28-81a0-c6b6ab590479 -> ../../sda1
lrwxrwxrwx 1 root root  10 Nov  7 12:16 FF09-EF8F -> ../../sda2

Replace UUID of /boot for sda1 and create another entry with the UUID of sda2. At the end, you should have the following for boot:

root@debian:/# grep boot /etc/fstab 
UUID=0a277628-c287-4d28-81a0-c6b6ab590479	/boot	ext2	defaults	0	2
UUID=FF09-EF8F	/boot/efi	vfat	defaults	0	2

Create /boot/efi mount point and mount it

# mkdir -p /boot/efi
# mount -a

root@debian:/# mount | grep boot
/dev/sda1 on /boot type ext2 (rw,relatime)
/dev/sda2 on /boot/efi type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro)

If you have created correctly the entries in /etc/fstab, you should have /boot/efi mounted now. If not, you will need to mount it manually. Do not forget to correct /etc/fstab.
mount -t vfat /dev/sda2 /boot/efi

Install grub-efi (will uninstall grub-pc)

# apt install grub-efi

root@debian:/# apt install grub-efi
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  grub-pc-bin
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
  efibootmgr grub-efi-amd64 grub-efi-amd64-bin grub-efi-amd64-signed mokutil shim-helpers-amd64-signed shim-signed shim-signed-common
  shim-unsigned
Recommended packages:
  secureboot-db
The following packages will be REMOVED:
  grub-pc
The following NEW packages will be installed:
  efibootmgr grub-efi grub-efi-amd64 grub-efi-amd64-bin grub-efi-amd64-signed mokutil shim-helpers-amd64-signed shim-signed
  shim-signed-common shim-unsigned
0 upgraded, 10 newly installed, 1 to remove and 0 not upgraded.
Need to get 2,557 kB of archives.
After this operation, 20.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

[...]

Install grub on disk

grub-install /dev/sda

root@debian:/# grub-install /dev/sda
Installing for x86_64-efi platform.
Installation finished. No error reported.

NOTE: if you forgot to switch to EFI in BIOS, you can still continue but you will have to execute the above command as follows, else you will encounter errors.

root@debian:/# grub-install --target x86_64-efi /dev/sda
Installing for x86_64-efi platform.
grub-install: warning: EFI variables are not supported on this system..
Installation finished. No error reported.

[h3]Now /boot/efi should be populated and you should have the following file present:[/h3
# file /boot/efi/EFI/debian/grubx64.efi

Also, debian entry should be listed when running the following command

# efibootmgr

root@vboxbox:~# efibootmgr 
BootCurrent: 0005
BootOrder: 0002,0001,0003,0000,0005,0004
Boot0000* Kingston DataTraveler SE9 Media 
Boot0000$ASN  
Boot0001* Virtual DVD-ROM [1]             
Boot0001$ASN  
Boot0002* EFI DVD/CDROM
Boot0002$ASN  
Boot0003* EFI Hard Drive
Boot0003$ASN  
Boot0004* EFI Internal Shell
Boot0004$ASN  
Boot0005* debian
Boot0005$ASN  
root@vboxbox:~#

Reboot, change boot mode to EFI and see the magic.

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root