]>
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> | |
b82f27e7 ZJS |
50 | <para>Generators are small executables that live in |
51 | <filename>&systemgeneratordir;/</filename> and other directories listed above. | |
52 | <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> | |
53 | will execute those binaries very early at bootup and at configuration reload time | |
54 | — before unit files are loaded. Their main purpose is to convert configuration | |
55 | that is not native into dynamically generated unit files.</para> | |
56 | ||
57 | <para>Each generator is called with three directory paths that are to be used for | |
58 | generator output. In these three directories, generators may dynamically generate | |
59 | unit files (regular ones, instances, as well as templates), unit file | |
60 | <filename>.d/</filename> drop-ins, and create symbolic links to unit files to add | |
61 | additional dependencies, create aliases, or instantiate existing templates. Those | |
62 | directories are included in the unit load path of | |
63 | <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, | |
64 | allowing generated configuration to extend or override existing | |
65 | definitions.</para> | |
66 | ||
67 | <para>Directory paths for generator output differ by priority: | |
68 | <filename>…/generator.early</filename> has priority higher than the admin | |
3b121157 | 69 | configuration in <filename>/etc/</filename>, while |
b82f27e7 | 70 | <filename>…/generator</filename> has lower priority than |
3b121157 ZJS |
71 | <filename>/etc/</filename> but higher than vendor configuration in |
72 | <filename>/usr/</filename>, and <filename>…/generator.late</filename> has priority | |
b82f27e7 ZJS |
73 | lower than all other configuration. See the next section and the discussion of |
74 | unit load paths and unit overriding in | |
75 | <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>. | |
76 | </para> | |
432d5965 ZJS |
77 | |
78 | <para>Generators are loaded from a set of paths determined during | |
a8eaaee7 | 79 | compilation, as listed above. System and user generators are loaded |
432d5965 ZJS |
80 | from directories with names ending in |
81 | <filename>system-generators/</filename> and | |
82 | <filename>user-generators/</filename>, respectively. Generators | |
83 | found in directories listed earlier override the ones with the | |
84 | same name in directories lower in the list. A symlink to | |
85 | <filename>/dev/null</filename> or an empty file can be used to | |
86 | mask a generator, thereby preventing it from running. Please note | |
87 | that the order of the two directories with the highest priority is | |
b938cb90 | 88 | reversed with respect to the unit load path, and generators in |
3b121157 ZJS |
89 | <filename>/run/</filename> overwrite those in |
90 | <filename>/etc/</filename>.</para> | |
432d5965 ZJS |
91 | |
92 | <para>After installing new generators or updating the | |
c4c086a2 | 93 | configuration, <command>systemctl daemon-reload</command> may be |
432d5965 ZJS |
94 | executed. This will delete the previous configuration created by |
95 | generators, re-run all generators, and cause | |
96 | <command>systemd</command> to reload units from disk. See | |
97 | <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry> | |
98 | for more information. | |
99 | </para> | |
100 | </refsect1> | |
101 | ||
102 | <refsect1> | |
b82f27e7 | 103 | <title>Output directories</title> |
432d5965 | 104 | |
b82f27e7 ZJS |
105 | <para>Generators are invoked with three arguments: paths to directories where |
106 | generators can place their generated unit files or symlinks. By default those | |
107 | paths are runtime directories that are included in the search path of | |
108 | <command>systemd</command>, but a generator may be called with different paths | |
109 | for debugging purposes.</para> | |
432d5965 ZJS |
110 | |
111 | <orderedlist> | |
112 | <listitem> | |
113 | <para><parameter>normal-dir</parameter></para> | |
b82f27e7 ZJS |
114 | <para>In normal use this is <filename>/run/systemd/generator</filename> in |
115 | case of the system generators and | |
116 | <filename>$XDG_RUNTIME_DIR/generator</filename> in case of the user | |
117 | generators. Unit files placed in this directory take precedence over vendor | |
118 | unit configuration but not over native user/administrator unit configuration. | |
119 | </para> | |
432d5965 ZJS |
120 | </listitem> |
121 | ||
122 | <listitem> | |
123 | <para><parameter>early-dir</parameter></para> | |
b82f27e7 ZJS |
124 | <para>In normal use this is <filename>/run/systemd/generator.early</filename> |
125 | in case of the system generators and | |
126 | <filename>$XDG_RUNTIME_DIR/generator.early</filename> in case of the user | |
127 | generators. Unit files placed in this directory override unit files in | |
3b121157 ZJS |
128 | <filename>/usr/</filename>, <filename>/run/</filename> and |
129 | <filename>/etc/</filename>. This means that unit files placed in this | |
b82f27e7 ZJS |
130 | directory take precedence over all normal configuration, both vendor and |
131 | user/administrator.</para> | |
432d5965 ZJS |
132 | </listitem> |
133 | ||
134 | <listitem> | |
135 | <para><parameter>late-dir</parameter></para> | |
b82f27e7 ZJS |
136 | <para>In normal use this is <filename>/run/systemd/generator.late</filename> |
137 | in case of the system generators and | |
138 | <filename>$XDG_RUNTIME_DIR/generator.late</filename> in case of the user | |
139 | generators. This directory may be used to extend the unit file tree without | |
140 | overriding any other unit files. Any native configuration files supplied by | |
141 | the vendor or user/administrator take precedence.</para> | |
432d5965 ZJS |
142 | </listitem> |
143 | </orderedlist> | |
80efdacd ZJS |
144 | </refsect1> |
145 | ||
146 | <refsect1> | |
147 | <title>Notes about writing generators</title> | |
148 | ||
149 | <itemizedlist> | |
150 | <listitem> | |
151 | <para>All generators are executed in parallel. That means all executables are | |
152 | started at the very same time and need to be able to cope with this | |
153 | parallelism. | |
154 | </para> | |
155 | </listitem> | |
156 | ||
157 | <listitem> | |
158 | <para>Generators are run very early at boot and cannot rely on any external | |
159 | services. They may not talk to any other process. That includes simple things | |
160 | such as logging to | |
161 | <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
162 | or <command>systemd</command> itself (this means: no | |
163 | <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>)! | |
3b121157 ZJS |
164 | Non-essential file systems like <filename>/var/</filename> and |
165 | <filename>/home/</filename> are mounted after generators have run. Generators | |
80efdacd | 166 | can however rely on the most basic kernel functionality to be available, |
3b121157 ZJS |
167 | including a mounted <filename>/sys/</filename>, <filename>/proc/</filename>, |
168 | <filename>/dev/</filename>, <filename>/usr/</filename>. | |
80efdacd ZJS |
169 | </para> |
170 | </listitem> | |
171 | ||
172 | <listitem> | |
173 | <para>Units written by generators are removed when the configuration is | |
174 | reloaded. That means the lifetime of the generated units is closely bound to | |
175 | the reload cycles of <command>systemd</command> itself.</para> | |
176 | </listitem> | |
177 | ||
178 | <listitem> | |
179 | <para>Generators should only be used to generate unit files and symlinks to | |
180 | them, not any other kind of configuration. Due to the lifecycle logic | |
181 | mentioned above, generators are not a good fit to generate dynamic | |
182 | configuration for other services. If you need to generate dynamic | |
183 | configuration for other services, do so in normal services you order before | |
184 | the service in question.</para> | |
185 | </listitem> | |
186 | ||
187 | <listitem> | |
188 | <para>Since | |
189 | <citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry> | |
190 | ||
191 | is not available (see above), log messages have to be written to | |
192 | <filename>/dev/kmsg</filename> instead.</para> | |
193 | </listitem> | |
194 | ||
195 | <listitem> | |
9c5ac5eb ZJS |
196 | <para>The generator should always include its own name in a comment at the top of the generated file, |
197 | so that the user can easily figure out which component created or amended a particular unit.</para> | |
198 | ||
199 | <para>The <varname>SourcePath=</varname> directive should be used in generated files to specify the | |
200 | source configuration file they are generated from. This makes things more easily understood by the | |
201 | user and also has the benefit that systemd can warn the user about configuration files that changed | |
202 | on disk but have not been read yet by systemd. The <varname>SourcePath=</varname> value does not have | |
203 | to be a file in a physical filesystem. For example, in the common case of the generator looking at | |
204 | the kernel command line, <option>SourcePath=/proc/cmdline</option> should be used.</para> | |
80efdacd ZJS |
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 | |
3b121157 | 211 | instantiate a template unit file from <filename>/usr/</filename> with a |
80efdacd ZJS |
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 | |
3b121157 ZJS |
229 | (mostly) means that stuff from <filename>/etc/</filename> should override |
230 | stuff from <filename>/usr/</filename>.</para> | |
80efdacd ZJS |
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 | 264 | files, but also to ensure that <filename>/etc/fstab</filename> overrides any |
3b121157 | 265 | vendor default from <filename>/usr/</filename>.</para> |
80efdacd ZJS |
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>, | |
4540e698 | 313 | <citerefentry><refentrytitle>systemd-xdg-autostart-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>, |
432d5965 | 314 | <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>, |
1bd2d4e3 ZJS |
315 | <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>, |
316 | <citerefentry><refentrytitle>systemd.environment-generator</refentrytitle><manvolnum>7</manvolnum></citerefentry> | |
432d5965 ZJS |
317 | </para> |
318 | </refsect1> | |
319 | </refentry> |