How to reduce the size of VMware disk

66,544

Solution 1

I've found the solution – you have to compact your hard drive:

Virtual Machine - Settings - General - Clean Up Virtual Machine

Solution 2

There are three steps:

  1. Defragment (no need to un/remount anything)

    sudo e4defrag /

    Ignore any errors. Some files like symlinks and device files can't be defragmented.

  2. Zero-fill all unused space so VMware knows it's indeed unused:

    dd if=/dev/zero of=wipefile bs=1M; sync; rm wipefile

  3. Run the shrink operation:

    sudo vmware-toolbox-cmd disk shrinkonly

Solution 3

Open VMware Toolbox in the guest (as root) and shrink the disk. (Documentation)


If you don't have VMware Tools, you can shrink the disk manually in two steps. First, clear the free space on the virtual disk using a tool like zerofree. Zerofree is available in Ubuntu, but since it requires the guest OS filesystem to be mounted read-only you may want to run it off a live CD. Parted Magic reportedly includes zerofree.

After zeroing the free space, open Virtual Machine Settings and compact the virtual disk.

Solution 4

Thinning (shrinking) VMDK disks on ESXi / vSphere Linux Guests

Note: "Shrinking" is often used interchangeably to refer to the process of reducing the size of a thin-type disk file. VMware uses the term "shrink" to refer to reducing the underlying size of the disk available to the guest, and "thinning" as the process most tend to use of recapturing unused space without altering the underlying available size.

Since ESXi does not support vmtools-based shrinking on Linux guests, the following steps must be used. See reference link below for more information.

  1. (if possible) Stop all disk write-intensive applications and services as the following steps will momentarily fill the target volume

  2. Defragment volume(s) on target VMDK and ignore any errors as symlinks/device files/etc. cannot be defragmented.

    # USE sudo IF NECESSARY IN YOUR CONFIG!
    e4defrag /
    # or e4defrag /mnt/MOUNTVOLUME
  1. Zero-fill all unused space:
    dd if=/dev/zero of=/mnt/MOUNTVOLUME/zeroes bs=1M; sync; rm /mnt/MOUNTVOLUME/zeroes
  1. Shut down & power off guest

  2. SSH to the ESXi host and issue these commands:

    # Check current vmdk allocation
    du -h /vmfs/volumes/volumename/vmname/vmname.vmdk

    # Shrink it
    vmkfstools -K /vmfs/volumes/volumename/vmname/vmname.vmdk

    # Check again to verify shrinkage
    du -h /vmfs/volumes/volumename/vmname/vmname.vmdk
  1. Power on the guest

Source

https://kb.vmware.com/s/article/1002019

Deletion of files in most file systems will not completely remove them; merely file tables will be altered. Use of freeware secure file deletion utilities are useful, such as Eraser or SDelete to zero out the space to 'zero' the free space on the volume, effectively clearing the free space of data. It is then, that the disk can be properly thinned. You can then use the vmkfstools -K command (ESXi/ESX 4.1 and later) to complete the block reclaim or use Storage vMotion to migrate the virtual machine to a datastore with different VMFS block size.

Solution 5

Use

sudo vmware-toolbox-cmd disk shrinkonly

from the command prompt in your Ubuntu guest.

(Tested on Ubuntu 14.04 LTS guest + Windows 10 Host + VMWare Workstation 12.1.1 )

Share:
66,544

Related videos on Youtube

Marcin Rybacki
Author by

Marcin Rybacki

Updated on September 17, 2022

Comments

  • Marcin Rybacki
    Marcin Rybacki almost 2 years

    My Ubuntu image VMware disk has grown to 5GB physical file size. It's a dynamically growing disk with the maximum capacity of 8GB. I cleaned some stuff inside Linux distro and now inside it occupies 3GB. Obviously the physical file size didn't shrink back automatically. Is there a way (if possible free of charge) to shrink this disk back to ~3GB?

    I need this to fit the image on the DVD.

  • Wizard79
    Wizard79 over 10 years
    Wow. Finally I found this answer. But... why compacting the virtual disk doesn't actually reclaim the free space?
  • DH4
    DH4 about 10 years
    The vmware toolbox UI is no more, but you can still use: "vmware-toolbox-cmd disk shrink /" which should do the job.
  • DH4
    DH4 about 10 years
    You need to do this from within the guest Ubuntu OS (assuming you have VMware Tools installed): "vmware-toolbox-cmd disk shrink /" which should do the job.
  • Clonkex
    Clonkex over 6 years
    This is the correct answer for VMWare Workstation. Thanks man, helped me a lot!
  • odiszapc
    odiszapc over 6 years
    Correct. Works fine on VMWare Workstation 14, Centos 7 Guest
  • paj
    paj over 6 years
    be careful if you run this with any mounted external volumes such as S3 - the zero fill command will try and create a 256TB wipefile in your S3 bucket...
  • Scott - Слава Україні
    Scott - Слава Україні about 5 years
    (1) The question is not about how to get the file system usage down; it says that the OP has already done that.  So your find command is not really relevant.  (2) What does vmkfstools -K do?  (3) Manually edit a file?  Really?  (4) I don’t understand your instructions for what change the user needs to make in the file.  (5) What are you saying about migrating to another datastore? (0) Most of what you say has already been covered. … … … … … … … … … … … … … … … … … … … … Please‌​ do not respond in comments; edit your answer to make it clearer and more complete.
  • Alex Che
    Alex Che about 5 years
    Worked for me for Windows 10 host and Ubuntu 16 guest. At first I thought that step 3 should be run on host, but I was wrong. After all the steps I also run the filesystem check as described here: askubuntu.com/a/953750/590877
  • Étienne
    Étienne almost 5 years
    +1, however I think using zerofree for step 2 would be much faster and much cleaner: manpages.ubuntu.com/manpages/xenial/man8/zerofree.8.html
  • Tom Hale
    Tom Hale almost 5 years
    man zerofree says it requires the FS to be unmounted or mounted ro, and only works on ext2, ext3 and ext4.
  • Collin Chaffin
    Collin Chaffin almost 5 years
    Well I'm a VMware guy (I would think anyone attempting any of this also should be) so not only is this answer the ONLY one that even applies to ESXi (since ESXi does NOT support shrink linux guests via vmtools) but ultimately made perfect sense to me. With that said, step 7 is not only unneeded, it's just incorrect. Since shrinking does not change the actual VOLUME size, you simply stop after step 6. Step 8 (storage migrating) is only used when you want to shrink a disk WITHOUT running vmkfstools which takes care of it forcibly which is why that solution also requires step 5a-SHUT DOWN guest.
  • Collin Chaffin
    Collin Chaffin almost 5 years
    See my answer above. The vmtools-based cmd listed in step 5 is also just incorrect as it's never been supported (or even works) on ANY ESXi host-based scenario.
  • Collin Chaffin
    Collin Chaffin almost 5 years
    This does not apply to ESXi. For that, see my answer below.
  • Silidrone
    Silidrone about 4 years
    Somehow I did all this, plus I did the defragment and compact from VMWare Workstation but I still have like 25 GB laying around. Running "ncdu /" shows 21 GB in total whilst my VM takes up 46 GB, its rather weird. I'm using Ubuntu 19.10
  • trogne
    trogne over 3 years
    The dd command gives "no space left on device". What to do in this case please ?
  • trogne
    trogne over 3 years
    "no space" even if I have more than twice the space available on the device.
  • Tom Hale
    Tom Hale over 3 years
    dd is expected to run out of space and print that message. The disk eventually gets full of NUL bytes, which are then immediately removed.
  • jamesdlin
    jamesdlin almost 3 years
    Step 1 is redundant and a waste of time since vmware-toolbox-cmd disk shrink will do that for you.
  • jamesdlin
    jamesdlin almost 3 years
    Steps 2 and 3 can be combined by running vmware-toolbox-cmd disk shrink (instead of shrinkonly), which will wipe and compact/shrink.
  • jamesdlin
    jamesdlin almost 3 years
    You should prefer using shrink instead of shrinkonly to wipe the disk. Without the wiping step, you will not reclaim unused space from remnants of deleted files.