Packaging:Tmpfiles.d

From FedoraProject

Jump to: navigation, search

Contents

Overview

tmpfiles.d is a service provided by both systemd and upstart in Fedora 15 and later for managing temporary files and directories for daemons. In this guideline we mainly concentrate on how it is used to populate /var/run and /var/lock. In Fedora 15 and later, /var/run and /var/lock are tmpfs filesystems. As such, they are created empty on every reboot. For files intended to be placed into those directories, this should normally not pose any problems. For directories, however, we often need to create the directories ahead of time. This is best done using the tmpfiles.d mechanism that both upstart and systemd share.

Note.png
EPEL difference
Like Fedora releases older than 15, EL-6 and older does not support tmpfiles.d.

tmpfiles.d configuration

Configuring tmpfiles.d just involves dropping a file into %{_sysconfdir}/tmpfiles.d/ that tells the init system what directories need to be created.

For example, if the package needs a few directories to be created in /var/run in order for it to run, the packager needs to create a file named %{name}.conf that is installed as %{_sysconfdir}/tmpfiles.d/%{name}.conf. That file has the following lines:

D /var/run/NAME 0710 root GROUP -

The format of the line is as follows:

Information on other options is available on the tmpfiles.d man page should you need to do something more advanced.

Example spec file

In the spec file, the packager needs to install the tmpfiles.d conf file and also make sure the directory is included in the rpm.

# tmpfiles.d configuration for the /var/run directory
Source1:  %{name}-tmpfiles.conf
Requires: initscripts
[...]

%install
mkdir -p %{buildroot}%{_sysconfdir}/tmpfiles.d
install -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/tmpfiles.d/%{name}.conf

# The next two lines may not be needed if the upstream's install script creates them
mkdir -p %{buildroot}%{_localstatedir}/run/
install -d -m 0710 %{buildroot}%{_localstatedir}/run/%{name}/
[...]

%files
%dir %{_localstatedir}/run/%{name}/
%config(noreplace) %{_sysconfdir}/tmpfiles.d/%{name}.conf

Files that the program places directly into /var/run or /var/lock or into subdirectories of those may be listed in the %files section as %ghost but you may also omit them entirely as the files will be cleaned up on every reboot.

Why not create the directories with XXXXXX instead?

There are multiple ways to try creating the directories but most suffer some disadvantage that tmpfiles.d addresses:

Have the daemon create the directory when it starts up

Many times, daemons run as an unprivileged user who would not be allowed to create new directories directly into /var/run or /var/lock. If the daemon does not drop privileges, then you can patch it to create the files and directories when the daemon starts and submit the patch upstream.

Have the init script create the directory when it starts up the daemon

Since the init script is run by root, before the daemon drops privileges, why not create the directories there?