Skip to content

Instantly share code, notes, and snippets.

@olivierlemoal
Last active May 9, 2022 15:43
Show Gist options
  • Save olivierlemoal/260d79d54c00bb4ab3e67f259cba6294 to your computer and use it in GitHub Desktop.
Save olivierlemoal/260d79d54c00bb4ab3e67f259cba6294 to your computer and use it in GitHub Desktop.
sudo qemu-system-x86_64 -drive file=amf_apl-gw,if=virtio,cache=off -m 1024 -enable-kvm -cpu host -vga virtio -device virtio-net,netdev=network0 -netdev tap,id=network0,ifname=tap0,script=no,downscript=no -runas $(whoami)
run as sudo to let qemu create tap devices, but use -runas user to drop privileges
====================================
Mutiple cores :
-smp cores=n
====================================
Telnet monitor
-monitor telnet:127.0.0.1:55555,server,nowait;
====================================
Drive :
Use either Virtio SCSI or Virtio BLK (https://www.qemu.org/2021/01/19/virtio-blk-scsi-configuration/)
virtio-scsi
The scsi-hd device is suitable for disk image files and host block devices when SCSI passthrough is not required. The scsi-block device offers SCSI passthrough and is preferred over scsi-generic due to higher performance.
-device virtio-scsi-pci,id=scsi \
-device scsi-hd,drive=drive0 \
-drive if=none,id=drive0,file=test.img,format=raw \
virtio-blk
Using virtio-blk with blockdev (https://www.qemu.org/docs/master/system/qemu-block-drivers.html)
-blockdev \
node-name=drive0,driver=qcow2,file.driver=file,file.node-name=file,file.filename=./a.qcow2 \
-device virtio-blk,drive=drive0,id=virtio0 \
====================================
Random mac :
printf -v macaddr "52:54:%02x:%02x:%02x:%02x" $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff )) $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff ))
-device virtio-net,netdev=network0,mac="$macaddr"
====================================
VM on same network :
/!\ use different MACs
VM1: -device virtio-net,netdev=network0,mac="$macaddr" -netdev tap,id=network0,ifname=tap0,script=no,downscript=no
VM2: -device virtio-net,netdev=network0,mac="$macaddr" -netdev tap,id=network0,ifname=tap1,script=no,downscript=no
# bridge together TAP interfaces
ip l set tap0 up
ip l set tap1 up
ip link add br0 type bridge
ip link set tap0 master br0
ip link set tap1 master br0
# host ip
ip a a 192.168.200.1/24 dev br0
====================================
To auto add to bridge :
➜ cat /etc/qemu-ifup
#!/bin/sh
set -x
switch=br0
if [ -n "$1" ];then
ip link set $1 up
sleep 0.5s
ip link set $1 master $switch
exit 0
else
echo "Error: no interface specified"
exit 1
fi
Remove "script=no" from params
====================================
Snapshot :
https://wiki.qemu.org/Documentation/CreateSnapshot
https://wiki.qemu.org/Features/Snapshots
====================================
Debugging with GDB :
https://en.wikibooks.org/wiki/QEMU/Debugging_with_QEMU
====================================
Headless
-append "console=ttyS0" -nographic - Normally, QEMU uses SDL to display the VGA output. With this option, you can totally disable graphical output so that QEMU is a simple command line application. The emulated serial port is redirected on the console. Therefore, you can still use QEMU to debug a Linux kernel with a serial console.
====================================
Mount QEMU image
sudo modprobe nbd max_part=8
sudo qemu-nbd --connect=/dev/nbd0 my_image
sudo mount /dev/nbd0p1 my_image
sudo qemu-nbd --disconnect /dev/nbd0
====================================
Create Images
qemu-img create -f qcow2 foobar.qcow2 100M
====================================
Arch Linux Guest
/etc/mkinitcpio.conf:
MODULES=(virtio virtio_blk virtio_pci virtio_net)
# mkinitpcio -p linux
====================================
UEFI
https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF#Plain_QEMU_without_libvirt
With secureboot :
# required machine type
-machine q35,smm=on,accel=kvm
# Due to the way some of the models work in edk2, we need to disable
# s3 resume. Without this option, qemu will appear to silently hang
# althouh it emits an error message on the ovmf_log
-global ICH9-LPC.disable_s3=1
# Secure!
-global driver=cfi.pflash01,property=secure,value=on
# Point to the firmware
-drive if=pflash,format=raw,unit=0,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.secboot.fd,readonly=on
# Point to your copy of the variables
-drive if=pflash,format=raw,file=OVMF_VARS.fd
====================================
Edit emulated machine
List
qemu-system-x86_64 -machine help
-machine pc-q35
====================================
USB Drive using xhci controller (USB3)
-device qemu-xhci \
-drive id=my_usb_disk,file=USB.qcow2,if=none \
-device usb-storage,drive=my_usb_disk
====================================
Boot Menu, e.g to boot from USB
-boot menu=on
===================================
Device list
-device help
Doc
https://wiki.qemu.org/Category:Completed_feature_pages
https://wiki.archlinux.org/index.php/QEMU
https://wiki.qemu.org/Documentation/Networking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment