Resize root partition on UEFI/GPT machines

It assumes you want to resize root partition (/) on a virtual machine and the partition you want to resize is the last one that has data. Swap would be ok if it’s last.

Example (disk already resized from 5GB to 20GB):

Disk /dev/sda: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 7DCC138E-820C-42FA-AFB4-3E9A89946AFB
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          194559   94.0 MiB    EF00
   2          194560         6639615   3.1 GiB     8300  root
   3         6639616        10483711   1.8 GiB     8200  swap
  1. resize the disk
  2. reboot?
  3. swapoff -a
  4. gdisk /dev/sda
  5. make all new sectors usable
    • go to expert mode with command ‘x’
    • use command ‘e’ to relocate backup data structures to the end of the disk
    • go back to menu (‘m’)
  6. delete partitions sda3 and sda2
  7. re-create partition sda2, type 8300
    • it should start from the same sector to preserve the data
    • leave some space at the end for the swap
  8. re-create sda3, type 8200
  9. optional: rename the partitions with the old (or new) names
  10. write parition table to disk
  11. mkswap /dev/sda3
  12. edit /ets/fstab and update UUID of the swap partition, it probably changed
  13. reboot
  14. resize2fs /dev/sda2

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.