Expanding your linux VM Disk: Resize LVM partition without creating a new one
Below is a friendly, step-by-step guide to resizing a Linux VM’s disk without creating a new partition. We’ll walk through extending the last partition on a disk, resizing a Physical Volume (PV), then resizing the associated Logical Volume (LV) within LVM.
Why Resize This Way?
When you run out of space, you could add a new partition, set up a new PV on it, and then merge it into your Volume Group. That’s often simpler at first glance. However, there are a few reasons to extend an existing partition rather than create a new one:
- More Straightforward Resizing: Resizing a single partition (and the corresponding PV) is usually more predictable than creating multiple PVs of various sizes. You don’t have to remember which PV belongs to which partition when everything lives on the same extended partition
- Consistency for Backups & Snapshots: Some backup strategies or snapshot tools prefer (or require) a simpler underlying structure. One partition with a single PV is easier to handle than multiple scattered partitions
- Less Complexity: Adding more partitions and more PVs can make your system’s storage layout harder to track. It’s cleaner to maintain a single partition; it also looks better ;)
TL;DR (Short Version)
If you only need the commands, here they are:
# 0. Install required dependencies
apt update && apt install -y gdisk parted
# 1. Check Disk Layout
lsblk
gdisk -l /dev/sda
# 2. (Optional) Move GPT structures to the end, if needed
sgdisk -e /dev/sda
# 3. Delete & recreate the last partition
sgdisk -d 3 -n 3:START_SECTOR:0 -t 3:8E00 /dev/sda
# 4. Reload partition table
partprobe -s
# 5. Resize the Physical Volume (PV)
pvresize /dev/sda3
# 6. Resize the Logical Volume (LV)
lvextend -r -l +100%FREE /dev/mapper/<YourVGName-YourLVName>
Step-by-Step Guide
Little note before we start, I assume that we want to extend the sda
partition. If you have a different disk, replace sda
with your disk name (e.g., sdb
, sdc
, etc.).
1. Identify the Target Disk and Partition
1.1 Find Your Disk Device Path
First, identify the disk you want to resize. You can use lsblk
to list all block devices and their partitions. Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 70G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 488M 0 part /boot
└─sda3 8:3 0 69G 0 part
├─myvg-root 254:0 0 68G 0 lvm /
└─myvg-swap 254:1 0 1G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
1.2 Verify the Target Partition Is the Last One
Look for the Start and End sectors to confirm your partition is at the end of the disk.
gdisk -l /dev/sda
Example output:
Number Start (sector) End (sector) Size Code Name
1 2048 1050623 512.0 MiB EF00
2 1050624 2050047 488.0 MiB 8300
3 2050048 146800606 69.0 GiB 8E00
1.3 Take note of Sector and Code
Make a note of the Start sector and Code for the last partition. You’ll need these values for step 2.2.
The Code is the partition type. In this example, 8E00
is the code for Linux LVM.
2. Resize the Partition
2.1 Move GPT Structures (Optional)
This step makes sure your GPT metadata is at the very end of the disk so you can fully use any additional space:
sgdisk -e /dev/sda
2.2 Delete and Recreate the Last Partition
Use the values you got from step 1.2 and enter them here. In my case this was 2050048
and 8E00
.
Critical: Keep the same start sector as before and extend (:0
) to the end. This won’t erase data if you do not change the start sector.
sgdisk -d 3 -n 3:2050048:0 -t 3:8E00 /dev/sda
-d 3
: Delete partition 3-n 3:2050048:0
: Create a new partition 3 starting at sector 2050048 and extending to the end of the disk-t 3:8E00
: Set the partition type to Linux LVM
2.3 Reload the Partition Table
The system won’t recognize the new partition size until you reload the partition table:
partprobe -s
3. Resize the Physical Volume (PV) and Logical Volume (LV)
3.1 Resize the Physical Volume
You’ll see something like /dev/sda3
mapped to your Volume Group (VG).
pvs
Identify which LV you want to grow (e.g., root
, data
, etc.).
lvs
3.2 Resize the Physical Volume
pvresize /dev/sda3
Your PV now recognizes the newly available space on the extended partition.
3.3 Resize the Logical Volume
Change myvg-root
with the LVM you actually want to extend (see step 1.1)
sudo lvextend -r -l +100%FREE /dev/mapper/myvg-root
-r
: Resize the filesystem after resizing the LV-l +100%FREE
: Use all available space/dev/mapper/<YourVGName-YourLVName>
: Replace with your VG and LV names
4. Verify the Resized Disk and Filesystem
Confirm the new size is reflected in your PV, LV, and filesystem.
pvs
lvs
df -h
Optional reboot: Not needed for the system itself to recognize the new size, but a reboot may help if some services or tools aren’t immediately recognizing the new space.