Forwarding guest ports
You may ocassionally find the need to expose VM ports to the internet, as would be the case when hosting some game server that only runs on Windows. The easiest way to get this done is to just use libvirt-hook-qemu.
Installing the hook
Begin by installing the prerequisites:
sudo xbps-install git makeClone the repo and change directory into it:
git clone https://github.com/saschpe/libvirt-hook-qemu cd libvirt-hook-qemuInstall the hook with:
sudo make installYou will have to restart the libvirt daemon next. Ensure no VM is running, then run:
sudo sv restart libvirtd
Configuring the ports
The file with the port forwarding configuration for the VM can be found at /etc/libvirt/hooks/hooks.json, so you can open it with e.g.:
sudo nano /etc/libvirt/hooks/hooks.json
Make sure to read the comments, they document the configuration pretty well. The usual steps it takes for configuring the ports are:
Replacing
example-guest-vm-name-please-changewith name of the target VM.Replacing
private_ipwith the IP shown by the NIC interface of the VM.Removing or commenting out
source_ip.Finally, setting the
port_maporport_rangeto your desired configuration.
Note that the configuration is only applied when the VM starts, so it is necessary to restart it for changes to take effect.
Uninstalling the hook
If at any point you wish to uninstall the hook, simply do:
sudo make uninstall
This will remove it while leaving configuration files intact.