How to resize partitions on Ubuntu 20.04


Firstly let's talk about what a partition is. A disk partition or partition is a section of the hard drive that is separated from other segments. Partitions enable users to divide a physical disk into logical sections. For example, allowing multiple operating systems to run on the same device.

Increasing the size of the / partition

Step 0 - Updating your system

Start by updating and upgrading your system by running the usual two apt commands

[email protected]:~# sudo apt update
[email protected]:~# sudo apt upgrade

Step 1 - Finding out the swap space.

By running the command fdisk -l we are able to find all the partitions. In this example we have a 50GB drive with 4GB of swap space, and we want to add an extra 100GB to our partition.

Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 sectors
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: 0x0041f8a8

Device     Boot    Start       End  Sectors Size Id Type
/dev/vda1           2048  96471039 96468992  46G 83 Linux
/dev/vda2       96471040 104857599  8386560   4G 82 Linux swap / Solaris

There is no need to reboot the server.

Step 2 - Turning off swap space.

In order to be able to increase the / partition you firstly need to turn off the swap space so make sure you are able to operate without it for a couple of minutes. To do that run the command

[email protected]:~# swapoff -a

Step 3 - Delete the swap partition

Start by running the command fdisk /dev/vda (Remember to switch it out for your own drive), and delete both partitions.

[email protected]:~# fdisk /dev/vda

Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Step 4 - Remaking the partitions

Step 4.1 - Remaking the original partition

We used to have 4GB of swap space, so we will need to keep at least that much for swap. Use the command n and then p to create a primary partition. Use the default value for the first sector of 2048. Then enter the size of the partition, for example +96G for 96 GB.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): +96G

Created a new partition 1 of type 'Linux' and of size 96 GiB.

Step 4.2 - Remaking the swap partition

Remaking the swap partition is a similar process to the primary one. Start by running the command n then p to make a new primary partition. You will be asked for the first and last sectors. Unless you want to make the swap partition bigger, using the default values for both is fine.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (201328640-209715199, default 201328640):
Last sector, +sectors or +size{K,M,G,T,P} (201328640-209715199, default 209715199):

Created a new partition 2 of type 'Linux' and of size 4 GiB.

After doing that we need to change the partition type since it was originally supposed to be Linus swap / Solaris as seen in the first steps. To do that we need to run the command t followed by the partition number (in our case 2), and type the right partition type. In order to list all partition types you may press L.

Command (m for help): t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
... There are more partition types here, run L on your machine for all available ones.
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT

Partition type (type L to list all types): 82

After which you are shown the message

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Step 5 - Finishing up

Save your work by running the w command inside fdisk

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

In order to not reboot the machine we will be running the partprobe command in order to refresh the table for the kernel.

[email protected]:~# apt install parted
[email protected]:~# partprobe

After being redirected to the prompt with no output, we need to resize /dev/vda1

[email protected]:~# resize2fs /dev/vda1
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3

And reinitialize /dev/vda2 as the new swap location.

[email protected]:~# mkswap /dev/vda2
Setting up swapspace version 1, size = 4193276 KiB
no label, UUID=2e2fa78e-0f45-48eb-a737-ddb79b5f8599

Finally edit /etc/fstab and swap out the UUID for swap with the new one

# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
UUID=73290e27-1372-4a5b-88e7-21905822c47e /               ext4    errors=remount-ro 0       1
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
UUID=2e2fa78e-0f45-48eb-a737-ddb79b5f8599       none    swap    sw      0       0

After editing the etc/fstab we need to re-enable swap.

[email protected]:~# swapon -a

Step 6 - Verifying your work

We can verify the validity of our resizing using the commands df or fdisk

Device     Boot     Start       End   Sectors Size Id Type
/dev/vda1            2048 201328639 201326592  96G 83 Linux
/dev/vda2       201328640 209715199   8386560   4G 82 Linux swap / Solaris

And that's it. We increased the size of our / partition without having to reboot our machine.