How To Resize Linux Partition Using Command Line

In this article you will learn how to resize Linux partition from Command Line on virtual machine (VMware, Proxmox, AWS, ..).

Тhis example is applicable for increase only for NON LVM partitions.

In our example we will use Debian, but this method can be used on other Linux distributives also (Ubuntu, RedHat).

Before starting resizing the partitions, be sure to make a backup copy of your data!

Preparation

Lets use Linux lsblk or df -h or command to see system disk space usage

# df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           198M 1000K  197M   1% /run
/dev/sda2       4.9G  2.7G  1.9G  60% /
tmpfs           989M     0  989M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           198M  4.0K  198M   1% /run/user/1000

Output shows /dev/sda2 usage is 60%.

Before increasing the Linux partition size you need increase disk size. First increase your VM (virtual machine) disk size from hypervisor (VMware, Proxmox, ..).

After increasing disk size you may have a problem: “GPT PMBR size mismatch will be corrected”. To fix that error read this article.

After increasing disk size to 5Gb execute the system command fdisk (for creation and manipulation of partition tables):

# fdisk -l

Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK
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: 2F1A07FF-A3DA-47BD-9643-824FFB90AE70

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 10483711 10479616   5G Linux filesystem

Now our disk size – /dev/sda is 10 GiB, and partition /dev/sda2 still 5G.

So we need expand /dev/sda1 partition.

Resize Partition fdisk

To resize partition first we need to delete it then create a new one with a larger size. Execute: fdisk /dev/sda

# fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

Chose p to print the partition table one more time and double check:

Command (m for help): p

Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK
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: 2F1A07FF-A3DA-47BD-9643-824FFB90AE70

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 10483711 10479616   5G Linux filesystem

In our case we will delete 2 partition (/dev/sda2).

To delete enter d, then chose Partition number 2:

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

Partition 2 has been deleted.

Add a New Partition

Now enter n to add a new partition, then partition number 1.

For First sector, and Last sector leave default values and press Enter.

On prompt: Do you want to remove the signature? [Y]es/[N]o: enter n:

Command (m for help): n
Partition number (2-128, default 2): 2
First sector (4096-20971486, default 4096):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (4096-20971486, default 20971486):

Created a new partition 2 of type 'Linux filesystem' and of size 10 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

You can see from output: “Created a new partition 2 of type ‘Linux filesystem’ and of size 10 GiB”.

Write Table to Disk

To write new table to disk and exit enter w:

write table to disk

and reboot the VM:

# reboot

Resize File System

For the final step increase the file system with command resize2fs:

# resize2fs /dev/sda2
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/sda2 is now 2620923 (4k) blocks long.

Once again let’s check if the partition size increased. As shown on example now we have 10G /dev/sda2:

Resize Linux Partition Using Command Line

Done!

Conclusion

We just learned how to resize Linux Partition using command line utilities: df, fdisk and resize2fs.

NOTE: You can resize non-LVM partition only on the last partition on the storage device! In case if you have other partitions like SWAP or extended you must delete and recreate them. You may loss data!

Discuss article in ArsTech Forum

   

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee

arstech

Leave a Reply