Vagrant and Hyper-V: Don't do it! vagrant hyperv

Vagrant and Hyper-V: Don’t do it!

I’ve used Vagrant since 2015 in combination with Virtualbox for creating development machines. Recently however I’m experiencing more issues with Virtualbox. For example CPUs getting stuck when assigning multiple CPUs to a VM and issues with auto adjusting the guest resolution when resizing the VM window. These annoyances drove me to try out Vagrant with Hyper-V (running an Ubuntu 21.04 guest on a Windows 11 host). In this blog post I’ll describe my experiences. In summary, it did not make me happy. A lot of things which work out of the box with Vagrant and VirtualBox require effort to get working in Hyper-V. Not only that but several alternative solutions are required outside of Hyper-V because of lack of features. I think I should try VMWare next to see if it will provide a better experience. You can download my Vagrantfile and provisioning script here.

Vagrant and Hyper-V: Don't do it! vagrant hyperv

Overview

I encountered many issues when trying out Vagrant with Hyper-V. When starting out I noticed, Vagrant needs to run as a user with administrative privileges due to the way Hyper-V works. You need to pick a specific image from the Vagrant cloud which supports Hyper-V (my usual VirtualBox images were not available in Hyper-V format).

Vagrant and Hyper-V: Don't do it! vagrant hyperv ubuntu boxes
Find a Hyper-V Vagrant box

There are also some other things to mind.

  • Networking
    Vagrant does not know how to configure Hyper-V networking. You have to do this yourself.
  • Sharing folders
    Several options are available from Vagrant to configure shared folders. SMB works for some people but not me. I used SSHFS
  • Disk
    The disk is slow, unless…
  • Clipboard integration
    Clipboard integration is lacking in Hyper-V Virtual Machine Connection. My alternatives are using XRDP or VNC.
  • Autoscaling the guest when you resize the VM Window
    It would be nice if changing the window size would change the guest resolution. I used VNC to achieve this.

Note that most of the solutions to the challenges I encountered are just workarounds for things not working out of the box. E.g. VNC instead of the Hyper-V Virtual Machine Connection application and sshfs instead of smb. These workarounds do require you to install additional tools on your host and guest.

Networking

Based on this, in order to get networking (specifically internet access) inside the guest working, you need to create a virtual switch and link that to the network adapter which has internet access (this is troublesome since sometimes I work using a wire and sometimes wireless). After you have done that, you need to explicitly indicate every time you run “vagrant up” which virtual switch you want to use. It could help to create 2 virtual switches linked to different adapters for wired and wireless working. Also a virtual switch is linked to only a single network. Vagrant allows you to select a single virtual switch. Due to this setup, you cannot easily create more complex network configurations from Vagrant in Hyper-V (e.g. have the VM linked to both an internal network and an external network). This is a limitation of Vagrant and not of Hyper-V.

Vagrant and Hyper-V: Don't do it! virtual switch manager
A virtual switch links a VM to a single adapter or network. You need to manually define it and Vagrant allows you to choose one for the creation of the VM.
Vagrant and Hyper-V: Don't do it! select your virtual switch
You need to select your virtual switch everytime manually when you do ‘vagrant up’

Shared folders

Mounting shares using smb is described here. I did not get this working however on Windows 11. There are quite a lot of variables involved and thus things that can go wrong. For example the Windows firewall configuration, virusscanner, whether you are using a local or a remote Windows account, the SMB protocol version used, the way you supply credentials in the Vagrantfile or in the command line, the way you supply the username/domain etc. Some people have reported they had this working for them on their machine but that experience did not appear to be reproducible for me. This indicates you can not trust this works for everyone and you are probably better off not using it altogether (especially if you want to share the machine) to avoid issues.

A good alternative to smb I found was the vagrant-sshfs plugin. This plugin however did not work for me out of the box. I encountered challenges with the sftp-server which needs to be present for the plugin to work. In order to get it working, I installed openssh inside MobaXterm (which needs to run with Administrative privileges) and ran Vagrant from within MobaXterm. You can then use the vagrant-sshfs plugin to mount a local folder. 

Vagrant and Hyper-V: Don't do it! 2022 02 15 10 23 47
Using the vagrant-sshfs plugin

Why does this work and smb not? Vagrant takes care of the SSH part so there is no host authentication integration required. This removes several of the previously mentioned variables.
There are several other ways to move files around

  • Use a remote resource. For example GitHub, Google Drive, OneDrive. You can use it in your VM. You might however not want to provide private credentials inside a VM, especially if it is reusable. 
  • Copy/paste files using a remote desktop solution such as for example RDP. RDP allows sharing the clipboard and copying files also works.
  • Mount the virtual disk image on the host (when it is not in use)
  • Use a (SSH, FTP or similar) server inside your client and connect from the host with a client which has a GUI which allows transferring files easily (for example WinSCP).

Disk

From here“A pretty substantial portion of the time spent running vagrant up is spent cloning the virtual hard drive. A faster way is to use differencing disks with Hyper-V. You can also turn on virtualization extensions, which allow nested virtualization within the VM (i.e. Docker with Hyper-V containers). Here are the lines to add to your Vagrantfile to add these features:”.

config.vm.provider "hyperv" do |h|
  h.enable_virtualization_extensions = true
  h.linked_clone = true
end

Clipboard integration

Sharing a clipboard is not straightforward. There is a ‘type clipboard contents’ option when using the Hyper-V Virtual Machine Connection application, this however does not always work and it does not allow you to copy anything but plain text.

Copy/paste. What can go wrong?

When you install XRDP in your guest (VirtualBox provides this on its own but this is not out of the box for Hyper-V), you can use the Remote Desktop Connection application from Windows to connect to your VM. This can be used to achieve clipboard sharing and allows you to copy/paste files.

Auto adjusting the guest resolution

Auto adjusting the guest resolution based on the host window size is not supported by Hyper-V Virtual Machine Connection application. You can statically set the resolution (e.g. here) but that is a manual task and requires a reboot to become effective. I also did not manage to get guest autoscaling working using XRDP. Also XRDP does not work out of the box if you are for example already logged in using the Hyper-V Virtual Machine Connection application.

I decided to go with VNC. Mind that this does not work out of the box with Ubuntu 21.10 (and probably above) due to its use of Wayland (read here). You can also encounter several other challenges, for example with clipboard copy/paste (see here).

I did the following on Ubuntu 21.04:

sudo apt install tigervnc-standalone-server tigervnc-xorg-extension tigervnc-viewer

Next for the user you would like to have access to (you can login with vagrant ssh):

vncpasswd
vncserver -localhost no

Now auto adjusting the guest resolution when resizing the VNC window works (connect to port 5901 on the guest with a VNC client). You first need to determine your guests IP though. With VirtualBox you usually use localhost and some port forwards but not with Hyper-V. There you need to determine the IP every time since it changes because it does DHCP via the network interface you have chosen.

Vagrant and Hyper-V: Don't do it! 2022 02 28 13 29 51
IP is variable

Having the IP fixed by configuring your DHCP server is also not straightforward because the MAC address assigned to the virtual switch is dynamic.

Vagrant and Hyper-V: Don't do it! 2022 02 28 13 32 41
Why can I not set the MAC address per virtual switch?

Finally

In my experience, the Vagrant / Hyper-V combination is lacking when comparing it to Vagrant / VirtualBox and I cannot recommend using it. Not only Vagrant is to blame for this. Hyper-V lacks many of the VirtualBox features, even though Hyper-V performed better and appeared more stable at first glance. You can work around them mostly but this requires effort and often also requires installing additional software on your host machine (think MobaXTerm, WinSCP, VNC client) or on your guest (VNC server, XRDP). This contrasts with the idea of isolation of the host and the guest machines with the only intersecting part being the virtualization software. 

The lack of features in the Vagrant / Hyper-V solution is probably an indication of how large the community is which uses this setup (not large). It is not a good thing to be on an exotic configuration.

Even though I would prefer Vagrant / VirtualBox, this combination has not been stable for me lately so I was required to look for alternatives. I’ll try VMWare Player next and will of course post my experiences.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.