Skip to content

Instantly share code, notes, and snippets.

@lockness-Ko
Created April 2, 2022 00:28
Show Gist options
  • Save lockness-Ko/e80fda31fc1e166dd231889d0b45e7ef to your computer and use it in GitHub Desktop.
Save lockness-Ko/e80fda31fc1e166dd231889d0b45e7ef to your computer and use it in GitHub Desktop.

> QEMU

If you are stuck, ctrl+f in the arch wiki page for qemu or DM me on discord.

> Installation

To install on debian-like systems (e.g. ubuntu, tails etc.) type the following commands to install QEMU:

sudo apt update
sudo apt -y full-upgrade
sudo apt autoremove
sudo apt clean
sudo apt install qemu qemu-system-gui qemu-system qemu-kvm

DO THIS IF YOU WANT TO ENABLE KVM

For help, go here https://help.ubuntu.com/community/KVM/Installation

  1. First, check if your CPU supports vmx:
egrep -c '(vmx|svm)' /proc/cpuinfo
  1. If the output is 1 or higher, go to the following step. if it is 0 or nothing you cannot use KVM:
  2. You will need to go into your BIOS and turn on Intel VT-x and VT-d. If you have an AMD cpu you will need to turn on AMD-v. This stuff will usually be on the 'virtualization' options in you BIOS.
  3. Boot back into ubuntu and type kvm-ok into a terminal
  4. If the output looks something like this you can use -enable-kvm and -cpu host in the arguments you use for qemu-system-x86_64:
INFO: /dev/kvm exists
KVM acceleration can be used
  1. If it looks like this, you cannot use -enable-kvm and -cpu host in the arguments you use for qemu-system-x86_64:
INFO: Your CPU does not support KVM extensions
KVM acceleration can NOT be used

> Basic commands (you will use these the most)

> qemu-system-x86_64

This is the most common command you will use. It is used to start a virtual machine. If you virtual machine is hanging go back to the terminal and press ctrl+c to force stop the vm.

Here are some of the most useful command line arguments:

# Block device commands
-hda <path/to/disk.image> # Mount a hard disk (you can do multiple files or specify a real hard drive by doing -hda /dev/sdX)
-cdrom <path/to/cdrom or iso> # Mount a cdrom or ISO image
-drive format=<qcow2 or raw>,file=<path/to/drive> # Used to mount drives manually. I only use this for mounting 'raw' disk images

# Boot options
-boot menu=on # Allow for entering a boot menu
-boot c # boot into the CD rom i think?
-boot d # boot into the first hard drive i think?
# ADVANCED BOOT OPTIONS, you probably wont need to use these
-kernel <path/to/custom/linux/kernel> # Specify a kernel so you do not need a bootloader to boot into the system. Useful for testing your own custom made linux distro
-initrd <path/to/initrd> # Boot with this initrd
-append "root=/dev/sdX console=ttyS0" # Append kernel parameters when booting

# Performance options
-smp <number of cores> # Specify the number of virtual cpu cores. This can be more than on your host IF you do not specify -cpu host although you wont get any performance gain
-cpu host # Used in conjunction with -enable-kvm
-enable-kvm # Enable KVM virtualization
-m <memory to allocate> # Allocate an amount of memory to the guest it can be Gigabytes (G) Megabytes (M) or Kilobytes (K) e.g. 4G or 512M or 1024K
# GRAPHICS PERFORMANCE OPTIONS
-vga virtio # Virtio is the most performant graphics driver. IF it doesn't work you can try qxl instead but that doesn't offer much more performance. For a list of graphics drivers go to the arch wiki
-display sdl,gl=on # Set the display drive to sdl and turn on OpenGL. If it complains, try '-display gtk,gl=on'

# Networking options (simple)
-nic user,hostfwd=tcp::2222-:22 # In this case, forward port 22 from the guest to port 2222 on the host. You can change these to any port numbers you want
-vnc, :0,password # Setup a vnc server (to view and control the VM) on port 590X where X is the number after the colon (:). You will have to change the password in the qemu monitor by running 'change vnc password' and inputting the desired password for the vnc connection.

# QEMU monitor
-monitor stdio # I use stdio because it's the easiest for me. Otherwise you can press ctrl+alt+2 on your keyboard

Here is what I use as my template command:

qemu-system-* -drive format=qcow2,file=image.cow -cdrom medium.iso -boot menu=on -m 4G -smp 4 -cpu host -enable-kvm -vga virtio -display sdl,gl=on -monitor stdio

> qemu-img

This is the second most common command you will use. It is a utility for creating or modifying virtual disk images.

The way you use it is like this

qemu-img <command>

Here is a list of commands I use:

# For creating disk images
create <options> <path/to/disk.image> <size>
# size can be G, M or K
-f <qcow2 or raw> # Specify the format

# e.g.
qemu-img create -f qcow2 image.cow 10G
qemu-img create -f raw rawimage.cow 512M

# For resizing disk images
resize <path/to/image> <+-size>
# size can be +, - or a specific size

#e.g.
qemu-img resize image.cow +1G
qemu-img resize rawimage.cow 1024K

# For converting formats
convert -f <current format> -O <output format> <path/to/disk.image> <path/to/output disk.image>

# e.g.
qemu-img convert -f vmdk -O qcow2 vmware.img qemu.cow
qemu-img convert -f qcow2 -O vpc qemy.cow windows_hyperv.img

List of formats for qemu-img convert:

Image format qemu-img create/convert argument
QCOW2 (KVM, Xen) qcow2
QED (KVM) qed
VDI (VirtualBox) vdi
VHD (Hyper-V) vpc
VMDK (VMWare) vmdk

^ This is another reason why qemu is great, it supports virtual disk images for all the popular hypervisors

> Keyboard shortcuts

Ctrl+Alt+G is to switch between sending keypresses between the VM and your host. Try this if your mouse is stuck in the VM Ctrl+Alt+F is to make the VM fullscreen. Ctrl+Alt+2 is to switch to the QEMU monitor Ctrl+Alt+1 is to switch back to the virtual machine from the QEMU monitor

IMPORTANT Press ctrl+alt+g before you enter fullscreen otherwise you wont send any keypresses or mouse movements to the VM

> QEMU Monitor

You will use this most commonly for snapshotting. For more info go to the command list.

# screenshot
screendump <name>.ppm # screenshot to ppm

# power/vm state
system_powerdown # it is what it is
stop # pause the vm
cont # continue

# WARNING! from here down, the commands depend on qcow2 image!

# snapshotting
savevm <name> # saves a snapshot with <name>
loadvm <name> # loads the snapshot with <name>
delvm <name> # deletes the vm with <name>
info snapshots # lists all snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment