How to extend XFS LVM in Red Hat

In this procedure, I will explain how to extend an XFS logical volume in Red Hat.

Scenario: The Customer requires adding a 1TB LUN to a Production Linux Database server and extending the /data partition by 1TB. The LUN is located on an attached IBM XIV Storage Cluster. The server is a Virtual Machine running on VMware. The filesystem is XFS. Expand the volume, keeping all existing data intact.

How to extend a logical volume (LVM) on Red Hat Linux with XFS filesystem

This is a very common task performed by Linux system administrators, but to others, this may seem a daunting task. The process may seem difficult, but it’s not when you get your head around how Red Hat manages the filesystem and Logical Volumes (LVM).

Step 1 – Create a VM Snapshot (Optional but Highly Recommended)

In my scenario, I will be working on a Linux Virtual Machine, which is hosted on a VMware cluster. Whatever your scenario, make sure you have some kind of snapshot or backup of the server.

Feel free to skip this bit if you don’t use virtualization.

Before changing disk configurations, it’s crucial to create a snapshot of your VM. This serves as a safety net, allowing you to revert to the current state if anything goes wrong during the LVM expansion process.

How to take a snapshot

  • VMware Center:
    If using VMware products, Use either the vSphere Client (thick client) or the vSphere Web Client. Both offer snapshot functionality. Right-click on the virtual machine and Look for the option “Take Snapshot” or similar.
  • PowerCLI: If you’re comfortable with scripting, VMware’s PowerCLI provides a powerful way to automate snapshot creation (and many other tasks).

Here is a script to get you started:

PowerShell
Connect-VIServer -Server <your_vcenter_server_name> -User <username> -Password <password>
$vm = Get-VM -Name <your_vm_name>
New-Snapshot -VM $vm -Name <snapshot_name> -Description <snapshot_description> -Memory $true -Quiesce $true
  • Replace <snapshot_name> with a descriptive name for your snapshot.
  • Replace <snapshot_description> with additional information (optional).
  • -Memory $true includes the VM’s memory state in the snapshot (optional).
  • -Quiesce $true attempts to ensure a consistent state of the VM’s file system (recommended if the VM has VMware Tools installed).

How to Take a Snapshot Using vSphere Web Client

  1. Log in to your vCenter Server.
  2. Locate your RHEL VM.
  3. Right-click on the VM and select “Snapshots” -> “Take Snapshot.”
  4. Provide a name and description for the snapshot.
  5. Choose the snapshot type (standard or quiesced).
  6. Click “Take Snapshot.”

Important Note: Taking a snapshot doesn’t guarantee 100% data recovery. It’s still a best practice to back up critical data regularly.

Step 2: Prepare Storage, SSH to VM, and Prep Disk

Next assign the disk resources to the virtual machine. You can see in the picture below I am assigning a new “hard disk 4” with 1024GB of storage.

Prepare Storage

  • Edit VM Settings: Right-click the VM in the vCenter inventory and select “Edit Settings.
  • Expand Hard Disk: Locate the existing hard disk you want to expand and click it.
  • Increase Capacity: Change the “Provisioned Size” to the desired new size (e.g., increase it by 1 TB).
  • Click OK: Save the changes to the VM’s configuration.
RedHatdisk
RedHatdisk

SSH to Virtual Machine

  • Open your SSH client.
  • Connect to your RHEL VM’s IP address using your credentials.
  • Once logged in, type: sudo su -
    • Enter your password when prompted.

Prep Disk

Now we need to ensure that the physical volume we attached before is being presented to the server.

Bash
sudo pvs

Review the output to identify the physical volume that needs to be extended. In the example below, you can see /dev/sdd1 is showing 1024GB in size. That is the volume I added previously.

RedHatdisk pvs
RedHatdisk pvs

Step 3: Identify and Partition the New Disk

  • Locate the New LUN:
    • Use lsblk or fdisk -l to list all disks attached to the server.
    • The newly added disk will likely be the last one listed.
    • Take note of its device name (e.g., /dev/sdb/dev/sdc, etc.).
  • Partition the Disk:
    • Use fdisk to partition the disk

Bash
fdisk /dev/[device_name]
#Replace [device_name] with correct disk - for example fdisk /dev/sdd1

  • Create a new partition:
    • Press n (new partition)
    • Press p (primary partition)
    • Press 1 (partition number)
    • Press Enter twice to accept the default start and end cylinders.
  • Set partition type:
    • Press t
    • Press L to list partition types.
    • Type 8e (Linux LVM)
    • Press Enter.
  • Write changes:
    • Press w to write changes and exit.
  • Inform the kernel about the changes:
    • Run partprobe to update the kernel’s partition table information.
Bash
partprobe

RedHatdisk
RedHatdisk

Step 4: Extend the LVM Volume Group and Logical Volume

  • List Volume Groups:
    • Run vgs to identify the volume group where your /data logical volume resides.

Bash
vgs

  • Extend the Volume Group:
Bash
vgextend [volume_group_name] /dev/[device_name]1
  • Replace [volume_group_name] and [device_name] with the correct values.
  • Extend the Logical Volume:

Bash
lvextend -L +[size] /dev/[volume_group_name]/data /dev/[device_name]1

  • Replace [size] with the desired size increase (e.g., +1T for 1 Terabyte, +1000G for 1000 Gigabytes).

Step 5: Resize the Filesystem

  • Resize the XFS Filesystem:

Bash
xfs_growfs /data

  • Verify the Changes:
    • Run df -h to confirm the filesystem has been extended.

Step 6: Clean Up

  • Delete the Snapshot:
    • Go back to your vSphere Client/Web Client and delete the snapshot taken in Step 1. This frees up the storage space consumed by the snapshot.

Additional Tips:

  • Error Handling: 
    If you encounter errors during the lvextend step, try adjusting the size slightly (e.g., -L +999G instead of -L +1T).
  • Alternative Filesystems: 
    If you’re using a different filesystem (e.g., ext4), use the appropriate resize command (e.g., resize2fs).
  • Documentation: 
    Always refer to the official Red Hat documentation for detailed instructions and troubleshooting.

RedHatdisk
RedHatdisk
Elsewhere On TurboGeek:  10 Linux Commands Every Sysadmin Should Know

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

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

Translate »