Webinar archive (source DABCC)

13 October 2012

Microsoft Hyper-V Howto copy files to your Guest vm?

Howto: Copy files from your workstation to your Guest vm

Powershell and Hyper-V go hand in hand, learn more about an easy way to create a VHD file and connect it to your virtual machine in this blog.

Where I came from

A few weeks ago I decided to start with Microsoft Windows 8, the world is changing and I thought I better change along. With Microsoft Windows 8 comes Hyper-V, Microsoft answer to VMware ESX/vSphere.

Sharing folders

With Microsoft Windows 7  I ran VMware Workstation and I used to share several folders on my laptop to share ISO or common installation files with the virtual machines. It's handy when you play around with new stuff and have to install virtual machine regulary. You don't have to download the same stuff over and over, it's easier to have DotNet stuff and stuff like that easy accesible for your virtual machine.

Hyper-V

I was kinda shocked to find out that Microsoft Hyper-V for Windows 8 lacked this kind of option, to share folders to a guest virtual machine. To people using Microsoft Hyper-V on Windows 8 for a longer time it might not have been a suprise but to me, using VMware products since like ever it was a suprise. Of course the Windows 8 version of Hyper-V is nothing compared to the server version, but then again VMware also has a client version and a server version of it hypervisor. So here I'm talking about the client version, with VMware it's Workstation and with Hyper-V, it's Hyper-V for Windows 8.

In the menu you can find a way to copy clipboard text to the machine, another feature not so greatly implemented. The clipboard option is to be found under the clipboard menu.

You can connect a ISO file to the guest virtual machine, this will be used mostely when you have to install a certain product. Connecting a ISO file is done in the Media/DVDDrive option from the menubar

Getting data

So the first thing I needed to figure out is how do I get data to my guest virtual machine when I can't connect my guest to the network. I have some virtual machine that run DNS/DHCP and other services I don't want to expose to outside networks. I usually create a internal VLAN'd network where my servers run freely without the chance of interrupting any network services  at the office or at customers.
So I still was stuck with getting the data, no way Hyper-V let me get my data.
After a few hours of frustration I decided to create an ISO of the folder I use mostly for standard software. This ISO I connected to the guest virtual machine and I was able to finish my work.

ISO Emulator

Still this didn't satisfy me, I didn't see myself creating an ISO everytime I needed a file... that's not gonna work. I went to search  on the Internet for a solution to have an ISO but not having to create one every time you want to add files. I was looking for a dynamical ISO or like @AndrewCooke said on Twitter a "Writeable ISO emulator".

@PeterNoorderijk pointed out, as you can read in the comments, that we live in the VHD/PowerShell era. He's right of course about that, these days it would be more logical to add a VHD to a guest virtual machine containing the data you need. 

How would this work?

With an ISO emulator you would assign a folder to be read by a service, that service would provide an virtaul ISO file to the guest virtual machine. The virtual machine read the ISO as is and will handle it like a normal ISO file. Everytime you add or delete files from the folder you would see the difference when you refresh your drive content in the virtual machine.

VHD and Powershell 

I wanted to see how easy it is to create a VHD, copy data on it and connect it to the guest virtual machine. I could have done that manually but I perfer to use Powershell for this. 

Create a VHD

With Microsoft Windows 8 it logical to use VHD for the disks are in VHD format. So (inspired by @PeterNoorderijk his comment) I created a VHD, copied files to it and attached it to the guest virtual machine.
Creating a VHD with Powershell is easy, so I started of with creating a 20GB VHD file.

The VHD file is created in the C:\VM folder on my local harddisk, as you can see below.

The VHD file is not mounted automatically and therefore not visible as a drive at this moment.

To copy data to the VHD file you need to mount it...but again PowerShell comes to the rescue.
After we mounted the disk we  have to initialize it..So let's see which disks we have


 So it turn out we have to initialize Disk 1. To initialize the disk, type the command as shown above "Initialize-Disk 1 -PartitionStyle MBR" .
Next job is to create a partition so that we can drop some data on the disk.

The last job is to format the volume.

The Format process is shown in your PS screen.

Of course this could be written in a more fashionable way by a real scripter but the proces is the same.
the disk is ready now and we can drop data on it..
After you copied the data to the disk you can dismount it... with PowerShell of course.

The disk is not visible anymore in Explorer.
Now we can connect it to the Guest in Hyper-V..again with Powershell.


If we look at the settings of the virtual machine we see this VHD is added.


This could be made as a script to connect/disconnect the file and make data available to virtual machines.

.....More Options

Is there another workaround beside connecting an ISO to the virtual machine? Yes there is another option that you can use. While searching the Internet I came along a blog Manesh Karunkaran.

http://geekswithblogs.net/manesh/archive/2010/05/26/share-files-and-internet-between-guest-os-and-the-host.aspx

He has written an excelent post about how to connect an internal network to share a folder to your virtual machine. Actually it's more like you access a shared folder from your virtual machine, a folder that is accesible by the network. It's nothing like with VMware, which is a connection purely between the virtual machine and the workstation. This connection is over the network, so the share is visible by anyone in the same network as you are, only security standing between them and your data.


Conclusion

I've been looking on the Internet for ways to access data when working in my guest virtual machine from my parent virtual machine. Three options are possible;

1. Create an ISO with the files you need;
2. Create an Internal network connection and create a network share of the folder where your data is;
3. Attach a VHD file to the guest VM.

I have to agree with @Peternoorderijk that the VHD option is by far the easiest option available. I still (perhaps because I was used to it) miss the VMware Workstation option but this one is workable for me.
I just have to create a script to connect and disconnect the disk, simple as that.