LXC/LXD install a VM on the Pi
I have had the Pi5 running a LXD container deploying a local website for a while, but wanted a separate container with a graphical login into a desktop rather than just a console. As I am using Ubuntu Server on the Pi5 and use Ubuntu desktop on my laptops, I decided to go with Ubuntu rather than any other flavour of Linux. I will try Arch Linux or Alma Linux later, but will use Ubuntu for now.
As an aside I have a couple of old Lenovo Thinkpads running Ubuntu which I use for Plex Media Server (soon to be transferred to the PI) and it turns out my Tai Chi instructor has the same machine on which he runs Arch Linux. He is a diehard Linux user using it for a couple of years more that I have (1999). That turned into very interesting conversation about our Linux experiences which resulted in me being late to pick up my wife from her Aqua Aerobics class.
Back to installing a VM on the Pi in a container.
It turns out that you can do this very easily with LXC/LXD, so here are the steps I used to set it up. The Ubuntu LXC/LXD documentation is great help.
To view the graphical desktop on the Pi5 from your laptop or desktop, you will need a viewer. There are several but virt-viewer is recommended. Install virt-viewer on your laptop or desktop and ssh -X into your Pi5. This will enable you to view the remote graphical display in the VM from your laptop.
sudo apt install virt-viewer
On the Pi5, initialize or launch an empty container with a --vm option and give it a name.
lxc launch <ubuntu-version> <container-name> --vm
If you want to just create it then use:
lxc init <ubuntu-version> <container-name> --vm
To run the VM you will need to configure it. Usually you configure with the number of CPUs and the amount of memory for the VM.
Add in the required machine configuration for the VM. I used 4GB of memory.. 2GB would probably work but felt like Ubuntu was struggling when I tried it. The option to set the memory is --config.limits.memory.
lxc launch images:<ubuntu/<version>/desktop ubuntu-desktop --vm --config limits.memory=4GiB
Config the instance for the number of CPUs to use:
lxc config set <container-name> limits.cpu=1 limits.memory=4GiB
Add in the amount of disk space that you want on the VM. 25GB is usually a minimum. I used 50GB.
Add the device to the VM that you want to load the image from for the destktop. i.e. a CDROM.
Start up the container to get the desktop installed use a console so that you can update it etc.
lxc start <container-name>
Install the lxd-agent-loader.
sudo apt install lxd-agent-loader
To quote documentation:
The LXD VM agent enables access to advanced LXD features such as file transfer and command spawning inside virtual machines run by LXD. This package contains init scripts that will automatically load the agent and run it when started in a LXD VM.
If you do not install the agent you not be able to do anything.
Stop the container
lxc stop <container-name>
Start it up again with console login i.e. with console=vga option
lxc start <container-name> console=vga
You should see the system boot up and the graphical desktop load up and start
Get rid of the CDROM device when Ubuntu prompts you to do so.
That is it.
There is a working LXC/LXD container with a graphical login on the Pi5.