Reducing the disk size of a vmware virtual machine. How to shrink an oversized VirtualBox virtual disk

While working with Hyper-v, you may need to resize hard drives virtual machines. Unfortunately, standard means Windows 2008 R2 You do not allow downsizing hard drive- only disk enlargement or conversion from dynamic to fixed (and vice versa).

VHD Resizer Utility

This utility allows you to both increase and decrease the size of your hard drive, both static and dynamic. Unfortunately, the official website of the program has ceased to exist. However, you can download the utility on the Internet or from us.

Powershell on Windows 2012

In earlier versions of Hyper-V, there was no functionality to reduce virtual disk space at all. Starting with Windows Server 2012, there is a standard ability to trim virtual disks, however, it is still not very easy to use. The fact is that to reduce the size of a VHD, there must be free unallocated space inside it. Otherwise, the Shrink option is not available in the GUI, and PowerShell will throw an error when trying to shrink the disk using Resize-VHD.

As an example, let's take the SRV01 virtual machine with a 40GB virtual disk and cut it down to 30GB. To do this, we first place the VM in a variable and display the properties of its virtual disk:

$vm = Get-VM -Name SRV01
Get-VHD -VMId $vm.VMId

In VHD properties we are interested in such parameters as:

  1. FileSize is the current size of the VHD file. Shows real volume disk space, which takes virtual disk.
  2. Size — the current size of the virtual disk. In the case of a dynamic VHD, it shows the maximum size to which it can grow.
  3. MinimumSize - the minimum size of the virtual disk, or the minimum possible size to which the VHD can be trimmed.

As can be seen from the example, we have a dynamically expandable VHD of 40GB in size, of which this moment A little more than 20GB are occupied. There is free space, but the current and minimum dimensions are almost identical, so cutting down the disk will not work.

For subsequent manipulations, the VM must be turned off, so we check its status and, if necessary, turn it off:

$vm.State $vm | Stop-VM-Force

Then we mount the VHD to the parent system and display the properties of the resulting disk:

Mount-VHD -Path $vm.HardDrives.Path -Passthru | Get-Disk

We take the resulting disk number and display all the partitions on it:

Get-Partition -DiskNumber 3

If the disk suddenly contains several small, incomprehensible partitions, you should not touch them. They contain important information, necessary for the system to boot and restore. We need a partition that has a drive letter. Let's cut it down to 30GB and check what happened:

Resize-Partition -DriveLetter E -Size 30GB
Get-Partition -DriveLetter E

Let's unmount the VHD from the parent system:

Dismount-VHD -Path $vm.HardDrives.Path

And cut it down to the minimum possible value:

Resize-VHD -Path $vm.HardDrives.Path -ToMinimumSize

As you can see, everything worked out, the virtual disk was reduced to 30GB. All that remains is to start the VM.

Good afternoon!. The task is simple, reduce the size of the vmdk virtual disk in ESXI 5 or 6.5. Such a need may arise due to incorrect planning, or simply due to incorrect information on the part of the person setting the task. What we have is a virtual machine with windows server 2008 R2 disk C 50 GB occupied by 20 GB and disk D 100 GB, and 1 is used, you need to reduce it, to 10, say, and so that it remains thick. Unfortunately, this cannot be done using standard means; it’s not like going into the settings in Hyper-V and taking away the required amount. But fortunately, there is a non-dreary way to solve this, namely the VMware vCenter Converter Standalone 5-6.2 utility, I described an example of its work.

Open the utility. Select "VMware Infrastructure virtual machine" and enter the credentials to connect to your host or vCenter server. Then next.

Select a host and virtual machine to reduce disk space.

We enter the destination and access data, in my example this is the same vcenter server.

Select the installation folder and name

We select a host, I recommend vmtools version 9, so that you can edit from the thick client in Windows and the datastore, otherwise you will have to downgrade the version of the virtual equipment.

And now the fun part, reducing the disks. Data copy type select Select volume and below on the hard drive there is the edit button, in the drop-down menu you can select the current size or the minimum, select the minimum, then after completing the conversion expand to the one you need, in fact, click next and the process begins.

Important. This instruction is not an officially supported solution for compressing vmdk disks, but the author of the article has used it more than once in a production environment. ESXi 5.1 was used when writing this article.

Before performing the procedure for reducing the size of a virtual disk on VMWare ESXi, you must:

  • Delete all snapshots virtual machine, otherwise you may damage the virtual disk
  • Create a complete or simply make a copy of the reduced vmdk file using the commands: cp vmname.vmdk backup_vmname.vmdk cp vmname-flat.vmdk backup_vmname-flat.vmdk

The procedure for reducing the size of a virtual disk consists of two stages:

Shrinking a partition inside a guest OS

First of all, you need to reduce the size of the disk partition inside the guest operating system. This can be done from Disk Management Manager (an operation supported since Windows Vista) or using third-party utilities (for example, Acronis Disk Director). If you don't do this, you may end up with an unhealthy file system after shrinking the virtual disk.

Let's say we want to reduce the disk size by 19.5 GB. This means the volume size should be reduced by 19968 MB (19.5 GB x 1024). This is the value that needs to be entered in the Shrink Volume wizard field. After reducing the partition size, there will be a certain amount of unallocated space on our disk (in our example 19.5 GB)

Reducing the size of a VMware virtual machine VMDK file

Let's move on to the second operation - I directly change the size of the VMDK file to file system VMWare VMFS.

  1. Shut down the virtual machine whose virtual disk size you want to reduce
  2. Connect via SSH to the console of the ESXi hypervisor on which the VM is registered
  3. Go to the directory where the VM vmdk file is stored (the path can be found in the properties of the virtual disk in the vSphere graphical client): cd /vmfs/volumes/datastore/VM-name

  4. Let's view the contents configuration file with *.vmdk extension using the cat command: cat VM-name.vmdk

    The disk vmdk size is set in the section #Extent description(after the RW symbols). IN in this case number indicated 83886080 (40 GB *1024*1024*1024 / 512)

  5. We want to reduce the size of the vmdk disk from 40 to 20 GB. This means that in the Extent description section you need to specify the value: 41943040 (20 GB*1024*1024*1024 / 512). Set the new virtual disk size using text editor(vi or nano).
  6. All that remains is to clone or migrate (Storage VMotion) the virtual machine to another storage (datastore). After moving the virtual machine files, its properties will display the new virtual disk size.

    Advice. If there is no vCenter, or there is only one VMFS storage, these operations are not available. In this case, you can clone the vmdk disk with the command:

    Vmkfstools -i vmname.vmdk vmname-new-size.vmdk

    Delete the original disk and clone the vmdk disk again, returning it to its previous name:

    Rm vmname.vmdk rm vmname-flat.vmdk vmkfstools -i vmname-new-size.vmdk vmname.vmdk

  7. All that remains is to launch the guest OS and make sure that the unallocated area has disappeared.

    Advice. If you used the vmkfstools utility in the previous step, you may need to reconnect the virtual disk in the VM settings, because its new size may not be displayed correctly.

So, we looked at one of the options for performing the operation of reducing the size of virtual vmdk disks on VMWare ESXi. If you need to reduce the size of a VMWare thin disk using unused space, we recommend using the instructions.

Note. The following schemes are often used as common alternative methods for compressing virtual vmdk disks:

  • Reducing the size of a virtual disk by converting a virtual machine using VMware vCenter Converter (when converting, the size of the resulting disk is specified to be smaller than the original)
  • Cloning the contents of a virtual disk using third party software(for example, Acronis True Image or Symantec Ghost32). The operation is performed by adding a new, smaller disk to the virtual machine and cloning the contents of the larger disk onto it. After which the large disk is removed.

Reducing the size of a virtual disk through conversion using VMware vCenter Converter Standalone

Another way to reduce the size of virtual disks in a VM on VMWare using the GUI is to use free converter — .

Flaw:

  • The method is not fast
  • no need to open into the vCenter or ESXi console of the host
  • an exact copy of the VM is created
  • the source will not break if there is an error in assigning parameters to the conversion task

Below are step-by-step instructions

Note.The virtual machine that we are going to convert (resize disks, etc.) must be off.

It's turned off.
That is, before conversion, on the source virtual machine you need to run: Shut down or Power Off, but not Suspend.

Specify the ESXi host address.

Select the source VM.

Specify the parameters of the new VM.

Go to disk editing mode (Data to copy -> Edit)

Select the copy mode: Select volumes to copy

Specify the desired disk size in the new VM. As you can see, the guest disk uses 48 GB, and the size of the disk itself is 150. We will reduce it to 60 GB.



We start the conversion process, turn off the original VM, turn on the new one. We check that the disk size has decreased, after which the original VM can be deleted.

Unlike the procedure for expanding the disk of a VMWare virtual machine, which can be performed directly on the fly, the procedure for reducing the size of a vmdk file on the VMWare ESXi hypervisor is a more complex task, and can be performed directly through GUI vSphere Client is basically impossible - specifying a smaller size simply won’t work.

Note

Note that in latest version Microsoft hypervisor - there is a technology for dynamically changing (both expanding and compressing) virtual disks called Hyper-V Online VHDX Resize.

This instruction is not an officially supported solution for compressing vmdk disks, but the author of the article has used it more than once in a production environment. ESXi 5.1 was used when writing this article.

Before performing the procedure for reducing the size of a virtual disk on VMWare ESXi, you must:

    Delete all snapshots of the virtual machine, otherwise you may damage the virtual disk

    Create a complete backup copy virtual machine or simply make a copy of the shrinkable vmdk file using the commands:

cp vmname.vmdk backup_vmname.vmdk cp vmname-flat.vmdk backup_vmname-flat.vmdk

The procedure for reducing the size of a virtual disk consists of two stages:

    Shrinking a partition inside a guest OS

    Reducing the size of a VMware virtual machine VMDK file

Shrinking a partition inside a guest OS

First of all, you need to reduce the size of the disk partition inside the guest operating system. This can be done from Disk Management Manager (Shrink Volume operation, supported since Windows Vista) or using third-party utilities (for example, Acronis Disk Director). If you don't do this, you may end up with an unhealthy file system after shrinking the virtual disk.

Let's say we want to reduce the disk size by 19.5 GB. This means the volume size should be reduced by 19968 MB (19.5 GB x 1024). This is the value that needs to be entered in the Shrink Volume wizard field. After reducing the partition size, there will be a certain amount of unallocated space on our disk (in our example 19.5 GB)

Reducing the size of a VMware virtual machine VMDK file

Let's move on to the second operation - I directly change the size of the VMDK file on the VMWare VMFS file system.

    Shut down the virtual machine whose virtual disk size you want to reduce

    Connect via SSH to the console of the ESXi hypervisor on which the VM is registered

    Go to the directory where the VM vmdk file is stored (the path can be found in the properties of the virtual disk in the vSphere graphical client):

cd /vmfs/volumes/datastore/VM-name

4. View the contents of the configuration file with the *.vmdk extension using the cat command:

cat VM-name.vmdk

The disk vmdk size is specified in the #Extent description section (after the RW symbols). In this case, the number indicated is 83886080 (40 GB * 1024 * 1024 * 1024 / 512)

5. We want to reduce the size of the vmdk disk from 40 to 20 GB. This means that in the Extent description section you need to specify the value: 41943040 (20 GB * 1024 * 1024 * 1024 / 512). Set the new virtual disk size using a text editor (vi or nano).

6. All that remains is to clone or migrate (Storage VMotion) the virtual machine to another storage (datastore). After moving the virtual machine files, its properties will display the new virtual disk size.

If there is no vCenter, or there is only one VMFS storage, these operations are not available. In this case, you can clone the vmdk disk with the command:

vmkfstools -i vmname.vmdk vmname-new-size.vmdk

Delete the original disk and clone the vmdk disk again, returning it to its previous name:

rm vmname-flat.vmdk

vmkfstools -i vmname-new-size.vmdk vmname.vmdk

7. All that remains is to launch the guest OS and make sure that the unallocated area has disappeared.

Advice. If you used the vmkfstools utility in the previous step, you may need to reconnect the virtual disk in the VM settings, because its new size may not be displayed correctly.

So, we looked at one of the options for performing the operation of reducing the size of virtual vmdk disks on VMWare ESXi. If you need to reduce the size of a VMWare thin disk using unused space, we recommend using these instructions.

At work I have several virtual machines running the ESXi 5 hypervisor, when I created the disks I made them thin, at some stage the size of the occupied disk grew, even though I deleted data in the virtual server, the disk remains the same size, how can I shrink the disk?

VMWare thin disks can significantly save space on the VMFS storage file system when creating a virtual machine. When created, such vmdk disks take up virtually no space on the file system and gradually grow to their maximum size as information is accumulated. However, the main problem with using thin disks is that they grow over time, even if you free up space inside the guest operating system.

Note. When deleting data in Windows OS (and this also applies to Linux), the system does not fill the occupied space with zeros, but simply marks the areas as deleted in the partition metadata (they can be used for recording). It turns out that the ESXi server cannot independently determine which of the blocks given to it are actually free

There is no standard means of compressing thin disks in ESXi, and the virtualization system itself does not know anything about the features of data storage inside the guest OS.

In this article, we will describe a simple way to compress a thin vmdk disk in ESXi.

  1. At the first stage, you need to help the hypervisor by filling out all free place on disks inside the guest OS with zeros. You can reset free blocks in Windows using Mark Rusinovich's SDelete utility (at the time of writing this article, the latest available version SDelete v1.61 (if you are using an earlier version of SDelete, please note that it may use slightly different parameters). You can download SDelete here: http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx.

    Unzip the archive to disk, open command line(with administrator rights, of course) and run the following command

    Delete –z c:

    In this case, c: is a virtual thin vmdk disk that we plan to compress.

    On Linux this operation can be done using dd

    Dd if=/dev/zero bs=64k of=zero.temp ; rm zero.temp

  2. Turn off the guest OS (Shutdown).

    Note. You can also compress a disk by migrating a virtual machine to another storage (Storage VMotion). However, we note that this method is not universal and only works if certain conditions are met.

  3. We connect to the ESXi server via SSH as root (don’t forget to enable the SSH service and allow incoming traffic on the ESXi firewall). Go to the directory with the virtual machine files:
    cd vmfs/volumes/storage1/vm1

    where storage1 is the name of the storage, and vm1 is the directory in which files with vmdk disks of the virtual machine are stored

  4. Let's display the “full” size of the virtual disk.
    ls -lh *.vmdk

    The “real” size of the virtual disk that it occupies on the file system can be found like this:

    Du –h *.vmdk

    As you can see, the thin disk takes up 33 GB of storage (its maximum size is 40 GB). Moreover, only 15 GB are occupied inside the Windows guest OS.

  5. We compress the vmdk disk using vmkfstools. The thin disk compression command would look like this:
    vmkfstools --punchzero /vmfs/volumes/storage1/vm1/vm1.vmdk

    The compression procedure in our example took about 5 minutes (the speed depends on the disk size and the speed and load of the storage and SAN network). After the compression procedure is completed, we will check the current size of the vmdk file.

    As you can see, the vmdk disk has shrunk to 15 GB!

Note. If there are several disks, this procedure must be performed for each one.