qemu - bunnyamin/bunnix GitHub Wiki

Qemu

  • If in headless mode, to exit the VM instance press Ctrl+A and release A but still holding Ctrl, then press C. A prompt should appear (qemu) in to which the command quit ends the VM instance.

Disk image

The qemuu-img can create (create) a disk image in a specific format (-f) as a file of a specific size. For example:

  • $ qemu-img create -f raw <file> 4G
  • $ qemu-img create -f raw <file> 4G

Convert from one disk image format to another. For example:

  • qemu-img convert -f raw -O qcow2 <file>.raw <file>.qcow2

Resize a disk image. For example:

  • qemu-img resize <file>.qcow2 +10G
  • qemu-img resize -f raw <file> +10G

Show information about the image qemu-img info <file>. For example,

file format: raw
virtual size: 10 GiB (10737418240 bytes)
disk size: 4 KiB

Mount

  • Convert from QCOW2 to raw qemu-img convert -p -O raw file.qcow2 file.raw.
  • Mount as loop device.

Hardware

Audio

  • List all valid sound cards qemu-system-x86_64 -soundhw help.
  • The paramater -soundhw <valid sound card> is deprecated.
  • Use the parameter -device <for most valid sound cards>.
    • For hda however two parameters must be provided -device intel-hda -device hda-duplex.
    • For pcspk the parameter is -machine pcspk-audiodev=<NAME>.

IOMMU (Input–Output Memory Management Unit)

Enable IOMMU in the Linux kernel:

  • AMD amd_iommu=on
  • Intel intel_iommu=on

Prevent devices which cannot be passed through iommu=pt.

Network

  • Networking

  • Port forward from host to guest -nic user,hostfwd=tcp::<HOST PORT>-<GUEST IP>:<GUEST PORT>.

    • The nic parameter creates a basic Network Interface Controller.

UEFI support

  • OVMF for virtual machines

  • Arch Linux pacman -S edk2-ovmf

  • Copy the non-volatile variable store $ cp /usr/share/edk2-ovmf/x64/OVMF_VARS.fd ~/vm/example.qemu.OVMF_VARS.fd.

  • The owner of the OVMF_VARS has to be the same as the user who will execute the Qemu instance referencing the OVMF_VARS.

  • Add to qemu arguments -drive if=pflash,format=raw,readonly,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd -drive if=pflash,format=raw,file=example.qemu.OVMF_VARS.fd.

Example

Headless instance daemonized for remote connection

With UEFI support.

qemu-system-x86_64 -boot order=d -name EXAMPLE_1 \
  -enable-kvm -m 2G -cpu host -device intel-hda -k sv \
  -daemonize -vnc :1 \
  -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd \
  -drive if=pflash,format=raw,file=~/vm/example1.qemu.OVMF_VARS.fd \
  -cdrom /obj/distro/release/2020.10.01-x86_64.iso \
  -drive file=~/vm/example1.qemu.raw,format=raw,index=0,media=disk

qemu-system-x86_64 -boot order=d -name EXAMPLE_2 \
  -enable-kvm -m 2G -cpu host -device intel-hda -k sv \
  -daemonize -vnc :2 \
  -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd \
  -drive if=pflash,format=raw,file=~/vm/example2.qemu.OVMF_VARS.fd \
  -cdrom /obj/distro/release/2022.10.01-x86_64.iso \
  -drive file=~/vm/example2.qemu.raw,format=raw,index=0,media=disk

The SSH connection:

ssh -vvx -p <SSH PORT>40001 \
  -i ~/.ssh/prv/[email protected] \
  <SERVER IP>[email protected] \
  -L <SSH PORT>40001:localhost:<VNC PORT>5901 \
  -L <SSH PORT>40002:localhost:<VNC PORT>5902

The VNC connection:

/usr/bin/sh -c 'vncviewer -SecurityTypes None -AcceptClipboard -SetPrimary \
  -SendClipboard -SendPrimary -FullColor -PreferredEncoding Tight \
  -QualityLevel 9 -CompressLevel 9 -DotWhenNoCursor localhost::<SSH PORT>40001'

Share directory between host and guest

The ~/share on the host should become available in the guest VM instance.

-drive file=fat:rw:~/share,index=1,media=disk,format=raw

The method has some limitations. For example,

  • filenames must be within the ASCII 7-bit range,
  • the host cannot write to the directory when the guest is accessing the directory.

Debug

Event Error Cause Consequence Remedy
Initializing the VM. Could not set up host forwarding rule 'tcp::<HOST PORT>-:<GUEST PORT>' The port is already in use. Cannot initialize VM. Release or change the port.
⚠️ **GitHub.com Fallback** ⚠️