Shrink KVM Virtual Machine LVM Partitioned Disk

|
Last Updated:
|
|

Is it possible to shrink KVM virtual machine LVM partitioned disk? Well, to be honest, this is an almost mission impossible task, but it is doable. Shrinking a disk may nuke your disk and cause a total loss of the data. Therefore, this is not an official guide on how to shrink a partition on KVM virtual machines, but rather risky steps i took to get my vm disk down. Be cautious!

Shrinking KVM Virtual Machine LVM Partitioned Disk

In this tutorial, we want to an LVM partitioned disk on a KVM virtual machine;

fdisk -l /dev/sda

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x75b85259

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 41940991 40939522 19.5G  5 Extended
/dev/sda5       1001472 41940991 40939520 19.5G 8e Linux LVM
lvs

  LV     VG          Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   bookworm-vg -wi-ao----  18.56g                                                    
  swap_1 bookworm-vg -wi-ao---- 980.00m

And this is how these partitions are mounted;

lsblk

NAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda                       8:0    0   20G  0 disk 
├─sda1                    8:1    0  487M  0 part /boot
├─sda2                    8:2    0    1K  0 part 
└─sda5                    8:5    0 19.5G  0 part 
  ├─bookworm--vg-root   254:0    0 18.6G  0 lvm  /
  └─bookworm--vg-swap_1 254:1    0  980M  0 lvm  [SWAP]

Update FSTAB to use Device Instead of UUID

As mentioned before, configure FSTAB to use device instead of UUID. When you shrink the partition, UUID may change and you wont be able to boot the virtual machine.

To get the device for each mapping, use the command below;

blkid

Sample output;


/dev/mapper/bookworm--vg-swap_1: UUID="9e491bc0-7d36-4f73-9692-dc1dacde28d3" TYPE="swap"
/dev/mapper/bookworm--vg-root: UUID="b449ef15-3467-4b59-b881-d7e15d385210" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sda5: UUID="JK72C1-ginB-1kph-t96A-NV4c-8H3e-6wxgpc" TYPE="LVM2_member" PARTUUID="75b85259-05"
/dev/sda1: UUID="ef714d79-6130-4ff8-8eba-1bac9303846c" BLOCK_SIZE="1024" TYPE="ext2" PARTUUID="75b85259-01"

Hence update the filesystem table;

sudo vim /etc/fstab

...
#                
/dev/mapper/bookworm--vg-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=ef714d79-6130-4ff8-8eba-1bac9303846c /boot           ext2    defaults        0       2
/dev/mapper/bookworm--vg-swap_1 none            swap    sw              0       0

We can updat the file like as shown below;


...
#                
/dev/mapper/bookworm--vg-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
#UUID=ef714d79-6130-4ff8-8eba-1bac9303846c /boot           ext2    defaults        0       2
/dev/sda1 /boot           ext2    defaults        0       2
#/dev/mapper/bookworm--vg-swap_1 none            swap    sw              0       0

Save the file and exit.

Note that I have also commented the SWAP partition line to avoid being mounted. I dont really need swap for now. This can easily be added later;

In fact, let’s remove the swap logival volume.

Find out the swap space device;

swapon

Sample output;


NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 980M   0B   -2

Turn off the swap;

swapoff /dev/dm-1

Deactivate the Swap logical volume;

lvchange -an /dev/bookworm-vg/swap_1

and remove it;

lvremove /dev/bookworm-vg/swap_1

Shrink the Physical Volume and Filesystem

To shrink a physcal volume, follow the guide below;

How to Reduce or Shrink Physical Volume in Linux

Shrink the Disk Size

After you have resized the logical partitions above, the disk size still hasn’t changed.

fdisk -l /dev/sda

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x75b85259

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2        999424 21979135 20979712   10G  5 Extended
/dev/sda5       1001472 21979135 20977664   10G 83 Linux

You can see above,

So, to shrink the disk, you need to power off the virtual machine;

virsh shutdown <vm-name>

Or just poweroff from the virt-manager.

Next, check the Disk information;

qemu-img info /media/kifarunix/vol02/kvm/images/bookworm.qcow2

image: /media/kifarunix/vol02/kvm/images/bookworm.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 2.14 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

As you can see, disk is still showing 20G. Let’s shrink it.

NOTE that we are shrinking it to 10244MiB. In order to be in safe situation, ensure that the size is a bit higher, for example, let’s set the size to be +512MiB more, hence, size of 10756MiB;

qemu-img resize --shrink /media/kifarunix/vol02/kvm/images/bookworm.qcow2 10756M

Check the information again;

qemu-img info /media/kifarunix/vol02/kvm/images/bookworm.qcow2

image: /media/kifarunix/vol02/kvm/images/bookworm.qcow2
file format: qcow2
virtual size: 10.5 GiB (11278483456 bytes)
disk size: 1.92 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

Confirm Disk Shrinkage on the System

Boot the machine and confirm that it boots and the disk is now shrinked!

fdisk -l /dev/sda

Disk /dev/sda: 10.5 GiB, 11278483456 bytes, 22028288 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x75b85259

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2        999424 21979135 20979712   10G  5 Extended
/dev/sda5       1001472 21979135 20977664   10G 83 Linux

Looks like it! Phew!

And that is all on shrinking KVM virtual machine LVM partitioned disk.

Other Tutorials

Easy Way to Decrease/Shrink KVM Virtual Machine Disk Size

Easy Way to Extend KVM Virtual Machine Disk Size

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment