Skip to content

Instantly share code, notes, and snippets.

@SaicharanKandukuri
Created January 5, 2023 16:21
Show Gist options
  • Save SaicharanKandukuri/f875d923938fb490c38575c158485de4 to your computer and use it in GitHub Desktop.
Save SaicharanKandukuri/f875d923938fb490c38575c158485de4 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -Eeuxo pipefail
# change it according to your perefed name
MACHINE_NAME="ubuntu-os"
QEMU_IMG="${MACHINE_NAME}.img"
SSH_PORT="5555"
OVMF_CODE="/usr/share/OVMF/OVMF_CODE_4M.ms.fd"
OVMF_VARS_ORIG="/usr/share/OVMF/OVMF_VARS_4M.ms.fd"
OVMF_VARS="$(basename "${OVMF_VARS_ORIG}")"
if [ ! -e "${QEMU_IMG}" ]; then
qemu-img create -f qcow2 "${QEMU_IMG}" 80G
fi
if [ ! -e "${OVMF_VARS}" ]; then
cp "${OVMF_VARS_ORIG}" "${OVMF_VARS}"
fi
qemu-system-x86_64 \
-enable-kvm \
-cpu host -smp cores=4,threads=1 -m 4096 \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0 \
-name "${MACHINE_NAME}" \
-drive file="${QEMU_IMG}",format=qcow2 \
-net nic,model=virtio -net user,hostfwd=tcp::${SSH_PORT}-:22 \
-vga virtio \
-machine q35,smm=on \
-global driver=cfi.pflash01,property=secure,value=on \
-drive if=pflash,format=raw,unit=0,file="${OVMF_CODE}",readonly=on \
-drive if=pflash,format=raw,unit=1,file="${OVMF_VARS}" \
$@
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment