Maintenance Automation - Boot2VHD

02.19.2011 08:02 by kbeckman | Comments

Sorry for the repost… For some reason, this post was lost in the latest site upgrade. Since then, I’ve made a few updates.

 

I’m finally getting around to the post I promised a few weeks ago where I mentioned some automation scripts for performing maintenance on your bootable VHDs. These automation scripts come in a 2-part format – maintenance from within the context of your bootable VHD and the maintenance of the VHD file itself. I highly recommend you check out my prior post, Boot2VHD Best Practices, before using these scripts. It will give you a better idea of what operations the scripts perform (and the reasons behind them).

 

Prerequisites

There are just a few prerequisites that you need to make sure you have installed if you want to use these scripts in their entirety. Make sure to install them to the locations mentioned below or you will have additional customization work to do on these scripts…

1)   7-Zip (Host OS) – This is an awesome freeware file compression utility. The scripts use 7-Zip for archiving the VHDs before and after the maintenance cycle. The scripts assume that you are using the 64-bit version of this application and it assumes that 7-Zip is installed at: C:\Program Files\7-Zip.

2)  You need to have a backup location set up with the following file structure. ..VHD Backups\  ..VHD Backups\Old\

image_thumb

3)   SysInternals Suite  (Bootable VHD) – You’ll need to download this application suite and unzip it to C:\Program Files (x86)\Sysinternals Suite. The main application we need for this process is SDelete – it’s used for zeroing-out the free space in your VHD and is required to prep your VHD for the compression process.

4)   Just a note here… This has only been tested with Windows 7 (64-bit), however it should also work with Windows Server 2008R2.

 

Notes and Disclaimer

You’ll definitely have to customize these scripts for your own environment. As long as the prerequisite applications are installed in the locations that I’ve mentioned, you shouldn’t have to change the prep script – Prep Bootable VHD for Maintenance Cycle.bat. I highly recommend backing up your VHDs before running the scripts for the first time. This will ensure that you’re able to rollback if something wasn’t configured properly. Be sure to read the comments included within the scripts. They should give you a good idea of what’s about to happen and how you should configure it for your own environment. Lastly, I want to mention that DiskPart requires that its scripts are in separate files when you’re automating anything. There are two text files included in the Host OS directory that contain the DiskPart tasks.

 

Script 1: Prep Bootable VHD for Maintenance Cycle.bat

Run this script as an administrator while booted into your VHD. This script is located in the Virtual Machine folder in the .zip file and is the first step in the maintenance cycle. This script opens the Disk Cleanup utility allowing you to select the clean up options to perform. Next it runs the defrag utility on your bootable VHD C: drive. Finally, it uses SDelete to zero out the free space on your VHD drive for the compression process later.

 

:: Run the DiskCleanup utility...
cleanmgr.exe /d c:

:: Run disk defrag...
defrag c: /H /U /V
defrag c: /H /X /U /V

cd "c:\program files (x86)\sysinternals suite"

:: Run SDelete to zero-out free space...
sdelete.exe -p 1 -c c:pause

  

Script 2: Run VHD Maintenance Cycle.bat

Run this script as an administrator while booted into your Host OS. This script contains the meat of the automated maintenance cycle and will require some configuration on your part to make sure you’re pointing to the right VHD and backup locations for your environment. The script assumes a single bootable VHD in the management cycle, but you can easily add the extra commands to support additional VHDs. In the section below, I explain every step of the maintenance workflow.

 

1) First the script deletes the old VHD backups in the <backup location>\old directory.

2) Next the script moves the backups from the prior maintenance cycle to the <backup location>\old directory.

 

:: Move backups from prior maintenance cycle to the ..\old directory...
del "d:\vhd backups\old\*.7z"
move "d:\vhd backups\*.7z" "d:\vhd backups\old"

 

3) Next the script uses a DiskPart command script located in the VHD Mgmt - Compact and Merge Disks.txt file to compact your disk. Be sure to edit this text file to contain the proper location of your disk.

  

[parent script]
:: Run Diskpart compact and merge tasks...
diskpart /s "D:\Git\System\Scripts\VHD Maintenance\Host OS\VHD Mgmt - Compact and Merge Disks.txt"


[diskpart script]
select vdisk file="v:\native\developer\ultimatex64.vhd"
compact vdisk

  

4) Finally, the script creates a backup of the VHD in the <backup location> directory.

 

:: Backup the VHDs...
call:ZipFile "d:\vhd backups\Ultimatex64_Development.7z" "v:\native\developer\ultimatex64_development.vhd"


::--------------------------------------------------
:: Creates a 7-Zip .7z archive.
:: Params:    %1 = destination archive
::            %2 = source file
:: http://www.dostips.com/DtTutoFunctions.php#FunctionTutorial.CreatingAFunction
::--------------------------------------------------
:ZipFile
cd "c:\program files\7-zip"
7z a -t7z %1 %2
goto:eof

 

I hope there’s some folks out there that find this script useful… It has saved me a lot of time by not having to babysit the maintenance process. Now all of my VHD maintenance runs after hours and I just check it in the morning. If anything fails, I’ve always got a backup! I’ve included links to two script packages – one that assumes a single bootable VHD (as described in this post) and another that assumes a parent bootable VHD with an associated differencing disk.

C4SC_VHD_Maintenance (Single).zip (3.34 kb)

C4SC_VHD_Maintenance (Differencing).zip (4.00 kb)

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.