VMware Communities
Entegy
Hot Shot
Hot Shot
Jump to solution

How do I clean up an OS X VM?

I have 10.8 in a virtual machine and right now, its VMDK is taking triple the space actually being used by the contents of the virtual drive. Normally, I would have expected the "Clean up recommended" message to appear but it seems to be that the clean up tool doesn't support OS X virtual machines.

Is there a safe way to reduce the size being taken by the VMDK?

Tags (2)
1 Solution

Accepted Solutions
WoodyZ
Immortal
Immortal
Jump to solution

First make sure you have a proper* backup of the Virtual Machine.

Delete any Snapshots.

In a Terminal from within the OS X Guest OS use the following command to prep for shrinking:

cat /dev/zero > wipefile; rm wipefile

After the prompt returns shutdown, not suspend, the Virtual Machine.

From a Terminal on the OS X Host use the following command to shrink the virtual hard disk.

Syntax: vmware-vdiskmanager -k <Fully Qualified Pathname to the Virtual Hard Disk>

Example: /Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -k /Users/WKZ/Documents/Virtual\ Machines.localized/Mac\ OS\ X\ 10.8.vmwarevm/Virtual\ Disk.vmdk

==========

* It is a known fact that Time Machine is not 100% reliable backing up/restoring Virtual Machines under all circumstances/conditions.  Also backing up Virtual Machines via Time Machine is disk/time intensive and wastes a tremendous amount of space for something that may be corrupt and worthless come time to restore it.  At a minimum I would exclude Virtual Machines from Time Machine and with the Virtual Machines shutdown, not suspended, and VMware Fusion closed then manually copy the Virtual Machines Package(s) to an alternate location, preferably on to a different physical hard disk.  Then keep the User Data that is stored within the Virtual Machine backed up off of the Virtual Machine on a regular basis so as to always have a current User Data Backup.  If you have to restore a properly backed up Virtual Machine that is not as current at least you'll have a working Virtual Machine and current User Data to go forward with when you find out your Time Machine Backup of the Virtual Machine fails.

Also have a look at: Best Practices for virtual machine backup (programs and data) in VMware Fusion (1013628)

View solution in original post

Reply
0 Kudos
13 Replies
razvanvilt
Contributor
Contributor
Jump to solution

This might initially increase the size of your VMDK, but you can make it smaller eventually.

  1. In the Virtual Machine: "sudo pmset hibernatemode 0; sudo rm -f /var/vm/sleepimage"
  2. Defragment the "Macintosh HD" partition inside your VM. There are tools for that such as iDefrag. If you don't want an extra license for your VM, convert your virtual disk to monolithicFlat (Preallocate enabled and without the split), temporarily rename your "-flat.vmdk" to "-flat.dmg", mount it in the host system and Defragment it from the host system.
  3. Eject the DMG file and rename it back to vmdk. In VMWare Fusion, disable preallocation of the disk space and add back the "split into 2GB files"

You can also for Fusion 6, which might be of more help with the AHCI controller, but I haven't checked since I always preallocate my virtual machines (for performance reasons).

Reply
0 Kudos
Entegy
Hot Shot
Hot Shot
Jump to solution

I can also for Fusion 6 what? It feels like there's a word missing there.

Reply
0 Kudos
razvanvilt
Contributor
Contributor
Jump to solution

My bad, I really should read the post again before submitting. "You can also wait for Fusion 6".

Logic for that:

  • Fusion presents the virtual disk drives to Mac OS X as AHCI disks
  • Mac OS X uses the TRIM command to clear the unused sectors
  • (Not verified, but expected) VMWare Fusion actually uses the sectors listed in the OS to reduce the size of the VMDK files.

The best thing you could do here is to ask the VMWare Fusion Beta community if AHCI trim actually reduces the VMDK file sizes.

One more note: from what I know, Mac OS X only enables TRIM for SSDs that have Apple in their name. So, in the VMDK you might want to add:

@disk:diskModel=APPLE SSD TS256B,diskSerialNumber=000000000000,diskSize=500107862016,diskVendor=APPLE

Reply
0 Kudos
Entegy
Hot Shot
Hot Shot
Jump to solution

I loaded the VM into the Tech Preview and upgraded the vm hardware. Nothing's changed. Dunno how long I need to leave it, although I'm seriously doubting this will automatically reduce VMDK size as that doesn't seem to be a feature of any other guest.

Reply
0 Kudos
razvanvilt
Contributor
Contributor
Jump to solution

I've checked, disklib in Fusion doesn't react to @disk or @partition entries. It only works for bootcamp disks (createType="partitionedDevice"). I'm attempting right now to create a new VMDK that does that. Interesting hack if it works.

Reply
0 Kudos
WoodyZ
Immortal
Immortal
Jump to solution

First make sure you have a proper* backup of the Virtual Machine.

Delete any Snapshots.

In a Terminal from within the OS X Guest OS use the following command to prep for shrinking:

cat /dev/zero > wipefile; rm wipefile

After the prompt returns shutdown, not suspend, the Virtual Machine.

From a Terminal on the OS X Host use the following command to shrink the virtual hard disk.

Syntax: vmware-vdiskmanager -k <Fully Qualified Pathname to the Virtual Hard Disk>

Example: /Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -k /Users/WKZ/Documents/Virtual\ Machines.localized/Mac\ OS\ X\ 10.8.vmwarevm/Virtual\ Disk.vmdk

==========

* It is a known fact that Time Machine is not 100% reliable backing up/restoring Virtual Machines under all circumstances/conditions.  Also backing up Virtual Machines via Time Machine is disk/time intensive and wastes a tremendous amount of space for something that may be corrupt and worthless come time to restore it.  At a minimum I would exclude Virtual Machines from Time Machine and with the Virtual Machines shutdown, not suspended, and VMware Fusion closed then manually copy the Virtual Machines Package(s) to an alternate location, preferably on to a different physical hard disk.  Then keep the User Data that is stored within the Virtual Machine backed up off of the Virtual Machine on a regular basis so as to always have a current User Data Backup.  If you have to restore a properly backed up Virtual Machine that is not as current at least you'll have a working Virtual Machine and current User Data to go forward with when you find out your Time Machine Backup of the Virtual Machine fails.

Also have a look at: Best Practices for virtual machine backup (programs and data) in VMware Fusion (1013628)

Reply
0 Kudos
Entegy
Hot Shot
Hot Shot
Jump to solution

Figured this out last night, forgot to post back (it was 1AM by that time)

Nonetheless, thanks WoodyZ!

Reply
0 Kudos
mudaltsov
Expert
Expert
Jump to solution

A slightly easier method - the shrink functionality is built into the VMware Tools command-line utility:

sudo /Library/Application\ Support/VMware\ Tools/vmware-tools-cli disk shrink /

This will first do a wipe of empty space one the file system (same as creating a zero-filled file) and show a progress in the guest Terminal. When that's done, the VMDK shrink will be invoked without shutting down the VM, and show up as a progress bar over the VM window.

The / is a path to the file system to wipe empty space on. If you have multiple partitions, you can wipe them individually and do a final shrink at the end:

vmware-tools-cli disk list to see the available locations for wipe

vmware-tools-cli disk wipe <location> to wipe each location (repeat multiple times), without the shrink operation

vmware-tools-cli disk shrinkonly to do the final shrink operation.

This is also explained by vmware-tools-cli help disk

WoodyZ
Immortal
Immortal
Jump to solution

I posted the old school method because I have a OS X 10.8 Virtual Machine that vmware-tools-cli disk shrink / refuses to function in and there are no Snapshots, so that not the issue and VMware Tools are up to date, even uninstalled/reinstalled.  None of the disk commands will work so I assumed that it doesn't work in OS X.  So I just now created a new OS X 10.8 Virtual Machine, installed VMware Tools and low and behold it worked.  I'll have to examine the two Virtual Machine to see why one is working and the other is not! Smiley Wink

Reply
0 Kudos
MacsRule
Hot Shot
Hot Shot
Jump to solution

FWIW, I just stumbled on sudo /Library/Application\ Support/VMware\ Tools/vmware-tools-cli disk shrink / in a MacInTouch posting as an alternative to vmware-tools-cli disk shrinkonly for shrinking Mac OS X VM's and tried it with remarkable results on Mac OS X 10.5 Server, 10.6 Server, 10.7 and 10.8 VM's. One of them even shrank close to two thirds, at least when used in conjunction with sudo pmset hibernatemode 0; sudo rm -f /var/vm/sleepimage at least once. And that's without needing to become root. Subsequent searching in the VMware Community for vmware-tools-cli disk shrinkonly brought me here.

I also tried vmware-tools-cli disk shrinkonly after plain shrink and found virtually no additional improvement. Altogether, I got a good 60 GB back! I discovered this just after I upgraded to Fusion 6.0.0 so I don't know if it'll work as well in Fusion 5. I had tried vmware-tools-cli disk shrinkonly alone in Fusion 5.0.3 and obtained almost no shrinkage so the effectiveness of plain shrink is really good news.Smiley Happy

Reply
0 Kudos
Entegy
Hot Shot
Hot Shot
Jump to solution

If it helps at all, the exact method I did was run Erase Free space in the VM via Disk Utility, then shut down the VM and did this at Terminal:

cd /Applications/VMware\ Fusion.app/Contents/Library/

./vmware-vdiskmanager -k /Users/Entegy/Documents/Virtual\ Machines.localized/OS\ X.vmwarevm/OS\ X.vmdk

Reply
0 Kudos
MacsRule
Hot Shot
Hot Shot
Jump to solution

I just compared VM sizes before and after running just the sleepimage and disk shrink commands in 10.7.4 and 10.8.5 VM's using a bootable backup for "before."

10.7 went from 24,691,617,585 bytes to 8,982,231,754 and 10.8 went from 41,680,058,903 bytes to 13,146,938,120 bytes! What kind of size reductions did you see using your method?

Reply
0 Kudos
vsilves
Contributor
Contributor
Jump to solution

In VMware Fusion 6.0.4 with Mac OS X 10.9.4 as the host, Mac OS X 10.10 as the guest, and VMware Tools installed in the guest OS, of course, you can shrink the virtual disk at once with the following command issued in the command line of the guest OS:

sudo  /Library/Application\ Support/VMware\ Tools/vmware-tools-cli disk shrink  /

The command is no different than the suggestions provided so far in this and other discussion threads, it will zero out the virtual disk, but then the difference is that it will automagically launch the virtual disk shrinking process from the virtual machine without need for user intervention. Once complete you can shut down the virtual machine or keep it powered.

For reference, the equivalent command for Linux as a guest OS in VMware Fusion is as follows:


sudo vmware-toolbox-cmd disk shrink  /

If you are interested on variants just issue the verb help disk, as in :


sudo vmware-toolbox-cmd help disk                                                                          (in Linux)

sudo /Library/Application\ Support/VMware\ Tools/vmware-tools-cli help disk      (in Mac OS X)

You should get an output similar to this:


disk: perform disk shrink operations

Usage: /Library/Application Support/VMware Tools/vmware-tools-cli disk <subcommand> [args]

Subcommands:

   list: list available locations

   shrink <location>: wipes and shrinks a file system at the given location

   shrinkonly: shrinks all disks

   wipe <location>: wipes a file system at the given location

Good luck everyone with your shrunk virtual machines.

Vicente

Reply
0 Kudos