Simple experiment with systemd-networkd and systemd-resolved

In my previous post, I wrote about how simple it was to create containers with systemd-nspawn.

But what if you wanted to expose to the outside network to a container? The rest of the world can’t add mymachines to /etc/nsswitch.conf and expect it to work, right?

And what if you were trying to reduce the installed dependencies in an operating system using systemd?

Enter systemd-networkd and systemd-resolved

Firstly, this Fedora 25 host is a kvm guest so I added a new network interface for “service” were I created the bridge (yes, with nmcli, why not learn it as well on the way?)

nmcli con add type bridge con-name Containers ifname Containers
nmcli con add type ethernet con-name br-slave-1 ifname ens8 master Containers
nmcli con up Containers

Then, in container test, I configured a rule to use DHCP (and left in a modicum of a template for static addresses, no… that’s not my network) and replaced /etc/resolve.conf with a symlink to the file systemd-resolved manages:

cat <<EOF > /etc/systemd/network/20-default.network
[Match]
Name=host0

[Network]
DHCP=yes
# or swap the above line by the lines below:
#Address=192.168.10.100/24
#Gateway=192.168.10.1
#DNS=8.8.8.8
EOF

rm /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

Finally, I enabled and started networkd and resolved:

systemctl enable systemd-networkd
systemctl enable systemd-resolved
systemctl start systemd-networkd
systemctl start systemd-resolved

A few seconds later…

-bash-4.3# ip addr list dev host0
2: host0@if29: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
 link/ether 06:14:9c:9e:ac:ca brd ff:ff:ff:ff:ff:ff link-netnsid 0
 inet 192.168.10.92/24 brd 192.168.10.255 scope global host0
 valid_lft forever preferred_lft forever

-bash-4.3# cat /etc/resolv.conf 
# This file is managed by systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known DNS servers.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 192.168.10.1

Happy hacking!

Simple experiment with systemd-nspawn containers

For this test I used Fedora 25. Your mileage might vary in other operating systems, some things may be the same, some may not be.

WARNING: you’ll need to disable selinux so to me this was merely an interesting experiment and it lead to increasing my knowledge, specially in relation to selinux + containers. Bad mix, no security, containers don’t contain, etc.

Many thanks to the nice people from #fedora and #selinux that graciously lent their time to help me when I was trying to use nspawn with selinux enabled. With their help, specially Grift from #selinux, we were actually able to run it, but only in a way I’m so uncomfortable with that I ultimately considered this experiment to  be a #fail as I’m definitely not going to use them like that any time soon: there’s still a lot of work to do in order to run containers with some security. I hope the Docker infatuation leads to an universal solution towards security + containers from the good engineers at Red Hat and others involved in that work.

But it certainly was a success in terms of contributing to more experience beyond a quickly expiring benefit of familiarity with OpenVZ.

Enough words, here’s how simply it was…

Firstly, let’s setup a template from which we’re going to copy to new instances. As I’m using Fedora 25, I used DNF’s capability to install under a directory:

dnf --releasever=25 \
 --installroot=/var/lib/machines/template-fedora-25 \
 -y install systemd passwd dnf fedora-release \
 iproute less vi procps-ng tcpdump iputils

You’ll only need the first three lines, though, the fourth was just a few more packaged I preferred to have in my template.

Secondly, you’ll probably like to do further customization in your template, so you’ll enter your container just like it was (well, is) an enhanced chroot:

cd /var/lib/machines
systemd-nspawn -D template-fedora-25

Now we have a console, and the sky is the limit for what you can setup, like for instance defining a default pasword for root with passwd (but maybe you’ll not want to do this in a production environment).

For some weird reason, passwd constantly failed manipulating authentication tokens, but I solved it quickly by merely reinstalling passwd (dnf -y reinstall passwd). Meh…

I also ran dnf -y clean all before exiting the container in order to clean up unnecessary space wasted with package meta data that will be expired quickly.

When you’re done customizing, exit the container with ctrl + ]]] in about a second.

Finally, we’re ready to preserve the template:

cd template-fedora-25
tar --selinux --acls --xattrs czvf \
    ../$(basename $( pwd ) )-$(date +%Y%m%d).tar.gz .
cd ..

We’re now ready to create a test container and launch it in the background:

mkdir test
cd test
tar --selinux --acls -xattrs xzvf \
    ../template-fedora-25-20170701.tar.gz
cd ..
machinectl start test

This container will probably not be able to run services exposed outside without help but you can login into its console with machinectl login test

You’ll also have automagic name resolution from your host computer to the containers it runs if you change the hosts entry in /etc/nsswitch.conf placing mymachines between files and dns (or as you see fit if otherwise in your setup):

hosts: files mymachines dns myhostname

If you had enable ssh in your container, you’d be able to do ssh test from the host machine. Or access a web server you installed in it. Who knows.

As you saw, despite a lot of words trying to explain every step of the way, it’s excruciatingly simple.

The next article (Simple experiment with systemd-networkd and systemd-resolved) expands this example with a bridge in the host machine in order to allow your containers to talk directly with the external world.

Happy hacking!

#lesigh… Random idiot speculates #systemd was compromised by Red Hat for the #NSA

Le sigh… it seems that in the discussion between SystemD or Upstart in Debian, a random idiot speculated that SystemD would have been adultered by Red Hat for the NSA.

Bravo, what a way to poison a sane discussion, troll.

Since it’s Free Software, how about you rebuild it from scratch and you check it out yourself whether it really and you better post some evidence, alright?

Speculating such garbage is not helpful.

pump.io as systemd service

So what’s wrong with this first very basic attempt to start pump.io as a systemd service?

[Unit]
Description=Pump.io
Requires=redis.service
After=redis.service

[Service]
Type=simple
ExecStart=/opt/pump.io/pump.io/bin/pump
Restart=on-abort

[Install]
WantedBy=multi-user.target

This is what’s wrong….

Sep 05 20:49:38 p.1407.org sudo[17482]: rms : TTY=pts/1 ; PWD=/opt/pump.io/pump.io ; USER=root ; COMMAND=/bin/systemctl start pumpio.service
Sep 05 20:49:38 p.1407.org systemd[1]: Starting Pump.io...
Sep 05 20:49:39 p.1407.org pump[17485]: path.js:360
Sep 05 20:49:39 p.1407.org pump[17485]: throw new TypeError('Arguments to path.join must be strings');
Sep 05 20:49:39 p.1407.org pump[17485]: ^
Sep 05 20:49:39 p.1407.org pump[17485]: TypeError: Arguments to path.join must be strings
Sep 05 20:49:39 p.1407.org pump[17485]: at path.js:360:15
Sep 05 20:49:39 p.1407.org pump[17485]: at Array.filter (native)
Sep 05 20:49:39 p.1407.org pump[17485]: at Object.exports.join (path.js:358:36)
Sep 05 20:49:39 p.1407.org pump[17485]: at getConfig (/opt/pump.io/pump.io/bin/pump:58:23)
Sep 05 20:49:39 p.1407.org pump[17485]: at main (/opt/pump.io/pump.io/bin/pump:40:18)
Sep 05 20:49:39 p.1407.org pump[17485]: at Object.<anonymous> (/opt/pump.io/pump.io/bin/pump:151:1)
Sep 05 20:49:39 p.1407.org pump[17485]: at Module._compile (module.js:456:26)
Sep 05 20:49:39 p.1407.org pump[17485]: at Object.Module._extensions..js (module.js:474:10)
Sep 05 20:49:39 p.1407.org pump[17485]: at Module.load (module.js:356:32)
Sep 05 20:49:39 p.1407.org pump[17485]: at Function.Module._load (module.js:312:12)
Sep 05 20:49:39 p.1407.org systemd[1]: pumpio.service: control process exited, code=exited status=8
Sep 05 20:49:39 p.1407.org systemd[1]: Failed to start Pump.io.
Sep 05 20:49:39 p.1407.org systemd[1]: Unit pumpio.service entered failed state.

An ideas why? Starting from the console with sudo /opt/pump.io/pump.io/bin/pump works fine.

Grrr…