How do I background a systemd-nspawn container?

5,915

Solution 1

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!

Solution 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.

Share:
5,915

Related videos on Youtube

muru
Author by

muru

I have been using Linux primarily since 2009, usually Arch Linux (&lt;3) and Ubuntu. I like C++, Python and Bash (but use zsh as my primary shell).

Updated on September 18, 2022

Comments

  • muru
    muru almost 2 years

    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.

    • muru
      muru almost 9 years
      @mikeserv machinectl kill -sTSTP foo seems to have no discernable effect.