Expanding Disk Partitions in Ubuntu VMs¶
Overview¶
When you increase the disk size of a virtual machine (e.g., in VMware, Azure, AWS, or other hypervisors), the underlying storage is expanded but the partition table and filesystem don't automatically grow. This guide covers how to expand partitions and filesystems in Ubuntu to use the newly allocated space.
Check Current Disk Usage¶
First, verify the current disk space and partition layout:
# Check filesystem usage
df -h
# Check block devices and partitions
lsblk
# Check partition details
sudo fdisk -l
You'll typically see that the disk size (e.g., /dev/sda) is larger than the partition size (e.g., /dev/sda1).
Method 1: Using growpart and resize2fs (Recommended)¶
This is the simplest method, especially for cloud VMs.
Step 1: Install cloud-guest-utils (if not present)¶
Step 2: Grow the partition¶
# Syntax: growpart <device> <partition-number>
sudo growpart /dev/sda 1
# For NVMe drives, use:
sudo growpart /dev/nvme0n1 1
Step 3: Resize the filesystem¶
For ext4 filesystems:
For xfs filesystems:
Step 4: Verify the changes¶
Method 2: Using parted (Alternative)¶
If growpart is not available, you can use parted:
Step 1: Launch parted¶
Step 2: Resize the partition¶
Step 3: Resize the filesystem¶
Method 3: Using fdisk (Manual Method)¶
Warning: This method requires deleting and recreating the partition. The data is preserved if done correctly, but proceed with caution.
Step 1: Launch fdisk¶
Step 2: Delete and recreate the partition¶
Command (m for help): p # Print partition table (note start sector)
Command (m for help): d # Delete partition
Partition number: 1
Command (m for help): n # Create new partition
Partition type: p # Primary
Partition number: 1
First sector: <press Enter to use default - must match old start sector>
Last sector: <press Enter to use all available space>
Command (m for help): p # Verify the new partition
Command (m for help): w # Write changes
Step 3: Reboot or inform the kernel¶
Step 4: Resize the filesystem¶
LVM (Logical Volume Manager) Partitions¶
If your Ubuntu installation uses LVM (common in server installations):
Step 1: Grow the partition (if needed)¶
Step 2: Resize the physical volume¶
Step 3: Extend the logical volume¶
# Check volume group and logical volume names
sudo vgdisplay
sudo lvdisplay
# Extend the logical volume (example names)
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
# Or specify a size
sudo lvextend -L +50G /dev/ubuntu-vg/ubuntu-lv
Step 4: Resize the filesystem¶
Troubleshooting¶
Partition still shows old size after resize¶
Reboot the system or run:
"The partition is being used" error with parted¶
The partition is mounted. You'll need to:
1. Boot from a live USB/CD, or
2. Use growpart or fdisk method which work on mounted partitions
Verify filesystem type¶
Check for errors after resize¶
sudo e2fsck -f /dev/sda1 # For ext4 (must be unmounted)
sudo xfs_repair /dev/sda1 # For xfs (must be unmounted)
Common Scenarios¶
Azure VMs¶
- Expand disk in Azure Portal
- Wait for the operation to complete
- SSH into the VM
- Run
growpartandresize2fs
VMware VMs¶
- Power off the VM
- Expand disk in VMware settings
- Power on the VM
- Run
growpartandresize2fs
AWS EC2 Instances¶
- Modify volume in EC2 Console
- Wait for the volume to show "optimizing"
- SSH into the instance
- Run
growpartandresize2fs
Best Practices¶
- Always backup before resizing partitions
- Take a VM snapshot before making changes
- Verify free space with
df -hbefore and after - Check for errors after resizing
- Plan for downtime if a reboot is required
Related Commands Reference¶
# Disk and partition information
lsblk -f # List block devices with filesystem
df -h # Disk space usage
sudo fdisk -l # List all partitions
sudo parted -l # List partition tables
# LVM commands
sudo pvdisplay # Physical volumes
sudo vgdisplay # Volume groups
sudo lvdisplay # Logical volumes
# Filesystem utilities
sudo resize2fs # Resize ext2/ext3/ext4
sudo xfs_growfs # Resize xfs
sudo e2fsck # Check ext filesystems