]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/systemd.generator.xml
final v236 update (#7649)
[thirdparty/systemd.git] / man / systemd.generator.xml
CommitLineData
432d5965
ZJS
1<?xml version='1.0'?> <!--*-nxml-*-->
2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4<!ENTITY % entities SYSTEM "custom-entities.ent" >
5%entities;
6]>
7
8<!--
572eb058
ZJS
9 SPDX-License-Identifier: LGPL-2.1+
10
432d5965
ZJS
11 This file is part of systemd.
12
13 Copyright 2015 Zbigniew Jędrzejewski-Szmek
14
15 systemd is free software; you can redistribute it and/or modify it
16 under the terms of the GNU Lesser General Public License as published by
17 the Free Software Foundation; either version 2.1 of the License, or
18 (at your option) any later version.
19
20 systemd is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
24
25 You should have received a copy of the GNU Lesser General Public License
26 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27-->
28
29<refentry id="systemd.generator">
30 <refentryinfo>
31 <title>systemd.generator</title>
32 <productname>systemd</productname>
33
34 <authorgroup>
35 <author>
36 <contrib>Developer</contrib>
37 <firstname>Lennart</firstname>
38 <surname>Poettering</surname>
39 <email>lennart@poettering.net</email>
40 </author>
41 </authorgroup>
42 </refentryinfo>
43
44 <refmeta>
45 <refentrytitle>systemd.generator</refentrytitle>
46 <manvolnum>7</manvolnum>
47 </refmeta>
48
49 <refnamediv>
50 <refname>systemd.generator</refname>
f95b0be7 51 <refpurpose>systemd unit generators</refpurpose>
432d5965
ZJS
52 </refnamediv>
53
54 <refsynopsisdiv>
55 <cmdsynopsis>
56 <command>/path/to/generator</command>
57 <arg choice="plain"><replaceable>normal-dir</replaceable></arg>
58 <arg choice="plain"><replaceable>early-dir</replaceable></arg>
59 <arg choice="plain"><replaceable>late-dir</replaceable></arg>
60 </cmdsynopsis>
61
62 <para>
63 <literallayout><filename>/run/systemd/system-generators/*</filename>
12b42c76 64<filename>/etc/systemd/system-generators/*</filename>
432d5965
ZJS
65<filename>/usr/local/lib/systemd/system-generators/*</filename>
66<filename>&systemgeneratordir;/*</filename></literallayout>
67 </para>
68
69 <para>
70 <literallayout><filename>/run/systemd/user-generators/*</filename>
12b42c76 71<filename>/etc/systemd/user-generators/*</filename>
432d5965
ZJS
72<filename>/usr/local/lib/systemd/user-generators/*</filename>
73<filename>&usergeneratordir;/*</filename></literallayout>
74 </para>
75 </refsynopsisdiv>
76
77 <refsect1>
78 <title>Description</title>
7655cd3d
LP
79 <para>Generators are small executables that live in <filename>&systemgeneratordir;/</filename> and other
80 directories listed above.
81 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> will execute those
82 binaries very early at bootup and at configuration reload time — before unit files are loaded. Generators may
83 dynamically generate unit files (regular ones, instances as well as templates) and unit file
84 <filename>.d/</filename> drop-ins, or create symbolic links to unit files to add additional dependencies or
85 instantiate existing templates, thus extending or overriding existing definitions. Their main purpose is to convert
86 configuration files that are not native unit files dynamically into native unit files.</para>
432d5965
ZJS
87
88 <para>Generators are loaded from a set of paths determined during
a8eaaee7 89 compilation, as listed above. System and user generators are loaded
432d5965
ZJS
90 from directories with names ending in
91 <filename>system-generators/</filename> and
92 <filename>user-generators/</filename>, respectively. Generators
93 found in directories listed earlier override the ones with the
94 same name in directories lower in the list. A symlink to
95 <filename>/dev/null</filename> or an empty file can be used to
96 mask a generator, thereby preventing it from running. Please note
97 that the order of the two directories with the highest priority is
b938cb90 98 reversed with respect to the unit load path, and generators in
432d5965
ZJS
99 <filename>/run</filename> overwrite those in
100 <filename>/etc</filename>.</para>
101
102 <para>After installing new generators or updating the
c4c086a2 103 configuration, <command>systemctl daemon-reload</command> may be
432d5965
ZJS
104 executed. This will delete the previous configuration created by
105 generators, re-run all generators, and cause
106 <command>systemd</command> to reload units from disk. See
107 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
108 for more information.
109 </para>
110 </refsect1>
111
112 <refsect1>
113 <title>Writing generators</title>
114
115 <para>Generators are invoked with three arguments: paths to
116 runtime directories where generators can place their generated
117 unit files or symlinks.</para>
118
119 <orderedlist>
120 <listitem>
121 <para><parameter>normal-dir</parameter></para>
122 <para>argv[1] may be used to override unit files in
123 <filename>/usr</filename>, but not those in
4b4a972d
JL
124 <filename>/run</filename> or in <filename>/etc</filename>.
125 This means that unit files placed
432d5965
ZJS
126 in this directory take precedence over vendor unit
127 configuration but not over native user/administrator unit
128 configuration.</para>
129 </listitem>
130
131 <listitem>
132 <para><parameter>early-dir</parameter></para>
133 <para>argv[2] may be used to override unit files in
4b4a972d 134 <filename>/usr</filename>, in <filename>/run</filename> and in
432d5965
ZJS
135 <filename>/etc</filename>. This means that unit files placed
136 in this directory take precedence over all configuration,
137 both vendor and user/administrator.</para>
138 </listitem>
139
140 <listitem>
141 <para><parameter>late-dir</parameter></para>
142 <para>argv[3] may be used to extend the unit file tree without
ff9b60f3 143 overriding any other unit files. Any native configuration
432d5965
ZJS
144 files supplied by the vendor or user/administrator take
145 precedence over the generated ones placed in this directory.
146 </para>
147 </listitem>
148 </orderedlist>
149
150 <refsect2>
151 <title>Notes</title>
152
153 <itemizedlist>
154 <listitem>
155 <para>
156 All generators are executed in parallel. That means all
157 executables are started at the very same time and need to
158 be able to cope with this parallelism.
159 </para>
160 </listitem>
161
162 <listitem>
163 <para>
164 Generators are run very early at boot and cannot rely on
165 any external services. They may not talk to any other
166 process. That includes simple things such as logging to
89fc708c 167 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
432d5965 168 or <command>systemd</command> itself (this means: no
0663a4a6 169 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)!
89fc708c
ZJS
170 Non-essential file systems like
171 <filename>/var</filename> and <filename>/home</filename>
172 are mounted after generators have run. Generators
173 can however rely on the most basic kernel functionality to be
174 available, including a mounted <filename>/sys</filename>,
175 <filename>/proc</filename>, <filename>/dev</filename>,
176 <filename>/usr</filename>.
432d5965
ZJS
177 </para>
178 </listitem>
179
180 <listitem>
181 <para>
a8eaaee7 182 Units written by generators are removed when the configuration
432d5965
ZJS
183 is reloaded. That means the lifetime of the generated
184 units is closely bound to the reload cycles of
185 <command>systemd</command> itself.
186 </para>
187 </listitem>
188
189 <listitem>
190 <para>
7655cd3d
LP
191 Generators should only be used to generate unit files and symlinks to them, not any other kind of
192 configuration. Due to the lifecycle logic mentioned above, generators are not a good fit to generate
193 dynamic configuration for other services. If you need to generate dynamic configuration for other services,
194 do so in normal services you order before the service in question.
432d5965
ZJS
195 </para>
196 </listitem>
197
198 <listitem>
199 <para>
200 Since
201 <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
b938cb90 202 is not available (see above), log messages have to be
432d5965
ZJS
203 written to <filename>/dev/kmsg</filename> instead.
204 </para>
205 </listitem>
206
207 <listitem>
208 <para>
209 It is a good idea to use the
210 <varname>SourcePath=</varname> directive in generated unit
211 files to specify the source configuration file you are
212 generating the unit from. This makes things more easily
213 understood by the user and also has the benefit that
214 systemd can warn the user about configuration files that
215 changed on disk but have not been read yet by systemd.
216 </para>
217 </listitem>
218
219 <listitem>
220 <para>
221 Generators may write out dynamic unit files or just hook
222 unit files into other units with the usual
223 <filename>.wants/</filename> or
b938cb90 224 <filename>.requires/</filename> symlinks. Often, it is
432d5965
ZJS
225 nicer to simply instantiate a template unit file from
226 <filename>/usr</filename> with a generator instead of
b938cb90 227 writing out entirely dynamic unit files. Of course, this
432d5965
ZJS
228 works only if a single parameter is to be used.
229 </para>
230 </listitem>
231
232 <listitem>
233 <para>
b938cb90 234 If you are careful, you can implement generators in shell
432d5965 235 scripts. We do recommend C code however, since generators
a8eaaee7 236 are executed synchronously and hence delay the
432d5965
ZJS
237 entire boot if they are slow.
238 </para>
239 </listitem>
240
241 <listitem>
242 <para>Regarding overriding semantics: there are two rules we
243 try to follow when thinking about the overriding semantics:
244 </para>
245
246 <orderedlist numeration="lowerroman">
247 <listitem>
248 <para>User configuration should override vendor
249 configuration. This (mostly) means that stuff from
250 <filename>/etc</filename> should override stuff from
251 <filename>/usr</filename>.</para>
252 </listitem>
253
254 <listitem>
255 <para>Native configuration should override non-native
256 configuration. This (mostly) means that stuff you
257 generate should never override native unit files for the
258 same purpose.</para>
259 </listitem>
260 </orderedlist>
261
262 <para>Of these two rules the first rule is probably the more
263 important one and breaks the second one sometimes. Hence,
264 when deciding whether to user argv[1], argv[2], or argv[3],
265 your default choice should probably be argv[1].</para>
266 </listitem>
267
268 <listitem>
269 <para>
270 Instead of heading off now and writing all kind of
271 generators for legacy configuration file formats, please
7ca41557 272 think twice! It is often a better idea to just deprecate
432d5965
ZJS
273 old stuff instead of keeping it artificially alive.
274 </para>
275 </listitem>
276 </itemizedlist>
277 </refsect2>
278 </refsect1>
279
280 <refsect1>
281 <title>Examples</title>
282 <example>
283 <title>systemd-fstab-generator</title>
284
285 <para><citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
286 converts <filename>/etc/fstab</filename> into native mount
287 units. It uses argv[1] as location to place the generated unit
288 files in order to allow the user to override
289 <filename>/etc/fstab</filename> with her own native unit files,
290 but also to ensure that <filename>/etc/fstab</filename>
291 overrides any vendor default from <filename>/usr</filename>.
292 </para>
293
294 <para>After editing <filename>/etc/fstab</filename>, the user
295 should invoke <command>systemctl daemon-reload</command>. This
296 will re-run all generators and cause <command>systemd</command>
297 to reload units from disk. To actually mount new directories
298 added to <filename>fstab</filename>, <command>systemctl start
299 <replaceable>/path/to/mountpoint</replaceable></command> or
300 <command>systemctl start local-fs.target</command> may be used.
301 </para>
302 </example>
303
304 <example>
305 <title>systemd-system-update-generator</title>
306
307 <para><citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>
308 temporarily redirects <filename>default.target</filename> to
0663a4a6 309 <filename>system-update.target</filename>, if a system update is
432d5965 310 scheduled. Since this needs to override the default user
b938cb90 311 configuration for <filename>default.target</filename>, it uses
432d5965 312 argv[2]. For details about this logic, see
49174f75
ZJS
313 <citerefentry><refentrytitle>systemd.offline-updates</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
314 </para>
432d5965
ZJS
315 </example>
316
317 <example>
b8e1d4d1 318 <title>Debugging a generator</title>
432d5965
ZJS
319
320 <programlisting>dir=$(mktemp -d)
321SYSTEMD_LOG_LEVEL=debug &systemgeneratordir;/systemd-fstab-generator \
322 "$dir" "$dir" "$dir"
323find $dir</programlisting>
324 </example>
325 </refsect1>
326
327 <refsect1>
328 <title>See also</title>
329
330 <para>
331 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
332 <citerefentry><refentrytitle>systemd-cryptsetup-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
333 <citerefentry><refentrytitle>systemd-debug-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
432d5965 334 <citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
3ba3a79d 335 <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
432d5965
ZJS
336 <citerefentry><refentrytitle>systemd-getty-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
337 <citerefentry><refentrytitle>systemd-gpt-auto-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
338 <citerefentry><refentrytitle>systemd-hibernate-resume-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
339 <citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
340 <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
341 <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
1bd2d4e3
ZJS
342 <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
343 <citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>
432d5965
ZJS
344 </para>
345 </refsect1>
346</refentry>