Virtual Machine Maintenance

Virtual Machine Overview

Working on Dynamics CRM projects usually requires a virtual environment because of the dependencies on IIS, Active Directory, SQL Server and sometimes SharePoint. Setting up environments that accommodate multiple team members quickly becomes costly in terms of overhead and a lot of hours to maintain over time. We have chosen to use local virtual machines (VM) as it enables us to work both independently and while offline. Local VMs also offer a great location for testing ideas with little or no impact to the rest of the team since in most cases, a snapshot can be created and discarded with relative ease. We also maintain a library of different environments based on their current and future engagements: a base windows VM for general use and testing, a base custom development environment, a CRM 2011 VM, a CRM 2013 VM, etc.

These days, development teams have several options for virtual environments, both paid and free. A few that the team at Celedon has used over the years:

VMWare – offering both desktop and Enterprise solutions, some free and some paid, across Windows, Linux and OSX

No matter the virtualization solution, a continual challenge we faced is the size of the virtual hard drive over time. We configure our VMs with dynamically growing virtual drives, so the VM starts out relatively small but will increase quickly as the VM is customized. A full development environment for CRM 2013 that includes CRM, Windows Server 2012, SQL Server 2012, Visual Studio 2013 and additional standard CRM tools usually expands to between 35 and 40GB. Hard drive space is not as much of an issue these days as a few years ago, but if you maintain several VMs, this adds up pretty fast! I usually keep 3 available on my laptop at any given time so I am easily using up 120 GB at any time. As you use the VMs, the dynamically expanding drives will use more space. For example, I recently needed to import a CRM backup database that was about 4 GB. So I copied it to my back up drive, which immediately added 4GB that to the size of my hard drive. Once restored, it added another 4GB immediately. Another common cause of virtual disk growth is the Windows Update service. I recently ran Windows Update and it added 800MB when downloading from the Windows Update server. All of that space balloons the virtual disk and chews up space on our host machines.

The question is, how do I get that space back and managing situations from causing my virtual drive to grow without bounds?

Some Helpful Housecleaning Steps

We have not found the über one step to avoid this issue. It’s just part of working with VMs, especially with solutions like Dynamics CRM that can require periodically moving your databases around. But we have found some basic steps that regain space on the baseline VM and keep things relatively clean over the long term. Below we outline some of our general housekeeping steps for a VM. These steps should apply to any of the virtualization solutions you choose for your team or engagement. For the purposes of this post, we will be discussing Windows Hyper-V on Windows 8.1 Pro running Windows Server 2012 R2 and CRM 2013. This distinction will become more relevant when compacting the virtual disk. There is a quirk to the compaction that we wanted to highlight. We usually run the following steps on a VM after a milestone, such as at the end of a sprint or a release cycle. Depending on the work during that period, we regularly see the compacting reclaim a 2-3 GB when complete.

The following overall steps should allow cleaning up and compacting your VM and reclaiming some of the host disk space.

Free Up Space – just keep the VM clean of junk

Defrag the virtual disk – within the VM, run defrag

Wipe free space – zero out any free space on the virtual disk

Compact the virtual disk – run the appropriate utility to compact the disk

Let’s dive into the details of each…

DISCLAIMER – Standard backup disclaimer: Please back up your VM before trying anything! These steps have worked fine for our team but as each VM may be configured differently, we definitely suggest a very close review of our proposed steps.

Free Up Space

Freeing up space on the virtual disk means pruning persisted data vs cleaning up temporary files. There are a few approaches to keeping both types of data clean.

Manage persisted data

Compress folders

The benefits of compressing a folder within windows is hit or miss depending on folder contents. If the files are already compressed, not much gain. If you have not compressed a folder, you can access it from the folder properties. Select Advanced and then check “Compress contents to save disk space” Below are some common items that we compress:

Source code folders

Source code is largely text and can compress at a high ratio. For example, my current source folder for compresses from 317MB to 185MB, reclaiming me about 132MB

C:\Windows\Installer

Some blogs I have read claim that you can delete this folder so long as you have the source media for installs, but we prefer to leave it intact and compressing it has caused us no issues so far. This can grow to several GB and will usually include a lot of already compressed MSI packages, but I have seen reductions by nearly 700MB

C:\Program Files (x86)\Microsoft SDKs

With Visual Studio, you also many SDKs as part of the installation that can take up a lot of space. There is usually no risk that we have seen with compressing these reference libraries.

Database backups

On my machine, I maintain several database backups for easy restore. These usually compress at a high ratio as well and can reclaim a lot of space if you are dealing with large databases.

Miscellaneous

Basically, any other stuff that can be compressed depending on your VM. For example, I have c:\tools that includes a lot of CRM specific tools that have no issue with compression.

Delete folders

If you have installation media for your tools and solutions available, you can get rid of a lot of stuff, freeing up a few GB of space. We have not seen any issues on deleting this stuff but, again I have the install media available if we DO have issues. The following are some items that I usually target for deletion. Also, if you don’t want to delete, you can just compress these or even copy off the virtual drive into a backup drive:

SQL Server

These folders contain some installation media for SQL Server. I have never had an issue running updates after removing these folders

C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap

C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap

Visual Studio

These folders contain packages included in the Visual Studio installation. I have not seen a definitive answer as to their purpose, but I believe they are used when adding and removing features in Visual Studio. Again, I have not seen issues with deleting these folders.

C:\ProgramData\Package Cache

C:\Windows\Installer\$PatchCache$

CRM Roll-up Update installation

Depending on the features installed, these folders include uninstallers for each RU for Dynamics CRM. If you never intend to roll back a version, these should be safe to remove

C:\windows\$CrmUninstall*

The list of roll-up updates can combine to a recovery of 2GB in some cases, depending on the service pack or Rollup Update you have deployed on the machine.

Manage temporary data

We regularly use CCLEANER (http://www.piriform.com/ccleaner) that cleans standard system junk like browser cache, temp files, and app specific settings. I know there are many other tools similar but CCLEANER is free and very easy to configure and execute. The Piriform team also releases regular updates as different versions of browsers and operating systems are released. In addition to the default settings, I also add a cleanup of the following to the Custom Files and Folders list:

Temporary ASP.NET files

Depending on OS and framework version, the files are located in somewhere similar to the following

The downloaded packages for most windows updates can be found at the following

C:\Windows\SoftwareDistribution\Download

User temp files

Though this folder is in the list for CCLEANER, it sometimes doesn’t clean completely

%userprofile%\AppData\Local\Application Data\temp

CRM Trace Logs

Depending on the install, but usually found under the following folder

C:\Program Files\Microsoft Dynamics CRM\Trace

WinSxS Cleanup (Windows 8.1 and Server 2012 R2)

One ongoing issue with Windows virtual machines is the WinSxS folder continuing to grow over time. I’ve read loads of posts on the component store and how you should never mess with it. But I just came across the following article: http://technet.microsoft.com/en-us/library/dn251565.aspx. Windows 8.1 and Server 2012 R2 now include tools to periodically clean up the component store.

Based on the post, files are cleaned up periodically but it does not say how long things are kept around before clearing. So as part of regular cleanup, you can run the tasks manually and reclaim space. See the items below. As an example, I just ran Windows update on an older baseline 2012 R2 VM and 700MB of updates translated to an additional 6 GB on my virtual disk. Running the commands below (from within the VM) reclaimed about 5GB of that space. Definitely read the whole post for more information.

Clean Up the WinSxS FolderUse the /ResetBase switch with the /StartComponentCleanup parameter

Using the /ResetBase switch with the /StartComponentCleanup parameter of DISM.exe on a running version of Windows 8.1 removes all superseded versions of every component in the component store.

From an elevated command prompt, type the following:

Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase

Miscellaneous
Whatever else your team regularly clean up specific to your development tasks, tools or custom solutions. This could be cache files, temporary builds, etc. This could also include maintenance provided by services such as SQL Server. I regularly shrink databases to ensure the minimal size for both the database and the log files. You can automate this or make it a manual process that you do during the periodic cleanup.

Defrag on the VM

Defragging the virtual disk from within the VM is required to compact and defrag the files contained within the virtual disk. I use Auslogics free defrag (http://www.auslogics.com/en/software/disk-defrag/) Piriform, the company that owns CCLEANER , also has a good free defrag tool (http://www.piriform.com/defraggler/download). These tools both seem to be faster, more configurable and more thorough than the windows defrag tool. Before defragging, I also turn off the paging file. I am not sure if this is necessary but seems to help with the compaction during defrag, moving all files together on the disk. Just be sure to turn it back on after your cleanup is complete. I also shut down services to speed things up and ensure files are not locked, like databases. Not sure if this one actually makes a big difference, but it’s not hard to shut down via a simple command file or PowerShell script. They will all restart on reboot, too. I run the option of Defrag and Optimize a few times just to get as compacted as possible. This may only make a small difference, but it doesn’t take long to run once initially compacted.

Zero out the empty space

This bit is necessary for the virtualization solution to truncate and actually free up the space. Even though the space is empty, the host will see that it contains data if it’s not been filled with nulls. This is because a delete in Windows is simply setting a flag that the file has been deleted in the file system. CCLEANER offers an option to Wipe Free Space but I use SDELETE from sysinternals (http://technet.microsoft.com/en-us/sysinternals/bb545046 ), a simple command line tool that runs pretty fast and is very thorough. A warning in case you are really fighting for disk space: this process will expand the VHDx a bit because it’s writing things out to your virtual disk. I’ve seen this process add 2-3 GB but I’ve never seen it fill out the entire disk.

Compact the virtual disk

Compacting a virtual disk actually truncates the unused space and reclaims the space in the virtual disk file. Each virtualization solution has its own method. For VMWare, it’s included in the disk utilities within the virtual machine settings. VMWare seems to compact fine right from the disk tools. So the above steps apply as well. Hyper-V on Windows 8/8.1 includes an option in the Hyper V UI but it does not seem to actually compact. For Hyper-V, I use a command file that I found while searching for a solution (If I find the blog post again, I will add a citation). The script will mount the VHDX in the host windows environment and run an optimize utility to compact the virtual disk. The script we use has the following commands:

Note that when running this Powershell script, you need to be running it as administrator or you will see access errors. An easy solution is to just run powershell ISE (C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe) as administrator and run the script from within.

Summary

If the cleanup removes a lot of data from disk, you can reclaim a significant percentage of disk space. For example, I just cleaned up one of our shared disks using these methods and was able to reclaim about 12 GB in the virtual disk file. In another case, where I have kept up my disk with regular cleanup, I can reclaim 2 or 3 GB of space regularly. Following these steps regularly can save the team a bit of hassle and save a bunch of space on everyone’s machines in the long run.

As always, any feedback and additions are welcome. As we receive suggestions, we will update this post with the new information.