{"id":17486,"date":"2023-06-24T23:29:00","date_gmt":"2023-06-24T20:29:00","guid":{"rendered":"https:\/\/kifarunix.com\/?p=17486"},"modified":"2024-03-10T09:43:28","modified_gmt":"2024-03-10T06:43:28","slug":"shrink-kvm-virtual-machine-lvm-partitioned-disk","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/shrink-kvm-virtual-machine-lvm-partitioned-disk\/","title":{"rendered":"Shrink KVM Virtual Machine LVM Partitioned Disk"},"content":{"rendered":"\n<p>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 <a href=\"https:\/\/www.linux-kvm.org\/page\/Main_Page\" target=\"_blank\" rel=\"noreferrer noopener\">KVM<\/a> virtual machines, but rather risky steps i took to get my vm disk down. Be cautious!<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#shrinking-kvm-virtual-machine-lvm-partitioned-disk\">Shrinking KVM Virtual Machine LVM Partitioned Disk<\/a><ul><li><a href=\"#update-fstab-to-use-device-instead-of-uuid\">Update FSTAB to use Device Instead of UUID<\/a><\/li><li><a href=\"#shrink-the-physical-volume-and-filesystem\">Shrink the Physical Volume and Filesystem<\/a><\/li><li><a href=\"#shrink-the-disk-size\">Shrink the Disk Size<\/a><\/li><li><a href=\"#confirm-disk-shrinkage-on-the-system\">Confirm Disk Shrinkage on the System<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"shrinking-kvm-virtual-machine-lvm-partitioned-disk\">Shrinking KVM Virtual Machine LVM Partitioned Disk<\/h2>\n\n\n\n<p>In this tutorial, we want to an LVM partitioned disk on a KVM virtual machine;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk -l \/dev\/sda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nDisk \/dev\/sda: 20 GiB, 21474836480 bytes, 41943040 sectors\nDisk model: QEMU HARDDISK   \nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisklabel type: dos\nDisk identifier: 0x75b85259\n\nDevice     Boot   Start      End  Sectors  Size Id Type\n\/dev\/sda1  *       2048   999423   997376  487M 83 Linux\n\/dev\/sda2       1001470 41940991 40939522 19.5G  5 Extended\n\/dev\/sda5       1001472 41940991 40939520 19.5G 8e Linux LVM\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>lvs<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\n  LV     VG          Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert\n  root   bookworm-vg -wi-ao----  18.56g                                                    \n  swap_1 bookworm-vg -wi-ao---- 980.00m\n<\/code><\/pre>\n\n\n\n<p>And this is how these partitions are mounted;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lsblk<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nNAME                    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS\nsda                       8:0    0   20G  0 disk \n\u251c\u2500sda1                    8:1    0  487M  0 part \/boot\n\u251c\u2500sda2                    8:2    0    1K  0 part \n\u2514\u2500sda5                    8:5    0 19.5G  0 part \n  \u251c\u2500bookworm--vg-root   254:0    0 18.6G  0 lvm  \/\n  \u2514\u2500bookworm--vg-swap_1 254:1    0  980M  0 lvm  [SWAP]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"update-fstab-to-use-device-instead-of-uuid\">Update FSTAB to use Device Instead of UUID<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>To get the device for each mapping, use the command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>blkid<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-sz\"><code>\n\/dev\/mapper\/bookworm--vg-swap_1: UUID=\"9e491bc0-7d36-4f73-9692-dc1dacde28d3\" TYPE=\"swap\"\n\/dev\/mapper\/bookworm--vg-root: UUID=\"b449ef15-3467-4b59-b881-d7e15d385210\" BLOCK_SIZE=\"4096\" TYPE=\"ext4\"\n\/dev\/sda5: UUID=\"JK72C1-ginB-1kph-t96A-NV4c-8H3e-6wxgpc\" TYPE=\"LVM2_member\" PARTUUID=\"75b85259-05\"\n\/dev\/sda1: UUID=\"ef714d79-6130-4ff8-8eba-1bac9303846c\" BLOCK_SIZE=\"1024\" TYPE=\"ext2\" PARTUUID=\"75b85259-01\"\n<\/code><\/pre>\n\n\n\n<p>Hence update the filesystem table;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/fstab<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\n# <file system> <mount point>   <type>  <options>       <dump>  <pass>\n\/dev\/mapper\/bookworm--vg-root \/               ext4    errors=remount-ro 0       1\n# \/boot was on \/dev\/sda1 during installation\nUUID=ef714d79-6130-4ff8-8eba-1bac9303846c \/boot           ext2    defaults        0       2\n\/dev\/mapper\/bookworm--vg-swap_1 none            swap    sw              0       0\n<\/code><\/pre>\n\n\n\n<p>We can updat the file like as shown below;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\n# <file system> <mount point>   <type>  <options>       <dump>  <pass>\n<strong>\/dev\/mapper\/bookworm--vg-root \/               ext4    errors=remount-ro 0       1\n# \/boot was on \/dev\/sda1 during installation\n#UUID=ef714d79-6130-4ff8-8eba-1bac9303846c \/boot           ext2    defaults        0       2\n\/dev\/sda1 \/boot           ext2    defaults        0       2\n#\/dev\/mapper\/bookworm--vg-swap_1 none            swap    sw              0       0<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Save the file and exit.<\/p>\n\n\n\n<p>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;<\/p>\n\n\n\n<p>In fact, let&#8217;s remove the swap logival volume.<\/p>\n\n\n\n<p>Find out the swap space device;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>swapon<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-sz\"><code>\nNAME      TYPE      SIZE USED PRIO\n\/dev\/dm-1 partition 980M   0B   -2\n<\/code><\/pre>\n\n\n\n<p>Turn off the swap;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>swapoff \/dev\/dm-1<\/code><\/pre>\n\n\n\n<p>Deactivate the Swap logical volume;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lvchange -an \/dev\/bookworm-vg\/swap_1<\/code><\/pre>\n\n\n\n<p>and remove it;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lvremove \/dev\/bookworm-vg\/swap_1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"shrink-the-physical-volume-and-filesystem\">Shrink the Physical Volume and Filesystem<\/h3>\n\n\n\n<p>To shrink a physcal volume, follow the guide below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-reduce-or-shrink-physical-volume-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Reduce or Shrink Physical Volume in Linux<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"shrink-the-disk-size\">Shrink the Disk Size<\/h3>\n\n\n\n<p>After you have resized the logical partitions above, the disk size still hasn&#8217;t changed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk -l \/dev\/sda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>Disk \/dev\/sda: 20 GiB, 21474836480 bytes, 41943040 sectors<\/strong>\nDisk model: QEMU HARDDISK   \nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisklabel type: dos\nDisk identifier: 0x75b85259\n\nDevice     Boot   Start      End  Sectors  Size Id Type\n\/dev\/sda1  *       2048   999423   997376  487M 83 Linux\n\/dev\/sda2        999424 21979135 20979712   10G  5 Extended\n\/dev\/sda5       1001472 21979135 20977664   10G 83 Linux\n<\/code><\/pre>\n\n\n\n<p>You can see above,<\/p>\n\n\n\n<p>So, to shrink the disk, you need to power off the virtual machine;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh shutdown &lt;vm-name&gt;<\/code><\/pre>\n\n\n\n<p>Or just poweroff from the virt-manager.<\/p>\n\n\n\n<p>Next, check the Disk information;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qemu-img info \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nimage: \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2\nfile format: qcow2\n<strong>virtual size: 20 GiB (21474836480 bytes)<\/strong>\ndisk size: 2.14 GiB\ncluster_size: 65536\nFormat specific information:\n    compat: 1.1\n    lazy refcounts: true\n    refcount bits: 16\n    corrupt: false\n<\/code><\/pre>\n\n\n\n<p>As you can see, disk is still showing 20G. Let&#8217;s shrink it.<\/p>\n\n\n\n<p>NOTE that we are shrinking it to 10244MiB. <strong>In order to be in safe situation, ensure that the size is a bit higher<\/strong>, for example, let&#8217;s set the size to be +512MiB more, hence, size of 10756MiB;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qemu-img resize --shrink \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2 10756M<\/code><\/pre>\n\n\n\n<p>Check the information again;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qemu-img info \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nimage: \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2\nfile format: qcow2\n<strong>virtual size: 10.5 GiB (11278483456 bytes)<\/strong>\ndisk size: 1.92 GiB\ncluster_size: 65536\nFormat specific information:\n    compat: 1.1\n    lazy refcounts: true\n    refcount bits: 16\n    corrupt: false\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"confirm-disk-shrinkage-on-the-system\">Confirm Disk Shrinkage on the System<\/h3>\n\n\n\n<p>Boot the machine and confirm that it boots and the disk is now shrinked!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk -l \/dev\/sda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>Disk \/dev\/sda: 10.5 GiB, 11278483456 bytes, 22028288 sectors<\/strong>\nDisk model: QEMU HARDDISK   \nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisklabel type: dos\nDisk identifier: 0x75b85259\n\nDevice     Boot   Start      End  Sectors  Size Id Type\n\/dev\/sda1  *       2048   999423   997376  487M 83 Linux\n\/dev\/sda2        999424 21979135 20979712   10G  5 Extended\n\/dev\/sda5       1001472 21979135 20977664   10G 83 Linux\n<\/code><\/pre>\n\n\n\n<p>Looks like it! Phew!<\/p>\n\n\n\n<p>And that is all on shrinking KVM virtual machine LVM partitioned disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/decrease-shrink-kvm-virtual-machine-disk-size\/\" target=\"_blank\" rel=\"noreferrer noopener\">Easy Way to Decrease\/Shrink KVM Virtual Machine Disk Size<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/easy-way-to-extend-kvm-virtual-machine-disk-size\/\" target=\"_blank\" rel=\"noreferrer noopener\">Easy Way to Extend KVM Virtual Machine Disk Size<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":10,"featured_media":17511,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[39,121,112,36],"tags":[6946,6948,6945,6949,6947,6944],"class_list":["post-17486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-storage","category-howtos","category-kvm","category-virtualization","tag-decrease-kvm-lvm-disk","tag-kvm-disk-size","tag-kvm-virtual-machine-shrink-the-size","tag-resizing-a-kvm-disk-image","tag-shrink-kvm-logical-partition-disk","tag-shrink-kvm-vm-disk-size","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/17486"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=17486"}],"version-history":[{"count":7,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/17486\/revisions"}],"predecessor-version":[{"id":20763,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/17486\/revisions\/20763"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/17511"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=17486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=17486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=17486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}