Search Tools Links Login

Add Virtual Hard Disk to a Hyper-V VM


Adding a new virtual hard disk to a virtual Machine (VM) is something that needs to be done from time to time, with any hypervisor. This process will walk you through adding a new virtual hard drive to a VM with Hyper-V.

What is a VHD or VHDX file?

VHD (Virtual Hard Disk) is a file format representing a virtual hard disk drive (HDD). It may contain what is found on a physical HDD, such as disk partitions and a file system, which in turn can contain files and folders. It is typically used as the hard disk of a virtual machine.

More information on formats and terminology can be found at Microsofts web page describing it.

Why Add a Second (or third or n VHD?

In some instances, it may be come necessary to add a secondary disk to any computer, virtual or physical, in order to expand exist data storage. For example, it is generally a good practice to separate the system drive, the programs drive, and the data drives, in the event that you are working with a "standalone" system.

The idea of separating the three types of data is for, simply put, protection. If the file system on the system drive has an issue, or the drive "crashes" for any reason that render access unusable, the data stored on the other drives in the system can still be accessed by other means (generally speaking!).

In this example, I'll show you the two-step process of attaching a secondary data drive to an existing VM.

The Process

As mentioned, there are two steps in the process of attaching and utilizing a second VHD file:

For this process, our guest VM will be utilizing Windows Server 2019.

Provisioning the VHD

The first part of the process is actually creating the VHD, and attaching it to the virtual machine. This is a fairly straightforward process, and if you follow the steps, you should be able to perform this task with no issue.

  1. On the Hyper-V host machine, open Hyper-V manager.
  2. In the left pane, select the hostname of the Hyper-V host. Virtual machines associated with that host will appear on the right.
  3. In the right pane, right-click on the virtual machine for which you would like to add a disk. Select Settings from the context menu.
  4. Now that you're in the settings for the virtual machine, browse down and select SCSI Controller from the list in the left pane.
  5. To add a hard drive, select Hard Drive from the refreshed list in the right pane, then click the Add button. This will start the new drive wizard. At this point you can create a new drive, or attach an existing drive. For this exercise, we're adding a new drive, so click the button labeled New.
  6. For disk type, you can choose what type of VHD file you'd like to use. For production VMs, it is recommend to utilize Fixed Size, but since this is a lab or test machine, we can use Dynamically Expanding as the disk type. An explanation of each type is given in the wizard screen. Once you select a disk type, click Next.
  7. On the Specify Name and Location screen, make sure to provide a meaningful name for the VHD file. It's a good idea to include the name of the VM, as well as the role of the disk in the name, so it'll be easier to find if you need to locate it later. After you are satisfied with the name and location, click Next.
  8. The next screen is the Configure Disk screen. There are three options, but we are interested in create a new, empty disk, so choose the first option. You'll also need to specify a size. In this demonstration, I am using 10 GB. Click Next when you are ready.
  9. Finally, you'll reach the Summary screen. If everything looks good, click Finish, or go back and make any necessary changes.
  10. After clicking Finish, you'll be brought back to the screen which shows the settings of the VM, with the addition of your new virtual hard disk. Click Ok to commit the changes.

If you'd like to perform the same function as the above steps with PowerShell, the following code will do the job. Just be sure to open PowerShell in elevated mode (right-click, run as administrator). Note: adjust the parameters to fit your situation.

import-module Hyper-V
New-VHD -Path s:\Hyper-V\MyNewDisk2.vhdx -SizeBytes 10GB -Dynamic
Add-VMHardDiskDrive -VMName Test001 -Path s:\Hyper-V\MyNewDisk2.vhdx

Configure the Disk Inside the VM

The next part of the process entails configuring the disk for use inside the VM, so the Windows operating system can utilize the fresh, raw disk space. This is also a necessary task when you add a hard drive to a real, physical computer.

  1. After logging in to the VM, either via RDP or console, open the Computer Management applet.
  2. In the left pane, select Disk Management under the Storage heading, in the management tree. At this point the right pane will refresh, displaying the current disk configuration of the VM. You'll notice your freshly added disk in the lower section. Note that it will have a status of being an Unknown disk, in an Offline state, with no allocated space. Let's fix that!
  3. Right-click the disk name (Disk1, in the example), and select Online. This brings the disk to a ready state for configuration.
  4. Right-click the disk name again, and select Initialize Disk. Accept the defaults (unless you have a special use case), and click Ok. This step will define the style or format of the partition to be used. After clicking the Ok button, you may notice available space of the disk shrink slightly. This is normal, as the file system itself utilizes some of the space.
  5. Next, right-click on the area where the size and Unallocated appears, and select New Simple Volume. Click through the wizard, accepting all the default values. There are other options in there, but for this basic example, we'll just use the defaults.

To accomplish this with PowerShell, you can run the following code in an elevated PowerShell session:

$DiskNumber = (Get-Disk | Where-Object IsOffline –Eq $True).Number
Initialize-Disk -Number $DiskNumber
New-Partition -UseMaximumSize -DiskNumber $DiskNumber | Format-Volume -FileSystem NTFS -NewFileSystemLabel MyNewDrive

Note: this code assumes there is only one drive that is not configured!

Summary

Your new virtual disk should now be ready for use inside the virtual machine. You can verify its presence by open Windows explorer inside the virtual machine, where you can view the list of attached hard drives.

About this post

Posted: 2020-05-13
By: dwirch
Viewed: 2,472 times

Categories

Hyper-V

Powershell

Windows

PowerShell Code Cache

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.