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.


18 Responses

  1. Hi Rob,

    First of all: Hyper-V in Windows 8 is the client hypervisor and not the competitor of VMWare ESX/ vSphere. The counterpart of VMWare ESX/ vSphere is Hyper-V server or the Hyper-V role in Windows Server 2012. Hyper-V server or Hyper-V in Windows Server 2012 has a lot more features than Hyper-V in Windows 8.

    But, let's get back to your conclusion. I think you're a little bit to fast with your conclusion. New technology requires 'new' knowledge. One off them is PowerShell 😉

    With PowerShell you simply can create a VHD file. You can mount this VHD, copy your stuff to it and connect it to your VM. It's just a couple of seconds and everything is done 🙂

    Hope this will help you!

    Regards, Peter Noorderijk

  2. robbeekmans says:

    Hi Peter,

    I'm aware of the differences between the two hyper-V version, I was talking about the client hypervisor running on windows 8. Of course you can use powershell to create a VHD file, mount it and connect to it.. but if it's a VHD or an ISO it's never dynamically.

    I was suprised that Hyper-v (client version) didn't have the ability to opy data from the parent OS, therefore I was seraching for a ISO/VHD emulator to solve this.

    Perhaps my conclusion should have stated that it was about the client version of Hyper-V, I started my blog with adding that differentation… will add it in the conclusion to.

    Thanks for the comment
    Regards Rob

  3. Hi Rob,

    I've installed Win8 to a 70 GB parttion on my HDD. I've created a VHD of the other part off my HDD (actualy it's my second partition). I can use this VHD local and connect it to any VM I want.

    By doing it in this way it's so dynamically as you want 🙂

    • robbeekmans says:

      Hi Peter,

      Thanks again for replying.
      The way you use it surely makes it easier to work with files from outside the guest virtual machine.
      To add files to the VHD I would mount it on the parent side and copy files..unmounting it and providing it to the guest, correct? It's a workable way, not exactly what I was looking for but it comes close.

      I'll work this out and add it to the blog post.

  4. Hi Rob,

    That's correct! BTW the 'PowerShell' part is a nice add-on to your article!

  5. Dan Salmon says:

    Great article. I went through the process and it works great except that the VM has to be offline to add the VHD. That unfortunately takes away a lot of the "dynamic" component of this solution. Have you encountered the same thing or did I miss something?

    Thanks
    Dan

    • robbeekmans says:

      Hi Dan,

      you are right the vm has o be offline and that takes away the flexibility….it's a pity but true

      You didn't miss anything
      geetings
      Rob

    • robbeekmans says:

      Hi Dan,

      I was also looking for a "VMware Worksation share folder-alike" solution but have failed to find one. creating the VHD was the best next thing.
      The best thing would be to have an ISO/VHD emulation running of some kind but I haven't seen one yet.

      So you didn't miss anything, you reached the point that also frustrated me.

      Grettings
      Rob

  6. Eshan says:

    Hi,
    I dont know if anyone else stumbled across this, but I just created an internal virtual switch (rather than an external one) which then allowed me to access any shared drives on the host.

  7. erik says:

    This is what I get (running as admin):

    PS C:Usersmy.name> Format-Volume -DriveLetter D -FileSystem NTFS -Full -Force
    Format-Volume : This command is not supported on x86 running in x64 environment
    At line:1 char:1
    + Format-Volume -DriveLetter D -FileSystem NTFS -Full -Force
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (StorageWMI:ROOT/Microsoft/…age/MSFT_Volume) [Format-Volume], CimExcepti
    on
    + FullyQualifiedErrorId : StorageWMI 7,Format-Volume

    • robbeekmans says:

      Hi Erik,

      I just ran the same commands again on my system, Windows 8 x64 and encountered no errors. In your error it says "This command is not supported on x86 running in x64 environment" could you tell me more about your setup?

      Greetings
      Rob

  8. Anonymous says:

    I get exactly the same error on a new 2012 build I am testing. It only seems to come into effect after bitlocker encryption of the C: & D: drives (The VMs are hosted on the D: drive). Am testing further to see if it really is BitLocker causing the issue (I'm sure it was working before I encrypted the volumes…)

  9. Anonymous says:

    ok disregard my earlier post today re the fomrat-volume x86/x64 error. The HTA that spawns the powershell command is actually running the 32-bit version of PowerShell, hence the error….

  10. 9000revs says:

    Why not share a folder on your Win8 machine and map to it from inside the VMs? I have done that and it works fine.

Leave a Reply

https://tracking.cirrusinsight.com/869c29e2-3a9b-48c5-9232-0b95e7993ae8/controlup-com-pixel-php