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