Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debian: Add a systemd service file for LMS #18

Merged
merged 3 commits into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ Source: logitechmediaserver
Section: sound
Priority: optional
Maintainer: Logitech/Slim Devices <debian@slimdevices.com>
Build-Depends: debhelper (>= 9)
Build-Depends: debhelper (>= 9), dh-systemd (>= 1.5)
Standards-Version: 3.9.5

Package: logitechmediaserver
Architecture: all
Conflicts: slimp3,slimserver,squeezecenter,squeezeboxserver
Replaces: slimp3,slimserver,squeezecenter,squeezeboxserver
Depends: perl (>= 5.8.8), adduser, libc6, libgcc1, libstdc++6, zlib1g, libio-socket-ssl-perl, ca-certificates
Depends: ${misc:Depends}, perl (>= 5.8.8), adduser, libc6, libgcc1, libstdc++6, zlib1g, libio-socket-ssl-perl, ca-certificates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: Perl 5.10 is now the minimum supported :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that's Debian 4 (Etch) out of the running !
I'll put in another PR.

Pre-Depends: ${misc:Pre-Depends}
Description: Streaming Audio Server
Logitech Media Server is a cross-platform streaming media server that supports a wide range
of formats, including AAC, AIFF, FLAC, Ogg Vorbis, MP3, WAV, and WMA.
8 changes: 7 additions & 1 deletion debian/logitechmediaserver.default
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# User to run Logitech Media Server as
# User to run Logitech Media Server as.
# Only applicable to platforms that do not use systemd.
# Running the server as a user other than the default squeezeboxserver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you'd stick with squeezeboxserver for the user but use logitechmediaserver in most other cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've chosen to stick with what the existing installer does, this proposed change only adds a systemd unit file to the existing installation.

The existing installation is somewhat "conflicted". It creates the user squeezeboxserver, and sets up directories under a squeezeboxserver hierarchy. (/var/lib/squeezeboxserver, /var/log/squeezeboxserver).

logitechmediaserver is used only (I think) as the service name (i.e. the init script is named /etc/init.d/logitechmediaserver) and some items in the /etc hierarchy use that name. (So we have /etc/default/logitechmediaserver, /etc/logrotate.d/logitechmediaserver, but /etc/squeezeboxserver).

The systemd unit file that this change installs is named logitechmediaserver.service. Keeping the service name consistent between SysV and systemd is important, otherwise systemd will launch two instances - once from the unit file and one from the SysV init script.

A tidying up exercise might be nice to do, but I wouldn't wish to incorporate it into this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree that keeping things as similar as possible to the current set up is the way to go. At the expense of maintaining the current inconsistency in naming.

# is not recommended.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a brief note explaining how to change the user with systemd

On platforms running systemd this will be ignored. 
If you want to change the user LMS runs as you need to use a 
systemd override file. 
With recent versions of systemd you can simply do this:
- sudo systemctl edit logitechmediaserver
- in that file add the line 
User=<new username>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly.

Bear in mind that the Debian installer sets everything up to run under user squeezeboxserver.

I would not personally be inclined to encourage users to depart from that. Knowledgeable users can figure it out for themselves.

That's my take, anyway. But I'm more than happy to change if that's what's wanted.

SLIMUSER=squeezeboxserver

# Additional options for the server.
# For example: SLIMOPTIONS="--failsafe --nomysqueezebox"
# Use '/usr/sbin/squeezeboxserver --help' to obtain a full listing.
SLIMOPTIONS=""
174 changes: 174 additions & 0 deletions debian/logitechmediaserver.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#=======================================================================
# Unit section
#=======================================================================

[Unit]

Description=Logitech Media Server

# Ensures LMS is shutdown before network. And that the network service
# has at least been started.

After=network.target

# Make LMS wait for network-online.target (if it gets started).
# See notes below.

After=network-online.target

#-----------------------------------------------------------------------
# Some notes on networking targets:
#-----------------------------------------------------------------------

# We leave it to the user to specify 'Wants=network-online.target' if he
# chooses, because it may not suit all users. But we specify
# 'After=network-online.target' because the SysV init script has
# essentially behaved that way by virtue of the 'Required-Start: $all'
# stanza included within it.
# This is also the approach taken in Debian's & Ubuntu's implementation
# of 'rc.local.service'.
#
# On systemd >= 217, user might execute:
# 'systemctl add-wants logitechmediaserver.service network-online.target'
#
# The effects of the 'After' and 'Wants' 'network-online' stanzas are
# very system dependent. They may or may not ensure that LMS starts up
# after network connections have been established, and they may or may
# not cause a delay in LMS start up.
#
# 'After=network-online.target' schedules LMS to start after the network
# is fully up, whatever that may mean. But it is only effective if the
# 'network-online.target' is active and, even then, it is only effective
# if the user's system has been, or can be, configured to wait
# appropriately for network connections to be established before
# reaching this target.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the very detailed comments

#
# 'Wants=network-online.target' would activate (pull in) that target.
#

#=======================================================================
# Service section
#=======================================================================

[Service]

#-----------------------------------------------------------------------
# Set run time variables, essentially replicating the SysV init script.
#-----------------------------------------------------------------------

# Use of '/etc/default/logitechmediaserver' to set run time variables
# is supported, for consistency with the init script.
# But setting 'SLIMUSER' is not supported. This unit file will always
# start LMS as user 'squeezeboxserver'.
# The user may override that, the methods available depend on the
# installed version of systemd.

# Note one consequence of this approach: LMS can now see environment
# variables that correspond to the defined run time variables, should it
# choose to look (it doesn't at present). This behaviour is an artefact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "defined run time variables" ?
I'd have thought that systemd mean less leakeage of environment between invoking environment and runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the shell variables that are defined in the init script and defaults file. These are used to build the LMS command line. Perhaps I might have said "defined run time parameters". Or something else. I'll try to find some better words.

A systemd unit file does not provide for parameter substitution (no "local variables"), so I am using environment variables to achieve the same trick, i.e. read the defaults file to set environment variables, which we can then use. But that, of necessity, leads to a rather full environment. I believe that some other projects may have adopted the same approach.

This is all predicated on the notion that we really do wish to support use of the existing defaults file. I suspect that its main role is in defining SLIMOPTIONS.

Another way of doing it would be to abandon the defaults file altogether and teach users how to edit their unit files, possibly after explaining to them why the upgrade didn't "just work"... :(

# of the approach adopted by this unit file and may well change. It
# should not be relied upon in any way.

# Set PATH - same value as set by SysV init script.

Environment="PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

# Default run time settings. These are used as arguments to LMS when it
# is launched.

Environment="LOGDIR=/var/log/squeezeboxserver/"
Environment="PREFSDIR=/var/lib/squeezeboxserver/prefs"
Environment="CACHEDIR=/var/lib/squeezeboxserver/cache"
Environment="CHARSET=utf8"

# Supplementary run time settings may be defined in
# '/etc/default/logitechmediaserver'. Entries made here would override
# the defaults defined above.

EnvironmentFile=-/etc/default/logitechmediaserver

#
# If you want to add additional options use
# /usr/sbin/squeezeboxserver --help
# for the supported options and place them into the file:
# /etc/default/logitechmediaserver.
# For example:
# SLIMOPTIONS="--failsafe --nomysqueezebox"
#

#-----------------------------------------------------------------------
# Set up user and logging.
#-----------------------------------------------------------------------

# Remark: User can't be parameterized in a unit file. We could choose to
# run LMS as root, and pass the USER option to it. But that adds
# unnecessary risk.

User=squeezeboxserver
Group=nogroup

# LMS will fail to start if a writeable log directory does not exist and
# it cannot create it. This can easily happen where '/var/log' is
# mounted on a temporary file system and requires root permissions to
# create.
# This stanza instructs systemd to create the standard directory
# '/var/log/squeezeboxserver' if it does not exist, with appropriate
# permissions and ownership.
# The user may specify a different directory by setting the 'LOGDIR'
# run time variable, but it is then his responsibility to ensure that
# it exists and is writeable.
# Note: Only effective in systemd >= 235. Earlier releases will see
# harmless 'Unknown lvalue' messages logged.

LogsDirectory=squeezeboxserver
LogsDirectoryMode=0755

#-----------------------------------------------------------------------
# Execution start
#-----------------------------------------------------------------------

# We want LMS to handle SIGPIPE, as it can help terminate misbehaving
# transcoder pipelines.

IgnoreSIGPIPE=no

# LMS is stable. So we don't want systemd to restart it if it fails.
# That just obscures errors.

Restart=no

# We do not instruct LMS to daemonize with the '--daemon' option,
# systemd prefers that we don't. We need 'Type=simple', or 'Type=exec'.

# Type currently defaults to 'simple', but may usefully default to
# 'exec' in future ('exec' available in systemd >= 240). So left unset.

#Type=simple

ExecStart=/usr/sbin/squeezeboxserver --prefsdir $PREFSDIR --logdir $LOGDIR --cachedir $CACHEDIR --charset $CHARSET $SLIMOPTIONS

#-----------------------------------------------------------------------
# Execution termination
#-----------------------------------------------------------------------

KillMode=control-group
KillSignal=SIGTERM

# Never send HUP, LMS uses this to reinitialize cache, etc.
SendSIGHUP=no

# Same timeout as SysV init script. We definitely want to send that
# final SIGKILL.
TimeoutStopSec=30
SendSIGKILL=Yes

#=======================================================================
# Install section
#=======================================================================

[Install]
# Directly equivalent to run levels 2,3,4.
# Also run level 5, 'graphical.target', because that pulls in
# 'multi-user.target'.

WantedBy=multi-user.target
11 changes: 10 additions & 1 deletion debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ source=$(CURDIR)/../server

# main packaging script based on dh7 syntax
%:
dh $@
dh $@ --with=systemd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does adding this arg do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It triggers debhelper into adding the necessary boiler plate to the install scripts to install the systemd unit file, and enable the unit. Explained here:

The method used is that published in the Debian systemd packaging wiki:
https://wiki.debian.org/Teams/pkg-systemd/Packaging


# Be nice to older systems (Debian version <= 7) by removing an 'init-system-helpers'
# package dependency if it has been generated. These systems cannot satisfy the
# dependency, and the LMS package cannot be easily installed if it is left in place.
# (debhelper <= 11 may add the dependency.)
# LMS will install successfully with or without this package.
override_dh_gencontrol:
dh_gencontrol
sed -i '/^Depends/s/init-system-helpers[^,]*,\s*//' ${base}/DEBIAN/control

# Overrides for variances within Logitech Media Server file locations
override_dh_perl:
Expand Down