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 [SWAP]
Here, the
Step 2: Create a New Partition
Next, create a new partition to allocate the unused space:
- Run
to modify the disk:fdisk
sudo fdisk /dev/sda
Follow these prompts:
- Press
to create a new partition.n
- Accept the default values for the starting and ending sectors.
- Press
to set the partition type, then entertfor LVM.8e
- Press
to write the changes and exit.w
Update the kernel’s partition table:
sudo partprobe /dev/sda
Step 3: Extend the Volume Group (VG)
Add the new partition to the existing
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:
- Identify the root Logical Volume:
sudo lvdisplay
The path should look like
Extend the Logical Volume and filesystem in a single step:
sudo lvextend -r -l +100%FREE /dev/mapper/rhel-root
-
: Resizes the filesystem alongside the LV.-r
-
: Allocates all remaining space in the Volume Group.+100%FREE
Verify the updated filesystem:
df -h
The root filesystem (
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 [SWAP]
└─sda4 8:4 0 301G 0 part
Key Changes:
- Disk Size:
- The
disk remains 350GB in size, as this represents the physical disk.sda
- The
- Partition
:sda4
- The new partition (
) is created and marked as typesda4. Its size is 301GB, which was the previously unallocated space on the disk.LVM
- This partition has been successfully added to the
Volume Group.rhel
- The new partition (
- Logical Volume
:rhel-root
- The Logical Volume
now spans the entire disk (350GB), which includes the space fromrhel-rootand the newly addedsda3.sda4
- The Logical Volume
- Other Partitions:
-
and/boot(/boot/efiandsda1) remain unchanged since they were not modified during the process.sda2
- The
volume (onrhel-swap) is also unchanged and remains at 5GB.sda3
-
This
I recorded the process below: