OK, so this is a little more than making a Chromecast work in a VM but this is what spurred me to this solution. As I do all my primary computing in virtualisation now I encountered an issue; I couldn’t reach my Chromecast from within a basic virtualised network. I use Qemu KVM, libvirtd and Virt-Manager to handle my VMs on a Linux host but the following principles are applicable to other platforms with different methods.
In a virtual NAT, all network traffic for your VM routes through your host machine using NAT. NAT has a number of advantages of it’s own, chiefly that it allows a large number of devices to utilise a single internet address and that it can make certain kinds of malicious traffic easier to block. Out of the box, a computer behind a NAT is harder to reach unless it’s trying to communicate with you first. It’s also very easy to deploy a virtual NAT adapter in your VM, it’s pretty much the universal default and will allow you to contact the host machine directly over the network adapter; bridge mode networking will not. I use the NAT adapter so that I can access scratch partitions on the host’s physical drives to share data as needed.
Bridge Mode Networking
Bridge mode networking is an extremely useful tool in a virtualised environment. Effectively it allows a VM to speak to your network in a direct manner, as it would if it had a true physical interface (you could assign it one of these with hardware passthrough too if you desired but that’s outside the scope of this). This is by far the simplest way you can get a Chromecast running in a VM as well as resolve some other problems. While I haven’t attempted to quantify the latencies, I feel that in general my network performance, particularly in games. However, a bridge adapter can only reach your host if the host has a separate network adapter to the one your bridge is on and both are connected to the same subnet via a switch or there is a route between them via router.
Now, neither of these adapters suited my needs perfectly. This host machine, like most, only has one physical network adapter at present. Fortunately, virtual adapters come at the extremely competitive price of zero dollars. The simple solution is to use them both in conjunction. In order for this to work, you’ll need DHCP or a static IP for the bridge adapter that allows it to communicate on your network – this is probably fine straight away in most home network setups. Simply add one of each, the bridge adapter will work for full communications on your LAN and keep internet latencies down. The NAT adapter will allow you to still network to your host machine, you can omit this if you do not need any network contact between the two (this is a nice, secure option if not needed). The extra bit of magic, for a Windows guest at least (I’ve not encountered related issues under linux), is to set the adapter priority so that network communications favour the bridge adapter for all cases bar when it must contact the host machine.
Implemented in Virt-Manager
For those with a similar setup to mine, here’s roughly what it looks like. This is presupposing some knowledge on your part. Feel free to query me for clarifications and I’m happy to update my guide with useful additions or respond.
- In virt-manager, open the desired virtual machine by double clicking on it. I’m using my Windows 10 VM here. Click the “i” button to access the virtual hardware configuration.
- Click Add Hardware at the bottom left and create a Bridge Adapter. Be aware that this adapter must be able to reach DHCP on the physical adapter you select or be given a suitable static IP address in the guest OS.
- For a Windows guest you should configure adapter priority to head off unexpected behaviours when Windows selects which NIC to forward packets to. In Windows 10, this is under Control Panel>Network Connections. In my case I want my bridge adapter favoured for all communications not destined for the VM host machine, so I right click the bridge adapter>Properties>Select Internet Protocol Version 4>Properties>Advanced and set the Interface Metric to 10. Repeat for the NAT adapter, set it to 20. Lower numbers are higher priority.
That’s pretty much it. There is further thought that you may want to put into this configuration depending on your exact requirements, feel free to comment any questions and I’ll try to get back to you.