Lxc-set-me-up: Set up a container to test Mir on assorted OSs

Like many projects we support a range of target systems and occasionally problems arise on them. Typically Debian/sid, Fedora/rawhide or Alpine/edge gets an update and we see a failure to build from source [FTBFS].

To debug these problems it is helpful to set up a test system, and lxc provides a useful way to do this.

For testing with Mir, it is convenient to have ssh access to this machine (Mir works over X-forwarding). Setting this up can be a small roadbump as the incantations to create a user with ssh access varies from one OS to another. To easy my way, and maybe yours, I’ve converted my notes to a helper script (shown below).

With this script I can now do something along the lines of:

$ lxc-set-me-up ubuntu:20.04      ubuntu-20-04
$ ssh -XC

After this, I can build and test Mir in the target environment whilst running on my normal desktop:

I hope this is useful. Or, maybe, there’s a better way that everyone knows but me?

#!/bin/sh -e

if [ $# -lt 2 ] || [ "$1" = "--help" ]; then
  echo "Script to set up a container with ssh access for the current user"
  echo "Usage: $0 <image> <name>"
  echo "Examples:"
  echo "  o lxc-set-me-up ubuntu:20.04       ubuntu-20-04"
  echo "  o lxc-set-me-up images:debian/sid  debian-sid"
  echo "  o lxc-set-me-up images:fedora/34   fedora-34"
  echo "  o lxc-set-me-up images:alpine/edge alpine-edge"
  exit 1

lxc launch "$1" "$2"
lxc config set "$2" limits.cpu 10
lxc config set "$2" security.nesting true
if [ "${1#images:fedora}" != "$1" ]; then
  lxc exec "$2" -- adduser "$USER"
  lxc exec "$2" -- usermod -aG wheel "$USER"
  lxc exec "$2" -- dnf install openssh-server --assumeyes
  lxc exec "$2" -- systemctl start sshd.service
elif [ "${1#images:alpine}" != "$1" ]; then
  lxc exec "$2" -- apk update
  lxc exec "$2" -- apk add openssh-server sudo
  lxc exec "$2" -- adduser -D "$USER"
  lxc exec "$2" -- adduser "$USER" wheel
  lxc exec "$2" -- rc-update add sshd
  lxc exec "$2" -- service sshd start
  echo "$USER:$USER" | lxc exec "$2" -- chpasswd
  lxc exec "$2" -- apt install --assume-yes openssh-server
  lxc exec "$2" -- adduser --disabled-password --gecos "$USER" "$USER"
  lxc exec "$2" -- adduser "$USER" sudo
echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" | lxc exec "$2" -- tee "/etc/sudoers.d/$USER" > /dev/null
lxc exec "$2" -- su "$USER" -c "mkdir -p  /home/$USER/.ssh"
lxc exec "$2" -- su "$USER" -c "cat > /home/$USER/.ssh/authorized_keys" < ~/.ssh/id_rsa.pub
lxc exec "$2" -- su "$USER" -c "cat <<EOT >> /home/$USER/.bashrc
if [ -f ~/.Xauthority ]; then
  export XAUTHORITY=~/.Xauthority
  ip4_address=$(lxc list "$2" -c 4 -f csv)
  [ -n "${ip4_address}" ]
  sleep 1
echo "${ip4_address}"
1 Like