1080*80 ad

Expanding KVM Virtual Disks Online: A Guide to Downtime-Free Extension

Live Resizing KVM Disks: A Step-by-Step Guide to Zero Downtime Expansion

Running out of disk space on a critical virtual machine is a common challenge for system administrators. In the past, expanding a virtual disk often meant scheduling downtime, a luxury many production environments cannot afford. Fortunately, with KVM, it’s possible to expand a virtual machine’s disk space online, without interrupting services.

This guide provides a comprehensive walkthrough of how to safely and effectively increase the size of a KVM virtual disk while the guest operating system is still running.

Before You Begin: The Golden Rule

Before attempting any disk operations, the most critical step is to ensure you have a complete, verified backup of your virtual machine. While the online resize process is generally safe, unforeseen issues can occur. A reliable backup is your ultimate safety net.

You should also identify the virtual disk format (e.g., qcow2, raw) and the partition and filesystem layout within the guest OS (e.g., ext4, XFS, LVM).

Step 1: Expand the Virtual Disk on the KVM Host

The first phase of the process takes place on the KVM hypervisor, or host machine. Here, you will increase the maximum size of the virtual disk file itself.

First, identify the exact path to the virtual disk file associated with your VM. You can do this by running:

virsh domblklist your_vm_name

This command will list the virtual disks (like vda or vdb) and their corresponding source files on the host (e.g., /var/lib/libvirt/images/your_vm_disk.qcow2).

Once you have the path, you have two primary methods for resizing the disk file:

  1. Using qemu-img: This is a powerful command-line tool for managing disk images. To add 50GB to your disk, you would use the resize command with a + prefix.

    qemu-img resize /var/lib/libvirt/images/your_vm_disk.qcow2 +50G
    

    This command instructs QEMU to increase the disk’s maximum capacity by 50 Gigabytes.

  2. Using virsh blockresize (Recommended): This is often the preferred method as it’s integrated directly with libvirt. It ensures the hypervisor is fully aware of the size change. This command sets the disk to a new, absolute total size.
    bash
    virsh blockresize your_vm_name vda --size 100G

    In this example, vda is the target disk, and we are setting its new total size to 100GB. Ensure the new size is larger than the current size.

After running either command, the virtual disk file on the host is larger, but the guest OS inside the VM is not yet aware of the new space.

Step 2: Extend the Partition and Filesystem in the Guest OS

Now, you must log in to the guest virtual machine to make the operating system recognize and use the newly allocated space.

Rescan the Disk Device

First, force the guest OS kernel to re-read the disk’s size information. This makes the OS aware that the underlying block device has grown. Replace sda with your actual block device name (e.g., vda, sdb).

echo 1 > /sys/class/block/sda/device/rescan

You can verify the change by running lsblk or fdisk -l /dev/sda. You should see that the total disk size has increased, but the partition size remains the same.

Resize the Partition

Next, you need to expand the partition to fill the new, unallocated space.

The Modern Method: growpart
The easiest and safest way to do this online is with the growpart utility, which is part of the cloud-utils-growpart package.

First, install it if it’s not already present:

  • On Debian/Ubuntu: sudo apt-get install cloud-guest-utils
  • On CentOS/RHEL: sudo yum install cloud-utils-growpart

Then, run the command, specifying the disk device and the partition number you want to expand. For example, to expand the second partition on disk sda:

sudo growpart /dev/sda 2

This command automatically extends the specified partition to fill all available contiguous space.

Resize the Filesystem

The final step is to expand the filesystem to utilize the newly enlarged partition. The command depends on the type of filesystem you are using.

  • For ext2, ext3, or ext4 filesystems: Use the resize2fs command.

    sudo resize2fs /dev/sda2
    

    (Assuming /dev/sda2 is the partition you expanded).

  • For XFS filesystems: Use the xfs_growfs command. Note that this command requires the mount point of the filesystem, not the device path.

    sudo xfs_growfs /
    

    (If the root filesystem is the one you expanded).

  • If Using LVM (Logical Volume Management): The process is slightly different. After rescanning the disk, you first extend the Physical Volume, then the Logical Volume, and finally resize the filesystem.

    1. pvresize /dev/sda2 (Extend the Physical Volume)
    2. lvextend -l +100%FREE /dev/mapper/vgname-lvname (Extend the Logical Volume to use all free space)
    3. resize2fs /dev/mapper/vgname-lvname (or xfs_growfs for XFS)

Step 3: Verify the Expansion

After completing the steps, verify that the new space is available. The simplest way is to use the df -h command.

df -h

The output should now show the increased total size and available space for the filesystem you expanded. You have successfully expanded your KVM virtual disk without any downtime. This live-resizing capability is a powerful feature for maintaining agile and responsive infrastructure.

Source: https://kifarunix.com/kvm-online-disk-expansion-how-to-extend-virtual-disks-without-downtime/

900*80 ad

      1080*80 ad