Skip to content

Instantly share code, notes, and snippets.

@PillTime
Last active January 30, 2023 22:17
Show Gist options
  • Save PillTime/eca1f7420b09a79492d71fc956bde783 to your computer and use it in GitHub Desktop.
Save PillTime/eca1f7420b09a79492d71fc956bde783 to your computer and use it in GitHub Desktop.
QEMU VM Creator
#!/bin/sh
case $1 in
'-h'|'--help')
printf 'Usage: %s VM_NAME CPUS MEMORY DISK\n' "$0"
exit 0
esac
[ $# -ne 4 ] && printf 'ERROR: Need 4 arguments. Check -h/--help.\n' && exit 1
mkdir "$1"
cp /usr/share/edk2-ovmf/x64/OVMF_VARS.fd "$1/uefi_vars.fd"
qemu-img create -f qcow2 "$1/disk.qcow2" "$4"
cat << EOF > "$1/start.sh"
#!/usr/bin/env sh
case \$1 in
'-h'|'--help')
printf 'Usage: %s [ISO]\\n' "\$0"
exit 0
esac
[ \$# -gt 1 ] && printf 'ERROR: Too many arguments. Check -h/--help.\\n' && exit 1
ISO=''
[ -n "\$1" ] && ISO="-device virtio-blk,drive=iso,bootindex=1 \\
-drive file=\$1,format=raw,if=none,media=cdrom,id=iso"
eval qemu-system-x86_64 \\
-enable-kvm \\
-machine q35 \\
-device intel-iommu \\
-cpu host \\
-smp $2 \\
-m $3 \\
-vga none \\
-display gtk,gl=on,full-screen=on \\
-device virtio-vga-gl \\
-nic user,model=virtio-net-pci \\
-device intel-hda \\
-device hda-duplex \\
-device virtio-balloon \\
-device virtio-blk,drive=disk,bootindex=2 \\
-drive file=disk.qcow2,format=qcow2,if=none,aio=native,cache.direct=on,id=disk \\
-drive if=pflash,format=raw,read-only=on,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd \\
-drive if=pflash,format=raw,file=uefi_vars.fd \\
"\$ISO"
EOF
chmod +x "$1/start.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment