14

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.

21

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

  • This is the correct answer for VMWare Workstation. Thanks man, helped me a lot! – Clonkex Nov 29 '17 at 0:19
  • Correct. Works fine on VMWare Workstation 14, Centos 7 Guest – odiszapc Feb 20 '18 at 13:32
  • 1
    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... – paj Mar 27 '18 at 8:46
  • 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 – Alex Che May 3 '19 at 14:15
  • 1
    +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 – Étienne Sep 11 '19 at 12:48
14

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

Virtual Machine - Settings - General - Clean Up Virtual Machine

  • Wow. Finally I found this answer. But... why compacting the virtual disk doesn't actually reclaim the free space? – Wizard79 Feb 12 '14 at 16:19
  • 7
    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. – DH4 Jun 12 '14 at 17:06
  • This does not apply to ESXi. For that, see my answer below. – Collin Chaffin Sep 24 '19 at 15:53
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.

  • 3
    The vmware toolbox UI is no more, but you can still use: "vmware-toolbox-cmd disk shrink /" which should do the job. – DH4 Jun 12 '14 at 16:52
3

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 )

1

VMWare harddisk resizing:

Expanding the dynamic virtual harddisk in VMWare is easy and does no longer require the command line tool vmware-vdiskmanager.exe located by default in C:\Program Files\VMware\VMware Workstation\ .

For resizing use the following command (Open a command prompt by pressing WIN+R and enter cmd): "C:\Program Files\VMware\VMware Workstation\vmware-vdiskmanager.exe" -x NEWSIZE "diskname.vmdk"

Procedure:

The following steps are:

  • Download here
  • Install
  • Open
  • Choose the partition, Click Resize/Move, Drag the handle to its full width
  • Click Apply
  • Agree to reboot
  • During startup, do not press any key whatsoever
  • Let the boot-up module perform its task
  • Check that your Windows system partition size increased.

For a visual rundown of the steps and additional info, see here.

0

These steps worked perfectly for me:

1) Zero-fill unused space

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

2) Shrink disk with vmware toolbox

sudo vmware-toolbox-cmd disk shrink /

Tested on Ubuntu 16.04, VMware Workstation 12.1.0 Player, Windows 7 Host

0

Shrinking disks is possible on ESXi.

  1. Find the large file and delete them.

    find / -type f -size +50M

  2. Defragment (no need to un/remount anything). Ignore any errors. Some files like symlinks and device files can't be defragmented.

    sudo e4defrag /

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

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

  4. Run the shrink operation: If 4 fails, proceed to 5.

    sudo vmware-toolbox-cmd disk shrinkonly

  5. Reboot the linux system with try option and edit the partition table with gparted. Shrink the root file system and delete & move swap partition near to root partition.

  6. SSH to ESXi, and issue this command:

    vmkfstools -K /vmfs/volumes/volumename/vmname/vmname.vmdk

  7. We need to edit the *.vmdk file. After the letters RW, defines the size of the VMware virtual disk (VMDK)

    *40 GB = 40 * 1024 * 1024 * 1024 / 512 = 83886080*

  8. Migrate it to another datastore to reflect the new size.

  • (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. – Scott Apr 25 '19 at 22:17
  • 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 Sep 24 '19 at 3:10
  • 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 Sep 24 '19 at 12:25
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.