Expanding disk partitions to use all the available (unallocated) disk space is a common issue among Linux Administrators, expecially when working in a VMware-based Cloud environment. This often happens when deploying a Linux VM from an existing template using managed services like Amazon AWS or MS Azure, when we usually end up with disk partitions smaller than the disk space allocated during the VM configuration phase; but it can also happen after the deployment, for example when our VM's main disk runs out of space - and we want to further expand it by allocating additional space from the vCenter.
In this post we'll briefly explain how to fullfill such task in most Linux operating systems - such as CentOS, RHEL, Ubuntu, Debian and more - using a simple & secure approach based on four convenient built-in tools: parted, partx, pvresize and lvextend. The best part about such techinque is that it doesn't even require a reboot, which makes it definitely better than other reboot-based approaches such as this one.
STEP #1: retrieve the names
The first thing we need to do is to retrieve the names of the disks and the partition we want to resize. To do that, execute the lsblk command to show the system's information about all available block devices. In this tutorial we'll take the following scenario as an example:
By looking at the above screenshot we can easily determine the following:
- The fixed disk is called sda and has a total size of 200GB.
- Inside the sda disk we do have two partitions: sda1 (boot) and sda2 (which hosts a Logical Volume Manager).
- The sda2 partition contains a single volume group (vg) and two logical volumes: lv_root (97.4G), which is where all our system files are located, and the lv_swap (2.1GB), which is the swap partition.
As we can easily see, we do have roughly ~100GB of unallocated space on the sda disk: in this tutorial we'll see how we can assign them to the lv_root logical volume, thus extending it from 97.4GB to 197.4GB (or so on).
STEP #2: perform the extend/resize task
Now that we've successfully recovered the disk name (sda), the partition name (sda2), and the names of the volume group (vg) and logical volume (lv_root) we want to resize/extend, we can proceed with the actual task.
Open a terminal window (with root permissions) and type the following commands, replacing the names of our sample scenario with those recovered early on:
parted ---pretend-input-tty /dev/sda resizepart 2 100%
partx -u /dev/sda
lvextend -r vg/lv_root /dev/sda2
Here's a brief explanation of the above commands:
- parted - this command extends the second partition of the sda disk (which is sda2) using 100% of the unallocated disk space
- partx - this command updates the kernel in-memory partition table
- pvresize - this command resizes the sda2 partition's physical volume using the newly allocated disk space
- lvextend - this command resizes the lv_root logical volume and filesystem using the newly available disk space
As soon as we issue these four commands, we can issue the lsblk command again and see what happened to our system's devices:
As we can see, the resize/extend operation has been completed without flaws: the lv_root logical volume (and filesystem) has been successfully extended from 97.4GB to 199.5 GB. The newly available space is also ready to use, without requiring a reboot.
That's it, at least for now: we sincerely hope that this post will help other System Administrators to resize/extend the disk space of their CentOS, RHEL, Ubuntu, or Debian Linux Virtual Machines.
One Comment on “Linux - Resize-Extend a disk partition with unallocated space (CentOS, Ubuntu, VM) How to resize/extend a disk partition, logical volume and filesystem of a CentOS, RHEL, Ubuntu, Debian Linux VM using built-in tools and without the need of a reboot”
Thanks for this. The step to get volume group and the logical volume was not clear these can be obtained by running ‘lvscan’ on your system. Read more here: https://manpages.ubuntu.com/manpages/jammy/man8/lvscan.8.html