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