Start a systemd service inside chroot from a non systemd based rootfs
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 Start A Systemd Service Inside Chroot From A Non Systemd Based Rootfs
00:34 Answer 1 Score 3
01:04 Answer 2 Score 43
02:18 Answer 3 Score 11
02:34 Answer 4 Score 2
02:50 Thank you
--
Full question
https://superuser.com/questions/688733/s...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#unix #systemd #chroot #sysvinit #openrc
#avk47
ANSWER 1
Score 43
A well-known problem in systemd distros (Arch Linux, OpenSUSE, Fedora).
Systemd replaces sysvinit, and provides one great advantage over this. In sysvinit, when you ask a service to start, it inherits the execution context of the person invoking the script, which includes environment variables, ulimits, and so on. Systemd improves on this at the contrary by notifying a daemon, which will start the service in a well-defined, healthy, constant environment, where of course the performances of the services are much easier to predict, since the environment is always the same.
This implies that, when I call systemctl from within the chroot, it is irrelevant that I am inside chroot, the environment that will be inherited is still that of PID 1, not my current one. But it gets worse than this: since communication sockets are placed inside /run/systemd, a process in a chroot will not even be able to talk to the init system!
So how do you go about chroot'ing in systemd distros?
If all you want to do is have a Linux container, this Arch Wiki page will tell you how to set up a Linux container in less than 30 seconds, thanks to
systemd-nspawn
.If instead you really want a chroot environment, this beautiful and crystal clear Web page will provide you with two working solutions (the second one is a modified version of the one offered at point #1).
ANSWER 2
Score 11
systemd
only ignores "services", so I just run the daemon commands manually.
So instead of
service sshd start
I use
/usr/sbin/sshd -D &
ANSWER 3
Score 3
No. Services are executed by systemd (pid 1), not by systemctl directly (which only sends a start request), and since systemd runs outside the chroot, so will the service.
Although technically it could be possible to implement this (by making systemctl somehow pass its root to systemd), it is somewhat unlikely to happen since there already is a tool for creating full containers (systemd-nspawn /somepath/to_root
). You could always contact the mailing list though.
ANSWER 4
Score 2
Faced this problem once tried to bring up network in rescue mode using network configuration from chroot. Finally this works for me:
service --skip-redirect <service> restart
or:
SYSTEMCTL_SKIP_REDIRECT=_ /etc/init.d/<service> restart