{"id":13546,"date":"2022-07-30T15:24:17","date_gmt":"2022-07-30T12:24:17","guid":{"rendered":"https:\/\/kifarunix.com\/?p=13546"},"modified":"2024-03-09T16:14:58","modified_gmt":"2024-03-09T13:14:58","slug":"easy-way-to-extend-kvm-virtual-machine-disk-size","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/easy-way-to-extend-kvm-virtual-machine-disk-size\/","title":{"rendered":"Easy Way to Extend KVM Virtual Machine Disk Size"},"content":{"rendered":"\n<p>This is a simple tutorial on an easy way to extend KVM virtual machine disk size. Disk usage in a VM is among the limited resources which sometimes require to be increased\/reduced. It is possible to resize the disk in KVM using QEMU.<\/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=\"#easy-way-to-extend-kvm-virtual-machine-disk-size\">Easy Way to Extend KVM Virtual Machine Disk Size<\/a><ul><li><a href=\"#get-the-correct-name-of-the-domain-virtual-machine\">Get the Correct Name of the Domain\/Virtual Machine<\/a><\/li><li><a href=\"#locate-domain-virtual-machine-disk-path\">Locate Domain\/Virtual Machine Disk Path<\/a><\/li><li><a href=\"#backup-the-virtual-machine\">Backup the Virtual Machine<\/a><\/li><li><a href=\"#extend-kvm-virtual-machine-disk-size\">Extend KVM Virtual Machine Disk Size<\/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=\"easy-way-to-extend-kvm-virtual-machine-disk-size\">Easy Way to Extend KVM Virtual Machine Disk Size<\/h2>\n\n\n\n<p>In this guide, we will use the qcow2 disk format, which is the default in KVM. Other formarts such as vmdk,qcow,cow, raw e.t.c can be extended in a similar way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"get-the-correct-name-of-the-domain-virtual-machine\">Get the Correct Name of the Domain\/Virtual Machine<\/h3>\n\n\n\n<p>Before you proceed, you need to check the properties of the VM you need to extend.<\/p>\n\n\n\n<p>Use virsh command to check the list of all running VMs so as to find the correct name of the VM\/domain you need to extend the disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh list --all<\/code><\/pre>\n\n\n\n<p>You will get a list similar to the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Id   Name         State\n----------------------------\n 15   almalinux8   running\n 17   opensuse     running<\/code><\/pre>\n\n\n\n<p>You can also use the GUI tool Virtual Machine Manager (VMM) to view the details\/state of VMs.<\/p>\n\n\n\n<p>Now select a VM you want to resize from list provided by the command above.<\/p>\n\n\n\n<p>In our case, we will be extending the <strong>almalinux8<\/strong> VM.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"locate-domain-virtual-machine-disk-path\">Locate Domain\/Virtual Machine Disk Path<\/h3>\n\n\n\n<p>Let\u2019s locate the disk path of the VM whose disk is to be extended using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh domblklist --domain almalinux8<\/code><\/pre>\n\n\n\n<p>Change the last part (almalinux8) to the name of your VM.<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Target   Source\n----------------------------------------------------\n vda      \/var\/lib\/libvirt\/images\/almalinux8.qcow2\n sda      -<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"backup-the-virtual-machine\">Backup the Virtual Machine<\/h3>\n\n\n\n<p>I recommended that you make a&nbsp;backup&nbsp;of the virtual machine you want to the extend the disk before you can proceed to avoid loses in case of any mistake.<\/p>\n\n\n\n<p>You can make a copy of the qcow2;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp \/var\/lib\/libvirt\/images\/almalinux8.qcow2 \/home\/vms\/almalinux8-COPY.qcow2<\/code><\/pre>\n\n\n\n<p>Or clone the current machine state and extend the disk on the cloned VM.<\/p>\n\n\n\n<p>Check our guide on <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\n\n\n<p>You can then power on the clone and extend the disk as follows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"extend-kvm-virtual-machine-disk-size\">Extend KVM Virtual Machine Disk Size<\/h3>\n\n\n\n<p>There are different ways in which you can extend KVM virtual machine disk size:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shutting down the VM, extending using <code>qemu-img<\/code>, then start the VM to complete disk expansion.<\/li>\n\n\n\n<li>Extending a live VM Disk<\/li>\n\n\n\n<li>Extending a powered-off VM Disk entirely from a host.<\/li>\n<\/ul>\n\n\n\n<p>In this guide, we will see how to extend KVM virtual machine disk size using the first method.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The first step is to power off the VM before moving to the next steps.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh shutdown --domain &lt;vm_name&gt;<\/code><\/pre>\n\n\n\n<p>You can also use GUI to shutdown.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>Next, you need to delete the VM snapshots if there are any. <strong>Note that you have to have backed up the VM before deleting snapshots.<\/strong><\/li>\n<\/ol>\n\n\n\n<p>You can delete the snapshots from GUI or delete using the command&nbsp;<strong>snapshot-delete<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh snapshot-list --domain &lt;vm_name&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh snapshot-delete --domain &lt;vm_name&gt; --snapshotname &lt;snapshot_name&gt;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>Check the size of the disk using&nbsp;<strong>fdisk<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk -l \/var\/lib\/libvirt\/images\/almalinux8.qcow2<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Disk \/var\/lib\/libvirt\/images\/almalinux8.qcow2: 20 GiB, 21478375424 bytes, 41949952 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<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>Extend the VM using&nbsp;<strong>qemu-img<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>You can specify the ultimate size or the size you want to add preceeded by&nbsp;<strong>+<\/strong>&nbsp;for adding and&nbsp;<strong>&#8211;<\/strong>&nbsp;for reducing. If we want the ultimate size to be 30 GB, we use 30G. We can also specify +30G to mean 30GB will be added to the existing size. We are adding 10 GB in our case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qemu-img resize \/var\/lib\/libvirt\/images\/almalinux8.qcow2 +10G<\/code><\/pre>\n\n\n\n<p>Again, check the size from fdisk<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk -l \/var\/lib\/libvirt\/images\/almalinux8.qcow2<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Disk \/var\/lib\/libvirt\/images\/almalinux8.qcow2: 20 GiB, 21478375424 bytes, 41949952 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\n<\/code><\/pre>\n\n\n\n<p>You can see that the disk has not increased. However, when checked with qemu-img command we note an increase in size.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>qemu-img info \/var\/lib\/libvirt\/images\/almalinux8.qcow2<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>image: \/var\/lib\/libvirt\/images\/almalinux8.qcow2\nfile format: qcow2\n<strong>virtual size: 30 GiB (32212254720 bytes)<\/strong>\ndisk size: 2.91 GiB\ncluster_size: 65536\nFormat specific information:\n    compat: 1.1\n    compression type: zlib\n    lazy refcounts: true\n    refcount bits: 16\n    corrupt: false\n    extended l2: false\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>Power on the VM<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>virsh start --domain &lt;vm_name&gt;<\/code><\/pre>\n\n\n\n<p>Login to your VM and check the disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lsblk<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>[root@localhost ~]# lsblk\nNAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT\nsr0                 11:0    1 1024M  0 rom  \n<strong>vda                252:0    0   30G  0 disk <\/strong>\n\u251c\u2500vda1             252:1    0    1G  0 part \/boot\n\u2514\u2500vda2             252:2    0   19G  0 part \n  \u251c\u2500almalinux-root 253:0    0   17G  0 lvm  \/\n  \u2514\u2500almalinux-swap 253:1    0    2G  0 lvm  [SWAP]\n\n<\/code><\/pre>\n\n\n\n<p>You can note that the total disk (vda) is 30 GB while the usable (vda1+vda2) is 20 GB.<\/p>\n\n\n\n<p>You need to expand the disk now.<\/p>\n\n\n\n<p>The fdisk utility will recognize the partition mismatch and will correct. Run fdisk against disk:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk \/dev\/vda<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>[root@localhost ~]# fdisk \/dev\/vda\n\nWelcome to fdisk (util-linux 2.32.1).\nChanges will remain in memory only, until you decide to write them.\nBe careful before using the write command.\n\n<\/code><\/pre>\n\n\n\n<p>write&nbsp;<strong>p<\/strong>&nbsp;and press enter to show the partition table.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Command (m for help): p\n<strong>Disk \/dev\/vda: 30 GiB, 32212254720 bytes, 62914560 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: 0x73223988\n\nDevice     Boot   Start      End  Sectors Size Id Type\n\/dev\/vda1  *       2048  2099199  2097152   1G 83 Linux\n\/dev\/vda2       2099200 41943039 39843840  19G 8e Linux LVM\n<\/code><\/pre>\n\n\n\n<p>Note that vda2 is the partition we need to resize in this example setup.<\/p>\n\n\n\n<p>So we need to delete it and allocate a new partition at the start of the same segment. So press&nbsp;<strong>d<\/strong>&nbsp;and enter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Command (m for help): d\nPartition number (1,2, default 2): 2\n\nPartition 2 has been deleted.<\/code><\/pre>\n\n\n\n<p>Then create a new partition.<\/p>\n\n\n\n<p>Press&nbsp;<strong>n<\/strong>&nbsp;and enter to accept defaults for partition number, first sector and last sector.<\/p>\n\n\n\n<p>When prompted to <strong>remove signature, enter NO<\/strong>.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Command (m for help): n\nPartition type\n   p   primary (1 primary, 0 extended, 3 free)\n   e   extended (container for logical partitions)\nSelect (default p): \n\nUsing default response p.\nPartition number (2-4, default 2): \nFirst sector (2099200-62914559, default 2099200): \nLast sector, +sectors or +size{K,M,G,T,P} (2099200-62914559, default 62914559): \n\nCreated a new partition 2 of type 'Linux' and of size 29 GiB.\nPartition #2 contains a LVM2_member signature.\n\n<strong>Do you want to remove the signature? [Y]es\/[N]o: N<\/strong>\n\n<\/code><\/pre>\n\n\n\n<p>Commit the changes using&nbsp;<strong>w<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Command (m for help): w\n\nThe partition table has been altered.\nSyncing disks.<\/code><\/pre>\n\n\n\n<p>At this point, there are two options;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the disk is partitioned using <strong>standard partitioning system<\/strong>, the disk extension is now done and you should just reboot the system.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>reboot<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the disk is partitioned dynamically using Logical Volume Management system, then proceed as follows;<\/li>\n<\/ul>\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 MOUNTPOINT\nsr0                 11:0    1 1024M  0 rom  \nvda                252:0    0   30G  0 disk \n\u251c\u2500vda1             252:1    0    1G  0 part \/boot\n\u2514\u2500vda2             252:2    0   29G  0 part \n  \u251c\u2500almalinux-root 253:0    0   17G  0 lvm  \/\n  \u2514\u2500almalinux-swap 253:1    0    2G  0 lvm  [SWAP]\n<\/code><\/pre>\n\n\n\n<p>We can now see that vda2 is ~30GB <strong>BUT<\/strong> the logical volume has not been extended.<\/p>\n\n\n\n<p>To finalize the disk expansion;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Extend the Physical Volume<\/li>\n\n\n\n<li>Extend the Volume Group<\/li>\n\n\n\n<li>Extend the logical volume<\/li>\n\n\n\n<li>Extend the filesystem.<\/li>\n<\/ul>\n\n\n\n<p>Extend the physical volume;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pvs<\/code><\/pre>\n\n\n\n<p>Output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  PV         VG        Fmt  Attr PSize   PFree\n  \/dev\/vda2  almalinux lvm2 a--  &lt;19.00g    0<\/code><\/pre>\n\n\n\n<p>Next, resize the physical volume,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pvresize \/dev\/vda2<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  Physical volume \"\/dev\/vda2\" changed\n  1 physical volume(s) resized or updated \/ 0 physical volume(s) not resized<\/code><\/pre>\n\n\n\n<p>Extend volume group;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vgextend almalinux \/dev\/vda2<\/code><\/pre>\n\n\n\n<p>Extend the logical volume to all Free space;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lvextend -l +100%FREE \/dev\/mapper\/almalinux-root<\/code><\/pre>\n\n\n\n<p>Resize the Filesystem;<\/p>\n\n\n\n<p>If using EXT4, then;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resize2fs \/dev\/mapper\/almalinux-root<\/code><\/pre>\n\n\n\n<p>If using XFS, then;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>xfs_growfs \/dev\/almalinux\/root<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nmeta-data=\/dev\/mapper\/almalinux-root isize=512    agcount=4, agsize=1113856 blks\n         =                       sectsz=512   attr=2, projid32bit=1\n         =                       crc=1        finobt=1, sparse=1, rmapbt=0\n         =                       reflink=1    bigtime=0 inobtcount=0\ndata     =                       bsize=4096   blocks=4455424, imaxpct=25\n         =                       sunit=0      swidth=0 blks\nnaming   =version 2              bsize=4096   ascii-ci=0, ftype=1\nlog      =internal log           bsize=4096   blocks=2560, version=2\n         =                       sectsz=512   sunit=0 blks, lazy-count=1\nrealtime =none                   extsz=4096   blocks=0, rtextents=0\ndata blocks changed from 4455424 to 7076864\n<\/code><\/pre>\n\n\n\n<p>Your disk should now be extended.<\/p>\n\n\n\n<p>Confirm with the df command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -hT<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nFilesystem                 Type      Size  Used Avail Use% Mounted on\ndevtmpfs                   devtmpfs  636M     0  636M   0% \/dev\ntmpfs                      tmpfs     655M     0  655M   0% \/dev\/shm\ntmpfs                      tmpfs     655M  8.6M  646M   2% \/run\ntmpfs                      tmpfs     655M     0  655M   0% \/sys\/fs\/cgroup\n\/dev\/mapper\/almalinux-root xfs        27G  2.1G   25G   8% \/\n\/dev\/vda1                  xfs      1014M  258M  757M  26% \/boot\ntmpfs                      tmpfs     131M     0  131M   0% \/run\/user\/0\n<\/code><\/pre>\n\n\n\n<p>And that is how you can easily extend KVM virtual machine disk size.<\/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\/start-and-stop-kvm-virtual-machines-from-command-line\/\" target=\"_blank\" rel=\"noreferrer noopener\">Start and Stop KVM Virtual Machines from Command Line<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/list-running-and-stopped-vms-on-kvm\/\">List Running and Stopped VMS on KVM<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple tutorial on an easy way to extend KVM virtual machine disk size. Disk usage in a VM is among the limited<\/p>\n","protected":false},"author":4,"featured_media":13608,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[39,121,112,43,36],"tags":[5608,5604,5606,5605,5610,5612,5609,5611,5607],"class_list":["post-13546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-storage","category-howtos","category-kvm","category-lvm","category-virtualization","tag-growpar","tag-increase-disk-size-for-kvm-virtual-machine","tag-kvm-virtual-machine-disk-expansion","tag-kvm-virtual-machine-disk-extension","tag-lvextend","tag-lvextend-100free","tag-pvresize","tag-vgextend","tag-xfs_grow","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\/13546"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=13546"}],"version-history":[{"count":15,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/13546\/revisions"}],"predecessor-version":[{"id":20582,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/13546\/revisions\/20582"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/13608"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=13546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=13546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=13546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}