Partager via


Connect a Virtual Hard Disk to a Virtual Machine Without Shutting It Down

Applies To: Windows Server 2008 R2

In some circumstances, you might not want to connect a virtual machine to an external network. For example, you might want to isolate a virtual machine from your domain network to test a Dynamic Host Configuration Protocol (DHCP) server or a domain controller. Because the virtual machine is isolated, you cannot move files from the virtualization server to the virtual machine by using the domain network. To overcome this limitation, you can create and attach a virtual hard disk on the virtualization server, copy to the virtual hard disk the files that you want to move to the virtual machine, and then attach the virtual hard disk to the virtual machine.

Note

Alternatively, you can use an internal virtual network to move files to a virtual machine. This alternative method does not require attaching and detaching a virtual hard disk, which temporarily disrupts access to the virtual hard disk from the virtualization server or from the virtual machine. For more information, see Use an Internal Virtual Network to Move Files to a Virtual Machine, in this guide.

A similar scenario can occur when a virtual machine is running out of disk space and you want to add another virtual hard disk without shutting down the virtual machine. You can attach a virtual hard disk to a virtual machine without shutting down that virtual machine only if you attach it to the SCSI controller of the virtual machine. To attach a virtual hard disk to an IDE controller, you must first shut down the virtual machine.

Scenario prerequisites

To test this scenario, you will need the following:

  • Complete all steps in the main section of this guide. After you complete all the steps, you will have a virtualization server and two virtual machines: Base Virtual Machine (used for creating new virtual machines) and Imported Virtual Machine.

  • Imported Virtual Machine is running, Windows Server 2008 R2 is configured, and the computer name for the virtual machine is VirtualMachine1.

Scenario steps

The following procedure explains how to create and attach a virtual hard disk to the virtualization server, copy files to the virtual hard disk, and then attach the disk to a virtual machine without shutting it down.

To create and connect a virtual hard disk without shutting down the virtual machine

  1. On the virtualization server, create and attach a new virtual hard disk as follows:

    1. Open Server Manager. Click Start, point to Administrative Tools, and then click Server Manager.

    2. In Server Manager, in the console tree, under Storage, click Disk Management.

    3. After the disks on the virtualization server are listed in Disk Management, in the console tree, right-click Disk Management, and then click Create VHD. The Create and Attach Virtual Hard Disk dialog box appears.

    4. Type or browse to the location on the virtualization server where you want to create the virtual hard disk, and type a name for it.

    5. Type a size for the virtual hard disk. Select MB for megabytes, GB for gigabytes, or TB for terabytes.

    6. Select the format of the virtual hard disk: dynamically expanding or fixed size.

Note

For more information about dynamically expanding and fixed-size virtual hard disks, see Planning for Disks and Storage (https://go.microsoft.com/fwlink/?LinkId=142362).

7.  To create the virtual hard disk and attach it to the virtualization server, click **OK**. After a few moments, the new virtual hard disk is listed in Disk Management with the other disks that are available on the virtualization server, and its state is listed as **Not Initialized**.  
      
8.  To initialize the virtual hard disk, right-click the virtual hard disk, and then click **Initialize Disk**. The **Initialize Disk** dialog box appears.  
      

Note

To perform actions on a disk in Disk Management, you must right-click the small pane on the left, where the disk name, disk type, disk size, and disk state are listed.

9.  On the **Initialize Disk** dialog box, click **OK**. After a few moments, the new virtual hard disk is initialized and listed as **Online**.  
      
10. To create a simple volume on the virtual hard disk, right-click the virtual hard disk, and then click **New Simple Volume**. The **New Simple Volume Wizard** opens.  
      

Note

To perform actions on the volumes of a disk in Disk Management, you must right-click the center pane, where the volume size and volume state are listed. For the new virtual hard disk, the center pane will list the state as Unallocated because the virtual hard disk does not have a volume created.

11. Follow the steps in the **New Simple Volume Wizard** to create the simple volume, assign it a drive letter, and perform a quick format. After a few moments, the volume in the virtual hard disk is listed as **Healthy**, and a drive letter is assigned to it. The virtual hard disk is now available on the virtualization server, and can be accessed to read and write information.  
      
  1. Optionally, copy files to the virtual hard disk so that they can be made available to the virtual machine.

  2. Detach the virtual hard disk from the virtualization server, as follows:

    1. In Disk Management, right-click the virtual hard disk, and then click Detach VHD. The Detach Virtual Hard Disk dialog box appears.

    2. To detach the virtual hard disk, ensure that the Delete the virtual hard disk file after removing the disk check box is not selected, and then click OK. After a few moments, the virtual hard disk is no longer listed in Disk Management.

  3. Attach the virtual hard disk to the SCSI controller of the Imported Virtual Machine virtual machine, as follows:

    1. On the virtualization server, open Hyper-V Manager. Click Start, point to Administrative Tools, and then click Hyper-V Manager.

    2. In Hyper-V Manager, under Virtual Machines, right-click Imported Virtual Machine, and then click Settings. The Settings for Imported Virtual Machine dialog box appears.

    3. In the left navigation pane, click SCSI Controller.

    4. In SCSI Controller, click Hard Drive, and then click Add. A hard drive is added to the SCSI controller and is automatically selected in the left navigation pane.

    5. In the Hard Drive properties, click Virtual hard disk (.vhd) file, and then click Browse.

    6. Browse to the location on the virtualization server where you created the virtual hard disk, and then click Open.

    7. To attach the virtual hard disk to the virtual machine, click OK. The virtual hard disk is now available on the virtual machine, and can be accessed to read and write information.

  4. Optionally, you can detach a virtual hard disk from the virtual machine and attach it again to the virtualization server, without turning off the virtual machine, as follows:

Warning

To avoid losing data, before you detach a virtual hard disk from a virtual machine, stop all applications on the virtual machine that read or write data to the virtual hard disk.

1.  In Hyper-V Manager, under **Virtual Machines**, right-click **Imported Virtual Machine**, and then click **Settings**. The **Settings for Imported Virtual Machine** dialog box appears.  
      
2.  In the left navigation pane, under **SCSI Controller**, click the virtual hard disk that you want to detach from the virtual machine.  
      
3.  In the **Hard Drive** properties, click **Remove**.  
      
4.  To detach the virtual hard disk from the virtual machine, click **OK**. The virtual disk is no longer attached to the virtual machine and can now be attached to the virtualization server.  
      
5.  On the virtualization server, open Server Manager. Click **Start**, point to **Administrative Tools**, and then click **Server Manager**.  
      
6.  In Server Manager, in the console tree, under **Storage**, click **Disk Management**.  
      
7.  After the disks on the virtualization server are listed in Disk Management, in the console tree, right-click **Disk Management**, and then click **Attach VHD**. The **Attach Virtual Hard Disk** dialog box appears.  
      
8.  Type or browse to the location on the virtualization server where you created the virtual hard disk, and then click **OK**. After a few moments, the virtual hard disk is listed in Disk Management with the other disks that are available on the virtualization server, and its state is listed as **Online**. The virtual hard disk is now available on the virtualization server, and can be accessed to read and write information.