Skip to content

QEMU-img Workstation Cheat Sheet

Unlock the full potential of QEMU-img workstation with these essential commands and configuration tips. Learn how to create and resize disk images, install firmware, and manage drivers for your virtual machines.


Install requirements

emerge sys-apps/usbutils

Create hard drive

qemu-img create -f qcow2 <choose_disk_name>.img 100G

Create custom-sized image

qemu-img create myimage.img mysize

Create qcow2 image with a maximum size of 10GB

qemu-img create -f qcow2 myimage.img 10G

Increase the size of a qcow2 image

qemu-img resize myimage.img 20G

Create RAW image with a maximum size of 10GB

qemu-img create -f raw myimage.img 10G

Increase the size of a RAW image

qemu-img resize -f raw myimage.img 20G

Display attributes of a RAW image

qemu-img info /var/lib/libvirt/images/disk1.img

Install OVMF Binary to get EFI Firmware File For libvirtd/libvirtd-guets

emerge sys-firmware/edk2-ovmf
ln -s /usr/share/edk2-ovmf /usr/share/OVMF

In order to use the firmware you can run qemu the following way

qemu-system-x86_64 -drive file=/usr/share/edk2-ovmf/OVMF.fd,if=pflash,format=raw,unit=0,readonly=on ....

Install virtiofs

emerge app-emulation/virtiofsd app-emulation/virtio-win

Example Install libvirtd and libvirt-guests

emerge --ask app-emulation/libvirt

Install the drivers on a Windows guest machine

Pass the ISO file to the guest machine and mount it as a CD-ROM image. Then, in the Windows guest, open the Device Manager and start the driver update wizard. Locate the appropriate device and click to update its driver. In the pop-up window, click to browse in the guest machine to locate the ISO image, and select the driver(s) from it to install.

/usr/share/drivers/windows/virtio-win.iso

By testing any livecd quickly you can add following to ~/.bbashrc and then you just have to type qq livecd.iso

qq() {
  local iso
  iso="$1"

  if [[ -z "$iso" || ! -f "$iso" ]]; then
    echo "Usage: qq /path/to/live.iso" >&2
    return 2
  fi

  qemu-system-x86_64 \
    -machine q35,accel=kvm \
    -cpu host \
    -smp 4 \
    -m 8192 \
    -boot order=d \
    -cdrom "$iso" \
    -nic user,model=virtio-net-pci \
    -device qemu-xhci,id=xhci \
    -device usb-tablet,bus=xhci.0 \
    -display gtk,gl=on \
    -device virtio-vga \
    -audiodev pipewire,id=snd0 \
    -device ich9-intel-hda \
    -device hda-duplex,audiodev=snd0 \
    -snapshot
}

Creating an Encrypted File Container Using LUKS and a Loop Device

# 1) Create a raw container file (acts like a virtual disk file)
# truncate creates a sparse file (uses space only as data is written)
truncate -s 100G vault.img

# Alternatively, fallocate usually preallocates the full space immediately
# (better for performance, worse for disk space flexibility)
# fallocate -l 100G vault.img
    
# 2) Attach the file to a loop device (exposes the file as a block device)
# --find finds the first free loop device
# --show prints the allocated loop device (e.g. /dev/loop0)
sudo losetup --find --show vault.img
# Example output: /dev/loop0
    
# 3) Initialize LUKS encryption on the loop device
# This encrypts the entire virtual disk
sudo cryptsetup luksFormat /dev/loop0

# Open (decrypt) the LUKS container and map it as /dev/mapper/vault
sudo cryptsetup open /dev/loop0 vault
    
# 4) Create a filesystem INSIDE the decrypted LUKS mapping
# The filesystem will be encrypted at rest
sudo mkfs.ext4 /dev/mapper/vault

# 5) Mount the encrypted filesystem
sudo mkdir -p /mnt/vault
sudo mount /dev/mapper/vault /mnt/vault