Adding an LXC container with/to virt-manager (Virtual Machine Manager)

Posted on Mon 20 March 2023 in LXC

بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ

Virtual Machine Manager (or virt-manager) is a desktop GUI tool meant to simplify the process of dealing with VMs or containers on Linux desktop machines.

Before I begin this tutorial I will give my verdict on Virtual Machine Manager regarding LXC containers. This software is terrible at what it is supposed to do for 2 main reasons:

  1. It has some of the worst documentation of any Open Source project (essentially none on their website)
  2. It doesn't actually help create an LXC container at all. This tutorial demonstrates how to 'connect' an existing LXC container (which we will create on the command-line) to the GUI software via the most arduous processes possible

I would urge people to consider alternative GUI options for LXC containers. But if you went through the effort of installing virt-manager (I will use this name to refer to the software in the rest of this tutorial), this guide is for you.

The source references for my tutorial are: Linux Containers via LXC and Libvirt

Accessing remote KVM/QEMU vms with virt-manager

Unprivileged LXC container with libvirt

Make sure LXC, virt-manager and all the packages needed for virt-manager to work with LXC are installed.

Create the LXC container

Run this command:

sudo lxc-create --template download --name testapp (interactive, so you choose the options step-by-step)

or this:

sudo lxc-create -n testapp -t download -- --dist ubuntu --release focal --arch amd64

A container called testapp should now exist.

Creating the XML file

Make a directory/folder for the XML file:

mkdir LXC

Within this directory create an XML file testapp.xml and use the following contents:

<domain type='lxc'>
  <name>testapp</name>
  <memory unit='KiB'>4276800</memory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64'>exe</type>
    <init>/lib/systemd/systemd</init>
  </os>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/lib/libvirt/libvirt_lxc</emulator>
    <filesystem type='mount' accessmode='passthrough'>
      <source dir='/var/lib/lxc/testapp/rootfs'/>
      <target dir='/'/>
    </filesystem>
    <interface type='network'>
      <source network='default'/>
    </interface>
    <console type='pty'>
      <target type='lxc' port='0'/>
    </console>
  </devices>
</domain>

Only 2 lines need to be changed when adding new containers:

<name>testapp</name>

and:

<source dir='/var/lib/lxc/testapp/rootfs'/>

Setting a root password

LXC containers come with no passwords and this is where the pointless need for virt-manager arises (as one needs to run commands which all but defeat the need for a GUI)

Start the container:

sudo lxc-start -n testapp

Go into the container root:

sudo lxc-attach -n testapp

and now set a root password by typing (and then following the prompts): passwd

Now stop the container:

sudo lxc-stop -n testapp

Start container within GUI

Open virt-manager and you will see testapp under LXC. Select run and once running, select open. This will take you to a command-line interface within virt-manager asking for login details. Enter root for the user and whatever password you used in the previous step.

Creating a base container to clone

A nifty trick with system containers that I've used for years is to create a 'base' container that contains all the required packages (wget, git, ssh and all the sundry library packages) and then clone this container for other projects. It also saves on bandwidth.


If you don't know how to use RSS and want email updates on my new content, consider Joining my Newsletter

The original content of this blog is a Waqf solely for the Pleasure of Allah. You are hereby granted full permission to copy, download, distribute, publish and share this content without modification under condition that full attribution is given to this author by creating a link either above or below the content that links back to the original source of the content. For any questions or ambiguity, you are requested to contact me via email for clarification.