When provisioning Red Hat Enterprise Linux (RHEL) servers with Terraform, managing disk space can be tricky, especially when VMs are deployed with additional root disk space. By default, the root partition often matches the size of the template disk, leaving any extra space unallocated. This post documents resolving this issue to ensure your servers fully utilise their allocated disk space.

I would also welcome if there is a way to achieve this through Terraform for ease.

The Problem

We recently deployed three RHEL 9.3 virtual machines (VMs) in a VMware vSphere environment using Terraform. Each VM was provisioned with a 350GB disk, yet the root partition only used 50GB—matching the size of the original template disk. The remaining space was unallocated and, therefore, unusable.

This mismatch occurred because Terraform doesn’t automatically adjust partitioning for larger disk sizes when provisioning from a smaller disk template. To resolve this, we had to manually extend the root partition to utilise the full disk size.

Keep me honest, does Terraform have the ability to do this?

The Solution

let’s take a look at how we can extend the root partition and filesystem on a live RHEL system without service disruption.

Step 1: Check the Disk and Partition Layout

First, examine the current disk layout and usage using these commands:


df -h<br>lsblk

Output:


Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   44G  5.3G   39G  13% /

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0  350G  0 disk
├─sda1          8:1    0  600M  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
├─sda3          8:3    0 48.4G  0 part
│ ├─rhel-root 253:0    0 43.4G  0 lvm  /
│ └─rhel-swap 253:1    0    5G  0 lvm  &#91;SWAP]

Here, the

sda
disk has a total size of 350GB, but only ~50GB is allocated. The goal is to use the remaining space.

Step 2: Create a New Partition

Next, create a new partition to allocate the unused space:

  1. Run
    fdisk
    to modify the disk:

sudo fdisk /dev/sda

Follow these prompts:

  • Press
    n
    to create a new partition.
  • Accept the default values for the starting and ending sectors.
  • Press
    t
    to set the partition type, then enter
    8e
    for LVM.
  • Press
    w
    to write the changes and exit.

Update the kernel’s partition table:


sudo partprobe /dev/sda

Step 3: Extend the Volume Group (VG)

Add the new partition to the existing

rhel
Volume Group:

Initialise the new partition for LVM:


sudo pvcreate /dev/sda4

Add the partition to the Volume Group:


sudo vgextend rhel /dev/sda4

Verify the updated Volume Group:


sudo vgdisplay

Look for the “Free PE / Size” field, which should now reflect the additional space.

Step 4: Extend the Logical Volume (LV) and Filesystem

Now that the Volume Group has more space, extend the Logical Volume and filesystem:

  1. Identify the root Logical Volume:

sudo lvdisplay

The path should look like

/dev/mapper/rhel-root
.

Extend the Logical Volume and filesystem in a single step:


sudo lvextend -r -l +100%FREE /dev/mapper/rhel-root
  • -r
    : Resizes the filesystem alongside the LV.
  • +100%FREE
    : Allocates all remaining space in the Volume Group.

Verify the updated filesystem:


df -h

The root filesystem (

/
) should now reflect the total disk size.


Filesystem Size Used Avail Use% Mounted on<br>/dev/mapper/rhel-root 350G 5.5G 345G 2% /<br>/dev/sda1 600M 100M 500M 17% /boot/efi<br>/dev/sda2 1.0G 200M 800M 20% /boot

lsblk

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0  350G  0 disk
├─sda1          8:1    0  600M  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
├─sda3          8:3    0   48G  0 part
│ ├─rhel-root 253:0    0  350G  0 lvm  /
│ └─rhel-swap 253:1    0    5G  0 lvm  &#91;SWAP]
└─sda4          8:4    0  301G  0 part

Key Changes:

  1. Disk Size:
    • The
      sda
      disk remains 350GB in size, as this represents the physical disk.
  2. Partition
    sda4
    :
    • The new partition (
      sda4
      ) is created and marked as type
      LVM
      . Its size is 301GB, which was the previously unallocated space on the disk.
    • This partition has been successfully added to the
      rhel
      Volume Group.
  3. Logical Volume
    rhel-root
    :
    • The Logical Volume
      rhel-root
      now spans the entire disk (350GB), which includes the space from
      sda3
      and the newly added
      sda4
      .
  4. Other Partitions:
    • /boot
      and
      /boot/efi
      (
      sda1
      and
      sda2
      ) remain unchanged since they were not modified during the process.
    • The
      rhel-swap
      volume (on
      sda3
      ) is also unchanged and remains at 5GB.

This

lsblk
output confirms that the unallocated space has been added to the
rhel-root
Logical Volume and is fully utilized by the root filesystem.

I recorded the process below:

Leave a Reply

Your email address will not be published. Required fields are marked *