Skip to content

Commit ac18441

Browse files
authored
Merge pull request #2575 from John15321/add-port-fwd-params
Add support for multiple port forwarding parameters in QEMU startup script
2 parents 9f41ee1 + 1a7b995 commit ac18441

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

build_library/qemu_template.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ IGNITION_CONFIG_FILE=""
1919
CONFIG_IMAGE=""
2020
SWTPM_DIR=
2121
SAFE_ARGS=0
22+
FORWARDED_PORTS=""
2223
USAGE="Usage: $0 [-a authorized_keys] [--] [qemu options...]
2324
Options:
2425
-i FILE File containing an Ignition config
@@ -28,6 +29,7 @@ Options:
2829
-a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub]
2930
-p PORT The port on localhost to map to the VM's sshd. [2222]
3031
-I FILE Set a custom image file.
32+
-f PORT Forward host_port:guest_port.
3133
-M MB Set VM memory in MBs.
3234
-T DIR Add a software TPM2 device through swtpm which stores secrets
3335
and the control socket to the given directory. This may need
@@ -84,6 +86,9 @@ while [ $# -ge 1 ]; do
8486
-p|-ssh-port)
8587
SSH_PORT="$2"
8688
shift 2 ;;
89+
-f|-forward-port)
90+
FORWARDED_PORTS="${FORWARDED_PORTS} $2"
91+
shift 2 ;;
8792
-s|-safe)
8893
SAFE_ARGS=1
8994
shift ;;
@@ -203,6 +208,15 @@ if [ -z "${CONFIG_IMAGE}" ]; then
203208
fi
204209
fi
205210

211+
# Process port forwards
212+
QEMU_FORWARDED_PORTS=""
213+
for port in ${FORWARDED_PORTS}; do
214+
host_port=${port%:*}
215+
guest_port=${port#*:}
216+
QEMU_FORWARDED_PORTS="${QEMU_FORWARDED_PORTS},hostfwd=tcp::${host_port}-:${guest_port}"
217+
done
218+
QEMU_FORWARDED_PORTS="${QEMU_FORWARDED_PORTS#,}"
219+
206220
# Start assembling our default command line arguments
207221
if [ "${SAFE_ARGS}" -eq 1 ]; then
208222
# Disable KVM, for testing things like UEFI which don't like it
@@ -288,7 +302,7 @@ case "${VM_BOARD}" in
288302
qemu-system-x86_64 \
289303
-name "$VM_NAME" \
290304
-m ${VM_MEMORY} \
291-
-netdev user,id=eth0,hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \
305+
-netdev user,id=eth0,${QEMU_FORWARDED_PORTS},hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \
292306
-device virtio-net-pci,netdev=eth0 \
293307
-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \
294308
"$@"
@@ -297,7 +311,7 @@ case "${VM_BOARD}" in
297311
qemu-system-aarch64 \
298312
-name "$VM_NAME" \
299313
-m ${VM_MEMORY} \
300-
-netdev user,id=eth0,hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \
314+
-netdev user,id=eth0,${QEMU_FORWARDED_PORTS},hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \
301315
-device virtio-net-device,netdev=eth0 \
302316
-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \
303317
"$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added support for multiple port forwarding parameters in the QEMU startup script. Users can now specify multiple port forwards using the `-f` option. ([flatcar/scripts#2575](https://github.com/flatcar/scripts/pull/2575))

0 commit comments

Comments
 (0)