find free port for spice (#29)

allows to run more VMs in parallel

Co-authored-by: Marian Gasparovic <marian.gasparovic@canonical.com>
This commit is contained in:
marosg42 2020-06-12 18:34:19 +02:00 committed by GitHub
parent df51b17d71
commit 7f408e59cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -92,8 +92,8 @@ function snapshot_info() {
}
function get_port() {
local PORT_START=22220
local PORT_RANGE=9
local PORT_START=$1
local PORT_RANGE=$2
while true; do
local CANDIDATE=$[${PORT_START} + (${RANDOM} % ${PORT_RANGE})]
(echo "" >/dev/tcp/127.0.0.1/${CANDIDATE}) >/dev/null 2>&1
@ -362,7 +362,7 @@ function vm_boot() {
fi
# Find a free port to expose ssh to the guest
local PORT=$(get_port)
local PORT=$(get_port 22220 9)
if [ -n "${PORT}" ]; then
NET="${NET},hostfwd=tcp::${PORT}-:22"
echo " - ssh: ${PORT}/tcp is connected. Login via 'ssh user@localhost -p ${PORT}'"
@ -370,6 +370,12 @@ function vm_boot() {
echo " - ssh: All ports for exposing ssh have been exhausted."
fi
# Find a free port for spice
local SPICE_PORT=$(get_port 5930 9)
if [ ! -n "${SPICE_PORT}" ]; then
echo " - spice: All spice ports have been exhausted."
fi
enable_usb_passthrough
# Boot the iso image
@ -438,7 +444,7 @@ function vm_boot() {
-rtc base=localtime,clock=host \
-object rng-random,id=rng0,filename=/dev/urandom \
-device virtio-rng-pci,rng=rng0 \
-spice port=5930,disable-ticketing \
-spice port=${SPICE_PORT},disable-ticketing \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \
@ -463,7 +469,7 @@ function vm_boot() {
-rtc base=localtime,clock=host \
-object rng-random,id=rng0,filename=/dev/urandom \
-device virtio-rng-pci,rng=rng0 \
-spice port=5930,disable-ticketing \
-spice port=${SPICE_PORT},disable-ticketing \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \