Large VHDs make for slow VMs – but why were they so large?

I was trying to do some work on a Vista VM I have under Hyper-V and it was running amazingly slow.  I know Vista is not a speed demon but my other VMs were running a whole lot better than this one so I thought I’d do some investigation.
 
I noticed that the server was doing an amazing lot of IO when trying to load the Vista VM so once the VM was finally up I looked at the size of the disk from inside the VM and wow it was 68GB … now this was an only slightly used VM so how had the disk size grown to 68GB? 
 
I found 2 things were at play here.
 
1) I checked the VHD size on disk and it was only 13GB which was more what I was expecting so why was the VM reporting 68GB internally?  Then I noticed that I had snapshotted the VM quite early on after I had first created it so in fact the real size was contained in the AVHD snapshop file which was 61GB in size … ouch … I know that snapshots cause a lot of additional IO so I deleted the snapshot which caused the AVHD file to be merged back into the VHD file when I shut the VM down … that took a while to merge but it gave me time to do some research on why the VHD had grown so large in the first place.
 
2) What I found is worth knowing if you are working with virtual clients … the culprit is System Restore.
Pretty much all I had done with this Vista VM since I built it was apply patches.   Of course everytime you apply a patch the system makes a restore point for system restore.  In Vista System Restore is on by default.  System Restore will use up to 15% of your hard disk to hold its Restore points before it deletes older restore points.  In XP you could control how much space System Restore uses thru the GUI but in Vista there is no GUI to do this and it is set at 15% by default.
 
Here’s the catch for a VM … if you use a dynamically expanding VHD for you VM then the system will only use what the disk space that it needs up to the max size that you specify …. but if you specify a 255GB maxsize for the VHD even if the system really only needs about 20GB, guess what System Restore thinks that it can use?   Yep, 15% of 255GB which is about 38GB before it starts deleting old restore points.    So basically my system had filled itself up with 38GB of system restore files.
 
Now to getting rid of them and stopping this happening again.   System Restore is basically a good thing but it needs to be managed for virtualized clients.   You can turn System Restore off in Vista by going to Computer|Properties and selecting System Protection on the left panel.   There is a check box that lets you turn it off (but not adjust the size it uses), and turning it off deletes all Restore Points (hence releasing all my surplus 38GB).   But the next trick with dynamically expanding VHDs is that just deleting content inside the VHD does not reduce its size.  You now have to go thru a process of compacting the VHD.
 
So to compact a VHD you now need to do at least 3 things:
1) Delete all unwanted content from inside the VHD e.g. Run Disk Cleanup, turn off system restore etc
2) Defragment the VHD using the inbuilt Defrag program or something like Defraggler (www.defraggler.com)
3) Shut down the VM and then Edit the disk thru the Hyper-V console and choose the Compact option … this can take a while
 
Once you do all that you should get a much smaller and more compact VHD that performs way quicker.
 
So the final issue is: If I want to turn System restore back on how do we prevent it doing this to us again?
 
The answer is the Vssadmin Resize ShadowStorage command that lets us set the maximum storage that System Restore will use.  So open a command prompt with Administrator priviledges and type in:
 
         Vssadmin Resize ShadowStorage /For=C: /On=C: /MaxSize=2GB
 
which will set the maximum amount of disk that System Restore will use to 2GB (min is 300MB).   Obviously this reduces the number of restore ponts that you can have but 2Gb can still hold a lot of restore points.
 
        Vssadmin List Shadowstorage
 
will show you what your Storage usage is currently set at.
 
So now my VM not only performs better but the VHD shouldn’t grow out of control either.   And my VHD file now is only 22GB and not 68GB.
 
This behaviour is really confined to desktop client environments such as XP, Vista and Windows 7.   System Restore is not available in Server environments (and Volume Shadow copy is turned off by default) so I haven’t investigated whether the same sort of VHD bloat could happen in Server environments.
 
Some links that helped me in this investigation:
 
 
 
 
 
 
Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s