Linux – Resize-Extend a disk partition with unallocated space (CentOS, Ubuntu, VM)

Configurare noua (How To)

Situatie

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.

Solutie

Pasi de urmat

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).

Linux - Resize-Extend a disk partition with unallocated space (CentOS, Ubuntu, VM)

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
pvresize /dev/sda2
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:

Linux - Resize-Extend a disk partition with unallocated space (CentOS, Ubuntu, VM)

Tip solutie

Permanent

Voteaza

(3 din 7 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?