8

I use systemd-nspawn to run a few containers. I can have them started in the background using systemctl start systemd-nspawn@foo. On occasion, however, I start with systemd-nspawn -bD foo. I couldn't find any way to send it to the background. Closing the terminal just kills the container as machinectl list shows. Can I do so, and if so, how?

I understand a container is much more than a single process, but in this sense, the expected effect is the same as backgrounding a process - I want the container running, but my original shell given back to me.

1
  • 1
    @mikeserv machinectl kill -sTSTP foo seems to have no discernable effect.
    – muru
    Commented Jul 5, 2015 at 15:52

2 Answers 2

2
+50

Ok, so, for what it's worth, the following was successful for me:

sudo systemd-nspawn -bxD/

Practically identical to yours, except I don't give the machine a name and I get an -x ephemeral btrfs snapshot of my / for the container's root.

That brought up the container's getty on my terminal's pty and I logged in to login and all.

I confess I was a bit stumped for a little while, but after a little poking at systemctl in the container w/ zsh <tab> completion I came up with (run from within the container):

systemctl stop console-getty.service

==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or other units.
Authenticating as: mikeserv
Password:
==== AUTHENTICATION COMPLETE ===

Which got the machine to surrender its terminal control. The only thing is, I started that with sudo - which also gets its own layer of terminal control to authenticate in the first place. This left me with a blank terminal, and no amount of kill -CONT "$(pgrep ksh)" was doing me any good. And so I was again stumped for a moment or two, but (in another terminal)...

sudo fuser -v /dev/pts/*

                     USER        PID ACCESS COMMAND
/dev/pts/0:          mikeserv   8347 F.... zsh
                     root      18003 F.... sudo
/dev/pts/13:         mikeserv   9553 F.... zsh
                     mikeserv  16838 F.... ksh
                     root      17657 F.... sudo
                     root      17658 F.... systemd-nspawn
/dev/pts/14:         root      17675 F.... systemd

Gave me the above list, and so I thought - what the hell?

sudo kill -STOP 17657

And - lo and behold - I had ksh back in the original terminal. To wrap it up, I needed to verify I could still access the machine, though, of course, else it would be useless:

machinectl -l

MACHINE                    CLASS     SERVICE
localhost-35ceaa76b1306897 container nspawn

Ok...

sudo machinectl login localhost-35ceaa76b1306897

Connected to machine localhost-35ceaa76b1306897. 
Press ^] three times within 1s to exit session.

Arch Linux 4.0.7-2-ARCH (pts/0)

localhost-35ceaa76b1306897 login:

And I got another getty on another terminal!

2

I have a similar issue. My approach is to use dtach to control attaching and detaching from the terminal that is running the container. Then inside the container I can run tmux / screen etc.

machinectl will allow you to run containers at boot, but as of this writing, debian stable does not have a version of systemd that includes this feature.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .