James O'Neill's Blog

May 26, 2009

How to: work with VHD files at the command line.

Filed under: Beta Products,How to,Virtualization,Windows 7,Windows Server 2008-R2 — jamesone111 @ 12:22 pm

Virtual Hard Disk (VHD) files have been given greater importance in Windows 7 and Server 2008 R2. They’ve always been used for hosting virtual machines (from the earliest Virtual PC through to Hyper-V) , and in Vista the complete image backup began to use VHD format, the iSCSI target software in Storage server – which is now available to TechNet subscribers. But in the new OSes we can boot from a VHD, mount VHDs and create VHDs in the standard OS.

In the library I created for Hyper-V, one of the first things I wanted to do was to be able to create VHDs and Mount and Unmount them. In fact when Hyper-V mounts the disk it doesn’t bring it on-line and flags it read only, so the PowerShell code I wrote not only had to call the WMI functions provided by hyper-V’s Image Management Service, but it also needed to invoke DiskPart.exe with a script to get the disk to a useful state. If the Index of the mounted VHD is stored in $diskIndex this line of PowerShell does that for me: 

@("select disk $diskIndex", "online disk" , "attributes disk clear readonly", "exit")  | Diskpart | Out-Null }

In Windows 7 and Server 2008 R2 you don’t need a script to call WMI objects to create and attach a VHD (note that Hyper-Vs image management service calls it Mounting/unmounting, and the newer Windows calls it attaching/detaching: it’s the same thing): it can be done from diskpart.exe – perhaps automating it as above – or from the storage part of the management console. Of course the MMC isn’t available if you are running on server Core, and the command line versions are also available when booting into Windows-PE to set up a machine so it useful to know what they are. DiskPart needs to be run  elevated (unless you are signed in as the account named administrator, or running Windows PE), it’s just as happy running from PowerShell as from CMD, provided you run as administrator. The following DiskPart commands will setup a VHD.

create vdisk file=<path>.vhd maximum=<size in MB> type=fixed 

select vdisk file=<path>.vhd

attach vdisk

create partition primary

active

assign letter=V

format quick fs=ntfs label=<OS Name>

exit

By default Create Vdisk makes a fixed VHD, you can also use type=Expandable;  a fixed VHD takes quite a bit longer as the file system creates a file the size specified by maximum and writes zero-filled blocks to the file (HELP CREATE VDISK will show you more options)

To mount a VHD , you select the virtual disk, and then attach it. Initially this disk won’t have any partitions on it (just like a freshly unwrapped hard disk), so the next step is to create a primary partition, make it active, give it a drive letter (I like to use V for VHD) and put a file system on it. Now you have a working drive V: of the specified size with an NTFS file system on it. You can unmount it by going back into diskpart and entering

select vdisk file=<path>.vhd 

detach vdisk

exit

and re-attach it with

select vdisk file=<path>.vhd 

detach vdisk

exit

In a later post I’ll cover how we can put an OS onto the VHD and how that OS can be customized, and also put up a link to a video of VHDs in action.

This post originally appeared on my technet blog.

About these ads

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: