Add the possibility to forward ports from host to guest

This commit is contained in:
Yannick Mauray 2020-06-24 15:42:37 +02:00
parent b7f8e859dc
commit b7d7944d88
No known key found for this signature in database
GPG Key ID: FECAE5D7CC370EF1

View File

@ -150,6 +150,22 @@ enable_usb_passthrough() {
fi
}
function enable_port_forwards() {
local HOSTFWDS=""
# Have any port forwards been requested?
if (( ${#port_forwards[@]} )); then
SEPARATOR=""
for FORWARD in "${port_forwards[@]}"; do
HOST_PORT=$(echo ${FORWARD} | cut -d':' -f1)
GUEST_PORT=$(echo ${FORWARD} | cut -d':' -f2)
HOSTFWDS="${HOSTFWDS}${SEPARATOR}hostfwd=tcp::${HOST_PORT}-:${GUEST_PORT}"
SEPARATOR=","
done
fi
echo ${HOSTFWDS}
}
function vm_boot() {
local VMNAME=$(basename "${VM}" .conf)
local VMDIR=$(dirname "${disk_img}")
@ -370,6 +386,12 @@ function vm_boot() {
echo " - ssh: All ports for exposing ssh have been exhausted."
fi
local HOSTFWDS=$(enable_port_forwards)
if [ ! -z "${HOSTFWDS}" ]; then
echo " - ports: forwarding requested ports"
NET="${NET},${HOSTFWDS}"
fi
# Find a free port for spice
local SPICE_PORT=$(get_port 5930 9)
if [ ! -n "${SPICE_PORT}" ]; then