Howto copy files to your Guest Microsoft Hyper-V   Leave a comment


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.

Posted 17/10/2012 by Petri in Hyper-V

Leave a comment