Merge pull request #2846 from flatcar/krnowak/qemu-script

Simplify and improve flatcar_production_qemu*.sh script
This commit is contained in:
Krzesimir Nowak 2025-04-23 15:15:43 +02:00 committed by GitHub
commit fb2682ddb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,6 +20,8 @@ CONFIG_IMAGE=""
SWTPM_DIR= SWTPM_DIR=
SAFE_ARGS=0 SAFE_ARGS=0
FORWARDED_PORTS="" FORWARDED_PORTS=""
PRIMARY_DISK_OPTS=""
DISKS=()
USAGE="Usage: $0 [-a authorized_keys] [--] [qemu options...] USAGE="Usage: $0 [-a authorized_keys] [--] [qemu options...]
Options: Options:
-i FILE File containing an Ignition config -i FILE File containing an Ignition config
@ -27,6 +29,13 @@ Options:
-u FILE Cloudinit user-data as either a cloud config or script. -u FILE Cloudinit user-data as either a cloud config or script.
-c FILE Config drive as an iso or fat filesystem image. -c FILE Config drive as an iso or fat filesystem image.
-a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub] -a FILE SSH public keys for login access. [~/.ssh/id_{dsa,rsa}.pub]
-d DISK Setup additional disk. Can be used multiple times to
setup multiple disks. The value is a path to an image
file, optionally followed by a comma and options to
pass to virtio-blk-pci device. For example -d
/tmp/qcow2-disk,serial=secondary.
-D OPTS Additional virtio-blk-pci options for primary
disk. For example serial=primary-disk.
-p PORT The port on localhost to map to the VM's sshd. [2222] -p PORT The port on localhost to map to the VM's sshd. [2222]
-I FILE Set a custom image file. -I FILE Set a custom image file.
-f PORT Forward host_port:guest_port. -f PORT Forward host_port:guest_port.
@ -83,6 +92,12 @@ while [ $# -ge 1 ]; do
check_conflict check_conflict
SSH_KEYS="$2" SSH_KEYS="$2"
shift 2 ;; shift 2 ;;
-d|-disk)
DISKS+=( "$2" )
shift 2 ;;
-D|-image-disk-opts)
PRIMARY_DISK_OPTS="$2"
shift 2 ;;
-p|-ssh-port) -p|-ssh-port)
SSH_PORT="$2" SSH_PORT="$2"
shift 2 ;; shift 2 ;;
@ -258,16 +273,29 @@ if [ -n "${CONFIG_IMAGE}" ]; then
fi fi
if [ -n "${VM_IMAGE}" ]; then if [ -n "${VM_IMAGE}" ]; then
case "${VM_BOARD}" in if [[ ,${PRIMARY_DISK_OPTS}, = *,drive=* || ,${PRIMARY_DISK_OPTS}, = *,bootindex=* ]]; then
amd64-usr) die "Can't override drive or bootindex options for primary disk"
set -- -drive if=virtio,file="${VM_IMAGE}" "$@" ;;
arm64-usr)
set -- -drive if=none,id=blk,file="${VM_IMAGE}" \
-device virtio-blk-device,drive=blk "$@"
;;
*) die "Unsupported arch" ;;
esac
fi fi
set -- -drive if=none,id=blk,file="${VM_IMAGE}" \
-device virtio-blk-pci,drive=blk,bootindex=1${PRIMARY_DISK_OPTS:+,}${PRIMARY_DISK_OPTS:-} "$@"
fi
declare -i id_counter=1
for disk in "${DISKS[@]}"; do
disk_id="flatcar-extra-disk-$((id_counter++))"
if [[ ${disk} = *,* ]]; then
disk_path=${disk%%,*}
disk_opts=${disk#*,}
else
disk_path=${disk}
disk_opts=
fi
set -- \
-drive "if=none,id=${disk_id},file=${disk_path}" \
-device "virtio-blk-pci,drive=${disk_id}${disk_opts:+,}${disk_opts:-}" \
"${@}"
done
if [ -n "${VM_KERNEL}" ]; then if [ -n "${VM_KERNEL}" ]; then
set -- -kernel "${VM_KERNEL}" "$@" set -- -kernel "${VM_KERNEL}" "$@"
@ -298,25 +326,18 @@ fi
case "${VM_BOARD}" in case "${VM_BOARD}" in
amd64-usr) amd64-usr)
# Default to KVM, fall back on full emulation QEMU_BIN=qemu-system-x86_64 ;;
qemu-system-x86_64 \ arm64-usr)
QEMU_BIN=qemu-system-aarch64 ;;
*) die "Unsupported arch" ;;
esac
"$QEMU_BIN" \
-name "$VM_NAME" \ -name "$VM_NAME" \
-m ${VM_MEMORY} \ -m ${VM_MEMORY} \
-netdev user,id=eth0${QEMU_FORWARDED_PORTS:+,}${QEMU_FORWARDED_PORTS},hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \ -netdev user,id=eth0${QEMU_FORWARDED_PORTS:+,}${QEMU_FORWARDED_PORTS},hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \
-device virtio-net-pci,netdev=eth0 \ -device virtio-net-pci,netdev=eth0 \
-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \ -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \
"$@" "$@"
;;
arm64-usr)
qemu-system-aarch64 \
-name "$VM_NAME" \
-m ${VM_MEMORY} \
-netdev user,id=eth0${QEMU_FORWARDED_PORTS:+,}${QEMU_FORWARDED_PORTS},hostfwd=tcp::"${SSH_PORT}"-:22,hostname="${VM_NAME}" \
-device virtio-net-device,netdev=eth0 \
-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \
"$@"
;;
*) die "Unsupported arch" ;;
esac
exit $? exit $?