Skip to content

Instantly share code, notes, and snippets.

@bgilbert
Last active January 5, 2023 19:44
Show Gist options
  • Save bgilbert/4f709e9a7cb066729e4c85b8023889f4 to your computer and use it in GitHub Desktop.
Save bgilbert/4f709e9a7cb066729e4c85b8023889f4 to your computer and use it in GitHub Desktop.
Wrapper for running QEMU with some common options
#!/bin/bash
set -euo pipefail
tpm=0
mem=2048
port=2222
tftpboot=
args=(-accel kvm -cpu host -object rng-random,filename=/dev/urandom,id=rng0)
while [ $# -ge 1 ]; do
case "$1" in
-m)
mem="$2"
shift 2
;;
-p)
port="$2"
shift 2
;;
-e)
args+=(-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd)
shift
;;
-t)
tpm=1
args+=(-chardev socket,id=chrtpm,path=tpm.sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0)
shift
;;
-i)
args+=(-fw_cfg name=opt/com.coreos/config,file="$2")
shift 2
;;
-d)
tftpboot="$tftpboot,tftp=$2"
shift 2
;;
-b)
tftpboot="$tftpboot,bootfile=$2"
shift 2
;;
--)
shift
remaining=$#
args+=("$@")
shift $remaining
;;
*)
echo "Unrecognized argument $1"
exit 1
;;
esac
done
args+=(-m $mem -netdev user,id=eth0,hostfwd=tcp::$port-:22,hostname="qemu"$tftpboot -device virtio-net-pci,netdev=eth0)
if [ $tpm = 1 ]; then
tpmdir=$(mktemp -d)
swtpm socket --tpm2 --ctrl type=unixio,path=tpm.sock --tpmstate dir="${tpmdir}" &
tpmpid=$!
trap "rm -r $tpmdir; kill $tpmpid" EXIT
fi
echo "qemu-system-x86_64 ${args[@]}"
qemu-system-x86_64 "${args[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment