Boot2VHD Best Practices

09.14.2010 08:35 by kbeckman | Comments

This post comes as a result of an impromptu presentation I was asked to give during a weekly developer meeting at work a while back. I thought I’d share with all…

 

I’m sure by now most developers with access to the Windows 7 operating system have tried the Boot2VHD feature that allows you to boot to a native virtual hard disk without having to suffer through using Virtual PC. If not, there’s a glut of information out there on how to do it. There’s none better than Scott Hanselman’s post -- “Less Virtual, More Machine – Windows 7 and the Magic of Boot to VHD”.  This post however is a bit different and contains information that you won’t be able to find easily or find in one place (at least I haven’t anyway).

 

So you’ve decided to reap the benefits of Boot2VHD, now what? Below are my suggestions (through my own trial-and-error experience) for keeping your bootable VHDs operating at top performance.

1) Turn Off System Protection (Restore Points)

 

system_restoreThe few times I’ve ever actually had to use System Restore was when I had to repair someone else’s machine without having to totally rebuild it. Even then, it has never been that often.  While this utility can be nice for the novice PC user, it ends up being a disk space hog (if configured incorrectly) for the rest of us. Since your bootable VHD is just a file on the file system, you should be compressing it and backing it up somewhere else anyway… If you back-up your VHDs properly, there’s no real reason for System Restore (this only applies to VHDs). Turning this off will save a lot of churn and allocated space on your bootable VHD, especially when you’re installing a lot of developer machine applications. Another plus is a slight gain in speed during software installations and during Windows updates as no restore point is created prior to installation. If you must use System Restore on your VHD, enable it only after you’ve configured a pristine VHD; configure the utility to use only about 2GB of hard disk space; and make sure you’re consistently deleting all but the last restore point.

 

 

2) Maintain a Separate Physical Drive or Partitions for Bootable VHDs and Anything with High Churn (Code, Databases, etc.)

Development PCs endure a lot of hard disk churn. At work I’m constantly pulling down code from various source control branches; all of the which are mature, enterprise applications that contain thousands of code files. This isn’t something that I want to pull down to my bootable VHD on a regular basis -- I want to keep it as clean as possible… Use two separate physical internal drives when you can. Use a separate logical partition if another internal drive is not an option.

 

My work PC has two internal hard disks. This is the optimal solution since you can keep your VHDs on a physical drive separate from the drive in which you keep all of your high churn files. Before the addition of the additional drive though, I only had a single, unpartitioned drive – a single C: drive. Since I didn’t have the option of a separate physical disk or partition, I created another VHD that I attached at startup to keep all source code and databases on. I created a .bat file to automatically attach the code VHD and dropped it in my startup folder so I don’t have to worry about executing the script every time. My home development machine has a single internal physical drive but a separate partition for code so i don’t have to worry about attaching another VHD drive. Both of these options are nice because you can get to the same source code from various bootable VHDs (say one is a bootable Windows 7 VHD for development and the other is a bootable Server 2008 R2 also for development). Using the same physical drive with partitions is obviously the less optimal solution though because your “high churn” drive is on the same physical drive as your bootable VHD.

 

3) Once Your Bootable Development VHD is Pristine, Difference It!

Eventually, you’ll get to the point where your bootable development VHD is pristine and set up exactly the way you want. When that time comes, clean it, zero out the free space and compress it (see steps 4b and 4c) and back it up somewhere. The first step here is to use the file properties and mark your pristine VHD as read-only. This will prevent you from accidentally corrupting it somehow. Create a differencing disk off of your pristine parent and use BCDedit to change your boot entry to boot to the differencing VHD instead.

 

To create a differencing disk…

diskpart> create vdisk file=”[vhd drive:]\bootable vhds\differencing.vhd” parent=”[vhd drive:]\bootbale vhds\parent-readonly.vhd

 

A note here: I’ve tried to create bootable differencing disks in a directory other than where the parent VHD resides. While this works just fine in the context of Virtual PC (where VHDs are not bootable), I haven’t had any luck doing this with my bootable VHDs. I’ve had to keep any bootable differencing VHDs in the same directory as the parent.

 

4) Monthly Maintenance

Bootable VHDs require regular maintenance just as a normal disk would. Perform the following steps regularly to keep your VHD operating at peak performance.

 

a) Run Clean Disk and Defragment Your Bootable VHD

You should already know how to do this. I just included the hyperlinks for documentation’s sake… Run this from your bootable VHD.

b) Use SDelete to Zero-Out Your Bootable VHD Free Space

SDelete is a necessary utility for any power user. Not only is it great for performing a DoD wipe of sensitive files and folders (every developer has been there at least once), it can zero-out free space on your VHD allowing you to compress it later using Diskpart. I’ll let you read further about the full capabilities of SDelete on your own. Below is the command line call for zeroing out your VHD free space. Please note that that Sysinternals Suite is not provided through an installer. You can get SDelete separately or as part of the entire Sysinternals Suite application suite. The directory below is where I keep SDelete on my bootable VHD and this may vary from machine to machine. Run this from your bootable VHD.

>cd “c:\program files (x86)\sysinternals suite”

>sdelete.exe –p 1 –c c:

 

c) Use Diskpart to Compact Your Bootable VHD

If you’ve configured an expandable VHD as your bootable VHD, you’ll need to compress it now and then to reclaim some space on your drive. This works for both single and differenced VHDs. This post has some great specifics about this and other Diskpart VHD operations. Run diskpart as an administrator from your host OS.

 

diskpart> select vdisk file=”[vhd drive:]\vhds\bootableVhd.vhd”

diskpart> compact vdisk

 

d) Run Clean Disk and Defragment Your Host Drive(s)

Run this from your host OS.

5) Periodically Merge Differencing Disks with Their Parent

Differencing VHDs maintain changes to its parent VHD at the block level. After using a differencing VHD for an extended period of time, the disk can grow quite large. Depending on how many significant differences it maintains from the parent, it can become quite slow because the virtual disk interpreter has to do a significant amount of reading from both the parent and differencing disks. Merging the differencing VHD back with its parent resolves this.  There are a few things to keep in mind during this procedure – 1) you’ll have to remember to mark your parent VHD as editable if you’ve previously marked it as read-only; 2) your differencing VHD becomes useless and you’ll need to create a new differencing VHD off of the parent; and 3) your parent disk will contain all of the changes from the differencing disk – not the other way around. I compact all of my differencing VHDs before merging them with their parent.

diskpart> select vdisk file=”[vhd drive:]\vhds\differencingVhd.vhd”

diskpart> merge vdisk depth=1

 

I hope you get some good use out of these performance best practices… I’ll be following up soon with another post that contains some automation scripts I’ve created to save you some time and manual headache while maintaining your VHDs.

Coding4StreetCred – Official Launch!!!

09.13.2010 07:42 by kbeckman | Comments

I’ve kept a blog at work for a long time now to post such things as technical details, refactoring techniques, configuration how-to’s and general information about the projects I’ve been involved in. All of that has been organization internal content and it’s all been in SharePoint. I’m now pleased to announce the official release of my public-facing technical blog – Coding4StreetCred! And I’m equally as excited to be using a blogging platform other than SharePoint!

 

Most of the content from here on out should be interesting, detailed and technically useful for a number of folks earning their keep as .NET developers. I hope the posts to come not only help the developers on my teams, but the larger developer community as well. Aside from all of the fame, notoriety, money, fast cars and women that I’ve heard come with having your own technical blog, giving back to the community has been the primary driver for this project. Without the technical contributions of so many individuals I’ve never met and the open source projects I’ve drawn from continuously over the years, my advancement as a software developer would have been much more difficult and my projects much less successful. In addition to community involvement, I hope this blog challenges me to become a better developer as I’m opening my work up to a larger attack surface for criticism.

 

Here’s a special thanks to the people and projects (in no particular order) that helped me finally get this project off the ground, my ideas out of my head, and my code into an ASP.NET Project…

Scott Hanselman – If you’ve never read or have never heard of ComputerZen, you need to point your RSS reader in Scott’s direction. This post in particular really got me started down the right track to releasing C4SC.

BlogEngine – BlogEngine.net is the open source project I started with to build this site. It’s a great project to start from and has a ton of out-of-the-box features to get you going… I’m really ready for another version built using VS2010 and .NET 4.0 though (I hope they’re listening).

DarkBlog – I started with and expanded this BlogEngine.net theme for C4SC.

GraffitiCreator – I’ve had the logo designed for C4SC for at least a year… One of the planned enhancements for this site is to generate a few more logos that cycle through randomly with each web post. C4SC is an agile shop though and that’s low on the priority list.

 

Check back soon for some real, bona fide technical content…