Managing Hyper-V From the Command Line (Part 7)

by [Published on 20 Dec. 2012 / Last Updated on 20 Dec. 2012]

This article will continue the discussion of managing Hyper-V from the command line by adding additional CPU cores and an extra virtual hard disk to a virtual machine.

If you would like to read the other parts in this article series please go to:

Introduction

In my previous article in this series, I showed you how you could create a brand new virtual machine from the command line. Although creating a new virtual machine is easy, hardware allocation for the new virtual machine sometimes isn’t. So far we have created a new virtual hard disk file and connected it to the new virtual machine, but the virtual machine is still using default hardware allocations. In this article, I want to show you how to fine tune this virtual machine’s configuration to make it usable.

Before I Begin

Before I get started, I want to quickly mention for the benefit of anyone who is just joining this series that the commands that I will be discussing are not native to Hyper-V. They are a part of a Hyper-V management library. Furthermore, this library must be imported into PowerShell each time that you plan on using any Hyper-V related commands. The import command is:

Import-Module “C:\Program Files\Modules\Hyperv\Hyperv.psd1”

The full instructions for downloading and installing the Hyper-V management library are provided in Part 1 of this series.

Incidentally, manually downloading the Hyper-V module for PowerShell and manually importing it will not always be necessary. The Hyper-V module is going to be included in Windows Server 2012.

What is Left?

When we initially created the virtual machine, we allocated 2048 MB of memory to it. After that, we created a virtual hard disk file and told the virtual machine to use it. Finally, we provided the virtual machine with a virtual network adapter. Technically the virtual machine could stand on its own right now. Even so, I want to walk you through the process of allocating a bit more hardware. For the purposes of this article, we will add an extra virtual CPU to the virtual machine and we will add a secondary virtual hard drive.

Virtual CPUs

Even though we already know that the virtual machine that we created was automatically provisioned with a single virtual CPU core, let’s pretend that we don’t know how many virtual CPUs have been allocated. The easiest way to determine virtual CPU allocation is to enter the Get-VMCPUCount command.

If you look at Figure A, you can see that I have used this command in two different ways. First, I entered the command by itself, which caused Hyper-V to return virtual CPU allocation information for every virtual machine on the host. Next, I entered the command again, but this time I specified the name of one specific virtual machine. This caused Hyper-V to return virtual CPU allocation information solely for that one virtual machine.


Figure A: The Get-VMCPUCount command returns information about virtual CPU allocation.

Regardless of whether we specify one virtual machine or look at all of the virtual machines, several key pieces of information are returned. First, take a look at the Quantity column. This column reflects the number of virtual CPUs that have been allocated to each virtual machine. Another column worth paying attention to is the Cores / Socket column. This column tells you how many physical CPU cores the server has for each CPU socket. Finally, the Socket Count column tells you the number of physical CPU sockets that the server is equipped with.

So now that we have determined the current virtual CPU count, let’s go ahead and set the number of virtual CPUs that are assigned to the virtual machine to two. You can allocate up to four virtual CPUs to a virtual machine, but the virtual machine’s operating system must support using the number of virtual CPUs that you provide it with. Furthermore, you cannot exceed the total number of physical cores that are installed in the server.

You can modify a virtual machine’s CPU allocation by simply specifying the name of the virtual machine and the number of CPU cores that you want to assign. For example, if you wanted to assign two virtual CPU cores to a virtual machine named Windows Server 8 Beta then you would use the following command:

Set-VMCPUCount “Windows Server 8 Beta” 2

If you look at Figure B, you can see that when I enter this command, the Quantity count changes from one to two.


Figure B: The virtual CPU quantity changes from one to two.

Technically this achieves the objective of increasing the number of virtual CPUs that are allocated to our new virtual machine. Even so, there are a few extra parameters that can be used along with this command that I want to mention. I won’t go into all of the parameters because they are listed in the documentation and some of them don’t really do anything too exciting. However, a few of the more useful parameters that you might want to use are:

Limit – The Limit parameter lets you specify the maximum amount of time that a virtual machine is allowed to use a physical CPU. The default limit is 100% usage.

Reservation – The Reservation parameter lets you reserve a percentage of CPU time solely for a specific virtual machine. By default the reservation is set at 0%.

Weight – The weight parameter lets you set a relative weight that affects how much CPU time a virtual machine will receive. The default weight is 100.

Adding an Additional Virtual Hard Disk

The last thing that I want to do to the newly created virtual machine is to provide it with an additional virtual hard disk. The procedure for doing so is very similar to the procedure that we already used to create the virtual machine’s primary virtual hard disk.

The first step in the process is to actually create the virtual hard disk file. This can be done through the now familiar New-VHD command. You can see an example of the command below:

New-VHD –VHDPath “F:\Windows Server 8 Beta\SecondVHD.vhd” –Size 16106127360

Next, we have to assign the virtual machine to a variable name ($VM in this case), and then attach the virtual hard disk to a virtual disk controller on the virtual machine. The two commands that will be required to complete this operation are:

$VM=Get-VM “Windows Server 8 Beta” –Server Hyper-V

Add-VMDisk –VM $VM –ControllerID 0 –LUN 0 –VHDPath “F:\Windows Server 8 Beta\SecondVHD.vhd”

You can see these commands and their result in Figure C. If you need any help understanding what these commands are doing then be sure to either check out Part 6 of this series or the documentation for the PowerShell module for Hyper-V.


Figure C: You can create and attach a new virtual hard disk file from the command line.

Now that we are done, take a look at Figure D. This is the virtual machine’s Settings page, as seen through the Hyper-V Manager. You will notice that the newly created virtual machine now has two virtual processors, a VM-Bus network adapter (which we created in Part 6), and two virtual hard drives.


Figure D: The virtual machine is now fully provisioned.

Conclusion

Now that I have shown you how to create and customize a virtual machine from the command line, there is one topic left that I want to discuss. In Part 8 I plan to conclude the series by talking about virtual machine snapshot management.

If you would like to read the other parts in this article series please go to:

Advertisement

Featured Links