When working with KVM virtual machines, disk space management is a common administrative task. The process differs between raw and qcow2 image formats, but both can be resized while maintaining existing data. Here's the complete workflow for Ubuntu guests on CentOS hosts.
First, verify your current disk setup on the host machine:
qemu-img info /var/lib/libvirt/images/ubuntu-vm.qcow2
This returns important information including virtual size, disk format, and actual disk usage.
For qcow2 format (recommended for most use cases):
qemu-img resize /var/lib/libvirt/images/ubuntu-vm.qcow2 +10G
For raw disk images:
truncate -s +10G /var/lib/libvirt/images/ubuntu-vm.raw
This adds 10GB to your existing 6GB disk. Adjust the size as needed.
After shutting down the VM, start it and check for new space in Ubuntu:
sudo fdisk -l sudo lsblk
If the disk doesn't show the new size immediately, you may need to rescan:
echo 1 > /sys/class/block/vda/device/rescan
For MBR partitions, use fdisk to delete and recreate the partition (keeping the same start sector):
sudo fdisk /dev/vda # Command sequence: d → n → p → 1 → (enter) → (enter) → w
For GPT partitions, use gdisk instead:
sudo gdisk /dev/vda # Command sequence: d → n → (enter) → (enter) → (enter) → w → Y
For ext4 filesystems:
sudo resize2fs /dev/vda1
For xfs filesystems:
sudo xfs_growfs /
For LVM setups, you'll need additional steps to extend the volume group and logical volume before resizing the filesystem.
Here's a complete bash script example for qcow2 images:
#!/bin/bash VM_NAME="ubuntu-vm" DISK_PATH="/var/lib/libvirt/images/${VM_NAME}.qcow2" SIZE_TO_ADD="10G" # Shutdown VM if running virsh shutdown $VM_NAME # Resize image qemu-img resize $DISK_PATH +$SIZE_TO_ADD # Start VM virsh start $VM_NAME # SSH into VM and run resizing commands ssh $VM_NAME << 'EOF' sudo growpart /dev/vda 1 sudo resize2fs /dev/vda1 EOF
- Always create backups before resizing disks
- For production systems, consider snapshotting the VM
- LVM provides more flexible disk management for complex setups
- Monitor disk usage with tools like 'df' and 'du' after resizing
If you encounter "No space left on device" errors after resizing, check:
dmesg | grep -i error journalctl -xe
Common issues include filesystem corruption (fix with fsck) or partition alignment problems.
When working with KVM virtualization, disk images are typically created as either raw or qcow2 format files. In this scenario, we're dealing with a QEMU disk image (likely qcow2) storing an Ubuntu guest on a CentOS host.
First, check the current disk size from the host system:
qemu-img info /path/to/vm-disk.qcow2
To increase the disk to (for example) 20GB:
qemu-img resize /path/to/vm-disk.qcow2 +14G
# Or absolute size:
qemu-img resize /path/to/vm-disk.qcow2 20G
For raw images:
truncate -s 20G /path/to/vm-disk.raw
After resizing the image, you need to extend the partition inside the Ubuntu guest:
First check available space:
lsblk
sudo fdisk -l
For LVM setups:
sudo pvresize /dev/sdaX
sudo lvextend -l +100%FREE /dev/ubuntu-vg/root
sudo resize2fs /dev/ubuntu-vg/root
For non-LVM ext4 partitions:
sudo growpart /dev/sda 1
sudo resize2fs /dev/sda1
If you're managing VMs through libvirt:
virsh shutdown ubuntu-vm
virsh vol-resize --pool default ubuntu-disk.qcow2 20G
virsh start ubuntu-vm
If you encounter "No space left on device" during resize, ensure:
- The host has sufficient storage
- The image isn't marked as immutable
- No snapshots are preventing resizing
- Always create backups before resizing
- Shut down the guest cleanly before resizing
- Verify filesystem integrity after resizing with fsck
- Consider using thin provisioning for future VMs