Qemu KVM Basics

In preparing to study for a Juniper certification I found that they offer vSRX as a qcow2 volume. I took this opportunity to brush up on qemu kvm again. The ability to use the command line to manage these VMs is incredible and it’s a cool skill to have. I will not be using virt-manager.


Creating A VM Volume

The first step to create a vm with qemu is to create a volume for the VM. We can do this with the following command:

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

This command uses the qcow2 format and creates the mydrive.qcow2 file with a 10 GB filesystem size. It will only take up as much space that is used inside of the drive so it won’t reach 10 GB until the drive is full on the VM side.

Create the VM

Next we need to boot the VM with your choice of bootable image.

qemu-system-x86_64 \
    -hda mydrive.qcow2 \
    -accel kvm \
    -m size=1g \
    -smp cores=1 \
    -vga virtio \
    -nic bridge,br=virbr0 \
    -cdrom ./manjaro.iso

So let’s break this down.

  1. -hda will - mount mydrive.qcow2 as the main drive
  2. -accel kvm - enables kvm acceleration
  3. -m size=1g - gives 1 GB of ram to the VM
  4. -smp cores=1 - gives 1 core to the VM
  5. -vga virtio - uses the virtio driver for the display
  6. -nic bridge,br=virbr0 - uses the default host nat network
  7. -cdrom ./manjaro.iso - mounts the cdrom to boot from

You should see a window pop up to install your operating system like usual. After the OS is finished installing shutdown the VM.

Start the VM

Now to start the VM you need to run this command every time.

qemu-system-x86_64 \
    -hda mydrive.qcow2 \
    -accel kvm \
    -m size=1g \
    -smp cores=1 \
    -vga virtio \
    -nic bridge,br=virbr0

Notice the only difference is that the cdrom argument is removed so that we don’t boot to the iso file again.

Enable SSH

You can enable ssh to the host if you would like to manage it over ssh from your host machine. The start command above connects to the default host/nat network. For most you just need to install openssh and start it up. I will be using manjaro for this tutorial.

pacman -Syu
pacman -S openssh

SSH to the Host

Now you can ssh to this host over the host network. To see the IP address check the guest.

ip a

Use the address you found to ssh to the host.

[email protected]:~/scratch/kvm-test$ ssh [email protected]
[email protected]'s password: 
Last login: Thu Mar 17 08:34:52 2022 from

Next steps

You can set all of these commands in a makefile for quick start/stop. In a future post I’ll combine kvm with terraform to make it even easier to start and stop environments.