]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/systemd.generator.xml
Merge pull request #18730 from yuwata/dhcp6-client-do-not-set-t1-t2-18090
[thirdparty/systemd.git] / man / systemd.generator.xml
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>&systemgeneratordir;/*</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>&usergeneratordir;/*</filename></literallayout>
45 </para>
46 </refsynopsisdiv>
47
48 <refsect1>
49 <title>Description</title>
50 <para>Generators are small executables placed in <filename>&systemgeneratordir;/</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 that is not native to the service manager into
55 dynamically generated unit files, symlinks or unit file drop-ins, so that they can extend the unit file
56 hierarchy the service manager subsequently loads and operates on.</para>
57
58 <para>Each generator is called with three directory paths that are to be used for
59 generator output. In these three directories, generators may dynamically generate
60 unit files (regular ones, instances, as well as templates), unit file
61 <filename>.d/</filename> drop-ins, and create symbolic links to unit files to add
62 additional dependencies, create aliases, or instantiate existing templates. Those
63 directories are included in the unit load path of
64 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
65 allowing generated configuration to extend or override existing
66 definitions.</para>
67
68 <para>Directory paths for generator output differ by priority:
69 <filename>…/generator.early</filename> has priority higher than the admin
70 configuration in <filename>/etc/</filename>, while
71 <filename>…/generator</filename> has lower priority than
72 <filename>/etc/</filename> but higher than vendor configuration in
73 <filename>/usr/</filename>, and <filename>…/generator.late</filename> has priority
74 lower than all other configuration. See the next section and the discussion of
75 unit load paths and unit overriding in
76 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.
77 </para>
78
79 <para>Generators are loaded from a set of paths determined during
80 compilation, as listed above. System and user generators are loaded
81 from directories with names ending in
82 <filename>system-generators/</filename> and
83 <filename>user-generators/</filename>, respectively. Generators
84 found in directories listed earlier override the ones with the
85 same name in directories lower in the list. A symlink to
86 <filename>/dev/null</filename> or an empty file can be used to
87 mask a generator, thereby preventing it from running. Please note
88 that the order of the two directories with the highest priority is
89 reversed with respect to the unit load path, and generators in
90 <filename>/run/</filename> overwrite those in
91 <filename>/etc/</filename>.</para>
92
93 <para>After installing new generators or updating the
94 configuration, <command>systemctl daemon-reload</command> may be
95 executed. This will delete the previous configuration created by
96 generators, re-run all generators, and cause
97 <command>systemd</command> to reload units from disk. See
98 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
99 for more information.
100 </para>
101 </refsect1>
102
103 <refsect1>
104 <title>Output directories</title>
105
106 <para>Generators are invoked with three arguments: paths to directories where
107 generators can place their generated unit files or symlinks. By default those
108 paths are runtime directories that are included in the search path of
109 <command>systemd</command>, but a generator may be called with different paths
110 for debugging purposes.</para>
111
112 <orderedlist>
113 <listitem>
114 <para><parameter>normal-dir</parameter></para>
115 <para>In normal use this is <filename>/run/systemd/generator</filename> in
116 case of the system generators and
117 <filename>$XDG_RUNTIME_DIR/generator</filename> in case of the user
118 generators. Unit files placed in this directory take precedence over vendor
119 unit configuration but not over native user/administrator unit configuration.
120 </para>
121 </listitem>
122
123 <listitem>
124 <para><parameter>early-dir</parameter></para>
125 <para>In normal use this is <filename>/run/systemd/generator.early</filename>
126 in case of the system generators and
127 <filename>$XDG_RUNTIME_DIR/generator.early</filename> in case of the user
128 generators. Unit files placed in this directory override unit files in
129 <filename>/usr/</filename>, <filename>/run/</filename> and
130 <filename>/etc/</filename>. This means that unit files placed in this
131 directory take precedence over all normal configuration, both vendor and
132 user/administrator.</para>
133 </listitem>
134
135 <listitem>
136 <para><parameter>late-dir</parameter></para>
137 <para>In normal use this is <filename>/run/systemd/generator.late</filename>
138 in case of the system generators and
139 <filename>$XDG_RUNTIME_DIR/generator.late</filename> in case of the user
140 generators. This directory may be used to extend the unit file tree without
141 overriding any other unit files. Any native configuration files supplied by
142 the vendor or user/administrator take precedence.</para>
143 </listitem>
144 </orderedlist>
145 </refsect1>
146
147 <refsect1>
148 <title>Notes about writing generators</title>
149
150 <itemizedlist>
151 <listitem>
152 <para>All generators are executed in parallel. That means all executables are
153 started at the very same time and need to be able to cope with this
154 parallelism.
155 </para>
156 </listitem>
157
158 <listitem>
159 <para>Generators are run very early at boot and cannot rely on any external services. They may not
160 talk to any other process. That includes simple things such as logging to <citerefentry
161 project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>, or
162 <command>systemd</command> itself (this means: no
163 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)!
164 Non-essential file systems like <filename>/var/</filename> and <filename>/home/</filename> are
165 mounted after generators have run. Generators can however rely on the most basic kernel functionality
166 to be available, as well as mounted <filename>/sys/</filename>, <filename>/proc/</filename>,
167 <filename>/dev/</filename>, <filename>/usr/</filename> and <filename>/run/</filename> file systems.
168 </para>
169 </listitem>
170
171 <listitem>
172 <para>Units written by generators are removed when the configuration is
173 reloaded. That means the lifetime of the generated units is closely bound to
174 the reload cycles of <command>systemd</command> itself.</para>
175 </listitem>
176
177 <listitem>
178 <para>Generators should only be used to generate unit files, <filename>.d/*.conf</filename> drop-ins
179 for them and symlinks to them, not any other kind of non-unit related configuration. Due to the
180 lifecycle logic mentioned above, generators are not a good fit to generate dynamic configuration for
181 other services. If you need to generate dynamic configuration for other services, do so in normal
182 services you order before the service in question.</para>
183
184 <para>Note that using the <varname>StandardInputData=</varname>/<varname>StandardInputText=</varname>
185 settings of service unit files (see
186 <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>), it
187 is possible to make arbitrary input data (including daemon-specific configuration) part of the unit
188 definitions, which often might be sufficient to embed data or configuration for other programs into
189 unit files in a native fashion.</para>
190 </listitem>
191
192 <listitem>
193 <para>Since
194 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
195
196 is not available (see above), log messages have to be written to
197 <filename>/dev/kmsg</filename> instead.</para>
198 </listitem>
199
200 <listitem>
201 <para>The generator should always include its own name in a comment at the top of the generated file,
202 so that the user can easily figure out which component created or amended a particular unit.</para>
203
204 <para>The <varname>SourcePath=</varname> directive should be used in generated files to specify the
205 source configuration file they are generated from. This makes things more easily understood by the
206 user and also has the benefit that systemd can warn the user about configuration files that changed
207 on disk but have not been read yet by systemd. The <varname>SourcePath=</varname> value does not have
208 to be a file in a physical filesystem. For example, in the common case of the generator looking at
209 the kernel command line, <option>SourcePath=/proc/cmdline</option> should be used.</para>
210 </listitem>
211
212 <listitem>
213 <para>Generators may write out dynamic unit files or just hook unit files
214 into other units with the usual <filename>.wants/</filename> or
215 <filename>.requires/</filename> symlinks. Often, it is nicer to simply
216 instantiate a template unit file from <filename>/usr/</filename> with a
217 generator instead of writing out entirely dynamic unit files. Of course, this
218 works only if a single parameter is to be used.</para>
219 </listitem>
220
221 <listitem>
222 <para>If you are careful, you can implement generators in shell scripts. We
223 do recommend C code however, since generators are executed synchronously and
224 hence delay the entire boot if they are slow.</para>
225 </listitem>
226
227 <listitem>
228 <para>Regarding overriding semantics: there are two rules we try to follow
229 when thinking about the overriding semantics:</para>
230
231 <orderedlist numeration="lowerroman">
232 <listitem>
233 <para>User configuration should override vendor configuration. This
234 (mostly) means that stuff from <filename>/etc/</filename> should override
235 stuff from <filename>/usr/</filename>.</para>
236 </listitem>
237
238 <listitem>
239 <para>Native configuration should override non-native configuration. This
240 (mostly) means that stuff you generate should never override native unit
241 files for the same purpose.</para>
242 </listitem>
243 </orderedlist>
244
245 <para>Of these two rules the first rule is probably the more important one
246 and breaks the second one sometimes. Hence, when deciding whether to use
247 argv[1], argv[2], or argv[3], your default choice should probably be
248 argv[1].</para>
249 </listitem>
250
251 <listitem>
252 <para>Instead of heading off now and writing all kind of generators for
253 legacy configuration file formats, please think twice! It is often a better
254 idea to just deprecate old stuff instead of keeping it artificially alive.
255 </para>
256 </listitem>
257 </itemizedlist>
258 </refsect1>
259
260 <refsect1>
261 <title>Examples</title>
262 <example>
263 <title>systemd-fstab-generator</title>
264
265 <para><citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
266 converts <filename>/etc/fstab</filename> into native mount units. It uses
267 argv[1] as location to place the generated unit files in order to allow the
268 user to override <filename>/etc/fstab</filename> with their own native unit
269 files, but also to ensure that <filename>/etc/fstab</filename> overrides any
270 vendor default from <filename>/usr/</filename>.</para>
271
272 <para>After editing <filename>/etc/fstab</filename>, the user should invoke
273 <command>systemctl daemon-reload</command>. This will re-run all generators and
274 cause <command>systemd</command> to reload units from disk. To actually mount
275 new directories added to <filename>fstab</filename>, <command>systemctl start
276 <replaceable>/path/to/mountpoint</replaceable></command> or <command>systemctl
277 start local-fs.target</command> may be used.</para>
278 </example>
279
280 <example>
281 <title>systemd-system-update-generator</title>
282
283 <para><citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
284 temporarily redirects <filename>default.target</filename> to
285 <filename>system-update.target</filename>, if a system update is
286 scheduled. Since this needs to override the default user configuration for
287 <filename>default.target</filename>, it uses argv[2]. For details about this
288 logic, see
289 <citerefentry><refentrytitle>systemd.offline-updates</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
290 </para>
291 </example>
292
293 <example>
294 <title>Debugging a generator</title>
295
296 <programlisting>dir=$(mktemp -d)
297 SYSTEMD_LOG_LEVEL=debug &systemgeneratordir;/systemd-fstab-generator \
298 "$dir" "$dir" "$dir"
299 find $dir</programlisting>
300 </example>
301 </refsect1>
302
303 <refsect1>
304 <title>See also</title>
305
306 <para>
307 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
308 <citerefentry><refentrytitle>systemd-cryptsetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
309 <citerefentry><refentrytitle>systemd-debug-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
310 <citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
311 <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
312 <citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
313 <citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
314 <citerefentry><refentrytitle>systemd-hibernate-resume-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
315 <citerefentry><refentrytitle>systemd-rc-local-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
316 <citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
317 <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
318 <citerefentry><refentrytitle>systemd-xdg-autostart-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
319 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
320 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
321 <citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>
322 </para>
323 </refsect1>
324 </refentry>