How to Increase Storage Partition in Ubuntu

Written by: Bagus Facsi Aginsa
Published at: 15 Sep 2021


This tutorial will show you how to increase a storage partition using growpart and resize2fs without any data loss. Before we start, note that in order to resize partition (extend), enough disk space must be available. We can not extend a partition if there are no free sectors/cylinders at the end of the partition to extend. So first, you must increase your machine storage size from your hypervisor, or add more physical hard disk if you on bare metal.

Prerequisite

  1. Ubuntu
  2. Free Disk Space (check using lsblk command)

Sudo Privileges

Before start, we make sure that we will have no permission issue on the configuration.

sudo su

Check Available Disk

To check the available disk, you can use this command:

lsblk

output:

NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0     7:0    0 99.4M  1 loop /snap/core/11606
loop1     7:1    0   91M  1 loop /snap/core/6350
sr0      11:0    1 1024M  0 rom
xvda    202:0    0  100G  0 disk
├─xvda1 202:1    0    1M  0 part
└─xvda2 202:2    0   20G  0 part /

You see that xvda partition has 100G but, the root partition (xvda2) has only 20G. This mean we have free disk space that’s not claimed yet.

If you check your Filesystem usage using this command:

df -h

output:

Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           393M  2.3M  391M   1% /run
/dev/xvda2       20G   20G     0 100% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

We already have used 100% of our /dev/xvda2 Filesystem. In this tutorial, we will increase the /dev/xvda2 to 100G without any data loss.

Grow the Partition

You need a library called cloud-guest-utils, install it using package manager

apt install cloud-guest-utils

After that, grow the xvda2 partition using this command

growpart /dev/xvda 2

This command mean we will grow /dev/xvda sub partition number 2.

After that, you can check that the partition is grown using lsblk

NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0     7:0    0 99.4M  1 loop /snap/core/11606
loop1     7:1    0   91M  1 loop /snap/core/6350
sr0      11:0    1 1024M  0 rom
xvda    202:0    0  100G  0 disk
├─xvda1 202:1    0    1M  0 part
└─xvda2 202:2    0  100G  0 part /

But we are not done yet. You can check by using df -h, the Filesystem is not resized yet:

Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           393M  2.3M  391M   1% /run
/dev/xvda2       20G   20G     0 100% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

Next, we must resize the Filesystem so the size of our disk increased to 100G

Resize the Filesystem

To resize the Filesystem, simply use this command

resize2fs /dev/xvda2

output:

resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/xvda2 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 13
The filesystem on /dev/xvda2 is now 26213883 (4k) blocks long.

And check the disk again using df -h:

root@master-node-1:/home/ubuntu# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           393M  2.4M  391M   1% /run
/dev/xvda2       99G   20G   75G  21% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

Thats it! You will see that the Filesystem size is already increased to 99G (not exact 100G because partially used by system).