Debian Codegraph @

man/systemd.generator.xml

raw
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.5/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" xmlns:xi="http://www.w3.org/2001/XInclude">
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="opt"><replaceable>early-dir</replaceable></arg>
30 <arg choice="opt"><replaceable>late-dir</replaceable></arg>
31 </cmdsynopsis>
32
33 <para><simplelist>
34 <member><filename>/run/systemd/system-generators/*</filename></member>
35 <member><filename>/etc/systemd/system-generators/*</filename></member>
36 <member><filename>/usr/local/lib/systemd/system-generators/*</filename></member>
37 <member><filename>&SYSTEM_GENERATOR_DIR;/*</filename></member>
38 </simplelist></para>
39
40 <para><simplelist>
41 <member><filename>/run/systemd/user-generators/*</filename></member>
42 <member><filename>/etc/systemd/user-generators/*</filename></member>
43 <member><filename>/usr/local/lib/systemd/user-generators/*</filename></member>
44 <member><filename>&USER_GENERATOR_DIR;/*</filename></member>
45 </simplelist></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><command>systemd</command> will call each generator with three directory paths that are to be used
60 for generator output. In these three directories, generators may dynamically generate unit files (regular
61 ones, instances, as well as templates), unit file <filename>.d/</filename> drop-ins, and create symbolic
62 links to unit files to add additional dependencies, create aliases, or instantiate existing templates.
63 Those directories are included in the unit load path, allowing generated configuration to extend or
64 override existing definitions. For tests, generators may be called with just one argument; the generator
65 should assume that all three paths are the same in that case.</para>
66
67 <para>Generators executed by the system manager are invoked in a sandbox with a private writable
68 <filename>/tmp/</filename> directory and where most of the file system is read-only except for the
69 generator output directories.</para>
70
71 <para>Directory paths for generator output differ by priority: <filename>…/generator.early</filename> has
72 priority higher than the admin configuration in <filename>/etc/</filename>, while
73 <filename>…/generator</filename> has lower priority than <filename>/etc/</filename> but higher than
74 vendor configuration in <filename>/usr/</filename>, and <filename>…/generator.late</filename> has
75 priority lower than all other configuration. See the next section and the discussion of unit load paths
76 and unit overriding in
77 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
78 </para>
79
80 <para>Generators are loaded from a set of paths determined during compilation, as listed above. System
81 and user generators are loaded from directories with names ending in
82 <filename>system-generators/</filename> and <filename>user-generators/</filename>,
83 respectively. Generators found in directories listed earlier override the ones with the same name in
84 directories lower in the list <xi:include href="standard-conf.xml" xpointer="usr-local-footnote" />.
85 A symlink to <filename>/dev/null</filename> or an empty file can be used to mask a generator, thereby
86 preventing it from running. Please note that the order of the two directories with the highest priority
87 is reversed with respect to the unit load path, and generators in <filename>/run/</filename> overwrite
88 those in <filename>/etc/</filename>.</para>
89
90 <para>After installing new generators or updating the configuration, <command>systemctl
91 daemon-reload</command> may be executed. This will delete the previous configuration created by
92 generators, re-run all generators, and cause <command>systemd</command> to reload units from disk. See
93 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> for more
94 information.
95 </para>
96 </refsect1>
97
98 <refsect1>
99 <title>Output directories</title>
100
101 <para>Generators are invoked with three arguments: paths to directories where generators can place their
102 generated unit files or symlinks. By default, those paths are runtime directories that are included in the
103 search path of <command>systemd</command>, but a generator may be called with different paths for
104 debugging purposes. If only one argument is provided, the generator should use the same directory as the
105 three output paths.</para>
106
107 <orderedlist>
108 <listitem>
109 <para><parameter>normal-dir</parameter></para>
110 <para>In normal use this is <filename>/run/systemd/generator</filename> in case of the system
111 generators and <filename>$XDG_RUNTIME_DIR/systemd/generator</filename> in case of the user
112 generators. Unit files placed in this directory take precedence over vendor unit configuration but
113 not over native user/administrator unit configuration.
114 </para>
115 </listitem>
116
117 <listitem>
118 <para><parameter>early-dir</parameter></para>
119 <para>In normal use this is <filename>/run/systemd/generator.early</filename> in case of the system
120 generators and <filename>$XDG_RUNTIME_DIR/systemd/generator.early</filename> in case of the user
121 generators. Unit files placed in this directory override unit files in <filename>/usr/</filename>,
122 <filename>/run/</filename> and <filename>/etc/</filename>. This means that unit files placed in this
123 directory take precedence over all normal configuration, both vendor and user/administrator.</para>
124 </listitem>
125
126 <listitem>
127 <para><parameter>late-dir</parameter></para>
128 <para>In normal use this is <filename>/run/systemd/generator.late</filename> in case of the system
129 generators and <filename>$XDG_RUNTIME_DIR/systemd/generator.late</filename> in case of the user
130 generators. This directory may be used to extend the unit file tree without overriding any other unit
131 files. Any native configuration files supplied by the vendor or user/administrator take
132 precedence.</para>
133 </listitem>
134 </orderedlist>
135
136 <para>Note: generators <emphasis>must not</emphasis> write to other locations or otherwise make changes
137 to system state. Generator output is supposed to last only until the next
138 <command>daemon-reload</command> or <command>daemon-reexec</command>; if the generator is replaced
139 or masked, its effects should vanish.</para>
140 </refsect1>
141
142 <refsect1>
143 <title>Environment</title>
144
145 <para>The service manager sets a number of environment variables when invoking generator
146 executables. They carry information about the execution context of the generator, in order to simplify
147 conditionalizing generators to specific environments. The following environment variables are set:</para>
148
149 <variablelist class='environment-variables'>
150 <varlistentry>
151 <term><varname>$SYSTEMD_SCOPE</varname></term>
152
153 <listitem><para>If the generator is invoked from the system service manager this variable is set to
154 <literal>system</literal>; if invoked from the per-user service manager it is set to
155 <literal>user</literal>.</para>
156
157 <xi:include href="version-info.xml" xpointer="v251"/></listitem>
158 </varlistentry>
159
160 <varlistentry>
161 <term><varname>$SYSTEMD_IN_INITRD</varname></term>
162
163 <listitem><para>If the generator is run as part of an initrd this is set to <literal>1</literal>. If
164 it is run from the regular host (i.e. after the transition from initrd to host) it is set to
165 <literal>0</literal>. This environment variable is only set for system generators.</para>
166
167 <xi:include href="version-info.xml" xpointer="v251"/></listitem>
168 </varlistentry>
169
170 <varlistentry>
171 <term><varname>$SYSTEMD_SOFT_REBOOTS_COUNT</varname></term>
172
173 <listitem><para>If the system has soft-rebooted, this variable is set to the count of soft-reboots.
174 This environment variable is only set for system generators.</para>
175
176 <xi:include href="version-info.xml" xpointer="v257"/></listitem>
177 </varlistentry>
178
179 <varlistentry>
180 <term><varname>$SYSTEMD_FIRST_BOOT</varname></term>
181
182 <listitem><para>If this boot-up cycle is considered a "first boot", this is set to
183 <literal>1</literal>; if it is a subsequent, regular boot it is set to <literal>0</literal>. For
184 details see the documentation of <varname>ConditionFirstBoot=</varname> in
185 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
186 environment variable is only set for system generators.</para>
187
188 <xi:include href="version-info.xml" xpointer="v251"/></listitem>
189 </varlistentry>
190
191 <varlistentry>
192 <term><varname>$SYSTEMD_VIRTUALIZATION</varname></term>
193
194 <listitem><para>If the service manager is run in a virtualized environment,
195 <varname>$SYSTEMD_VIRTUALIZATION</varname> is set to a pair of strings, separated by a colon. The
196 first string is either <literal>vm</literal> or <literal>container</literal>, categorizing the type
197 of virtualization. The second string identifies the implementation of the virtualization
198 technology. If no virtualization is detected this variable will not be set. This data is identical to
199 what
200 <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
201 detects and reports, and uses the same vocabulary of virtualization implementation
202 identifiers.</para>
203
204 <xi:include href="version-info.xml" xpointer="v251"/></listitem>
205 </varlistentry>
206
207 <varlistentry>
208 <term><varname>$SYSTEMD_ARCHITECTURE</varname></term>
209
210 <listitem><para>This variable is set to a short identifier of the reported architecture of the
211 system. For details about defined values, see documentation of
212 <varname>ConditionArchitecture=</varname> in
213 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
214
215 <xi:include href="version-info.xml" xpointer="v251"/></listitem>
216 </varlistentry>
217
218 <varlistentry>
219 <term><varname>$CREDENTIALS_DIRECTORY</varname></term>
220 <term><varname>$ENCRYPTED_CREDENTIALS_DIRECTORY</varname></term>
221
222 <listitem><para>If set, refers to the directory system credentials have been placed in. Credentials
223 passed into the system in plaintext form will be placed in <varname>$CREDENTIALS_DIRECTORY</varname>,
224 and those passed in in encrypted form will be placed in
225 <varname>$ENCRYPTED_CREDENTIALS_DIRECTORY</varname>. Use the
226 <citerefentry><refentrytitle>systemd-creds</refentrytitle><manvolnum>1</manvolnum></citerefentry>
227 command to automatically decrypt/authenticate credentials passed in, if needed. Specifically, use the
228 <command>systemd-creds --system cat</command> command.</para>
229
230 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
231 </varlistentry>
232
233 <varlistentry>
234 <term><varname>$SYSTEMD_CONFIDENTIAL_VIRTUALIZATION</varname></term>
235
236 <listitem><para>If the service manager is run in a confidential virtualized environment,
237 <varname>$SYSTEMD_CONFIDENTIAL_VIRTUALIZATION</varname> is set to a string that identifies
238 the confidential virtualization hardware technology. If no confidential virtualization is
239 detected this variable will not be set. This data is identical to what
240 <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
241 detects and reports, and uses the same vocabulary of confidential virtualization
242 technology identifiers.</para>
243
244 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
245 </varlistentry>
246 </variablelist>
247 </refsect1>
248
249 <refsect1>
250 <title>Notes about writing generators</title>
251
252 <itemizedlist>
253 <listitem>
254 <para>All generators are executed in parallel. That means all executables are started at the very
255 same time and need to be able to cope with this parallelism.
256 </para>
257 </listitem>
258
259 <listitem>
260 <para>Generators are run very early at boot and cannot rely on any external services. They may not
261 talk to any other process. That includes simple things such as logging to <citerefentry
262 project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>, or
263 <command>systemd</command> itself (this means: no
264 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)!
265 Non-essential file systems like <filename>/var/</filename> and <filename>/home/</filename> are
266 mounted after generators have run. Generators can however rely on the most basic kernel functionality
267 to be available, as well as mounted <filename>/sys/</filename>, <filename>/proc/</filename>,
268 <filename>/dev/</filename>, <filename>/usr/</filename> and <filename>/run/</filename> file systems.
269 </para>
270 </listitem>
271
272 <listitem>
273 <para>Units written by generators are removed when the configuration is reloaded. That means the
274 lifetime of the generated units is closely bound to the reload cycles of <command>systemd</command>
275 itself.</para>
276 </listitem>
277
278 <listitem>
279 <para>Generators should only be used to generate unit files, <filename>.d/*.conf</filename> drop-ins
280 for them and symlinks to them, not any other kind of non-unit related configuration. Due to the
281 lifecycle logic mentioned above, generators are not a good fit to generate dynamic configuration for
282 other services. If you need to generate dynamic configuration for other services, do so in normal
283 services you order before the service in question.</para>
284
285 <para>Note that using the <varname>StandardInputData=</varname>/<varname>StandardInputText=</varname>
286 settings of service unit files (see
287 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>), it
288 is possible to make arbitrary input data (including daemon-specific configuration) part of the unit
289 definitions, which often might be sufficient to embed data or configuration for other programs into
290 unit files in a native fashion.</para>
291 </listitem>
292
293 <listitem>
294 <para>Since
295 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
296
297 is not available (see above), log messages have to be written to <filename>/dev/kmsg</filename>
298 instead.</para>
299 </listitem>
300
301 <listitem>
302 <para>The generator should always include its own name in a comment at the top of the generated file,
303 so that the user can easily figure out which component created or amended a particular unit.</para>
304
305 <para>The <varname>SourcePath=</varname> directive should be used in generated files to specify the
306 source configuration file they are generated from. This makes things more easily understood by the
307 user and also has the benefit that systemd can warn the user about configuration files that changed
308 on disk but have not been read yet by systemd. The <varname>SourcePath=</varname> value does not have
309 to be a file in a physical filesystem. For example, in the common case of the generator looking at
310 the kernel command line, <option>SourcePath=/proc/cmdline</option> should be used.</para>
311 </listitem>
312
313 <listitem>
314 <para>Generators may write out dynamic unit files or just hook unit files into other units with the
315 usual <filename>.wants/</filename> or <filename>.requires/</filename> symlinks. Often, it is nicer to
316 simply instantiate a template unit file from <filename>/usr/</filename> with a generator instead of
317 writing out entirely dynamic unit files. Of course, this works only if a single parameter is to be
318 used.</para>
319 </listitem>
320
321 <listitem>
322 <para>If you are careful, you can implement generators in shell scripts. We do recommend C code
323 however, since generators are executed synchronously and hence delay the entire boot if they are
324 slow.</para>
325 </listitem>
326
327 <listitem>
328 <para>Regarding overriding semantics: there are two rules we try to follow when thinking about the
329 overriding semantics:</para>
330
331 <orderedlist numeration="lowerroman">
332 <listitem>
333 <para>User configuration should override vendor configuration. This (mostly) means that stuff
334 from <filename>/etc/</filename> should override stuff from <filename>/usr/</filename>.</para>
335 </listitem>
336
337 <listitem>
338 <para>Native configuration should override non-native configuration. This (mostly) means that
339 stuff you generate should never override native unit files for the same purpose.</para>
340 </listitem>
341 </orderedlist>
342
343 <para>Of these two rules the first rule is probably the more important one and breaks the second one
344 sometimes. Hence, when deciding whether to use argv[1], argv[2], or argv[3], your default choice
345 should probably be argv[1].</para>
346 </listitem>
347
348 <listitem>
349 <para>Instead of heading off now and writing all kind of generators for legacy configuration file
350 formats, please think twice! It is often a better idea to just deprecate old stuff instead of keeping
351 it artificially alive.
352 </para>
353 </listitem>
354 </itemizedlist>
355 </refsect1>
356
357 <refsect1>
358 <title>Examples</title>
359 <example>
360 <title>systemd-fstab-generator</title>
361
362 <para><citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
363 converts <filename>/etc/fstab</filename> into native mount units. It uses argv[1] as location to place
364 the generated unit files in order to allow the user to override <filename>/etc/fstab</filename> with
365 their own native unit files, but also to ensure that <filename>/etc/fstab</filename> overrides any
366 vendor default from <filename>/usr/</filename>.</para>
367
368 <para>After editing <filename>/etc/fstab</filename>, the user should invoke <command>systemctl
369 daemon-reload</command>. This will re-run all generators and cause <command>systemd</command> to reload
370 units from disk. To actually mount new directories added to <filename>fstab</filename>,
371 <command>systemctl start <replaceable>/path/to/mountpoint</replaceable></command> or <command>systemctl
372 start local-fs.target</command> may be used.</para>
373 </example>
374
375 <example>
376 <title>systemd-system-update-generator</title>
377
378 <para><citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
379 temporarily redirects <filename>default.target</filename> to <filename>system-update.target</filename>,
380 if a system update is scheduled. Since this needs to override the default user configuration for
381 <filename>default.target</filename>, it uses argv[2]. For details about this logic, see
382 <citerefentry><refentrytitle>systemd.offline-updates</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
383 </para>
384 </example>
385
386 <example>
387 <title>Debugging a generator</title>
388
389 <programlisting>dir=$(mktemp -d)
390 SYSTEMD_LOG_LEVEL=debug &SYSTEM_GENERATOR_DIR;/systemd-fstab-generator \
391 "$dir" "$dir" "$dir"
392 find $dir</programlisting>
393 </example>
394 </refsect1>
395
396 <refsect1>
397 <title>See Also</title>
398
399 <para><simplelist type="inline">
400 <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
401 <member><citerefentry><refentrytitle>systemd-cryptsetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
402 <member><citerefentry><refentrytitle>systemd-debug-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
403 <member><citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
404 <member><citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
405 <member><citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
406 <member><citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
407 <member><citerefentry><refentrytitle>systemd-hibernate-resume-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
408 <member><citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
409 <member><citerefentry><refentrytitle>systemd-xdg-autostart-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
410 <member><citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
411 <member><citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
412 <member><citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry></member>
413 </simplelist></para>
414 </refsect1>
415 </refentry>