From Fedora Project Wiki

(apache|httpd => %{name})
Line 7: Line 7:
Configuring tmpfiles.d just involves dropping a file into <code>%{_sysconfdir}/tmpfiles.d/</code> that tells the init system what directories need to be created.
Configuring tmpfiles.d just involves dropping a file into <code>%{_sysconfdir}/tmpfiles.d/</code> that tells the init system what directories need to be created.


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


<pre>
<pre>
D /var/run/httpd 0710 root apache -
D /var/run/NAME 0710 root GROUP -
</pre>
</pre>


Line 16: Line 16:


* <code>D</code> specifies that a directory is to be created if it doesn't exist; empties it if it does exist.
* <code>D</code> specifies that a directory is to be created if it doesn't exist; empties it if it does exist.
* <code>/var/run/httpd</code> is the filesystem path to create
* <code>/var/run/NAME</code> is the filesystem path to create.  Substitute NAME with the name of the directory that's needed
* <code>0710</code> are the permissions to apply to the directory when it is created
* <code>0710</code> are the permissions to apply to the directory when it is created
* <code>root</code> is the owner of the directory
* <code>root</code> is the owner of the directory.  You can change this if needed.
* <code>apache</code> is the group that owns the directory
* <code>GROUP</code> is the group that owns the directory.  GROUP with the group that should own the directory.
* <code>-</code> the last field is for age (if used should be a time specification such as <code>1min</code>) which specifies to delete some files in the directory automatically in regular intervals. This is mostly useful for directories such as /tmp and is seldom used by packages.
* <code>-</code> the last field is for age (if used should be a time specification such as <code>1min</code>) which specifies to delete some files in the directory automatically in regular intervals. This is mostly useful for directories such as /tmp and is seldom used by packages.


Line 29: Line 29:
<pre>
<pre>
# tmpfiles.d configuration for apache's /var/run directory
# tmpfiles.d configuration for apache's /var/run directory
Source1:  httpd.conf
Source1:  %{name}.conf
Requires: initscripts
Requires: initscripts
[...]
[...]
Line 39: Line 39:
# The next two lines may not be needed if the upstream's install script creates them
# The next two lines may not be needed if the upstream's install script creates them
mkdir -p %{buildroot}%{_localstatedir}/run/
mkdir -p %{buildroot}%{_localstatedir}/run/
install -d -m 0710 %{buildroot}%{_localstatedir}/run/httpd/
install -d -m 0710 %{buildroot}%{_localstatedir}/run/%{name}/
[...]
[...]


%files
%files
%defattr(0644, root, root, 0755)
%defattr(0644, root, root, 0755)
%dir %{_localstatedir}/run/httpd/
%dir %{_localstatedir}/run/%{name}/
%config(noreplace) %{_sysconfdir}/tmpfiles.d/httpd.conf
%config(noreplace) %{_sysconfdir}/tmpfiles.d/%{name}.conf
</pre>
</pre>



Revision as of 17:20, 12 January 2011

This page is a draft only
It is still under construction and content may change. Do not rely on the information on this page.

tmpfiles.d is a service provided by both systemd and upstart for managing temporary files and directories. In this guideline we mainly oncentrate on how it is used to populate /var/run and /var/lock. In Fedora 15 and above, /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.

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:

  • D specifies that a directory is to be created if it doesn't exist; empties it if it does exist.
  • /var/run/NAME is the filesystem path to create. Substitute NAME with the name of the directory that's needed
  • 0710 are the permissions to apply to the directory when it is created
  • root is the owner of the directory. You can change this if needed.
  • GROUP is the group that owns the directory. GROUP with the group that should own the directory.
  • - the last field is for age (if used should be a time specification such as 1min) which specifies to delete some files in the directory automatically in regular intervals. This is mostly useful for directories such as /tmp and is seldom used by packages.

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 apache's /var/run directory
Source1:  %{name}.conf
Requires: initscripts
[...]

%install
mkdir -p %{buildroot}%{_sysconfdir}/tmpfiles.d
install -m 0644 %{source1} %{buildroot}%{_sysconfdir}/tmpfiles.d/

# 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
%defattr(0644, root, root, 0755)
%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 this is not required 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?

  • This code would need to be implemented in every init script packaged. Since both upstart and systemd support tmpfiles.d, we can cut down on the number of places we have to put code like this.
  • Having to add the mkdir to the systemd unit files when tmpfiles.d is already in place introduces the need to run shell code for that init script. Systemd is no longer able to handle starting the daemon by itself which slows things down. The shell code also introduces imperative constructs into the otherwise declarative structure which is nice to avoid.
  • Properly labelling the created directories is done automatically by the tmpfiles.d mechanism but would have to be manually done by the init script.