QEMU setup of simple virtual machine on Arch Linux

bike9876@エボ猫.コム

This describes how to set up a very simple virtual machine (VM) running Arch Linux (from an Arch Linux host).

I use QEMU to manage my VMs (eg I don’t use libvirt), as my setup and needs are so easy.

(Arch Linux) install packages

sudo pacman -S qemu-base

This is the “base” set of qemu packages. Any VM will be headless - it will be managed either with a vnc viewer or via ssh (see below).

Set up LV

I use logical volumes (LVs) for my filesystems. I’m going to set up a LV to hold my VMs, put a filesystem on it, and add to /etc/fstab. (All this is optional.)

sudo -s # become root

vgname=MYVGNAME # volume group name: change as needed
# LV will be called "vms" and will be mounted at /vms
lvcreate -L 40g $vgname -n vms
mkfs.ext4 -m 1 /dev/$vgname/vms # -m 1: reserve 1% of space for root (not 5% default)
echo "/dev/$vgname/vms /vms ext4 rw,relatime 0 2" >> /etc/fstab

systemctl daemon-reload # if systemd is present

mount /vms

mkdir -p /vms/qemu/images # QEMU VM images will go here

exit # exit root

# I want the images to be owned by my "normal" user
sudo chown -R $(whoami):$(whoami) /vms/qemu

# For convenience, make a symlink to /vms/qemu from ~/qemu in my home directory
ln -s /vms/qemu ~/qemu

Create image

I just use VMs to make it easy to test things on linux. Arch Linux provides image files for the latest builds of arch linux (both “basic” and “cloud” - see https://gitlab.archlinux.org/archlinux/arch-boxes).

Download from <mirror>/images/ where <mirror> is a convenient Arch Linux mirror site (see /etc/pacman.d/mirrorlist or https://archlinux.org/mirrors/status/). Choose the version and variant (“basic” or “cloud”, I always use “basic”).

Download the .qcow2 and .qcow2.sig files, eg (if using latest basic version)

cd ~/qemu/images
curl -O https://mirror.server.net/archlinux/images/latest/Arch-Linux-x86_64-basic.qcow2
curl -O https://mirror.server.net/archlinux/images/latest/Arch-Linux-x86_64-basic.qcow2.sig

(change the mirror url as needed).

Check the .qcow2 file against its signature:

gpg --keyserver-options auto-key-retrieve --verify Arch-Linux-x86_64-basic.qcow2.sig

(should see Good signature from "arch-boxes <arch-boxes@archlinux.org>").

Start up

qemu-system-x86_64 -m 1G -accel kvm -drive file=Arch-Linux-x86_64-basic.qcow2,format=qcow2 -nic user,hostfwd=tcp::2222-:22

Notes:

View the VM console:

vncviewer :5900 # change port number as needed (see output of qemu-system-x86_64 command)

(vncviewer can be installed by installing the tigervnc package in Arch Linux).

Once the VM has fully booted up (can see on vncviewer, or just wait eg 60s), ssh to the VM from the host:

ssh localhost -l arch -p 2222

(the arch-box images have user arch with password arch preconfigured. User arch can become root via sudo (with no password needed)).

Refs