Connect to a removable drive in a Hyper V Virtual Machine (Windows User Series)

Introduction

This post is based on using Windows 10 on the host and Windows Server 2012 R2 on the guest (Virtual Machine), but on Windows 7, 8 and 8.1 things work similarly.

People coming from a VMWare environment might wonder what this is all about. What is the point, right?

I must admit, sharing or mapping a drive in a Hyper V virtual machine (a.k.a. “guest”) is not such a nobrainer as in VMWare. But no worries, with a little bit of effort it can be done easily.

So, what is the problem?

Well, I mount a VeraCrypt container on my host machine, and want to see the drive in my guest machine.  Or plug in a USB pen drive or removable harddisk, and use it on the guest machine.

Common ways to connect to a (removable) drive

I did it my way

I was looking for a more convenient way, that is, one I have to set up only once, and does not depend on the RDP protocol.

In my opinion a networkshare was the way to go.

But there were a few challenges:

  1. Windows tends to “forget” a shared drive or folder once this drive has been gone (usb media removed or VeraCrypt container unmounted). This is a problem, because then you have to configure the removable drive for sharing every time it is mounted.
  2. I did not want to do a round trip to my router, I want it to be a host/guest only party.

But I managed to get it working, and in the next paragraph I will tell you how.

myway3

Steps to do it “my way”

Again (see my previous post), the magic word to get this working is .. a junction!

Alright, having said that, where do we start?

We need set up a few things;

  1. An internal network connection between guest and host OS.
  2. A junction pointing to a removable drive letter on the host.
  3. Share that junction to one or more Windows users on the host.
  4. Create a mapped network drive on the guest.

Important note : if you want to map more than one drive on the host, you have to edit the hosts file on the virtual machine. After Step 4 below, I will explain how to do this.

Are you ready? Let’s roll up our sleeves and get going!

Step 1 – Set up an internal network connection between guest and host OS

1a – Create a Virtual Network Switch

Start Hyper V Manager, and select Virtual Switch Manager from the Actions pane.

WinS01E02_Step1a1

Create a new virtual network switch of type Internal.

WinS01E02_Step1a2

Give the Virtual Switch a name (I couldn’t think of a better name than “Internal”), leave other options as they are by default and press OK.

WinS01E02_Step1a3

1b – Add a network adapter to the virtual machine and configure it to use the Internal Switch

Now, open the Settings for the virtual machine.

WinS01E02_Step1b1

Choose Add HardwareNetwork AdapterWinS01E02_Step1b2

Configure the network adapter to use the Virtual switch Internal (or any other name you gave it). Press OK.

WinS01E02_Step1b3

1c – Find the Internal IP Address of the host

Still on the host, start a command prompt and type ipconfig. 

Then search for an Ethernet Adapter vEthernet (Internal) and write down the Autoconfiguration IPv4 Address. You will need it in a few minutes.

WinS01E02_Step1c1

 

Step 2 – Create a junction pointing to a removable drive letter on the host

Insert your USB storage media or mount a VeraCrypt File container.

For the example I use drive P on the host. Please replace P everywhere with the driveletter of your removable media.

Now find a folder on a fixed drive, e.g. where you store your Hyper V Virtual Machines. In the example I will use E:\HYPER_V\SHARES

Start a command prompt and type

mklink /j “E:\HYPER_V\SHARES\P” “P:\”

 

Replace the foldername and the drive letter with your values.
The output of the command window should confirm that the junction was made:
WinS01E02_Step2a

People who have read my previous post in my Windows User Series might remember that I preached that it is not recommended to make junctions pointing to removable drives. Well, this post proves that there is at least one exception to this rule of thumb!

Step 3 – Share that junction to one or more Windows users on the host.

Open the Properties of the junction E:\HYPER_V\SHARES\P and select tab Sharing. Press Advanced Sharing.

WinS01E02_Step3a

 

 

Select Share this folder and press Permissions.

WinS01E02_Step3b

Remove Everyone from Share Permissions and add at least one user from the host. Give this user the appropriate permissions.

Tip: if possible, use a (special) account from the host of which the password never changes, for reasons I will tell below.

WinS01E02_Step3c

 

Step 4 – Create a mapped network drive on the guest

Now, in Hyper V Manager, start the Virtual machine.

Once the Virtual machine is started, log in, start a Windows Explorer and choose Map Network Drive from the menu.

Choose a Drive (can be the same letter as on the host, but this is not necessary).

For Folder, type \\<Host Internal IP Address>\<Share Name>

(or \\<Host name from hosts file>\<Share Name> if you have modified the hosts file)

Check Reconnect at sign-in (if you think this is convenient).

If you do not want a permanently mapped drive, you could create a cmd file with a net use command, and execute it when you need to map the drive. 

Check Connect using different credentials.

WinS01E02_Step4a

In the Windows Security screen, enter a username of the host that has permissions on the share you created earlier.

Note: to indicate that you use a username of the host machine, type the host computer name and a backslash in front of the user name .

If you check Remember my credentials, the credentials are stored in the Windows Credential Manager of the guest (Virtual Machine), so you don’t have to provide them every time you access the mapped drive. But don’t forget, that when you change the password from the account on the host, you have to change the saved password also in the Credential Manager of the guest (accessible through Control Panel). When you are using names from the hosts file instead of the IP address, the same credentials will even occur multiple times, e.g. for “host1”, “host2”, etc! Therefore, it is best to use a (special) account from the host of which the password never changes.

WinS01E02_Step4b

That’s it!

WinS01E02_Step4c

Remember, as soon as you plug in the removable drive on the host, it will be available immediately in the Virtual Machine.

If you get this message, you know that the removable drive is not available on the host (yet). You have to plug it in first.

WinS01E02_Step4d

Appendix – Only needed if you want to map more than one drive: edit the hosts file.

Following steps 1 to 4 you can map only one drive, because if you try to connect a second drive, your will get a “Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed… ” error as shown below.

WinS01E02_Limitations

To map multiple drives, you have to do a little trick so that your guest thinks you are connecting to different computers ..

Start Windows explorer on the guest

Go to your virtual machine, start Windows Explorer, and go to the folder C:\Windows\System32\drivers\etc.

 Copy and adjust the hosts file

Copy the file “hosts” (it does not have a file name extension) to a different temporary folder, e.g. your desktop folder.

Open the file with a text editor, e.g. Notepad and add the Internal IPv4 adress of the host, followed by a space and then a name. The trick really is to add multiple lines with the same IP adress but a different name. I just keep it simple by numbering the names but you can do whatever you want, as long as the names are different.

169.254.151.11 host1
169.254.151.11 host2

Finally your hosts file should look similar to the one below (I have added a few more so my hosts file is “future proof”):

WinS01E02_Step5b

Save and close the file and copy it back to the original folder C:\Windows\System32\drivers\etc. Confirm to replace the existing file.

Now walk trough Steps 2 to 4 for each network drive you want to map. Instead of using the IP address, use a name from the hosts file, for each mapped drive a different name, and you will not get the “Multiple connections” error.

Here you will see an example of the endresult: I mapped drives H, P and Z using names host1, host2 and host3.

WinS01E02_Step53

Conclusion / wrap up

In this blog post, I have explained how to map a removable drive on the host operating system as a network drive in a Hyper V Virtual Machine.

By using a junction on a fixed drive on the host, a Windows problem forgetting that a removable drive was shared, can be circumvented.

By using an Internal network connection, there is no router round trip, nor a dependency to use a remote desktop connection to start up the guest. Also multiple virtual machines could have access to the same drive at the same time.

Finally by changing the hosts file, you can connect to multiple (removable) drives on the host computer.

(c) 2016 hansmichiels.com – Do not steal the contents – spread the link instead – thank you.

Hans Michiels

Hans Michiels

Hans is an Independent Business Intelligence and Datawarehouse Consultant & Microsoft SQL Server Consultant, working in the Netherlands. He has been working in the software industry since 1996, with SQL Server since the year 2001, and since 2008 he has a primary focus on datawarehouse- and business intelligence projects using Microsoft technology, using a Datavault and Kimball architecture. He has a special interest in Datawarehouse Automation and Metadata driven solutions. * Certified in Data Vault Modeling: Certified Data Vault 2.0 Practitioner, CDVDM (aka Data Vault 1.0) * Certified in MS SQL Server: * MCSA (Microsoft Certified Solutions Associate) SQL Server 2012 - MCITP Business Intelligence Developer 2005/2008 - MCITP Database Developer 2005/2008 - MCITP Database Administrator 2005/2008

More Posts

Leave a Reply

Your e-mail address will not be published. Required fields are marked *