KVM: How to Expand or Shrink a Virtual Machine Disk Size?

In this text we’ll speak about how to resize KVM digital machine disks for standard disk sorts. We’ll present how to accurately enhance a digital machine disk dimension within the KVM hypervisor on Linux with out shedding knowledge and talk about the choices to scale back disk dimension.


  • How to Increase/Extend a KVM Virtual Machine Disk Size?
  • How to Reduce Disk Size of KVM Virtual Machine?

How to Increase/Extend a KVM Virtual Machine Disk Size?

Increasing a Virtual Disk on KVM

Let’s think about an instance of accelerating the scale of a 20GB digital disk. Turn off the VM earlier than altering its digital disk settings.

To view the digital disk setting of a KVM digital machine, use this command:

qemu-img information /path_to_disk

You will see the end result like this:

qemu-img info - get-kvm virtual disk size

We see that there are two fields that present the scale: virtual_size and disk_size:

  • virtual_size – is the scale of a digital disk set when creating or increasing the disk (on this instance, the utmost disk dimension is 20GB);
  • disk_size — is the present dimension of the disk file, i. e., how a lot house the disk occupies on a bodily server (applies solely to the qcow disk format). In our instance, the digital disk takes up only one.6GB on the storage.

And some phrases about KVM disk sorts (codecs). I like to recommend utilizing qcow2 as a substitute of uncooked when creating digital machines in KVM. Later I’ll clarify why.

In the following step, I’ll enhance the scale of the digital machine by 5GB (to correctly prolong the disk, a digital machine should not have any snapshots!).
# qemu-img resize /path to disk +5G

Image resized.

If you test the digital disk settings, you’ll discover that it has been expanded:

# qemu-img information /path_to_disk

picture: /path_to_disk
file format: qcow2
digital dimension: 25G (26843535600 bytes)
disk dimension: 1.6G
cluster_size: 65536
Format particular info:
compat: zero.10
refcount bits: 16

We have achieved a a part of our job, however we additionally want some actions within the visitor OS of digital machine. Now we’ll present how to increase the disk dimension in visitor CentOS 7 and Windows Server 2012 R2.

If you’re going to add one other digital disk in your VM within the KVM hypervisor, qemu-img and virsh instruments are used.

How to Extend a Partition in a Virtual Machine Running CentOS Linux?

After growing the digital disk dimension on the KVM server facet, begin your digital machine and join to it. We’ll use the arduous disk partition administration instrument fdisk to prolong partition and file system dimension.

Connect to the VM Guest OS over SSH and ensure that further 5 Gb of disk house seems.

df –h
fdisk -l

extend a partition on linux centos vm

The screenshot reveals that /dev/vda2 has the scale of 20GB, and the obtainable disk house is 25GB.

Let’s prolong /dev/vda2 to the utmost obtainable dimension:

# fdisk /dev/vda

Welcome to fdisk (util-linux 2.23.2).
Changes will stay in reminiscence solely, till you resolve to write them.
Be cautious earlier than utilizing the write command.
Command (m for assist): d
Partition quantity (1,2, default 2):
Partition 2 is deleted
Command (m for assist): n
Partition sort:
p major (1 major, zero prolonged, three free)
e prolonged
Select (default p): p
Partition quantity (2-Four, default 2):
First sector (1050624-52428799, default 1050624):
Using default worth 1050624
Last sector, +sectors or +dimension (1050624-52428799, default 52428799):
Using default worth 52428799
Partition 2 of sort Linux and of dimension 24.5 GiB is about
Command (m for assist): w
The partition desk has been altered!
Calling ioctl() to re-read partition desk.
WARNING: Re-reading the partition desk failed with error 16: Device or useful resource busy.
The kernel nonetheless makes use of the previous desk. The new desk shall be used on the subsequent reboot or after you run partprobe(eight) or kpartx(eight)
Syncing disks.

Then restart the digital machine and run the command that can apply new disk dimension:

# xfs_growfs /dev/vda2

meta-data=/dev/vda2 isize=512 agcount=Four, agsize=1277888 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=zero spinodes=zero
knowledge = bsize=4096 blocks=5111552, imaxpct=25
= sunit=zero swidth=zero blks
naming =model 2 bsize=4096 ascii-ci=zero ftype=1
log =inside bsize=4096 blocks=2560, model=2
= sectsz=512 sunit=zero blks, lazy-count=1
realtime =none extsz=4096 blocks=zero, rtextents=zero
knowledge blocks modified from 5111552 to 6422272

# df -h

Filesystem Size Used Avail Use% Mounted on
devtmpfs 485M zero 485M zero% /dev
tmpfs 496M zero 496M zero% /dev/shm
tmpfs 496M 6.6M 489M 2% /run
tmpfs 496M zero 496M zero% /sys/fs/cgroup
/dev/vda2 25G 1.3G 24G 6% /
/dev/vda1 488M 100M 353M 23% /boot
tmpfs 100M zero 100M zero% /run/person/zero

As a end result, we’ve got bought an prolonged partition /dev/vda2. Here is what precisely we’ve got achieved:

  • fdisk /dev/vda — we ran fdisk and related to /dev/vda;
  • /d — we eliminated the partition. By default, it was the partition 2 that we would have liked, so we simply pressed Enter within the subsequent merchandise;
  • /n – we created a new partition and pressed Enter or p, then pressed Enter once more, for the reason that partition we had been making an attempt to create could be 2 by default;
  • When specifying the primary and the final sector we additionally simply press Enter in order for you to prolong the partition to the utmost. If you need to set the precise dimension, enter the disk dimension within the final sector as proven within the menu +dimension;
  • /w — to apply new settings.

After the VM restart, test the disk utilizing this command:

xfs_growfs /dev/vda2

Some sources advocate utilizing the next command :

resize2fs /dev/vda2

But it gained’twork for the XFS file system!

Thus, you might have prolonged the disk dimension on the digital machine working CentOS 7.

How to Increase the Disk Size in Guest Windows Server?

Connect to the Windows Server digital machine utilizing RDP or VNC and increase the disk.

  1. Open Server Manager -> Tools -> Computer Management;
  2. Select Storage -> Disk Management within the left pane. All of your disk partitions and unallocated house added will seem; windows server vm - unallocated space on disk
  3. Select the partition you need to enhance and click on Extend Volume;windows extend volume
  4. By default all obtainable house shall be supplied to enhance the disk dimension;extend volume wizard
  5. Click Next to end the extension. After that, the scale of your partition shall be elevated. new volume size on windows

I would love to observe that prior to your digital machine disk extension I like to recommend to again up the digital disk. Stop the digital machine and duplicate the disk picture to the backup listing having some free house. If one thing goes flawed through the extension course of, you’ll at all times give you the option to restore the digital disk picture from the backup.

How to Reduce Disk Size of KVM Virtual Machine?

In my work I usually come throughout this subject, however sadly, you can not safely scale back the disk dimension of a digital machine in KVM. The solely acceptable method to lower digital disk dimension on a bodily storage is to convert the disk.

There are a lot of guides it the online telling how to scale back the disk house in a KVM digital machine, however this doesn’t work.

I’ll give some examples I got here throughout and examined.

Reducing KVM Disk Size Using Qemu

Some sources describe actions you are able to do with qemu instrument. They supply to lower a digital machine disk house utilizing this command:

qemu-img resize /path_to_disk -5G — to shrink the disk house by 5G

Or the next choice the place you set the precise digital disk dimension:

qemu-img resize /path_to_disk 25G — to set the disk dimension to 25G

What occurs after working this command? After beginning the server, the system gained’t boot:

Unable to confirm superblock, persevering with...
Sorry, couldn't discover legitimate secondary superblock. Exiting now.

qemu-img resize - trying to reduce virtual disk size on kvm

I attempted to lower the disk dimension within the system utilizing fdisk first, however the system won’t boot both even when you skip shrinking your disk on the bodily server. And it’s fairly logical. The partition we are attempting to lower is a system one, and the OS gained’t boot for the reason that info on it’s clearly worn out when making an attempt to lower its dimension.

Decreasing a Virtual Disk Size Using Image Replacement

Less standard variant in these guides is to lower the disk house utilizing disk substitute. It signifies that you create a new digital machine with the disk dimension you want. Then you change the brand new disk picture with the previous one, and it really works based mostly on the descriptions in several sources. I checked this technique, and the file system turned inoperable like within the earlier variant. Here is an instance of the command:

virt-resize /old_disk_image /new_disk_image

There had been additionally some strategies that used disk conversion from uncooked to qcow2 format, however I’m initially create machines on this format and right here is the reason why I achieve this.

KVM Disk Formats and QCOW2 Disk Compression

In the start of the article I discussed these two disk codecs: uncooked and qcow.

Raw benefits are most efficiency and generality. But it has a lot of drawbacks, the principle of them are:

  • The digital disk file occupies all of the house allotted to it on a bodily server;
  • You can not take snapshots.

Qcow2 is the native QEMU and QEMU-KVM hypervisor format. It is probably the most handy digital disk format from all different codecs supported in KVM. The disk picture will increase as knowledge are gathered in a digital machine, and snapshots are supported.

What are the benefits of qcow2? You don’t want to lower the scale of your digital disk because it occupies solely as a lot house on a server storage because it wants. If knowledge are continually overwritten in your server, and the disk has swollen, you may simply compress it. Let’s think about this selection. I’ll refill some digital disk house with zeroes after which delete the file:

dd if=/dev/zero of=/mytempfile
rm -rf /mytempfile

Qcow2 disk format file size

When you test it out of your server, earlier the scale of the disk picture was 2.4G, after which it turned 5.9G:

# du -sh /image_path

2.4G *****

# du -sh /image_path

5.9G *****

It signifies that after deleting the data from the digital machine, the digital disk dimension has not shrunk. In order the digital disk file might get its precise dimension, I exploit the next technique:

Back up the digital disk file, cease the digital machine after which carry out the next steps:

qemu-img convert -O qcow2 /old_image /new_image

Then you may evaluate the sizes of each disks:

# du -sh /new_disk

1.6G /****

# du -sh /old_disk

5.8G /****

As you may see, the scale of the compressed disk is 1.6G. Rename the brand new disk picture file as you want and begin the digital machine:
# df -h

Filesystem Size Used Avail Use% Mounted on
devtmpfs 485M zero 485M zero% /dev
tmpfs 496M zero 496M zero% /dev/shm
tmpfs 496M 6.6M 489M 2% /run
tmpfs 496M zero 496M zero% /sys/fs/cgroup
/dev/vda2 25G 1.3G 24G 6% /
/dev/vda1 488M 100M 353M 23% /boot
tmpfs 100M zero 100M zero% /run/person/zero

This technique works. All the time I’ve been utilizing it, I’ve not had any scenario when the file system in a digital machine failed. But I strongly advocate to again up the disk prior doing something so as not to lose any knowledge.

This is all I needed to inform on this article. If anyone of this web site guests know some other actually actionable strategies to lower a disk dimension on KVM (not a compression), describe them right here and I’ll check them.

Check Also

How to Enable and Configure MPIO on Windows Server 2016/2012R2?

In this text we’ll contemplate how to set up and configure MPIO on Windows Server …

Leave a Reply

Your email address will not be published. Required fields are marked *