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.
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).
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
mkdir -p /vms
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
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>").
qemu-system-x86_64 -m 1G -cpu host -smp 2 -accel kvm -drive file=Arch-Linux-x86_64-basic.qcow2,format=qcow2 -nic user,model=virtio-net-pci,hostfwd=tcp::2222-:22
Notes:
qemu-system-x86_64 is the command to emulate a
x86_64 target architecture. See
man (1) qemu-system-x86_64,
qemu-system-x86_64 -help,
qemu-system-x86_64 -nic help, qemu-system-x86_64 -nic model=help
etc.-m 1G specifies RAM of 1GB for the VM-cpu host means the VM sees the same CPU as the host,
with all supported host features-smp 2 means the VM sees 2 CPU cores-accel kvm specifies to use kvm acceleration ([kvm], [kvm-vs-qemu])-nic user,model=virtio-net-pci,hostfwd=tcp::2222-:22
specifies to use “user networking” [qemu-devices-net][qemu-networking], use the virtio-net-pci
virtual network adaptor, and to forward the VM’s port 22 (ie sshd) to
the guest’s port 2222. [qemu-nic-param]
has a discussion of the -nic option, [virtio-vs-e1000-vs-rtl8139]
discusses the advantages of the virtio nic model over
others. “user networking” means the VM will be in its own local network
10.0.2.0/24.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)).
To allow the VM to be in the same network as the host, a network bridge needs to be set up.
This needs /usr/lib/qemu/qemu-bridge-helper to be
present [qemu-helper-networking]
(the above installation of qemu-base will do this). It
needs to be setuid (which Arch Linux does on installation).
/etc/qemu/bridge.conf needs to allow the bridge device
that will be created. In Arch Linux, this is already set up - it has the
single line
allow virbr0
so qemu will allow the VM to join the bridge virbr0.
Create and activate the bridge
sudo ip link add name virbr0 type bridge
sudo ip link set dev virbr0 up
Add an interface to the bridge
sudo ip link set dev eno1 master virbr0
eno1 is my ethernet interface (see output of
ip link show). A wifi adapter is less likely to support
bridging.