How to Expand a Disk in a Linux (VM): Step-by-Step Guide

Expanding a disk in a virtual machine is a common task for system administrators. This blog demonstrates how to increase a disk size from 30 GB to 100 GB, scan for the new size, and expand the associated logical volume. We’ll use the following steps:

  • Initial disk size: 30 GB
  • Target disk size: 100 GB
  • Hostnames and setup: We’ll use a hostname vmhost01 for illustration.

Step 1: Verify Existing Disk Configuration

Before making any changes, inspect the current disk setup.

Commands:

# List physical volumes with their associated devices
pvs -o+devices

# Display block devices
lsblk 

Example Output:

[root@vmhost01 ~]# pvs -o+devices
  PV         VG     Fmt  Attr PSize    PFree Devices     
  /dev/sda6  vg00   lvm2 a--    60.00g    0  /dev/sda6(0)
  /dev/sdb   vg_app lvm2 a--  <130.00g    0  /dev/sdb(0)
  /dev/sdc   vg00   lvm2 a--   <30.00g    0  /dev/sdc(0)

[root@vmhost01 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   70G  0 disk 
├─sda1            8:1    0  512M  0 part /boot
├─sda2            8:2    0    4G  0 part [SWAP]
└─sda6            8:6    0   60G  0 part 
  └─vg00-root   253:0    0   60G  0 lvm  /
sdc               8:32   0   30G  0 disk 
  └─vg00-root   253:0    0   30G  0 lvm  / 

Step 2: Rescan the Disk

After increasing the disk size in your VM’s hypervisor, the OS must rescan the disk to detect the new size.

Command:

echo 1 > /sys/block/sdc/device/rescan 

Verify the Change:

lsblk 

Example Output:

[root@vmhost01 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdc               8:32   0  100G  0 disk 
  └─vg00-root   253:0    0   30G  0 lvm  / 

Step 3: Resize the Physical Volume

Next, resize the physical volume (/dev/sdc) to reflect the new size.

Command:

pvresize /dev/sdc 

Verify the Updated Size:

pvs -o+devices 

Example Output:

[root@vmhost01 ~]# pvs -o+devices
  PV         VG     Fmt  Attr PSize   PFree Devices     
  /dev/sdc   vg00   lvm2 a--  100.00g 40.00g /dev/sdc(0) 

Step 4: Extend the Logical Volume

With the physical volume resized, extend the logical volume.

Commands:

# Extend the logical volume
lvextend -l +100%FREE /dev/vg00/root

# Resize the filesystem
resize2fs /dev/vg00/root 

Example Output:

[root@vmhost01 ~]# lvextend -l +100%FREE /dev/vg00/root
  Size of logical volume vg00/root changed from 60.00 GiB to 100.00 GiB.

[root@vmhost01 ~]# resize2fs /dev/vg00/root
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/vg00/root is mounted on /; on-line resizing required
The filesystem on /dev/vg00/root is now 100G. 

Step 5: Verify the Expansion

Finally, confirm the logical volume and filesystem have been expanded.

Commands:

lsblk
df -h 

Example Output:

[root@vmhost01 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdc               8:32   0  100G  0 disk 
  └─vg00-root   253:0    0  100G  0 lvm  /

[root@vmhost01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg00-root 100G   10G   90G  10% / 

Using these steps, we successfully expanded a disk from 30 GB to 100 GB, ensuring the logical volume and filesystem utilized the additional space. This procedure can be adapted to other environments with different configurations.

You may also like...