How to make iSCSI connections in Hyper-V

Learn the tools and commands you need to make iSCSI connections in Hyper-V - without the iscsicli.exe command line tool.

Microsoft's Hyper-V and iSCSI complement each other like peanut butter and jelly. Hyper-V's low-cost, simplified virtualization works well with iSCSI's easy-to-use data storage. Part of this obvious pairing stems from the fact that iSCSI-initiating software is available natively in Windows Server 2008.

But configuring iSCSI's multiple connections can be problematic. The iSCSI Initiator administrative tool isn't Microsoft's finest example of an easy-to-understand wizard. One alternative is to use the iscsicli.exe command line tool to establish a connection between a Hyper-V server and an iSCSI target to host a virtual machine (VM). In this tip, I'll explore this method and explain the commands you'll need to complete the connection.

Benefits of iSCSI in small virtualization deployments
Combining the cost benefits of Hyper-V and iSCSI -- especially when compared with the Fibre Channel alternative -- makes iSCSI a smart solution for small and medium-sized virtual environments. Many administrators who choose Hyper-V virtualization find themselves using iSCSI to place their servers in storage. In addition, iSCSI has established itself as a viable option to support the performance needs of virtual storage and has proven to perform well even on slow networks and less-than-stellar disks. It worked well even in a worst-case-scenario performance test that I detail in this blog post from Concentrated Technology on clustered Hyper-V. Using the iscsicli command to connect to Hyper-V servers
Now let's discuss how you can use the iscsicli command line tool to set up a simple connection between a Hyper-V server and an iSCSI target. The purpose of this connection is to host a Hyper-V VM. In this example, I explain the commands you need to enable and prepare the iSCSI initiator, connect to targets and prepare those targets for use. This example assumes that you've already completed the necessary tasks at your iSCSI target to create a volume and properly expose its logical unit number (LUN) to a Hyper-V server. In this example, the iSCSI target portal is located at, and the exposed target name is "hyper-v".

First, you need to use three commands to enable and prepare the iSCSI initiator for use. They are the following:

netsh advfirewall firewall set rule group="iSCSI Service" new enable=yes
sc config msiscsi start= auto
net start msiscsi

The first command creates four necessary rules in the Windows firewall for connecting the initiator to a target. The second and third commands here enable the Service Control Manager to set the start mode to Automatic and then start the Microsoft iSCSI Initiator Service. Completing these three tasks makes the host's iSCSI initiator ready for use.

The next step is to connect the initiator to the target. With a single network interface card (NIC) connecting to a single target portal, this process requires only five lines:

iscsicli addTargetPortal 3260
iscsicli listTargets
iscsicli qLoginTarget hyper-v
iscsicli persistentLoginTarget hyper-v T * * * * * * * * * * * * * * * 0
iscsicli bindPersistentVolumes

In the five lines above, the iscsicli command is used to connect the initiator to the target portal at over port 3260. Line two lists the available targets, which in this example is limited to the hyper-v target only. The third command logs into the hyper-v target, followed immediately by the fourth command which instructs the server to make the target persistent. This ensures that the initiator logs back into the target after a reboot. The final command forces the initiator to determine and perpetuate any exposed volumes.

In the fourth line, you'll notice a strange syntax; there are several switches that can modify the persistentLoginTarget command. The first switch, T, ensures that the LUN is exposed as a device, making it a fully functional disk. The 18th value, 0, specifies the number of target mappings the initiator should use when logging into the target. The * characters in the middle are used as placeholders.

The final steps in the process use the diskpart.exe command-line tool to bring a disk online, create a read/write partition and quickly format it for use. You can do so by creating a diskpart script and invoking that script with the command diskpart.exe /s {scriptFile.txt}. Below you'll find a sample script file to get you started, where the end result is a remote iSCSI disk that is ready for Hyper-V to use. This script assumes that your iSCSI disk is disk 1:

select disk 1
online disk
attributes disk clear readonly
create partition primary
select partition 1
format fs=ntfs label=Hyper-V quick
assign letter=Z

If you're familiar with iSCSI connections, you know that the scripts discussed here are fairly basic in how they set up the connection between initiator and target. Most iSCSI implementations use more than one connection over multiple network interface cards. This ensures that multiple paths are available in case one fails or experiences a problem.

If your environment uses multi-pathing and iSCSI's Multipath I/O, you'll need to repeat the addTargetPortal and persistentLogonTarget commands for each NIC and connection. You'll also want to add the logon flag ISCSI_LOGIN_FLAG_MULTIPATH_ENABLED to ensure that Multipath I/O is enabled for each connection. Details about each of these commands can be found in Appendix C of the Microsoft iSCSI Software Initiator Version 2.0 Users Guide.

Read more on SAN, NAS, solid state, RAID