Passing usb devices from the main system to a virtual system
Bhyve supports passing usb devices from the host system to the virtual system, so we can connect an external usb device from our FreeBSD host to our Windows virtual system in the Bhyve hypervisor.
To get started, let's install Bhyve and configure it, then proceed to install Windows, open a terminal and enter the command:
pkg install vm-bhyve bhyve-firmware
Then, using any text editor convenient for you, such as ee or mc, add the following lines to /etc/rc.conf:
vm_enable="YES"
vm_dir="/vm"
You also need to add the following line to /boot/loader.conf:
hw.vmm.amdvi.enable="1"
Then run vm init and copy all the templates to the virtual machine folder:
vm init
cp /usr/local/share/examples/vm-bhyve/* /vm/.templates
Now let's create a switch through which the Internet will go to our virtual machines:
vm switch create public
vm switch add public re0
Next, create a configuration file using the template for Windows and a disk image named Win or any other:
vm create -t windows -s 50G Win
Install Windows by running the command, and use any vnc client to connect to the system. The default address is 0.0.0.0.0:5900 or you can look it up using the command:
vm list
vm install Win/Windows-10.iso
After installing the system, we need to see the list of our USB devices:
pciconf -lv
or search for our USB controllers like this:
pciconf -lv | grep USB
pciconf -lv | grep xhci
For example, it will be xhci0@pci0:2:0:0:
To transfer the USB controller to Bhyve, run this command:
devctl set driver -f pci0:2:0:0:0 ppt
Then we see if the device is ready
vm passthru
If so, then configure the virtual machine configuration file. Go to the virtual machine folder and edit it by adding the following line:
cd vm/Win/Win.conf
ee Win.conf
#USB
passthru0="2/0/0"
Then we start the vm start Win virtual machine and connect any external USB device.
Then, to make USB devices available again on the host system, do this:
devctl set driver -f pci0:2:0:0 xhci
If you want passthru to be enabled permanently, add this to /boot/loader.conf:
pptdevs="2/0/0"
BSD is simple and fun!