Since I’ve been running archlinux on some of my systems, one thing I’ve found useful is systemd-nspawn. systemd-nspawn containers (or chroots on non-systemd systems) give you a quick way to install a Linux distribution, that can run inside an existing Linux system.
Some cases where systemd-nspawn containers (referred to as containers in this document) are useful:
- At one point, OpenEmbedded would not build with GCC 4.8 (this is no longer the case with recent versions of OE). So a Debian or Ubuntu OS container was a quick way to get builds going again.
- For product build systems (may live for many years), typically OE will eventually break as you upgrade the workstation distribution. For projects that need a long-lived OpenEmbedded build system, setting it up in a chroot makes a lot of sense.
- Someone might be having a compile or build problem with a distribution you don’t currently have installed. With containers, you can quickly set up a test distribution to reproduce problems.
- I’ve had cases where I need an older version of Qt for a project, but my workstation includes a newer version. Again, setting up a OS container is sometimes simpler than getting two versions of Qt to dwell together peaceably in the same distribution.
- Backing up or replicating your entire build system is very easy — simply rsync the OS container directory to another machine.
So the solution is to select a relatively stable, long-lived distribution to host your product builds. Debian is good choice. Because the container is simply a directory in the host workstation filesystem, you can use host workstation tools (editors, git, etc) directly in the container filesystem. The only thing you need to use the chroot for is the actual building. If you make sure the user ID is the same between your workstation and nspawn container, then permissions are seamless — you can easily access files in the container from the context of your host workstation.
To set up a nspawn-container:
- Install debootstrap. On arch systems, this needs to be obtained for the AUR.
- host: sudo debootstrap –arch=amd64 wheezy ~/debian-wheezy/
- host: sudo systemd-nspawn -D ~/debian-wheezy/
- container: apt-get update && apt-get install ssh
- container: edit /etc/ssh/sshd_config, and set port to something other than 22 (23 in this example)
- container: /etc/init.d/ssh start
(This systemd-nspawn man page gives examples for setting up other distributions.)
To set up a user in your container:
- host: id
- will return something like: uid=1000(cbrake) gid=100(users) …
- container: adduser –uid 1000 -gid 100 cbrake
- host: ssh-copy-id -p 23 localhost (will copy public key to container)
Now, on the host system, you can simply “ssh -p23 localhost” any time you want to log into the container. Soft links between the project workspace on the host system, and the container can also make shifting between the two easier.
An alternative way to start the container once its set up is:
sudo systemd-nspawn -D ~/debian-wheezy /sbin/init
Its also handy to make the shell prompt in the container slightly different than the host OS so that you can easily tell the difference. To accomplish this, add the following to ~/.profile in the container OS:
export PS1=”[\u@wheezy \w]\$ “
To create a service that starts your container, put something like the following in /lib/systemd/system/debian-wheezy.service
[Unit]Description=Debian Wheezy[Service]ExecStart=/usr/bin/systemd-nspawn -D /scratch/debian-wheezy/ /sbin/init 3KillMode=process[Install]WantedBy=multi-user.target
Hopefully this gives you a quick overview of how OS containers can be set up, and used in your OpenEmbedded build systems.