libvirt

From NixNet
This article is part of a series of guides that describe NixNet's setup in excruciating detail. If you would like to follow them, please start at the Infrastructure page.

Server

The following commands should be executed as the root user. This is going to be a headless system so no graphical components are necessary; --no-install-recommends will prevent them from being installed and lead to a lighter OS.

apt install --no-install-recommends qemu-kvm qemu-utils libvirt-clients libvirt-daemon-system netcat-openbsd dnsmasq

After installing these, the libvirt processes will start automatically so we can connect to it locally.

We also want a nonroot user for managing these VMs.

useradd -m <name> -s /bin/bash -G libvirt
mkdir /home/<name>/.ssh
cp ~/.ssh/authorized_keys /home/<name>/.ssh
chown -R <name>: /home/<name>

The next line is only necessary if you use kitty as your terminal.

cp -r ~/.terminfo /home/<name>/
cp ~/.bashrc /home/<name>/
chown -R <name>: /home/<name>

Before setting your local machine up for managing VMs, download an image or two. I always recommend Debian of course. Depending on what application it'll be used for, you may want to consider Debian Testing as it will have more up-to-date packages. I also recommend Alpine Linux if you want something more lightweight. Keep in mind, however, that this will require more setup. Unfortunately, some applications have a hard requirement for Ubuntu so you may want to download that as well. All of these images should be stored in /var/lib/libvirt/images

Local

Switch this to virsh and document process

virt-manager will be used to manage these VMs and it can be installed on virtually (heh) any Linux system.

apt-get install virt-manager (Debian/Ubuntu)
pacman -S virt-manager (Arch)
yum install virt-manager (Fedora)
emerge virt-manager (Gentoo)
pkg_add virt-manager (OpenBSD)

Open the Virtual Machine Manager application then go to File -> Add connection. Set the first field to QEMU/KVM, check the SSH box, fill in Username with what you used above for <name>, and your hostname is whatever you have set in your SSH config. I also recommend checking Autoconnect but it's not absolutely necessary.

VM creation is very specific to the service you'll be running in it. However, here is a general overview. To create a new VM, click the button in the top left then choose Local install media then click Browse. You'll be able to choose your .iso there. After making your selection, clear the text in the Choose the operating system . . . field then start typing the name of whatever the OS. In the case of debian-10.4.0-amd64-netinst.iso, you'd want to type Debian then select Debian 10. Continue and allocate whatever RAM/CPU you want to give the server; The defaults will generally be adequate but you may want to bump them up a little. Storage is even more specific to the service but 30 GB will typically do well unless you're running a storage or media server. In those cases, you'll want a lot more. Continuing, name the volume whatever you'd like (nc1 for Nextcloud, ws1 for a webserver, something along those lines), and optionally tick the box to Customise configuration before install. I do recommend this as you'll be able to delete some extraneous features like a virtual sound card.

VM config

  1. Boot Options
    1. ✅ Start virtual machine on host boot up
  2. Right-click and remove Sound <card>

Unless you know you want to change something else, leave the rest as it is for now and click ✅ Begin Installation at the top of that window. Once it's finished, you'll be dropped to the OS's installer screen. See the related guides for Debian, Alpine, and Ubuntu.