Share via


Creating Virtual Hard Disks

Applies To: Windows 7, Windows Server 2008 R2

For IT professionals who are getting started with using virtual hard disks (VHDs), this topic describes some of the basic operations that you can perform including creating .vhd files and attaching them so they appear as a disk on the computer. You can perform these operations by using the Disk Management MMC snap-in and the DiskPart command line tool.

In this topic:

  • Using Disk Management

  • Using DiskPart

For information about more advanced tasks that you can perform to modify VHDs, see Servicing and Backing Up Virtual Hard Disks.

Using Disk Management

This section explains how to use the Disk Management MMC snap-in to perform the following operations:

  • Create a new .vhd file by using the available disk space on the computer, and then save it to the location that you specify.

  • Attach (sometimes referred to as “mounts” or “surfaces”) the .vhd file so that it shows up as a disk and assigns it a drive letter.

  • Detach (sometimes referred to as “unmounts” or “unsurfaces”) the .vhd file and unassigns a drive letter.

To create and attach a VHD file by using Disk Management

  1. Open Disk Management (click Start, type diskmgmt.msc in the Search box, and then press ENTER).

  2. Select Create VHD from the Action menu. This launches a dialog box that you can use to specify the following settings for the VHD:

    • Location: This is the location and file name where the new VHD will be saved when it is created. For example: E:\test.vhd.

    • Virtual hard disk size: This is the size of the VHD. Note that if you create a dynamically expanding VHD, the virtual hard disk size is the maximum size the VHD will expand to.

    • Virtual hard disk format: This is the format of the VHD: dynamically expanding or fixed. Note that when you create a dynamically expanding VHD, Windows does not test for free space on the physical computer based on the maximum size requested. Therefore it is possible to create a dynamically expanding VHD with a maximum size that is larger than the available free space on the hard disk drive of the physical computer. The maximum size of a dynamically expanding VHD is 2,040 GB.

  3. Click OK to create the VHD. After it is created, the VHD is attached and it appears as an uninitialized disk.

  4. To initialize the disk, right-click the disk name in the Disk Management pane and click Initialize Disk. (In the previous example, you would right-click the area that says Disk 3.)

  5. Click OK to confirm that you want to initialize the disk.

  6. After the VHD is initialized, you can treat the VHD as any other disk, including creating new volumes, formatting volumes, and assigning drive letters to volumes within the VHD. For example, right-click the unallocated space and click New Simple Volume. Follow the instructions in the New Simple Volume Wizard to select the volume size, drive letter, format, and disk block size.

  7. Optionally, you can detach the VHD so that it no longer shows up as a disk. You should always detach VHDs prior to restarting the operating system to prevent data corruption. To do this, right-click the disk name, and then click Detach VHD.

Alternatively, if you have an existing .vhd file that you want to attach as a disk, use the following procedure.

To attach an existing VHD by using Disk Management

  1. Select Attach VHD from the Action menu.

  2. Enter the path to the .vhd file and specify whether it should be attached as a read-only disk.

      

      

  3. Click OK. After the VHD is attached, you can treat it as any other disk. For example, you can create new volumes, format volumes, and assign drive letters to volumes within the VHD. Additionally you can browse existing volumes within the VHD.

  4. To detach the VHD, right-click the disk name, and then click Detach VHD.

Using DiskPart

If you prefer to work from the command line, you can use the DiskPart command-line tool to perform these operations. For more information about DiskPart commands, see DiskPart (https://go.microsoft.com/fwlink/?LinkId=128458).

The following procedure provides an example for how to create and configure a VHD by using DiskPart.

To create and configure a VHD

  1. To start the DiskPart command interpreter, open an elevated Command Prompt window (click Start, right-click Command Prompt, and click Run as administrator) and type:

    diskpart
    
  2. To create a new 2 GB dynamically expanding .vhd file (called Test.vhd) and save it to the C:\vhd folder, type the following command. If you do not specify the type=expandable parameter, DiskPart will create a fixed VHD. For more information about the syntax of this command, see Create vdisk (https://go.microsoft.com/fwlink/?LinkID=205510).

    create vdisk file=c:\vhd\test.vhd maximum=2000 type=expandable
    

Note

Another type of VHD that you can create with Diskpart is a differencing VHD. A differencing VHD (sometimes referred to as a “child” VHD) is similar to a dynamically expanding VHD, but it contains only the modified disk blocks of the associated parent VHD. The parent VHD is read-only, so you must modify the child VHD. To create a differencing VHD, use the parent command. For example, to create a differencing VHD (called Child.vhd) that is the child of Test.vhd, type: create vdisk file=”c:\vhd\child.vhd” parent=”c:\vhd\test.vhd”.

  1. To view the .vhd file that you just created, type:

    list vdisk
    
  2. To attach the VHD, type:

    attach vdisk
    
  3. Optionally, to view information about the VHD, type:

    detail vdisk
    
  4. To create a 1 GB primary partition inside the new VHD, type:

    create partition primary size=1000
    
  5. To format the partition, type:

    format fs=ntfs label=”test volume” quick
    
  6. To assign the drive letter M, type:

    assign letter=m
    
  7. Optionally, you can detach the VHD so that it no longer shows up as a disk. You should always detach VHDs prior to restarting the operating system to prevent data corruption. To do this, first select the file that you want to detach (select vdisk file="c:\vhd\test.vhd"), and then type detach vdisk.

Additionally, you can perform the following operations by using DiskPart:

  • Expand the size of a VHD. To expand the maximum size available in a VHD, first ensure that the VHD is detached. Then, select it (type select vdisk file=<full path>), and then type expand vdisk maximum=<new size>. For example, expand vdisk maximum=3000 will expand a VHD to be 3 GB. To view information about the VHD, type detail vdisk.

  • Extend a volume within a VHD. You can extend a volume within a VHD into free unallocated space within the VHD. To do this, ensure that the VHD is attached. Then select the volume that you want to extend (for example, type select volume=<drive letter>), and then type extend size=<increase size>. For example, extend size=1000 will increase the size of a partition by 1 GB. For more information about the syntax of this command, see Extend (https://go.microsoft.com/fwlink/?LinkId=204232).

  • Compact a dynamically expanding VHD. Reducing the size of dynamically expanding VHDs is useful because these files increase in size as you add files, but they do not automatically reduce in size when you delete files. To compact a dynamically expanding VHD, first ensure that the VHD is either detached or attached as read-only. Then select the VHD (type select vdisk file=<full path>), and type compact vdisk.

    To view information about the size of the VHD, type detail vdisk. For dynamically expanding VHDs, the virtual size is the maximum size of the VHD (specified when you created the VHD), and the physical size is the actual size. For fixed VHDs, these values are the same.

  • Merge a differencing VHD. To merge a differencing VHD with its parent VHD, first ensure that the differencing VHD is detached. Then select the VHD (type select vdisk file=<full path>), and type Merge vdisk depth=1. Note that Depth=1 indicates that one level of the differencing chain will be merged. If you specify a number larger than 1, the VHD must have a differencing chain that is more than 2. For more information about the syntax of the Merge parameter, see Merge vdisk (https://go.microsoft.com/fwlink/?LinkId=204233).