31

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,314

14 Answers14

44

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
Tom Hale
  • 2,708
21

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

Virtual Machine - Settings - General - Clean Up Virtual Machine

slhck
  • 235,242
6

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: VMware knowledgebase

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
  • 294
  • 2
  • 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
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
  • 209
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.

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

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:

    sudo vmware-toolbox-cmd disk shrinkonly
    
    1. If this fails, 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.
  5. SSH to ESXi, and issue this command:

    vmkfstools -K /vmfs/volumes/volumename/vmname/vmname.vmdk
    
  6. 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
    
  7. Migrate it to another datastore to reflect the new size.

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,314
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 on a Windows 10 guest with NTFS, not checked with a Linux guest.

casiosmu
  • 191
0

Just use the Live CD from this Open Source Project, on Linux Hosts / Guests ( this Live CD has the zerofree utility & gparted application, built in, along with many other goodies; also read the instructions on the site ) :

https://sourceforge.net/projects/live-cd-with-zerofree-utility/

This will also help in reducing the size of an exported virtual machine appliance as well ( i.e. .ova file ).

As others mentioned, after zerofree is run, use the shrink option, to reduce the virtual disk file size.

P.S. A 32-Bit Version ( i686 architecture ) of this Live CD is available at: https://sourceforge.net/projects/live-cd-with-zerofree-32-bit/

For very OLD Architectures, Please try this Live CD : https://sourceforge.net/projects/live-cd-with-zerofree-x86/

0

Inside your Ubuntu VM run the fstrim command to zero the free space on all mounted filesystems:

sudo fstrim --all --verbose

After that is done, depending on your VMWare environment, you'll need to:

Amos
  • 1
0

For ESXi hosts with a Linux guest VM

Tested on ESXi 8.0 U2 with a Debian 12 VM

  1. Backup the VM as appropriate

  2. Shut down the VM!

  3. Download the SystemRescue live ISO

  4. Mount the ISO to the VM

  5. Boot with Default Options

  6. startx

  7. Terminal:

  8. mountall

  9. Note down each partition's /mnt address for partitions which show OK

  10. For each mount shown:

    1. sudo e4defrag /mnt/{Partition}
    • Replace {Partition} with the partition name, E.G. docker--vg-root

    • Do not do anything to /mnt/sda1

    • E.G.

      sudo e4defrag /mnt/docker--vg-root && \
      sudo e4defrag /mnt/docker--vg-var && \
      sudo e4defrag /mnt/docker--vg-tmp && \
      sudo e4defrag /mnt/docker--vg-home
      

      • This will defragment files on the partitions
      Failures can be ignored
      • A disk with about 100GB of junk data + 20GB of true data, with a max size of 500GB - should take about 5 minutes to complete

  11. shutdown -r now

  12. Boot with Default Options

  13. startx

  14. Terminal:

  15. fdisk -l

  16. For each disk (partition) shown:

    1. zerofree -v /dev/mapper/{Partition}
    • Replace {Partition} with the partition name, E.G. docker--vg-root
    • Do not do anything to:
      • The Devices shown at the top, E.G. /dev/sda1
      • /dev/loop0
      • /dev/mapper/*-swap_1
    • E.G.
      zerofree -v /dev/mapper/docker--vg-root && \
      zerofree -v /dev/mapper/docker--vg-var && \
      zerofree -v /dev/mapper/docker--vg-tmp && \
      zerofree -v /dev/mapper/docker--vg-home
      

    • This will zero all deleted blocks
    • A disk with about 100GB of junk data + 20GB of true data, with a max size of 500GB - should take about 5-10 minutes to complete

  17. shutdown now

  18. Dismount the ISO

  19. SSH to the ESXi host:

    cd "/vmfs/volumes/Main RAID Array"    # E.G.
    ls
    cd Docker-Debian12                    # E.G.
    ls
    vmkfstools -K Docker-Debian12.vmdk    # E.G.
    exit
    

    A disk with about 100GB of junk data + 20GB of true data, with a max size of 500GB - should take about 10 minutes to complete

  20. vCenter > VMs > Docker-Debian12 E.G.

  21. Observe that the disk usage has fallen

  22. Spin up the VM

0

One more Solution would be: (I had a 2GB WIN 3.11 old System, which uses only 50 MB, which should be fit to an 512MB IDE Dongle)

  1. Reduce the (f.e. 2 GB-)PARTION by booting with the ISO file f.e. EASEUS_Partition_Master_image.iso in Guest OS
  2. open Runtime's DiskExplorer for NTFS(free Demo Verions is ok) Download here
  3. load the 2 GB image f.e. Casy.vmdk in DiskExplorer (Demo is fine.)
  4. Select from Sector 0 to End-Sector [512MB * 512 Byte/Sector = ] 1048576
  5. Save that selected BYTES to an Image-File (f.e. 512MBCASY.img) by clicking EDIT > Copy to File
  6. Open the 512MBCASY.img in WinIMAGE.exe [version 10+] Download here
  7. Save the IMG File as VHDK File Format by clicking Disk > Convert Virtual Harddisk Image and select *.vmdk Format
  8. Make a new VM in VMWare and define a HDD with that VHDK File.
  9. If you want to write the *.img file to a DONGLE (IDE f.e. 512 MB or SATA Drive) do with WinImage.exe following: Image -> Export to an IMAGE with MBR and save the image as `*.ima'
  10. Donwload RUFUS here and write hte *.ima file to the selected Drive