When you've successfully cloned an Ubuntu VM to a larger virtual disk but find the root partition still constrained by its original size, here's the complete technical walkthrough to expand your LVM2 partition to utilize the full disk capacity.
First, confirm your current storage configuration:
sudo fdisk -l
sudo vgdisplay
sudo lvdisplay
Using fdisk
to extend the partition without losing data:
sudo fdisk /dev/sda
# Command sequence:
d (delete partition)
n (new primary partition)
1 (partition number)
[Enter] (default first sector)
[Enter] (default last sector)
t (change type)
8e (Linux LVM)
w (write changes)
After rebooting, expand the physical volume:
sudo pvresize /dev/sda1
sudo pvdisplay
For ext4 filesystem (common for Ubuntu):
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
If using XFS instead:
sudo lvextend -L+92G /dev/mapper/ubuntu--vg-ubuntu--lv
sudo xfs_growfs /
Confirm the new space allocation:
df -h
lsblk
- Always backup critical data before partition operations
- Ensure no snapshots are active in VirtualBox during the process
- Check for swap partition conflicts before resizing
When you clone an Ubuntu VM to a larger virtual disk in VirtualBox, you'll often find the root partition still using the original smaller size. This happens because the partition table gets copied exactly during cloning, while the underlying storage expands.
Before proceeding, ensure:
- You have root/sudo access
- The VM is using LVM2 (check with
lsblk
orpvdisplay
) - You've successfully expanded the virtual disk
1. Verify Disk Layout
First, check your current disk and partition setup:
sudo fdisk -l
lsblk
2. Resize the Partition
Use parted
to resize the partition to use all available space:
sudo parted /dev/sda
(parted) print free
(parted) resizepart 2 100%
(parted) quit
3. Extend the Physical Volume
Now extend the physical volume to use the new space:
sudo pvresize /dev/sda2
sudo pvdisplay
4. Extend the Logical Volume
Identify your root volume group and extend it:
sudo vgdisplay
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-root
5. Resize the Filesystem
Finally, resize the filesystem (for ext4):
sudo resize2fs /dev/mapper/ubuntu--vg-root
df -h
For a more direct LVM approach:
# Check free space
sudo vgdisplay
# Extend the logical volume
sudo lvextend -L +92G /dev/mapper/ubuntu--vg-root
# Resize filesystem
sudo resize2fs /dev/mapper/ubuntu--vg-root
- If
resize2fs
fails, try unmounting first or use a live CD - For XFS filesystems, use
xfs_growfs
instead - Always backup important data before resizing
For frequent use, create a resize script:
#!/bin/bash
# Auto-resize LVM root partition
echo "Resizing partition..."
sudo parted /dev/sda resizepart 2 100% >/dev/null
sudo pvresize /dev/sda2 >/dev/null
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-root >/dev/null
sudo resize2fs /dev/mapper/ubuntu--vg-root >/dev/null
echo "Resize complete. Current disk usage:"
df -h | grep root