]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/systemd.generator.xml
man: Add xinclude namespace
[thirdparty/systemd.git] / man / systemd.generator.xml
CommitLineData
432d5965 1<?xml version='1.0'?> <!--*-nxml-*-->
3a54a157 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
432d5965
ZJS
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4<!ENTITY % entities SYSTEM "custom-entities.ent" >
5%entities;
6]>
db9ecf05 7<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
432d5965 8
4623eecb 9<refentry id="systemd.generator" xmlns:xi="http://www.w3.org/2001/XInclude">
432d5965
ZJS
10 <refentryinfo>
11 <title>systemd.generator</title>
12 <productname>systemd</productname>
432d5965
ZJS
13 </refentryinfo>
14
15 <refmeta>
16 <refentrytitle>systemd.generator</refentrytitle>
17 <manvolnum>7</manvolnum>
18 </refmeta>
19
20 <refnamediv>
21 <refname>systemd.generator</refname>
f95b0be7 22 <refpurpose>systemd unit generators</refpurpose>
432d5965
ZJS
23 </refnamediv>
24
25 <refsynopsisdiv>
26 <cmdsynopsis>
f8b68539 27 <command index='false'>/path/to/generator</command>
432d5965 28 <arg choice="plain"><replaceable>normal-dir</replaceable></arg>
17021368
ZJS
29 <arg choice="option"><replaceable>early-dir</replaceable></arg>
30 <arg choice="option"><replaceable>late-dir</replaceable></arg>
432d5965
ZJS
31 </cmdsynopsis>
32
33 <para>
34 <literallayout><filename>/run/systemd/system-generators/*</filename>
12b42c76 35<filename>/etc/systemd/system-generators/*</filename>
432d5965 36<filename>/usr/local/lib/systemd/system-generators/*</filename>
23b8071d 37<filename>&SYSTEM_GENERATOR_DIR;/*</filename></literallayout>
432d5965
ZJS
38 </para>
39
40 <para>
41 <literallayout><filename>/run/systemd/user-generators/*</filename>
12b42c76 42<filename>/etc/systemd/user-generators/*</filename>
432d5965 43<filename>/usr/local/lib/systemd/user-generators/*</filename>
23b8071d 44<filename>&USER_GENERATOR_DIR;/*</filename></literallayout>
432d5965
ZJS
45 </para>
46 </refsynopsisdiv>
47
48 <refsect1>
49 <title>Description</title>
23b8071d 50 <para>Generators are small executables placed in <filename>&SYSTEM_GENERATOR_DIR;/</filename> and other
3acf00a5
LP
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
a1d05574
LP
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
17021368
ZJS
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>
a1d05574
LP
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
b82f27e7
ZJS
73 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
74 </para>
432d5965 75
a1d05574
LP
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.
432d5965
ZJS
90 </para>
91 </refsect1>
92
93 <refsect1>
b82f27e7 94 <title>Output directories</title>
432d5965 95
a1d05574
LP
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
17021368 99 debugging purposes. If only one argument is provided, the generator should use the same directory as the
835b781f 100 three output paths.</para>
432d5965
ZJS
101
102 <orderedlist>
103 <listitem>
104 <para><parameter>normal-dir</parameter></para>
a1d05574 105 <para>In normal use this is <filename>/run/systemd/generator</filename> in case of the system
80c7d4b8
DDM
106 generators and <filename>$XDG_RUNTIME_DIR/systemd/generator</filename> in case of the user
107 generators. Unit files placed in this directory take precedence over vendor unit configuration but
108 not over native user/administrator unit configuration.
b82f27e7 109 </para>
432d5965
ZJS
110 </listitem>
111
112 <listitem>
113 <para><parameter>early-dir</parameter></para>
a1d05574 114 <para>In normal use this is <filename>/run/systemd/generator.early</filename> in case of the system
80c7d4b8 115 generators and <filename>$XDG_RUNTIME_DIR/systemd/generator.early</filename> in case of the user
a1d05574
LP
116 generators. Unit files placed in this directory override unit files in <filename>/usr/</filename>,
117 <filename>/run/</filename> and <filename>/etc/</filename>. This means that unit files placed in this
118 directory take precedence over all normal configuration, both vendor and user/administrator.</para>
432d5965
ZJS
119 </listitem>
120
121 <listitem>
122 <para><parameter>late-dir</parameter></para>
a1d05574 123 <para>In normal use this is <filename>/run/systemd/generator.late</filename> in case of the system
80c7d4b8 124 generators and <filename>$XDG_RUNTIME_DIR/systemd/generator.late</filename> in case of the user
a1d05574
LP
125 generators. This directory may be used to extend the unit file tree without overriding any other unit
126 files. Any native configuration files supplied by the vendor or user/administrator take
127 precedence.</para>
432d5965
ZJS
128 </listitem>
129 </orderedlist>
2bd88a29
ZJS
130
131 <para>Note: generators <emphasis>must not</emphasis> write to other locations or otherwise make changes
132 to system state. Generator output is supposed to last only until the next
133 <command>daemon-reload</command> or <command>daemon-reexec</command>; if the generator is replaced
134 or masked, its effects should vanish.</para>
80efdacd
ZJS
135 </refsect1>
136
5b9d199a
LP
137 <refsect1>
138 <title>Environment</title>
139
140 <para>The service manager sets a number of environment variables when invoking generator
141 executables. They carry information about the execution context of the generator, in order to simplify
142 conditionalizing generators to specific environments. The following environment variables are set:</para>
143
144 <variablelist class='environment-variables'>
145 <varlistentry>
146 <term><varname>$SYSTEMD_SCOPE</varname></term>
147
148 <listitem><para>If the generator is invoked from the system service manager this variable is set to
149 <literal>system</literal>; if invoked from the per-user service manager it is set to
150 <literal>user</literal>.</para></listitem>
151 </varlistentry>
152
153 <varlistentry>
154 <term><varname>$SYSTEMD_IN_INITRD</varname></term>
155
55c041b4
LP
156 <listitem><para>If the generator is run as part of an initrd this is set to <literal>1</literal>. If
157 it is run from the regular host (i.e. after the transition from initrd to host) it is set to
158 <literal>0</literal>. This environment variable is only set for system generators.</para></listitem>
5b9d199a
LP
159 </varlistentry>
160
161 <varlistentry>
162 <term><varname>$SYSTEMD_FIRST_BOOT</varname></term>
163
164 <listitem><para>If this boot-up cycle is considered a "first boot", this is set to
165 <literal>1</literal>; if it is a subsequent, regular boot it is set to <literal>0</literal>. For
166 details see the documentation of <varname>ConditionFirstBoot=</varname> in
167 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
168 environment variable is only set for system generators.</para></listitem>
169 </varlistentry>
170
171 <varlistentry>
172 <term><varname>$SYSTEMD_VIRTUALIZATION</varname></term>
173
174 <listitem><para>If the service manager is run in a virtualized environment,
175 <varname>$SYSTEMD_VIRTUALIZATION</varname> is set to a pair of strings, separated by a colon. The
176 first string is either <literal>vm</literal> or <literal>container</literal>, categorizing the type
177 of virtualization. The second string identifies the implementation of the virtualization
178 technology. If no virtualization is detected this variable will not be set. This data is identical to
179 what
180 <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
181 detects and reports, and uses the same vocabulary of virtualization implementation
182 identifiers.</para></listitem>
183 </varlistentry>
184
185 <varlistentry>
186 <term><varname>$SYSTEMD_ARCHITECTURE</varname></term>
187
188 <listitem><para>This variable is set to a short identifier of the reported architecture of the
189 system. For details about defined values, see documentation of
190 <varname>ConditionArchitecture=</varname> in
191 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para></listitem>
192 </varlistentry>
771c7629
LP
193
194 <varlistentry>
195 <term><varname>$CREDENTIALS_DIRECTORY</varname></term>
196 <term><varname>$ENCRYPTED_CREDENTIALS_DIRECTORY</varname></term>
197
198 <listitem><para>If set, refers to the directory system credentials have been placed in. Credentials
199 passed into the system in plaintext form will be placed in <varname>$CREDENTIALS_DIRECTORY</varname>,
200 and those passed in in encrypted form will be placed in
201 <varname>$ENCRYPTED_CREDENTIALS_DIRECTORY</varname>. Use the
202 <citerefentry><refentrytitle>systemd-creds</refentrytitle><manvolnum>1</manvolnum></citerefentry>
203 command to automatically decrypt/authenticate credentials passed in, if needed. Specifically, use the
204 <command>systemd-creds --system cat</command> command.</para></listitem>
205 </varlistentry>
206
08951245
DB
207 <varlistentry>
208 <term><varname>$SYSTEMD_CONFIDENTIAL_VIRTUALIZATION</varname></term>
209
210 <listitem><para>If the service manager is run in a confidential virtualized environment,
211 <varname>$SYSTEMD_CONFIDENTIAL_VIRTUALIZATION</varname> is set to a string that identifies
212 the confidential virtualization hardware technology. If no confidential virtualization is
213 detected this variable will not be set. This data is identical to what
214 <citerefentry><refentrytitle>systemd-detect-virt</refentrytitle><manvolnum>1</manvolnum></citerefentry>
215 detects and reports, and uses the same vocabulary of confidential virtualization
216 technology identifiers.</para></listitem>
217 </varlistentry>
5b9d199a
LP
218 </variablelist>
219 </refsect1>
220
80efdacd
ZJS
221 <refsect1>
222 <title>Notes about writing generators</title>
223
224 <itemizedlist>
225 <listitem>
a1d05574
LP
226 <para>All generators are executed in parallel. That means all executables are started at the very
227 same time and need to be able to cope with this parallelism.
80efdacd
ZJS
228 </para>
229 </listitem>
230
231 <listitem>
3acf00a5
LP
232 <para>Generators are run very early at boot and cannot rely on any external services. They may not
233 talk to any other process. That includes simple things such as logging to <citerefentry
234 project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>, or
235 <command>systemd</command> itself (this means: no
80efdacd 236 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)!
3acf00a5
LP
237 Non-essential file systems like <filename>/var/</filename> and <filename>/home/</filename> are
238 mounted after generators have run. Generators can however rely on the most basic kernel functionality
239 to be available, as well as mounted <filename>/sys/</filename>, <filename>/proc/</filename>,
240 <filename>/dev/</filename>, <filename>/usr/</filename> and <filename>/run/</filename> file systems.
80efdacd
ZJS
241 </para>
242 </listitem>
243
244 <listitem>
a1d05574
LP
245 <para>Units written by generators are removed when the configuration is reloaded. That means the
246 lifetime of the generated units is closely bound to the reload cycles of <command>systemd</command>
247 itself.</para>
80efdacd
ZJS
248 </listitem>
249
250 <listitem>
3acf00a5
LP
251 <para>Generators should only be used to generate unit files, <filename>.d/*.conf</filename> drop-ins
252 for them and symlinks to them, not any other kind of non-unit related configuration. Due to the
253 lifecycle logic mentioned above, generators are not a good fit to generate dynamic configuration for
254 other services. If you need to generate dynamic configuration for other services, do so in normal
255 services you order before the service in question.</para>
256
257 <para>Note that using the <varname>StandardInputData=</varname>/<varname>StandardInputText=</varname>
258 settings of service unit files (see
259 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>), it
260 is possible to make arbitrary input data (including daemon-specific configuration) part of the unit
261 definitions, which often might be sufficient to embed data or configuration for other programs into
262 unit files in a native fashion.</para>
80efdacd
ZJS
263 </listitem>
264
265 <listitem>
266 <para>Since
267 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
268
a1d05574
LP
269 is not available (see above), log messages have to be written to <filename>/dev/kmsg</filename>
270 instead.</para>
80efdacd
ZJS
271 </listitem>
272
273 <listitem>
9c5ac5eb
ZJS
274 <para>The generator should always include its own name in a comment at the top of the generated file,
275 so that the user can easily figure out which component created or amended a particular unit.</para>
276
277 <para>The <varname>SourcePath=</varname> directive should be used in generated files to specify the
278 source configuration file they are generated from. This makes things more easily understood by the
279 user and also has the benefit that systemd can warn the user about configuration files that changed
280 on disk but have not been read yet by systemd. The <varname>SourcePath=</varname> value does not have
281 to be a file in a physical filesystem. For example, in the common case of the generator looking at
282 the kernel command line, <option>SourcePath=/proc/cmdline</option> should be used.</para>
80efdacd
ZJS
283 </listitem>
284
285 <listitem>
a1d05574
LP
286 <para>Generators may write out dynamic unit files or just hook unit files into other units with the
287 usual <filename>.wants/</filename> or <filename>.requires/</filename> symlinks. Often, it is nicer to
288 simply instantiate a template unit file from <filename>/usr/</filename> with a generator instead of
289 writing out entirely dynamic unit files. Of course, this works only if a single parameter is to be
290 used.</para>
80efdacd 291 </listitem>
432d5965 292
80efdacd 293 <listitem>
a1d05574
LP
294 <para>If you are careful, you can implement generators in shell scripts. We do recommend C code
295 however, since generators are executed synchronously and hence delay the entire boot if they are
296 slow.</para>
80efdacd
ZJS
297 </listitem>
298
299 <listitem>
a1d05574
LP
300 <para>Regarding overriding semantics: there are two rules we try to follow when thinking about the
301 overriding semantics:</para>
80efdacd
ZJS
302
303 <orderedlist numeration="lowerroman">
304 <listitem>
a1d05574
LP
305 <para>User configuration should override vendor configuration. This (mostly) means that stuff
306 from <filename>/etc/</filename> should override stuff from <filename>/usr/</filename>.</para>
80efdacd
ZJS
307 </listitem>
308
309 <listitem>
a1d05574
LP
310 <para>Native configuration should override non-native configuration. This (mostly) means that
311 stuff you generate should never override native unit files for the same purpose.</para>
80efdacd
ZJS
312 </listitem>
313 </orderedlist>
314
a1d05574
LP
315 <para>Of these two rules the first rule is probably the more important one and breaks the second one
316 sometimes. Hence, when deciding whether to use argv[1], argv[2], or argv[3], your default choice
317 should probably be argv[1].</para>
80efdacd
ZJS
318 </listitem>
319
320 <listitem>
a1d05574
LP
321 <para>Instead of heading off now and writing all kind of generators for legacy configuration file
322 formats, please think twice! It is often a better idea to just deprecate old stuff instead of keeping
323 it artificially alive.
80efdacd
ZJS
324 </para>
325 </listitem>
326 </itemizedlist>
432d5965
ZJS
327 </refsect1>
328
329 <refsect1>
330 <title>Examples</title>
331 <example>
332 <title>systemd-fstab-generator</title>
333
334 <para><citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
a1d05574
LP
335 converts <filename>/etc/fstab</filename> into native mount units. It uses argv[1] as location to place
336 the generated unit files in order to allow the user to override <filename>/etc/fstab</filename> with
337 their own native unit files, but also to ensure that <filename>/etc/fstab</filename> overrides any
3b121157 338 vendor default from <filename>/usr/</filename>.</para>
80efdacd 339
a1d05574
LP
340 <para>After editing <filename>/etc/fstab</filename>, the user should invoke <command>systemctl
341 daemon-reload</command>. This will re-run all generators and cause <command>systemd</command> to reload
342 units from disk. To actually mount new directories added to <filename>fstab</filename>,
343 <command>systemctl start <replaceable>/path/to/mountpoint</replaceable></command> or <command>systemctl
80efdacd 344 start local-fs.target</command> may be used.</para>
432d5965
ZJS
345 </example>
346
347 <example>
348 <title>systemd-system-update-generator</title>
349
350 <para><citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
a1d05574
LP
351 temporarily redirects <filename>default.target</filename> to <filename>system-update.target</filename>,
352 if a system update is scheduled. Since this needs to override the default user configuration for
353 <filename>default.target</filename>, it uses argv[2]. For details about this logic, see
49174f75
ZJS
354 <citerefentry><refentrytitle>systemd.offline-updates</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
355 </para>
432d5965
ZJS
356 </example>
357
358 <example>
b8e1d4d1 359 <title>Debugging a generator</title>
432d5965
ZJS
360
361 <programlisting>dir=$(mktemp -d)
23b8071d 362SYSTEMD_LOG_LEVEL=debug &SYSTEM_GENERATOR_DIR;/systemd-fstab-generator \
432d5965
ZJS
363 "$dir" "$dir" "$dir"
364find $dir</programlisting>
365 </example>
366 </refsect1>
367
368 <refsect1>
369 <title>See also</title>
370
371 <para>
372 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
373 <citerefentry><refentrytitle>systemd-cryptsetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
374 <citerefentry><refentrytitle>systemd-debug-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
432d5965 375 <citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
3ba3a79d 376 <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
432d5965
ZJS
377 <citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
378 <citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
379 <citerefentry><refentrytitle>systemd-hibernate-resume-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
245992a0 380 <citerefentry><refentrytitle>systemd-rc-local-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
432d5965
ZJS
381 <citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
382 <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
4540e698 383 <citerefentry><refentrytitle>systemd-xdg-autostart-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
432d5965 384 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1bd2d4e3
ZJS
385 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
386 <citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>
432d5965
ZJS
387 </para>
388 </refsect1>
389</refentry>