Search This Blog

Monday, August 31, 2026

Expanding a virtual disk on a live Linux virtual machine does not require downtime, complex LVM setups, or reboots.

Managing disk space on Linux virtual machines often involves two recurring tasks: attaching a brand-new virtual disk (for example, offloading /var/log) and expanding an existing partition online when data grows. Both workflows can be performed on a running VM with zero downtime, no LVM overhead, and without requiring a reboot.


Part 1: Adding, Partitioning & Mounting a New Disk (e.g., /var/log)

If you have just attached a new virtual disk to your VM, you need to scan the SCSI bus, partition the drive, format the filesystem, and configure persistent mounting.

1. Scan All SCSI Hosts for the New Disk

Trigger a bus rescan across all SCSI hosts so the kernel discovers the newly attached block device without restarting:

for SCSI_HOST in /sys/class/scsi_host/*; do echo "- - -" > $SCSI_HOST/scan; done

2. Create the Partition

Use gdisk (or fdisk) to initialize the partition table on the new device (e.g., /dev/sdb):

gdisk /dev/sdb

(Create a new partition /dev/sdb1 using default parameters and write the changes.)

3. Format the Partition

Format the partition to ext4 and assign a filesystem label (e.g., VARLOG):

mkfs.ext4 -L VARLOG /dev/sdb1

4. Migrate Existing Data & Mount

Mount the new partition to a temporary location, move the existing log files, and mount the partition directly to /var/log:

mount /dev/sdb1 /mnt && mv /var/log/* /mnt && umount /mnt && mount /dev/sdb1 /var/log && df -h

5. Make the Mount Persistent

Open /etc/fstab in your editor:

vi /etc/fstab

Append the following entry:

/dev/sdb1                    /var/log          ext4    defaults        0      2

Part 2: Enlarge a Disk and Partition Online (No Reboot)

When the disk runs low on space, you can expand the virtual disk from your hypervisor and grow the filesystem dynamically.

Prerequisites & Limitations

  • No Active Snapshots: Ensure hypervisor snapshots are consolidated before resizing the disk.
  • Partition Position: Online expansion applies directly to the last or only partition on the block device.
  • Required Tool: Install growpart:
    sudo apt-get install -y cloud-utils        # Debian/Ubuntu
    sudo dnf install -y cloud-utils-growpart   # RHEL/CentOS/Rocky

Step-by-Step Expansion

  1. Increase Disk Size in Hypervisor: Expand the provisioned size in VMware, Proxmox, or cloud portal.
  2. Rescan the Specific Device:
    echo 1 > /sys/class/block/sdb/device/rescan

    Verify the new capacity in dmesg:

    dmesg | tail -n 10
    # Look for: "sdb: detected capacity change from X to Y"
  3. Grow the Partition Table:
    growpart /dev/sdb 1
  4. Resize the Filesystem:

    For EXT4:

    resize2fs /dev/sdb1

    For XFS:

    xfs_growfs /var/log

Automation via Cron

To automate live expansion on recurring data drives, you can schedule a script in /usr/local/bin/disk_resize.sh:

#!/usr/bin/env bash
set -euo pipefail

# Rescan device
echo 1 > /sys/class/block/sdb/device/rescan
sleep 2

# Grow partition and resize ext4
if growpart /dev/sdb 1; then
    resize2fs /dev/sdb1
    echo "[$(date)] Successfully expanded /dev/sdb1"
fi

Schedule it in /etc/crontab:

*/5 * * * * root /usr/local/bin/disk_resize.sh > /var/log/disk_resize.log 2>&1

No comments:

Post a Comment