Skip to content

Instantly share code, notes, and snippets.

@sulincix
Last active December 8, 2021 13:01
Show Gist options
  • Save sulincix/474701f03aa895cf7cf0d4b63f2914c4 to your computer and use it in GitHub Desktop.
Save sulincix/474701f03aa895cf7cf0d4b63f2914c4 to your computer and use it in GitHub Desktop.
Qemu run virtual machine with virtio
#!/bin/bash
bios="/sys/devices/virtual/dmi/id/"
UUID=$(cat $bios/product_uuid 2>/dev/null)
SERIAL=$(cat $bios/product_serial 2>/dev/null)
DP="none"
if qemu-system-$(uname -m) -display help | grep sdl &>/dev/null ; then
DP="sdl"
elif qemu-system-$(uname -m) -display help | grep gtk &>/dev/null ; then
DP="gtk"
fi
CPU=$(nproc)
if [[ ! -f "$1" ]] ; then
echo "Usage: qemu-run [disk.img] [cdrom.iso]"
exit 1
fi
RAM=$(free -m | sed -n "2,2p" | awk '{print $2}')
exec qemu-system-$(uname -m) -m size=$((${RAM}-1))M,slots=$CPU,maxmem=${RAM}M \
-smp cores=$(($CPU/2)),threads=2 --enable-kvm -cpu host -device AC97 \
-net user,hostfwd=tcp::2222-:22 -net nic,model=virtio ${1:+ -drive file="$1",if=virtio} ${2:+ -cdrom $2} \
-smbios type=0,vendor="$(cat $bios/bios_vendor)",version="$(cat $bios/bios_version)",release="$(cat $bios/bios_release)",date=$(date +%d/%m/%y) \
-smbios type=1,manufacturer="$(cat $bios/bios_vendor)",product="$(cat $bios/product_name)",${SERIAL:+serial="$SERIAL"},${UUID:+uuid="$UUID"},sku="$(cat $bios/product_sku)",family="$(cat $bios/product_family)" \
-display $DP -vga virtio -no-acpi -boot menu=on -name QEMU-$$ -serial stdio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment