{"id":14637,"date":"2023-06-24T14:56:15","date_gmt":"2023-06-24T11:56:15","guid":{"rendered":"https:\/\/kifarunix.com\/?p=14637"},"modified":"2024-03-10T09:44:36","modified_gmt":"2024-03-10T06:44:36","slug":"decrease-shrink-kvm-virtual-machine-disk-size","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/decrease-shrink-kvm-virtual-machine-disk-size\/","title":{"rendered":"Easy Way to Decrease\/Shrink KVM Virtual Machine Disk Size"},"content":{"rendered":"\n<p>In this tutorial, you will learn an easy way to decrease\/shrink KVM virtual machine disk size. In most cases, there is always a need to expand\/increase the size of a <a href=\"https:\/\/www.linux-kvm.org\/page\/Main_Page\" target=\"_blank\" rel=\"noreferrer noopener\">KVM<\/a> virtual machine disk size. However, there are situations whereby you might assign a KVM virtual machine &#8220;too much&#8221; disk space than it is actually required. This may prompt you to want to reduce\/shrink the disk size.<\/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-disk-size\">Shrinking KVM Virtual Machine Disk Size<\/a><ul><li><a href=\"#types-of-partitions\">Types of Partitions<\/a><ul><li><a href=\"#disk-with-primary-and-extended-logical-partitions\">Disk with Primary and Extended\/Logical Partitions<\/a><\/li><li><a href=\"#lvm-partitioned-disk\">LVM Partitioned Disk<\/a><\/li><\/ul><\/li><li><a href=\"#backup-the-virtual-machine-disk\">Backup the Virtual Machine Disk<\/a><\/li><li><a href=\"#update-fstab-to-use-device-instead-of-uuid\">Update FSTAB to use Device Instead of UUID<\/a><\/li><li><a href=\"#decrease-shrink-virtual-machine-partition-primary-extended\">Decrease\/Shrink Virtual Machine Partition (Primary\/Extended)<\/a><ul><li><a href=\"#attach-and-boot-virtual-machine-from-g-parted-editor-live-iso\">Attach Gparted Live ISO to the Virtual Machine<\/a><\/li><li><a href=\"#decrease-shrink-virtual-machine-partition-and-filesystem-on-g-parted-editor\">Decrease\/Shrink Virtual Machine Partition and Filesystem on GParted Editor<\/a><\/li><li><a href=\"#poweroff-the-virtual-machine\">Poweroff the Virtual Machine<\/a><\/li><li><a href=\"#detach-g-parted-live-iso-from-the-vm\">Detach GParted Live ISO from the VM<\/a><\/li><li><a href=\"#reduce-shrink-disk-size-of-the-virtual-machine-to-an-appropriate-size\">Reduce\/Shrink Disk size of the virtual machine to an appropriate size<\/a><\/li><\/ul><\/li><li><a href=\"#confirm-disk-shrinkage\">Confirm Disk Shrinkage<\/a><\/li><li><a href=\"#more-tutorials\">More Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"shrinking-kvm-virtual-machine-disk-size\">Shrinking KVM Virtual Machine Disk Size<\/h2>\n\n\n\n<p>Well, I must say that decreasing\/shrinking virtual machine disk size is not a walk in the park exercise. Any wrong move will make your virtual machine unusable. Be cautions, as you will solely take responsibility for what happens to your Virtual Machine!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"types-of-partitions\">Types of Partitions<\/h3>\n\n\n\n<p>In this tutorial, we have 2 virtual machines with different type of partitions;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Disk with Primary and Extended\/Logical Partitions<\/li>\n\n\n\n<li>LVM partitioned disk<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"disk-with-primary-and-extended-logical-partitions\">Disk with Primary and Extended\/Logical Partitions<\/h4>\n\n\n\n<p>Consider this one with primary, extended\/logical partitions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fdisk -l \/dev\/vda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nDisk \/dev\/vda: 20 GiB, 21474836480 bytes, 41943040 sectors\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: 0xbc1d1b85\n\nDevice     Boot    Start      End  Sectors  Size Id Type\n\/dev\/vda1  *        2048 39942143 39940096   19G 83 Linux\n\/dev\/vda2       39944190 41940991  1996802  975M  5 Extended\n\/dev\/vda5       39944192 41940991  1996800  975M 82 Linux swap \/ Solaris\n<\/code><\/pre>\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\nvda    254:0    0   20G  0 disk \n\u251c\u2500vda1 254:1    0   19G  0 part \/\n\u251c\u2500vda2 254:2    0    1K  0 part \n\u2514\u2500vda5 254:5    0  975M  0 part [SWAP]\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"lvm-partitioned-disk\">LVM Partitioned Disk<\/h4>\n\n\n\n<p>Another one with just two primary partitions and a boot partition (LVM);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fdisk -l \/dev\/vda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nDisk \/dev\/sda: 39.06 GiB, 41943040000 bytes, 81920000 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: gpt\nDisk identifier: 55B779AD-FE40-4978-9911-787F868D006B\n\nDevice       Start      End  Sectors  Size Type\n\/dev\/sda1     2048     4095     2048    1M BIOS boot\n\/dev\/sda2     4096  3719167  3715072  1.8G Linux filesystem\n\/dev\/sda3  3719168 81919966 78200799 37.3G Linux filesystem\n<\/code><\/pre>\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<p>As an example, the command below shows disk space consumption on the root filesystem;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -hT -P \/<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\nFilesystem                    Type  Size  Used Avail Use% Mounted on\n\/dev\/mapper\/bookworm--vg-root ext4   19G  1.2G   16G   7% \/\n<\/code><\/pre>\n\n\n\n<p>As you can see, the virtual machine is assigned 20GB of disk space while it is actually using less than 2GB.<\/p>\n\n\n\n<p>Check the guide below to learn how to shrink LVM partitioned disk of a KVM virtual machine.<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/shrink-kvm-virtual-machine-lvm-partitioned-disk\/\" target=\"_blank\" rel=\"noreferrer noopener\">Shrink KVM Virtual Machine LVM Partitioned Disk<\/a><\/p>\n\n\n\n<p>You can also get image information using <strong><code>qemu-img<\/code><\/strong> command (<strong>the vm using the image should be powered off to use this command<\/strong>);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo qemu-img info \/path\/to\/image.qcow2<\/code><\/pre>\n\n\n\n<p>E.g;<\/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<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nimage: \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2\nfile format: qcow2\nvirtual size: 20 GiB (21474836480 bytes)\ndisk size: 2.77 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>In a situation where every shred of the disk space matters most, you might need to be allocating disk space on need basis.<\/p>\n\n\n\n<p>In our demo system, 20GB is too much and we would like to decrease\/shrink the disk space by 10GB to bring the total disk space allocated to the virtual machine to 10GB respectively.<\/p>\n\n\n\n<p>To reduce KVM virtual machine disk size, there are steps you need to follow;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Backup the virtual machine disk<\/li>\n\n\n\n<li>Decrease\/Shrink the system partition<\/li>\n\n\n\n<li>Decrease\/Shrink partition file-system to match to align with the partition<\/li>\n\n\n\n<li>Reduce\/Shrink the disk size of the virtual machine to an appropriate size.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"backup-the-virtual-machine-disk\">Backup the Virtual Machine Disk<\/h3>\n\n\n\n<p>If you are planning on taking this risky path of decreasing or shrinking your virtual machine root file-system partition, then the backup step is very much necessary, unless you are willing to start things off from the scratch and waste time all over again!<\/p>\n\n\n\n<p>Thus, to backup up your virtual machine, you can simply clone it or just copy the image;<\/p>\n\n\n\n<p>Thus, get the domain name of the virtual machine and power it off;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh list<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\n Id   Name       State\n--------------------------\n 6    bookworm   running\n 7    jelly      running\n\n<\/code><\/pre>\n\n\n\n<p>Shut the respective virtual machine down;<\/p>\n\n\n\n<p>Power off the virtual machine<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl poweroff<\/code><\/pre>\n\n\n\n<p>Or poweroff the virtual machine using virsh command;<\/p>\n\n\n\n<p>e.g<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh shutdown jelly<\/code><\/pre>\n\n\n\n<p>You can backup by either clone or copy;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp jelly{,-copy}.qcow2<\/code><\/pre>\n\n\n\n<p>or (replace vm-domain with the name of your virtual machine);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virt-clone --original <strong>vm-domain<\/strong> --auto-clone<\/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>Once you have backed up your virtual machine, power it on, login to it so you can shrink your partition.<\/p>\n\n\n\n<p>Next, before you can decrease or shrink the virtual machine partition, 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\/vda5: UUID=\"833fdaf3-5d1d-4f94-ba27-ad9607360385\" TYPE=\"swap\" PARTUUID=\"bc1d1b85-05\"\n\/dev\/vda1: UUID=\"e0a51bb6-1266-44b2-a4e2-67263548f77c\" BLOCK_SIZE=\"4096\" TYPE=\"ext4\" PARTUUID=\"bc1d1b85-01\"\n<\/code><\/pre>\n\n\n\n<p>Hence;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/fstab<\/code><\/pre>\n\n\n\n<p>It may look like this before the changes;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n# \/etc\/fstab: static file system information.\n#\n# Use 'blkid' to print the universally unique identifier for a\n# device; this may be used with UUID= as a more robust way to name devices\n# that works even if disks are added and removed. See fstab(5).\n#\n# systemd generates mount units based on this file, see systemd.mount(5).\n# Please run 'systemctl daemon-reload' after making changes here.\n#\n# <file system> <mount point>   <type>  <options>       <dump>  <pass>\n# \/ was on \/dev\/vda1 during installation\n<strong>UUID=e0a51bb6-1266-44b2-a4e2-67263548f77c \/               ext4    errors=remount-ro 0       1<\/strong>\n# swap was on \/dev\/vda5 during installation\n<strong>UUID=833fdaf3-5d1d-4f94-ba27-ad9607360385 none            swap    sw              0       0<\/strong>\n<\/code><\/pre>\n\n\n\n<p>The root partition is using UUID. You need to change this line therefore;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UUID=e0a51bb6-1266-44b2-a4e2-67263548f77c \/               ext4    errors=remount-ro 0       1<\/code><\/pre>\n\n\n\n<p>Similarly, <strong>comment<\/strong> the swap line, you can add swap later on;<\/p>\n\n\n\n<pre class=\"scroll-sz\"><code>\n# &lt;file system> &lt;mount point>   &lt;type>  &lt;options>       &lt;dump>  &lt;pass>\n# \/ was on \/dev\/vda1 during installation\n#UUID=e0a51bb6-1266-44b2-a4e2-67263548f77c \/               ext4    errors=remount-ro 0       1\n<strong>\/dev\/vda1 \/               ext4    errors=remount-ro 0       1\n<\/strong># swap was on \/dev\/vda5 during installation\n<strong>#UUID=833fdaf3-5d1d-4f94-ba27-ad9607360385 none            swap    sw              0       0<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Save the file and exit;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"decrease-shrink-virtual-machine-partition-primary-extended\">Decrease\/Shrink Virtual Machine Partition (Primary\/Extended)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"attach-and-boot-virtual-machine-from-g-parted-editor-live-iso\">Attach and Boot Virtual Machine from GParted Editor Live ISO<\/h4>\n\n\n\n<p><strong>Note that this is for the disk with primary and extended\/logical partitions;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fdisk -l \/dev\/vda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nDisk \/dev\/vda: 20 GiB, 21474836480 bytes, 41943040 sectors\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: 0xbc1d1b85\n\nDevice     Boot    Start      End  Sectors  Size Id Type\n<strong>\/dev\/vda1  *        2048 39942143 39940096   19G 83 Linux\n\/dev\/vda2       39944190 41940991  1996802  975M  5 Extended\n\/dev\/vda5       39944192 41940991  1996800  975M 82 Linux swap \/ Solaris<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Well, as you much you can use fdisk to shrink the partition, it can really mess you big time. Hence, I recommend using Gparted Live ISO to shrink the partition and subsequently update the file-system appropriately.<\/p>\n\n\n\n<p>Thus, download <a href=\"https:\/\/gparted.org\/download.php\" target=\"_blank\" rel=\"noreferrer noopener\">Gparted Live ISO<\/a> on to your Host system;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/downloads.sourceforge.net\/gparted\/gparted-live-1.5.0-1-amd64.iso<\/code><\/pre>\n\n\n\n<p>Poweroff the virtual machine<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl poweroff<\/code><\/pre>\n\n\n\n<p>Or poweroff the virtual machine using virsh command;<\/p>\n\n\n\n<p>e.g<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh shutdown bookworm<\/code><\/pre>\n\n\n\n<p>Launch the virt-manager;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>open the virtual machine details;<\/li>\n\n\n\n<li>Click <strong>Add hardware<\/strong> button at the bottom;<\/li>\n\n\n\n<li>Under storage, click select or create custom image;<\/li>\n\n\n\n<li>Click <strong>manage<\/strong> to select the ISO from your host machine and attach to the virtual machine;<\/li>\n\n\n\n<li>Ensure device type is <strong>Disk device<\/strong> and bus type is <strong>VirtIO<\/strong>.<\/li>\n\n\n\n<li>Click <strong>Finish<\/strong> to attach<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1526\" height=\"806\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/attach-live-gparted-iso.png\" alt=\"Decrease\/Shrink KVM Virtual Machine Disk Size\" class=\"wp-image-17478\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/attach-live-gparted-iso.png?v=1687603576 1526w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/attach-live-gparted-iso-768x406.png?v=1687603576 768w\" sizes=\"(max-width: 1526px) 100vw, 1526px\" \/><\/figure>\n\n\n\n<p>Configure the VM to boot using Gparted Live ISO  under boot options (disk 2 as you can see in the screenshot above);<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1374\" height=\"809\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/boot-from-gparted-live-iso.png\" alt=\"Decrease\/Shrink KVM Virtual Machine Disk Size\" class=\"wp-image-17479\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/boot-from-gparted-live-iso.png?v=1687603669 1374w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/boot-from-gparted-live-iso-768x452.png?v=1687603669 768w\" sizes=\"(max-width: 1374px) 100vw, 1374px\" \/><\/figure>\n\n\n\n<p>Apply the changes and start the virtual machine.<\/p>\n\n\n\n<p>Open the graphical console of the vm and boot Gparted Live with default settings;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1465\" height=\"858\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/boot-gparted-live-default-settings.png\" alt=\"Decrease\/Shrink KVM Virtual Machine Disk Size\" class=\"wp-image-17389\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/boot-gparted-live-default-settings.png?v=1687463014 1465w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/boot-gparted-live-default-settings-768x450.png?v=1687463014 768w\" sizes=\"(max-width: 1465px) 100vw, 1465px\" \/><\/figure>\n\n\n\n<p>Press ENTER at any given prompt to accept the default settings. for the <strong>keymap (Dont touch keymap),<\/strong> the language and star X to use GParted automatically.<\/p>\n\n\n\n<p>You should now land on GParted editor interface.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1104\" height=\"827\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/gparted-interface-.png\" alt=\"Decrease\/Shrink KVM Virtual Machine Disk Size\" class=\"wp-image-17393\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/gparted-interface-.png?v=1687463741 1104w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/gparted-interface--768x575.png?v=1687463741 768w\" sizes=\"(max-width: 1104px) 100vw, 1104px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"decrease-shrink-virtual-machine-partition-and-filesystem-on-g-parted-editor\">Decrease\/Shrink Virtual Machine Partition and Filesystem on GParted Editor<\/h4>\n\n\n\n<p>From the Gparted Interface, select the partition, right click and click Resize\/Move.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1063\" height=\"820\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/resize-disk.png\" alt=\"\" class=\"wp-image-17396\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/resize-disk.png?v=1687464454 1063w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/resize-disk-768x592.png?v=1687464454 768w\" sizes=\"(max-width: 1063px) 100vw, 1063px\" \/><\/figure>\n\n\n\n<p>Under the new size (MiB), enter size you want to reduce the disk to and press Enter. e.g in our case, we are reducing it to 10GB so new size is10240MiB (enter the size and click just outside the box);<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1058\" height=\"800\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/shrink-disk-size.png\" alt=\"\" class=\"wp-image-17397\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/shrink-disk-size.png?v=1687464465 1058w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/shrink-disk-size-768x581.png?v=1687464465 768w\" sizes=\"(max-width: 1058px) 100vw, 1058px\" \/><\/figure>\n\n\n\n<p>Click <strong>Resize\/Move<\/strong> to resize the partition and you should can now see used and unused space.<\/p>\n\n\n\n<p><strong>You can also delete the swap partition<\/strong>.<\/p>\n\n\n\n<p id=\"shrink-filesystem\">To apply the changes, click the green tick at the top and <strong>apply<\/strong>. This will shrink the partition and update the filesystem accordingly.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1054\" height=\"798\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/filesystem-resize.png\" alt=\"\" class=\"wp-image-17398\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/filesystem-resize.png?v=1687464481 1054w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/06\/filesystem-resize-768x581.png?v=1687464481 768w\" sizes=\"(max-width: 1054px) 100vw, 1054px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"poweroff-the-virtual-machine\">Poweroff the Virtual Machine<\/h4>\n\n\n\n<p>Once the changes are applied, shutdown the virtual machine.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"detach-g-parted-live-iso-from-the-vm\">Detach GParted Live ISO from the VM<\/h4>\n\n\n\n<p>Next, reconfigure the VM to boot from the main disk by detaching the GParted Live ISO file and apply the changes and start it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"reduce-shrink-disk-size-of-the-virtual-machine-to-an-appropriate-size\">Reduce\/Shrink Disk size of the virtual machine to an appropriate size<\/h4>\n\n\n\n<p>Once the system boots, login and confirm the disk size;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -hT -P \/<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\nFilesystem     Type  Size  Used Avail Use% Mounted on\n\/dev\/vda1      ext4  9.7G  4.4G  4.9G  48% \/\n<\/code><\/pre>\n\n\n\n<p>Down from 20G to ~10G, -:).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fdisk -l \/dev\/sda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>Disk \/dev\/vda: 20 GiB, 21474836480 bytes, 41943040 sectors<\/strong>\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: 0xbc1d1b85\n\nDevice     Boot    Start      End  Sectors  Size Id Type\n<strong>\/dev\/vda1  *        2048 20973567 20971520   10G 83 Linux<\/strong>\n\/dev\/vda2       39944190 41940991  1996802  975M  5 Extended\n\/dev\/vda5       39944192 41940991  1996800  975M 82 Linux swap \/ Solaris\n<\/code><\/pre>\n\n\n\n<p>As you can see, the filesystem has been shrinked to 10G but disk still shows 20G. Thus , on the host system, shrink the size of the virtual machine disk image using <strong><code>qemu-img<\/code><\/strong> command.<\/p>\n\n\n\n<p><strong>When shrinking with <code>qemu-img<\/code> command, do not use exact same size. Make it a little bigger!<\/strong><\/p>\n\n\n\n<p>Therefore, shutdown the VM again!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh shutdown &lt;virtual-machine-name&gt;<\/code><\/pre>\n\n\n\n<p>E.g;<\/p>\n\n\n\n<p>Shrink the disk size;<\/p>\n\n\n\n<p>(<strong><em>Replace \/path\/to\/image.qcow2 accordingly<\/em><\/strong>)<\/p>\n\n\n\n<p>NOTE: You cannot shrink if the image has snapshots!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo qemu-img resize --shrink \/path\/to\/image.qcow2 10752M<\/code><\/pre>\n\n\n\n<p>We are setting the total size to 10G (10240 MiB) but see, I added a 500M up;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qemu-img resize --shrink \/media\/kifarunix\/vol02\/kvm\/images\/bookworm.qcow2 10752M<\/code><\/pre>\n\n\n\n<p>Confirm the virtual disk size;<\/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 (11274289152 bytes)<\/strong>\ndisk size: 5.73 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>See now, image is resized.<\/p>\n\n\n\n<p>Hold your breathe and reboot the VM! If you get to the login page, then you are good to go, ninja!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"confirm-disk-shrinkage\">Confirm Disk Shrinkage<\/h3>\n\n\n\n<p>Login and confirm!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fdisk -l \/dev\/vda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>Disk \/dev\/vda: 10.5 GiB, 11274289152 bytes, 22020096 sectors\n<\/strong>Units: 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: 0xbc1d1b85\n\nDevice     Boot Start      End  Sectors Size Id Type\n\/dev\/vda1  *     2048 20973567 20971520  10G 83 Linux\n<\/code><\/pre>\n\n\n\n<p>And that is it!<\/p>\n\n\n\n<p>That completes our guide on how to shrink KVM Virtual Machine disk size, especially on disks with primary and extended\/logical partitions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"more-tutorials\">More Tutorials<\/h3>\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<p><a href=\"https:\/\/kifarunix.com\/install-kvm-on-oracle-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install KVM on Oracle Linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-clone-kvm-virtual-machines\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Clone KVM Virtual Machines<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn an easy way to decrease\/shrink KVM virtual machine disk size. In most cases, there is always a need to<\/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":[6941,6942,6943,6940,6939],"class_list":["post-14637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-storage","category-howtos","category-kvm","category-virtualization","tag-disk-size-virtual-machine-shrink","tag-how-to-decrease-the-size-of-a-kvm-virtual-machine-disk-image","tag-how-to-reduce-the-size-of-kvm-qcow2-images","tag-reduce-kvm-virtual-machine-disk-size","tag-shrink-kvm-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\/14637"}],"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=14637"}],"version-history":[{"count":33,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/14637\/revisions"}],"predecessor-version":[{"id":20765,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/14637\/revisions\/20765"}],"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=14637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=14637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=14637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}