27

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.

jamesdlin
  • 3,012
  • 22
  • 32
Marcin Rybacki
  • 559
  • 1
  • 4
  • 9

10 Answers10

38

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; /bin/rm wipefile

  3. Run the shrink operation:

    sudo vmware-toolbox-cmd disk shrinkonly

Anubioz
  • 121
  • 5
Tom Hale
  • 2,274
  • 2
  • 24
  • 35
  • This is the correct answer for VMWare Workstation. Thanks man, helped me a lot! – Clonkex Nov 29 '17 at 00: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 08: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: https://askubuntu.com/a/953750/590877 – Alex Che May 03 '19 at 14:15
  • 2
    +1, however I think using zerofree for step 2 would be much faster and much cleaner: http://manpages.ubuntu.com/manpages/xenial/man8/zerofree.8.html – Étienne Sep 11 '19 at 12:48
  • 1
    [`man zerofree`](https://manned.org/zerofree) says it requires the FS to be unmounted or mounted `ro`, and only works on ext2, ext3 and ext4. – Tom Hale Sep 12 '19 at 06:14
  • 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 – Silidrone Apr 20 '20 at 23:03
  • The dd command gives "no space left on device". What to do in this case please ? – trogne Oct 31 '20 at 23:20
  • "no space" even if I have more than twice the space available on the device. – trogne Oct 31 '20 at 23:48
  • 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. – Tom Hale Nov 01 '20 at 04:03
  • 1
    Steps 2 and 3 can be combined by running `vmware-toolbox-cmd disk shrink` (instead of `shrinkonly`), which will wipe *and* compact/shrink. – jamesdlin Aug 10 '21 at 12:00
  • Just using `vmware-toolbox-cmd disk shrink /` is enough. No other steps needed. You need to add a mountpoint at the end, in my example `/` for root partition. – Phlogi Mar 05 '23 at 11:54
18

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

Virtual Machine - Settings - General - Clean Up Virtual Machine

slhck
  • 223,558
  • 70
  • 607
  • 592
Marcin Rybacki
  • 559
  • 1
  • 4
  • 9
  • 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
  • 11
    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
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.

Collin Chaffin
  • 274
  • 1
  • 8
4

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.

nandhp
  • 201
  • 1
  • 5
  • 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 )

YasharF
  • 159
  • 6
  • 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. – jamesdlin Aug 10 '21 at 12:06
2

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.

Lorenz Lo Sauer
  • 938
  • 2
  • 12
  • 18
1

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

dontbyteme
  • 111
  • 3
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 03: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

Why do sparse disks normally grow monotonically?

If your VM uses sparse disks, your .vmdk files conceptually use only as much disk space on the host as the VM uses. However, the VM and the guest OS have different notions of what's "in use".

When an OS deletes a file, traditionally it does not do anything to the contents of the file. The filesystem instead marks whatever space the file used as available. This is what allows undeletion to work if the content hasn't been overwritten yet.

The OS knows that the space formerly used by the deleted file is no longer in being actively used, but the VM doesn't; there's still data there. The VM therefore (usually) can't automatically reclaim the space from deleted files1.

1 If an OS knows that it's using an SSD, when it deletes a file, it can send TRIM commands to the disk to tell the disk that that space is no longer used. I don't know whether VMware products take advantage of that yet, however.

Disk shrinking (general and traditional approach)

There are two phases to shrinking a virtual disk:

  1. Wiping.
  2. Compacting (which confusingly sometimes is also referred to as "shrinking").

Wiping fills the guest file system with a zero-filled file to overwrite leftover contents from previously deleted files. This can generate an out-of-space message in the guest, but that's normal and expected. This must be done by a process in the guest. Typically this is done by VMware Tools, but there are other programs that can do this.

Compacting removes the unused (wiped/zeroed) parts of the .vmdk file on the host. Doing this properly depends on the wiping step because that's how it identifies parts that don't need to be preserved. This step requires free disk space on the host because, to avoid data loss on failure, disk compaction will create new disk files. You will need at least as much free space on the host as the size of the largest .vmdk file. (It therefore is advantageous to use split disks, which need much less free space on the host than monolithic disks when shrinking, when defragmenting, or when consolidating snapshots.)

Note that you can't shrink .vmdk files if there are snapshots; the wiping stage will write only to the current delta disk, so there's no way for the compacting step to identify parts of base disks that should be considered unused.

Windows guests with VMware Workstation Pro

Windows guests with VMware Workstation Pro have another, superior option: Clean Up Disks. Power off the VM and choose: VM > Manage > Clean Up Disks. This is much, much faster than using the traditional shrink approach because it directly examines the guest OS filesystem to identify used and unused parts of the disk and therefore does not need the wiping step. Additionally it works if the VM has snapshots. I don't remember offhand if it has the same requirements for free space on the host, but given how much faster it is, any free space requirements it has are likely to be much, much lower.

jamesdlin
  • 3,012
  • 22
  • 32
0

In my Workstation 16 Player the mentioned VMware toolbox is not available, but in the settings the disk can be compacted:

  1. shut down virtual machine
  2. Edit machine settings->Tab Hardware->Hard disk
  3. There are buttons to Defragment and also Compact the disk.

Worked for me with Win10 guest with ntfs, not checked with linux guest.

casiosmu
  • 181
  • 8