Showing posts with label virtual. Show all posts
Showing posts with label virtual. Show all posts

Tuesday, August 30, 2011

Create & Boot VHD

This is just quick list for creating and booting from a Virtual Hard Disk (VHD) in Windows 7.

1a. Creating VHD from scratch


If you’re creating a VHD from scratch (that is not a child-disk based on another VHD) this is the commands you need to run from command prompt:
:\> diskpart

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: xxxxxxxx


DISKPART> create vdisk file=[FILEPATH] type=expandable maximum 50000

  100 percent completed

DiskPart successfully created the virtual disk file.

DISKPART> exit

The ‘:\>’ means command prompt, typically it would be ‘c:\>’ or something similar, but the actual directory you’re in doesn’t matter for the command.

Running the command ‘Diskpart’ will start the DOS-utility that we’ll use for creating the VHD. When diskpart starts, the command prompt will change to ‘DISKPART>’. Now you can type the command ‘create vdisk’ to create the actual VHD.

The command has only two required arguments; ‘file’ and ‘maximum’. The ‘file’-argument specifies where to output the vhd and the name of the vhd-file. So [FILEPATH] in the example above will typically be ‘D:\VHD\MyNewVhd.vhd’.

The ‘maximum’ argument specifies the size of the VHD in megabytes. You can choose between to types of disks (the ‘type’ argument); either fixed, meaning that the VHD file will be created with the size specified by the ‘maximum’ argument, or expandable, which means it will only be as big as the data on the disk requires but not larger than ‘maximum’. The default is fixed and if you have lots of space available it’s the recommended setting as it’s a bit faster than expandable disks. But on my laptop I do not have unlimited storage, so here I’ll go with the expandable option and setting the maximum file size to about 50 GB. It’s also a bit cheaper to do backups of smaller files, so expandable has it’s advantage there.

1b. Creating a differencing VHD


If you already have a base-VHD that you want to use as a parent-disk, this is the commands you need to run from command prompt:

:\> diskpart

Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: xxxxxxxx


DISKPART> create vdisk file=[FILEPATH] parent=[PARENT_FILEPATH]

100 percent completed

DiskPart successfully created the virtual disk file.

DISKPART> exit

As with the 1a) option above, the [FILEPATH] specifies the path and name of the disk to create. The ‘parent’ argument should be pretty obvious and [PARENT_FILEPATH] is the fully qualified name of the existing VHD you want to use as base-image.

Note that you cannot specify ‘maximum’ or ‘type’ as arguments for a differencing disk because the size of the child disk is set from the parent. It’s also possible to merge a diff-disk with it’s parent at a later point, to create a new VHD.

Remember to make your parent-disk read-only before you use it for differencing disk. Failing to do so and then accidentally starting up or booting directly in to your parent VHD, will render your differencing disks useless (and yes; I learned that the hard way).

Another gotcha to be aware of; if you want to boot in to the new differencing disk, it needs to be located on the same disk volume as the parent disk. They can be in different folders, but they cannot reside on another volume – not even if the volumes is on the same physical disk.

2. Boot it up!


When the VHD is ready to use, issue these commands to add them to your boot menu;
:\>bcdedit /copy {current} /d “My virtual boot entry”

The entry was successfully copied to {df399c86-f723-11df-85b6-8d1c50594e14}.

:\>bcdedit /set {df399c86-f723-11df-85b6-8d1c50594e14} device vhd=[locate]\Path\To\Disk.vhd

The operation completed successfully.

:\>bcdedit /set {#GUID#} osdevice vhd=[locate]\Path\To\Disk.vhd

The operation completed successfully.

:\>bcdedit /set {#GUID#} detecthal on

The operation completed successfully.

If you run the ‘bcdedit’ command without any arguments, you should be seeing your new boot entry at the bottom. And it should look something like this;

Windows Boot Loader
-------------------
identifier              {df399c86-f723-11df-85b6-8d1c50594e14}
device                  vhd=[locate]\Path\To\Disk.vhd
path                    \Windows\system32\winload.exe
description             My virtual boot entry
locale                  en-US
inherit                 {bootloadersettings}
recoverysequence        {df399c79-f723-11df-85b6-8d1c50594e14}
recoveryenabled         Yes
osdevice                vhd=[locate]\Path\To\Disk.vhd
systemroot              \Windows
resumeobject            {df399c77-f723-11df-85b6-8d1c50594e14}
nx                      OptIn
detecthal               Yes


The first command, ‘bcdedit /copy’ will copy the default boot entry and create a new with the name specified by the /d argument. Then you’ll use the /set argument to modify the new entry. You’ll need to copy the id that will be displayed after the initial copy command, and input this as the first argument for the /set command.

There’s 3 things that need to be set; the device, the osdevice and the detecthal. The first to are similar and takes the path to the VHD you want to boot from as input. Note the ‘[LOCATE]’ syntax in the path; this will tell the boot manager to figure out which drive to locate the VHD on. So instead of ‘vhd=d:\path\to\disk.vhd’, you  need to enter ‘vhd=[locate]\path\to\disk.vhd’.

A little tip if you have spaces in the path or filename for the VHD; surround the path with apostrophes (‘).

The last /set command will give some instructions to the kernel to detect certain hardware information (HAL = Hardware Abstraction Layer), which is needed on some x86-based system.
And that’s it; Ready to boot!

Resources


In some of my earlier posts I wrote a bit about how to compact VHDs, how to automate Windows install on VHDs and some pros and cons with virtual machines.

Also, David Loongnecker has some good tips in his blog post Tips for Booting/Using VHDs in Windows 7.

Thursday, August 4, 2011

Compact virtual space

Keeping a virtual machine tidy will make it run faster and the VHD file itself smaller. Here’s a little list of things you can do to tidy it up a bit.

The usual suspects


First things to do is what you’ll do on any machine – virtual or not – to keep things as optimal as possible; The usual maintenance jobs includes deleting temporary files, emptying the recycle bin, cleaning up the registry, etc. I’ve found Glary Utilities (links at the end of this blog post) to be a good tool for this. Just install the free version and run the ‘Scan for issues’ under ‘1-click maintenance’.

If this is a fresh VHD which haven’t been used much you’ll probably won’t gain much from the usual housekeeping, but defragmenting and checking the disk (chkdsk) and Windows system files (sfc /scannow) seldom hurts.

Shrink wrap


To shrink the size of the VHD file you can compact the disk, but before you do that you need to pre-compact it. Compacting the disk is an operation you’ll do on the VHD-file, while pre-compacting is something you’ll do on the running virtual machine to make the compacting operation as effective as possible.

To pre-compact the VHD you need to run the precompact.exe which you can find on an ISO in the Virtual PC install directory (on my box the precompact.iso is in the folder c:\program files (x86)\windows virtual pc\integration components, but the exact location will vary depending on your OS and Virtual PC version).

precompact.exe must be run inside the virtual machine. You can either boot into it or fire it up in Virtual PC. Once you’re running the virtual machine you need to open the command line and navigate to the folder where that precompact.exe resides. Make sure you’re running with administrator privileges and run the following command:

:\> precompact –Silent –SetDisks:C

The last parameter specifies which disks to pre-compact and if not specified it will pre-compact all disks. In my case that wouldn’t be very wise, since I also have access to the host partition from my virtual machines.

When pre-compacting is done (can take quite some time depending on the size of your disk(s)), shut down the virtual machine.

Now it’s time for the actual compact process, so from your host OS fire up the command line and start the disk part utility;

:\> diskpart

When running the diskpart, enter the following commands (assuming the VHD file is named ‘parent.vhd’ and is in a folder called ‘vhd’ on partition ‘d’);

DISKPART> select vdisk file=”d:\vhd\parent.vhd”
DISKPART> attach vdisk readonly
DISKPART> compact vdisk
DISKPART> detach vdisk
DISKPART> exit

And that’s about it. You should now have a small and fast virtual machine (everything’s relative, right?)

Resources


Glary Utilities – Optimization software for Windows. Comes in two flavors; a free version for the basic optimization that’ll be good enough for most users and a paid version with more advanced tools.

Tuesday, August 31, 2010

Waking up to a New Virtual Reality

My first experience with a virtualized developer environment was back in 2007. I was taking over a project that had been in prod for a a year or so. It wasn’t a very large project and I was just in to fix some bugs and add some features.

But anyone who has been added to a project ‘just’ to add some value knows it can be a tedious affair just to get the development environment up and running. This was a project based on EpiServer (a SharePoint’ish Content Management System) running on ASP.NET 2.0 on the frontend and Sql Server 2005 in the backend.

New VRI would probably have had to spend at least 2 or 3 days just to get my dev machine setup for this project. That would be; if I could get my dev environment set up.

I had never worked on EpiServer before and so just set this up would have been a long and windy road. The version we were running was a couple of versions old and the web contained little to none information on the version we were running. Even getting the correct installer would probably take a couple of days…

Lucky for me this project had been using VMware to virtualize both the developer, test and staging environments, and so getting my first dev build up was merely a matter of installing the VMware client, copy over the virtual image and start it up. Almost too easy!

The dev machine in this case was a Windows XP image with Visual Studio 2008 and Sql Server 2005. An environment quit fit for virtualization. My next project was onsite at a customer with an already setup up machine, and so I didn’t have the chance to virtualize anything there. But starting on a new project again in late 2008, a decided to give VMware a try again.

This time it was on a Vista box (with bitlocker) and a dev environment that also required Vista. Needless to say; Vista rendered, to my big disappointment, useless to both be virtualized and to be the virtualization host. At least for a developer environment.

Then came Windows 7 around and I saw some blog posts on how you could boot directly off a Virtualized Hard Drive (VHD). Meaning you would only suffer about 3-5% performance loss due to virtualization. The only hardware that is actually virtual is the hard drive. Everything else, CPU, memory, graphic card, network, usb, are all non-virtualized. You’re running directly off the hardware.

I didn’t take the time to test out VHD boot as long as I had my dev environment already set up and everything was running fine. But about a month ago I got a brand new Lenovo W510, and so I finally got the ‘excuse’ I needed to give virtualization a new chance. Gold

And I can tell you; so far it’s been pure gold!

From what I’ve experienced so far, here are the pros and cons of VHD native boot on Windows 7:

Pros

- Easy to set up new dev environment for testing out new tools, framework, languages or what-have-you. You just need to keep a copy of your ‘base images’ so that you can start fresh from there.

- Easy to get new members of a team up and running. A little disclaimer here as I haven’t actually tried this, but it should only be a matter of running sysprep with the ‘generalize’ option on the virtual machine.

- Backup is just a file copy operation

- Getting up and running on a new physical machine is just a matter of installing Windows 7, edit the boot manager (bcdedit) and copy the VHD-file over to your new machine

- “Avoid” BitLocker; Now this might seem like a strange thing to do, but as a consultant I have two sets of security manuals to confirm to. One for my employer and one for the customer that hires me. Now the security regulation is seldom at level between these, and so I always have to be set up to meet whoever has the highest security bar. Most often that would be my employer.

And as every dev knows; the more layers of security you add to your machine, the longer does your compile take. For some reason I was not equipped with a lot of patience at birth – and I haven’t gotten any since – and so sluggish machines does not suit me well. BitLocker, enterprise anti-virus clients, and other well-intended enterprise security apps, can really suck the life out of any machine. If all you need to do your job is Outlook, Word, Excel and a browser, that would be fine. When you need to compile a 65-project-large solution 400 times a day, it isn’t. And so if I’m working for a client who doesn’t require disk encryption and sluggish anti-virus software, then I’m perfectly fine with that.

Cons

Source of performance numbers- A performance hit; I’ve seen 3-5%, but those numbers apparently came out of Scott Hanselman’s butt (his words, not mine) so take that for what it’s worth (I could make a pun and say that ain’t worth sh**, but I’ll refrain myself from adding that kind of toilet humor to this blog). What I can say, though, is that I can’t tell the difference between running on my virtual and my ‘real’ Windows 7 installation. In a blind-test I don’t think I’d be able to tell them apart.

- Hibernation is not supported on the virtualized machine

- Calculation of the Windows Experience Index (WEI) not supported

For me the pros outweigh the cons. The loss in performance is leveled out by not running BitLocker (which also gives you a 3-5% perf hit). Hibernation is nice, but I can live without it and I still have WEI on the host.

 

My next blog posts will cover how I created my VHDs and got my multi-boot set up.