Sub topic 5
Using space from an enlarged virtual or cloud disk
What this chapter covers
Sometimes you don't add a disk. You make the existing one bigger. On a virtual machine or cloud instance, you can raise the size of a disk from the hypervisor console or the provider's API. The guest operating system then has a larger device, but LVM doesn't use the extra space until you tell each layer about it.
This chapter walks through that path from the enlarged device up to the physical volume (PV). You stop when the volume group (VG) shows more free space. Extending the logical volume (LV) and the filesystem are covered in the chapters that follow.
Assumed prerequisites:
- You are comfortable with
sudoand the command line. - You know what a block device, a partition, and a mount point are.
- You can read
lsblkanddfoutput. - You know the PV, VG, and LV layers from earlier in this tutorial.
Why this path differs from adding a disk
When you add a new disk, LVM gets a new PV through pvcreate and vgextend. When you enlarge an existing disk, the PV already exists and belongs to the VG. Nothing needs to be created or added. Each layer between the hardware and LVM has to notice the new size, in order:
- The hypervisor or cloud platform enlarges the virtual disk.
- The guest kernel rescans the device and sees the new size.
- The partition that holds the PV is grown to fill the disk. This step is skipped if the PV sits directly on the whole disk.
pvresizegrows the PV to fill its partition.- The VG's free space (
VFree) increases automatically.
This is the only part of the tutorial that touches the partition table, so it needs the most care. A wrong partition edit can make the disk unbootable or unreadable. Take a snapshot or backup first.
Step 1: Enlarge the disk on the platform side
How you enlarge the disk depends on the platform:
- On VMware, Hyper-V, or KVM, you edit the virtual disk size in the VM settings or with the platform's CLI.
- On AWS, you modify the EBS volume size. Some providers allow only one modification per volume in a given window, so pick the size carefully.
- Some platforms require the VM to be stopped or the disk detached. Check your provider's documentation.
You can only grow a virtual disk this way. Never shrink one on the platform side. The partitions and LVM metadata inside it would be cut off, and data would be lost.
Take a snapshot of the disk before you continue. A snapshot is the quickest way back if the partition step goes wrong.
Step 2: Make the kernel see the new size
First, compare what the kernel reports with what you expect:
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS /dev/sdaExample output when the disk was enlarged from 100 GiB to 150 GiB but the kernel hasn't noticed:
NAME SIZE TYPE FSTYPE MOUNTPOINTS
sda 100G disk
├─sda1 1M part
├─sda2 1G part ext4 /boot
└─sda3 99G part LVM2_member
├─vg_data-lv_app 80G lvm ext4 /srv/data
└─vg_data-lv_log 19G lvm xfs /var/log/appThe disk still shows 100G. Ask the kernel to rescan it:
echo 1 | sudo tee /sys/class/block/sda/device/rescan
lsblk -o NAME,SIZE,TYPE /dev/sdaThe tee form is needed because a plain sudo echo 1 > file runs the redirection as your normal user, which cannot write to that file.
Whether you need this step depends on the device type:
| Device type | Example name | Rescan needed? |
|---|---|---|
| SCSI or SATA emulation | `/dev/sda` | Usually yes, using the `rescan` file |
| virtio-blk | `/dev/vda` | Often detected automatically; check `lsblk` first |
| NVMe (EBS on Nitro instances) | `/dev/nvme0n1` | Usually detected automatically |
If lsblk already shows the larger size, skip the rescan. If the rescan file does not exist, the device type does not use it. In that case a reboot is the fallback, but it is rarely needed.
If the size still has not changed after a rescan, stop and check the platform. The resize may not have finished, or you may have enlarged a different disk. Use lsblk -o NAME,SERIAL,SIZE to match the serial to the disk the platform console shows.
Step 3: Grow the partition
If the PV sits on a partition, as /dev/sda3 does in this example, the partition must grow before LVM can use the space. growpart does this, and it is available from the cloud-guest-utils package on Debian and Ubuntu or cloud-utils-growpart on RHEL-family systems.
It takes the disk and the partition number as two separate arguments. This is a common source of mistakes:
sudo growpart --dry-run /dev/sda 3
sudo growpart /dev/sda 3The --dry-run form reports what would change without writing anything. Real output looks like this:
CHANGED: partition=3 start=2101248 old: size=207616000 end=209717247 new: size=312475615 end=314576862growpart works on mounted, in-use partitions on common setups, and it keeps the partition's start sector unchanged. For GPT disks it also moves the backup partition table to the new end of the disk.
Run lsblk again. The partition should now be larger, and the LVs on top of it unchanged.
When growpart refuses
growpart fails safely. These are the cases you will meet most often:
NOCHANGE: partition 3 could only be grown by ...usually means the kernel still sees the old disk size, so go back to the rescan. It can also mean the partition already fills the disk.- It refuses if another partition follows the one you want to grow, because there is no free space after it. You would have to move or delete that partition, which is outside what
growpartdoes. Plan layouts so the LVM partition is last. - On an old MBR disk where the PV is inside an extended (logical) partition, you must also grow the extended partition.
growpartdoesn't handle that case, so usepartedorfdiskwith great care, or use a snapshot and restore if unsure. - Exit codes help in scripts. In
growpart, 0 means the partition changed and 1 means there was nothing to change. A higher code (2) means failure.
When the PV is on the whole disk
If pvs lists /dev/sdb and not /dev/sdb1, the PV was created directly on the whole disk and there is no partition. Skip this step entirely and go to pvresize on the disk device.
When there is a layer in between
If the PV sits inside another block layer, such as LUKS encryption or software RAID, that layer must be resized first. For LUKS, you run cryptsetup resize on the opened mapping before pvresize. Check the output of lsblk to see whether a crypt or raid entry sits between the partition and the PV.
Step 4: Resize the physical volume
pvresize reads the current size of the device and updates the LVM metadata to match. It works while the volume group is active and its filesystems are mounted:
sudo pvresize /dev/sda3Expected output:
Physical volume "/dev/sda3" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resizedIf it reports 0 physical volume(s) resized or updated / 1 physical volume(s) not resized, the PV already filled the device. That means an earlier layer didn't grow, so recheck the partition size with lsblk.
No LV changes and no data moves. LVM only records that the PV has more extents available.
Step 5: Confirm the extra space is free in the VG
sudo pvs /dev/sda3
sudo vgs vg_dataExample output:
PV VG Fmt Attr PSize PFree
/dev/sda3 vg_data lvm2 a-- <149.00g 50.00g
VG #PV #LV #SN Attr VSize VFree
vg_data 1 2 0 wz--n- <149.00g 50.00gThe VFree value rose by about the amount you added to the disk (50 GiB here). The size is slightly under the raw increase because the first partition and LVM metadata take a little space. Compare VFree before and after, and don't expect it to match the platform's number exactly.
This is where this chapter ends. The VG has free extents, but the LV and the filesystem are still their old size. df -h /srv/data will not change yet. The next steps (lvextend, then resize2fs or xfs_growfs) turn the free extents into usable space.
The whole sequence as a script
For a single-PV server where you often grow a partition-based PV, you can wrap the steps in a script. It checks its arguments, handles NVMe-style partition names, and stops at the first real error:
#!/usr/bin/env bash
# Usage: sudo ./grow-pv.sh /dev/sda 3
set -euo pipefail
DISK=${1:?usage: $0 DISK PARTNUM (for example: /dev/sda 3)}
PARTNUM=${2:?usage: $0 DISK PARTNUM (for example: /dev/sda 3)}
# Disks whose names end in a digit (nvme0n1, mmcblk0) use a "p" separator.
case "$DISK" in
*[0-9]) PV="${DISK}p${PARTNUM}" ;;
*) PV="${DISK}${PARTNUM}" ;;
esac
name=$(basename "$DISK")
rescan="/sys/class/block/${name}/device/rescan"
if [ -w "$rescan" ]; then
echo 1 > "$rescan"
fi
# growpart exits 1 when there is nothing to change; treat that as fine.
growpart "$DISK" "$PARTNUM" || [ $? -eq 1 ]
pvresize "$PV"
pvs "$PV"
vgsTreat this as a convenience for a layout you already understand. Run the commands by hand the first time, so you see each layer change.
Common mistakes and edge cases
- Writing
growpart /dev/sda3. The command needs the disk and the partition number as separate arguments:growpart /dev/sda 3. - Running
pvresizebefore the partition grows. It succeeds but changes nothing, so check the "not resized" line. - Expecting
dfto change.dfreports the filesystem, not the PV or VG. It stays the same until the filesystem is grown. - Forgetting the partition-table type. GPT and MBR behave differently. Check with
sudo parted /dev/sda printorlsblk -o NAME,PTTYPE. - Growing the wrong disk. Device names can change after a reboot or when disks are attached. Confirm the disk by size and serial before you run
growpart. - Shrinking on the platform side. This destroys data. Platform-side resizing is a one-way grow operation.
- Multiple PVs in one VG. Only the PV on the enlarged disk grows. Other PVs are untouched, and
VFreeis the total across all of them.
Review of what changed
| Layer | Command that grows it | How to check |
|---|---|---|
| Virtual disk | Platform console or API | `lsblk` after rescan |
| Partition | `growpart DISK N` | `lsblk` |
| Physical volume | `pvresize PV` | `pvs` (`PSize`, `PFree`) |
| Volume group | Automatic | `vgs` (`VFree`) |
| Logical volume | `lvextend` (next chapter) | `lvs` |
| Filesystem | `resize2fs` or `xfs_growfs` | `df -hT` |
Exercise
A cloud VM's disk was enlarged from 100 GiB to 200 GiB. You run sudo growpart /dev/sda 3 and get NOCHANGE: partition 3 could only be grown by 0, but lsblk still shows sda at 100G. What is the likely cause, and what do you do?
Show answer
The kernel has not noticed the new disk size, so there is no free space to grow into. Rescan the device with echo 1 | sudo tee /sys/class/block/sda/device/rescan (or check that the platform finished the resize), confirm lsblk shows 200G, and run growpart again.
Exercise
After growing the partition and running pvresize /dev/sda3, vgs shows 50 GiB of VFree, but df -h /srv/data still shows the old size. Is something wrong? Which commands finish the job?
Show answer
Nothing is wrong. The extra space is only free extents in the VG. Extend the logical volume with lvextend (for example sudo lvextend -r -l +100%FREE /dev/vg_data/lv_app), and grow the filesystem with resize2fs for ext4 or xfs_growfs for XFS, unless -r already did it.
Exercise
pvs lists the PV as /dev/sdb and not /dev/sdb1. After the platform enlarges the disk and the kernel rescans it, which of the steps in this chapter can you skip, and which command do you run?
Show answer
Skip growpart, because there is no partition to grow. After the rescan, run sudo pvresize /dev/sdb and check vgs for the new VFree.