QEMU Cheatsheet

先前因为各种各样的动机用了 QEMU。读 Arch wiki 的时候大致做了个笔记记录常用的使用方法。

  1. Install qemu, qemu-arch-extra (optional, for other architectures)

  2. Disk images

    Create disk image qemu-img create -f qcow2 windows_7.cow 32G

  3. Start the virtual machine from CD drive (with iso file)

    bash
    qemu-system-x86_64 -cdrom iso_image_path -boot order=d -drive file=disk_image_path,format=qcow2
  4. Memory -m 2G

  5. Boot menu -boot menu=on

  6. Ctrl+Alt+G to escape mouse

  7. Enable KVM: append -enable-kvm

  8. Enable Hyper-V enlightenments for Windows guests

    bash
    -cpu host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time
  9. CPU cores: -smp 2

  10. Network

    • By default QEMU should automatically set up network for guest.
    • Port forwarding
      • -nic user,hostfwd=tcp::8080-:80: forward guest 80 to host 8080
  11. Share data between host&guest

    The default user-mode networking allows the guest to access the host OS at the IP address 10.0.2.2.

  12. 3D acceleration

    GPU passthrough. Failed on my machine.

  13. UEFI

    • Install edk2-ovmf
    • cp /usr/share/edk2-ovmf/x64/OVMF_VARS.fd
    • -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
    • -drive if=pflash,format=raw,file=/tmp/MY_VARS.fd