Preferred/Idiomatic method to enable a systemd.service from a Debian package installation
I have a custom Debian package I've built for our software that creates a systemd service. What is the preferred/idiomatic way to have the installation of said package enable/start the service at install time?
What I did was add the following three lines to my postinst
script:
systemctl --system daemon-reload
systemctl enable my_service.service
systemctl start my_service.service
This worked fine until we tried to include loading this package in our initial image creation running under debootstrap
at which point it turned out that systemctl
appears to want a lot of environment stuff set up that just isn't normally up in that process.
I'm looking at a similar situation, I want to configure services in a debootstrap rootfs. It seems systemctl enable
simply reads the targets in the [Install]
section of a unit file and creates the correct symlinks. So if you know what services you want to start under which targets, I think the correct answer might be to just create the correct symlinks. e.g.
ln -s /etc/systemd/system/multi-user.target.wants/ssh.service \
/lib/systemd/system/ssh.service
The other two steps (daemon-reload
and start
) you don't want to do when installing into a debootstrap chroot, but then I guess your problem is how to just do the symlink when in the debootstrap env and the normal way otherwise....
Edit for debootstrap
As it happens I'm also using debootstrap
and I've found systemctl enable
calls work fine so long as the calling user is root
(to avoid the Failed to connect to bus
warning.) daemon-reload
and start
probably don't make sense during an install under chroot since you're not in a live environment. So your postinst
probably wants to detect chroot and skip those lines. Or set an env variable or pass some flag that your installer script can read.
IM_DEBOOTSTRAPPING=yes dpkg -i mypackage.deb # tells postinst to skip systemctl start