]>
Commit | Line | Data |
---|---|---|
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 ZJS |
8 | |
9 | <refentry id="systemd.generator"> | |
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 ZJS |
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> | |
12b42c76 | 35 | <filename>/etc/systemd/system-generators/*</filename> |
432d5965 ZJS |
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> | |
12b42c76 | 42 | <filename>/etc/systemd/user-generators/*</filename> |
432d5965 ZJS |
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> | |
3acf00a5 LP |
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> | |
b82f27e7 ZJS |
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 | |
3b121157 | 70 | configuration in <filename>/etc/</filename>, while |
b82f27e7 | 71 | <filename>…/generator</filename> has lower priority than |
3b121157 ZJS |
72 | <filename>/etc/</filename> but higher than vendor configuration in |
73 | <filename>/usr/</filename>, and <filename>…/generator.late</filename> has priority | |
b82f27e7 ZJS |
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> | |
432d5965 ZJS |
78 | |
79 | <para>Generators are loaded from a set of paths determined during | |
a8eaaee7 | 80 | compilation, as listed above. System and user generators are loaded |
432d5965 ZJS |
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 | |
b938cb90 | 89 | reversed with respect to the unit load path, and generators in |
3b121157 ZJS |
90 | <filename>/run/</filename> overwrite those in |
91 | <filename>/etc/</filename>.</para> | |
432d5965 ZJS |
92 | |
93 | <para>After installing new generators or updating the | |
c4c086a2 | 94 | configuration, <command>systemctl daemon-reload</command> may be |
432d5965 ZJS |
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> | |
b82f27e7 | 104 | <title>Output directories</title> |
432d5965 | 105 | |
b82f27e7 ZJS |
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> | |
432d5965 ZJS |
111 | |
112 | <orderedlist> | |
113 | <listitem> | |
114 | <para><parameter>normal-dir</parameter></para> | |
b82f27e7 ZJS |
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> | |
432d5965 ZJS |
121 | </listitem> |
122 | ||
123 | <listitem> | |
124 | <para><parameter>early-dir</parameter></para> | |
b82f27e7 ZJS |
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 | |
3b121157 ZJS |
129 | <filename>/usr/</filename>, <filename>/run/</filename> and |
130 | <filename>/etc/</filename>. This means that unit files placed in this | |
b82f27e7 ZJS |
131 | directory take precedence over all normal configuration, both vendor and |
132 | user/administrator.</para> | |
432d5965 ZJS |
133 | </listitem> |
134 | ||
135 | <listitem> | |
136 | <para><parameter>late-dir</parameter></para> | |
b82f27e7 ZJS |
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> | |
432d5965 ZJS |
143 | </listitem> |
144 | </orderedlist> | |
80efdacd ZJS |
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> | |
3acf00a5 LP |
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 | |
80efdacd | 163 | <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)! |
3acf00a5 LP |
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. | |
80efdacd ZJS |
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> | |
3acf00a5 LP |
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> | |
80efdacd ZJS |
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> | |
9c5ac5eb ZJS |
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> | |
80efdacd ZJS |
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 | |
3b121157 | 216 | instantiate a template unit file from <filename>/usr/</filename> with a |
80efdacd ZJS |
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> | |
432d5965 | 220 | |
80efdacd ZJS |
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 | |
3b121157 ZJS |
234 | (mostly) means that stuff from <filename>/etc/</filename> should override |
235 | stuff from <filename>/usr/</filename>.</para> | |
80efdacd ZJS |
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> | |
432d5965 ZJS |
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> | |
80efdacd ZJS |
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 | |
f16eb8b0 | 268 | user to override <filename>/etc/fstab</filename> with their own native unit |
80efdacd | 269 | files, but also to ensure that <filename>/etc/fstab</filename> overrides any |
3b121157 | 270 | vendor default from <filename>/usr/</filename>.</para> |
80efdacd ZJS |
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> | |
432d5965 ZJS |
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 | |
0663a4a6 | 285 | <filename>system-update.target</filename>, if a system update is |
80efdacd ZJS |
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 | |
49174f75 ZJS |
289 | <citerefentry><refentrytitle>systemd.offline-updates</refentrytitle><manvolnum>7</manvolnum></citerefentry>. |
290 | </para> | |
432d5965 ZJS |
291 | </example> |
292 | ||
293 | <example> | |
b8e1d4d1 | 294 | <title>Debugging a generator</title> |
432d5965 ZJS |
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>, | |
432d5965 | 310 | <citerefentry><refentrytitle>systemd-fstab-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>, |
3ba3a79d | 311 | <citerefentry project='man-pages'><refentrytitle>fstab</refentrytitle><manvolnum>5</manvolnum></citerefentry>, |
432d5965 ZJS |
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>, | |
245992a0 | 315 | <citerefentry><refentrytitle>systemd-rc-local-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>, |
432d5965 ZJS |
316 | <citerefentry><refentrytitle>systemd-system-update-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>, |
317 | <citerefentry><refentrytitle>systemd-sysv-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>, | |
4540e698 | 318 | <citerefentry><refentrytitle>systemd-xdg-autostart-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>, |
432d5965 | 319 | <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>, |
1bd2d4e3 ZJS |
320 | <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>, |
321 | <citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry> | |
432d5965 ZJS |
322 | </para> |
323 | </refsect1> | |
324 | </refentry> |