1<?xml version='1.0'?> <!--*-nxml-*-->
2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3  "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4<!ENTITY % entities SYSTEM "custom-entities.ent" >
5%entities;
6]>
7<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
8
9<refentry id="systemd.generator">
10  <refentryinfo>
11    <title>systemd.generator</title>
12    <productname>systemd</productname>
13  </refentryinfo>
14
15  <refmeta>
16    <refentrytitle>systemd.generator</refentrytitle>
17    <manvolnum>7</manvolnum>
18  </refmeta>
19
20  <refnamediv>
21    <refname>systemd.generator</refname>
22    <refpurpose>systemd unit generators</refpurpose>
23  </refnamediv>
24
25  <refsynopsisdiv>
26    <cmdsynopsis>
27      <command index='false'>/path/to/generator</command>
28      <arg choice="plain"><replaceable>normal-dir</replaceable></arg>
29      <arg choice="plain"><replaceable>early-dir</replaceable></arg>
30      <arg choice="plain"><replaceable>late-dir</replaceable></arg>
31    </cmdsynopsis>
32
33    <para>
34      <literallayout><filename>/run/systemd/system-generators/*</filename>
35<filename>/etc/systemd/system-generators/*</filename>
36<filename>/usr/local/lib/systemd/system-generators/*</filename>
37<filename>&SYSTEM_GENERATOR_DIR;/*</filename></literallayout>
38    </para>
39
40    <para>
41      <literallayout><filename>/run/systemd/user-generators/*</filename>
42<filename>/etc/systemd/user-generators/*</filename>
43<filename>/usr/local/lib/systemd/user-generators/*</filename>
44<filename>&USER_GENERATOR_DIR;/*</filename></literallayout>
45    </para>
46  </refsynopsisdiv>
47
48  <refsect1>
49    <title>Description</title>
50    <para>Generators are small executables placed in <filename>&SYSTEM_GENERATOR_DIR;/</filename> and other
51    directories listed above.
52    <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> will execute
53    these binaries very early at bootup and at configuration reload time — before unit files are
54    loaded. Their main purpose is to convert configuration and execution context parameters that are not
55    native to the service manager into dynamically generated unit files, symlinks or unit file drop-ins, so
56    that they can extend the unit file hierarchy the service manager subsequently loads and operates
57    on.</para>
58
59    <para>Each generator is called with three directory paths that are to be used for generator output. In
60    these three directories, generators may dynamically generate unit files (regular ones, instances, as well
61    as templates), unit file <filename>.d/</filename> drop-ins, and create symbolic links to unit files to
62    add additional dependencies, create aliases, or instantiate existing templates. Those directories are
63    included in the unit load path of
64    <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, allowing
65    generated configuration to extend or override existing definitions.</para>
66
67    <para>Directory paths for generator output differ by priority: <filename>…/generator.early</filename> has
68    priority higher than the admin configuration in <filename>/etc/</filename>, while
69    <filename>…/generator</filename> has lower priority than <filename>/etc/</filename> but higher than
70    vendor configuration in <filename>/usr/</filename>, and <filename>…/generator.late</filename> has
71    priority lower than all other configuration. See the next section and the discussion of unit load paths
72    and unit overriding in
73    <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
74    </para>
75
76    <para>Generators are loaded from a set of paths determined during compilation, as listed above. System
77    and user generators are loaded from directories with names ending in
78    <filename>system-generators/</filename> and <filename>user-generators/</filename>,
79    respectively. Generators found in directories listed earlier override the ones with the same name in
80    directories lower in the list. A symlink to <filename>/dev/null</filename> or an empty file can be used
81    to mask a generator, thereby preventing it from running. Please note that the order of the two
82    directories with the highest priority is reversed with respect to the unit load path, and generators in
83    <filename>/run/</filename> overwrite those in <filename>/etc/</filename>.</para>
84
85    <para>After installing new generators or updating the configuration, <command>systemctl
86    daemon-reload</command> may be executed. This will delete the previous configuration created by
87    generators, re-run all generators, and cause <command>systemd</command> to reload units from disk. See
88    <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> for more
89    information.
90    </para>
91  </refsect1>
92
93  <refsect1>
94    <title>Output directories</title>
95
96    <para>Generators are invoked with three arguments: paths to directories where generators can place their
97    generated unit files or symlinks. By default those paths are runtime directories that are included in the
98    search path of <command>systemd</command>, but a generator may be called with different paths for
99    debugging purposes.</para>
100
101    <orderedlist>
102      <listitem>
103        <para><parameter>normal-dir</parameter></para>
104        <para>In normal use this is <filename>/run/systemd/generator</filename> in case of the system
105        generators and <filename>$XDG_RUNTIME_DIR/generator</filename> in case of the user generators. Unit
106        files placed in this directory take precedence over vendor unit configuration but not over native
107        user/administrator unit configuration.
108        </para>
109      </listitem>
110
111      <listitem>
112        <para><parameter>early-dir</parameter></para>
113        <para>In normal use this is <filename>/run/systemd/generator.early</filename> in case of the system
114        generators and <filename>$XDG_RUNTIME_DIR/generator.early</filename> in case of the user
115        generators. Unit files placed in this directory override unit files in <filename>/usr/</filename>,
116        <filename>/run/</filename> and <filename>/etc/</filename>. This means that unit files placed in this
117        directory take precedence over all normal configuration, both vendor and user/administrator.</para>
118      </listitem>
119
120      <listitem>
121        <para><parameter>late-dir</parameter></para>
122        <para>In normal use this is <filename>/run/systemd/generator.late</filename> in case of the system
123        generators and <filename>$XDG_RUNTIME_DIR/generator.late</filename> in case of the user
124        generators. This directory may be used to extend the unit file tree without overriding any other unit
125        files. Any native configuration files supplied by the vendor or user/administrator take
126        precedence.</para>
127      </listitem>
128    </orderedlist>
129  </refsect1>
130
131  <refsect1>
132    <title>Environment</title>
133
134    <para>The service manager sets a number of environment variables when invoking generator
135    executables. They carry information about the execution context of the generator, in order to simplify
136    conditionalizing generators to specific environments. The following environment variables are set:</para>
137
138    <variablelist class='environment-variables'>
139      <varlistentry>
140        <term><varname>$SYSTEMD_SCOPE</varname></term>
141
142        <listitem><para>If the generator is invoked from the system service manager this variable is set to
143        <literal>system</literal>; if invoked from the per-user service manager it is set to
144        <literal>user</literal>.</para></listitem>
145      </varlistentry>
146
147      <varlistentry>
148        <term><varname>$SYSTEMD_IN_INITRD</varname></term>
149
150        <listitem><para>If the generator is run as part of an initial RAM file system (initrd) this is set to
151        <literal>1</literal>. If it is run from the regular host (i.e. after the transition from initrd to
152        host) it is set to <literal>0</literal>. This environment variable is only set for system
153        generators.</para></listitem>
154      </varlistentry>
155
156      <varlistentry>
157        <term><varname>$SYSTEMD_FIRST_BOOT</varname></term>
158
159        <listitem><para>If this boot-up cycle is considered a "first boot", this is set to
160        <literal>1</literal>; if it is a subsequent, regular boot it is set to <literal>0</literal>. For
161        details see the documentation of <varname>ConditionFirstBoot=</varname> in
162        <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
163        environment variable is only set for system generators.</para></listitem>
164      </varlistentry>
165
166      <varlistentry>
167        <term><varname>$SYSTEMD_VIRTUALIZATION</varname></term>
168
169        <listitem><para>If the service manager is run in a virtualized environment,
170        <varname>$SYSTEMD_VIRTUALIZATION</varname> is set to a pair of strings, separated by a colon. The
171        first string is either <literal>vm</literal> or <literal>container</literal>, categorizing the type
172        of virtualization. The second string identifies the implementation of the virtualization
173        technology. If no virtualization is detected this variable will not be set. This data is identical to
174        what
175        <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
176        detects and reports, and uses the same vocabulary of virtualization implementation
177        identifiers.</para></listitem>
178      </varlistentry>
179
180      <varlistentry>
181        <term><varname>$SYSTEMD_ARCHITECTURE</varname></term>
182
183        <listitem><para>This variable is set to a short identifier of the reported architecture of the
184        system. For details about defined values, see documentation of
185        <varname>ConditionArchitecture=</varname> in
186        <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para></listitem>
187      </varlistentry>
188    </variablelist>
189  </refsect1>
190
191  <refsect1>
192    <title>Notes about writing generators</title>
193
194    <itemizedlist>
195      <listitem>
196        <para>All generators are executed in parallel. That means all executables are started at the very
197        same time and need to be able to cope with this parallelism.
198        </para>
199      </listitem>
200
201      <listitem>
202        <para>Generators are run very early at boot and cannot rely on any external services. They may not
203        talk to any other process. That includes simple things such as logging to <citerefentry
204        project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>, or
205        <command>systemd</command> itself (this means: no
206        <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)!
207        Non-essential file systems like <filename>/var/</filename> and <filename>/home/</filename> are
208        mounted after generators have run. Generators can however rely on the most basic kernel functionality
209        to be available, as well as mounted <filename>/sys/</filename>, <filename>/proc/</filename>,
210        <filename>/dev/</filename>, <filename>/usr/</filename> and <filename>/run/</filename> file systems.
211        </para>
212      </listitem>
213
214      <listitem>
215        <para>Units written by generators are removed when the configuration is reloaded. That means the
216        lifetime of the generated units is closely bound to the reload cycles of <command>systemd</command>
217        itself.</para>
218      </listitem>
219
220      <listitem>
221        <para>Generators should only be used to generate unit files, <filename>.d/*.conf</filename> drop-ins
222        for them and symlinks to them, not any other kind of non-unit related configuration. Due to the
223        lifecycle logic mentioned above, generators are not a good fit to generate dynamic configuration for
224        other services. If you need to generate dynamic configuration for other services, do so in normal
225        services you order before the service in question.</para>
226
227        <para>Note that using the <varname>StandardInputData=</varname>/<varname>StandardInputText=</varname>
228        settings of service unit files (see
229        <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>), it
230        is possible to make arbitrary input data (including daemon-specific configuration) part of the unit
231        definitions, which often might be sufficient to embed data or configuration for other programs into
232        unit files in a native fashion.</para>
233      </listitem>
234
235      <listitem>
236        <para>Since
237        <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
238
239        is not available (see above), log messages have to be written to <filename>/dev/kmsg</filename>
240        instead.</para>
241      </listitem>
242
243      <listitem>
244        <para>The generator should always include its own name in a comment at the top of the generated file,
245        so that the user can easily figure out which component created or amended a particular unit.</para>
246
247        <para>The <varname>SourcePath=</varname> directive should be used in generated files to specify the
248        source configuration file they are generated from. This makes things more easily understood by the
249        user and also has the benefit that systemd can warn the user about configuration files that changed
250        on disk but have not been read yet by systemd. The <varname>SourcePath=</varname> value does not have
251        to be a file in a physical filesystem. For example, in the common case of the generator looking at
252        the kernel command line, <option>SourcePath=/proc/cmdline</option> should be used.</para>
253      </listitem>
254
255      <listitem>
256        <para>Generators may write out dynamic unit files or just hook unit files into other units with the
257        usual <filename>.wants/</filename> or <filename>.requires/</filename> symlinks. Often, it is nicer to
258        simply instantiate a template unit file from <filename>/usr/</filename> with a generator instead of
259        writing out entirely dynamic unit files. Of course, this works only if a single parameter is to be
260        used.</para>
261      </listitem>
262
263      <listitem>
264        <para>If you are careful, you can implement generators in shell scripts. We do recommend C code
265        however, since generators are executed synchronously and hence delay the entire boot if they are
266        slow.</para>
267      </listitem>
268
269      <listitem>
270        <para>Regarding overriding semantics: there are two rules we try to follow when thinking about the
271        overriding semantics:</para>
272
273        <orderedlist numeration="lowerroman">
274          <listitem>
275            <para>User configuration should override vendor configuration. This (mostly) means that stuff
276            from <filename>/etc/</filename> should override stuff from <filename>/usr/</filename>.</para>
277          </listitem>
278
279          <listitem>
280            <para>Native configuration should override non-native configuration. This (mostly) means that
281            stuff you generate should never override native unit files for the same purpose.</para>
282          </listitem>
283        </orderedlist>
284
285        <para>Of these two rules the first rule is probably the more important one and breaks the second one
286        sometimes. Hence, when deciding whether to use argv[1], argv[2], or argv[3], your default choice
287        should probably be argv[1].</para>
288      </listitem>
289
290      <listitem>
291        <para>Instead of heading off now and writing all kind of generators for legacy configuration file
292        formats, please think twice! It is often a better idea to just deprecate old stuff instead of keeping
293        it artificially alive.
294        </para>
295      </listitem>
296    </itemizedlist>
297  </refsect1>
298
299  <refsect1>
300    <title>Examples</title>
301    <example>
302      <title>systemd-fstab-generator</title>
303
304      <para><citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
305      converts <filename>/etc/fstab</filename> into native mount units. It uses argv[1] as location to place
306      the generated unit files in order to allow the user to override <filename>/etc/fstab</filename> with
307      their own native unit files, but also to ensure that <filename>/etc/fstab</filename> overrides any
308      vendor default from <filename>/usr/</filename>.</para>
309
310      <para>After editing <filename>/etc/fstab</filename>, the user should invoke <command>systemctl
311      daemon-reload</command>. This will re-run all generators and cause <command>systemd</command> to reload
312      units from disk. To actually mount new directories added to <filename>fstab</filename>,
313      <command>systemctl start <replaceable>/path/to/mountpoint</replaceable></command> or <command>systemctl
314      start local-fs.target</command> may be used.</para>
315    </example>
316
317    <example>
318      <title>systemd-system-update-generator</title>
319
320      <para><citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
321      temporarily redirects <filename>default.target</filename> to <filename>system-update.target</filename>,
322      if a system update is scheduled. Since this needs to override the default user configuration for
323      <filename>default.target</filename>, it uses argv[2]. For details about this logic, see
324      <citerefentry><refentrytitle>systemd.offline-updates</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
325      </para>
326    </example>
327
328    <example>
329      <title>Debugging a generator</title>
330
331      <programlisting>dir=$(mktemp -d)
332SYSTEMD_LOG_LEVEL=debug &SYSTEM_GENERATOR_DIR;/systemd-fstab-generator \
333        "$dir" "$dir" "$dir"
334find $dir</programlisting>
335    </example>
336  </refsect1>
337
338  <refsect1>
339    <title>See also</title>
340
341    <para>
342      <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
343      <citerefentry><refentrytitle>systemd-cryptsetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
344      <citerefentry><refentrytitle>systemd-debug-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
345      <citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
346      <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
347      <citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
348      <citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
349      <citerefentry><refentrytitle>systemd-hibernate-resume-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
350      <citerefentry><refentrytitle>systemd-rc-local-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
351      <citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
352      <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
353      <citerefentry><refentrytitle>systemd-xdg-autostart-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
354      <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
355      <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
356      <citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>
357    </para>
358  </refsect1>
359</refentry>
360