1SYSTEMD.GENERATOR(7) systemd.generator SYSTEMD.GENERATOR(7)
2
3
4
6 systemd.generator - systemd unit generators
7
9 /path/to/generator normal-dir [early-dir] [late-dir]
10
11 /run/systemd/system-generators/*
12 /etc/systemd/system-generators/*
13 /usr/local/lib/systemd/system-generators/*
14 /usr/lib/systemd/system-generators/*
15
16 /run/systemd/user-generators/*
17 /etc/systemd/user-generators/*
18 /usr/local/lib/systemd/user-generators/*
19 /usr/lib/systemd/user-generators/*
20
21
23 Generators are small executables placed in
24 /usr/lib/systemd/system-generators/ and other directories listed above.
25 systemd(1) will execute these binaries very early at bootup and at
26 configuration reload time — before unit files are loaded. Their main
27 purpose is to convert configuration and execution context parameters
28 that are not native to the service manager into dynamically generated
29 unit files, symlinks or unit file drop-ins, so that they can extend the
30 unit file hierarchy the service manager subsequently loads and operates
31 on.
32
33 systemd will call each generator with three directory paths that are to
34 be used for generator output. In these three directories, generators
35 may dynamically generate unit files (regular ones, instances, as well
36 as templates), unit file .d/ drop-ins, and create symbolic links to
37 unit files to add additional dependencies, create aliases, or
38 instantiate existing templates. Those directories are included in the
39 unit load path, allowing generated configuration to extend or override
40 existing definitions. For tests, generators may be called with just one
41 argument; the generator should assume that all three paths are the same
42 in that case.
43
44 Directory paths for generator output differ by priority:
45 .../generator.early has priority higher than the admin configuration in
46 /etc/, while .../generator has lower priority than /etc/ but higher
47 than vendor configuration in /usr/, and .../generator.late has priority
48 lower than all other configuration. See the next section and the
49 discussion of unit load paths and unit overriding in systemd.unit(5).
50
51 Generators are loaded from a set of paths determined during
52 compilation, as listed above. System and user generators are loaded
53 from directories with names ending in system-generators/ and
54 user-generators/, respectively. Generators found in directories listed
55 earlier override the ones with the same name in directories lower in
56 the list. A symlink to /dev/null or an empty file can be used to mask a
57 generator, thereby preventing it from running. Please note that the
58 order of the two directories with the highest priority is reversed with
59 respect to the unit load path, and generators in /run/ overwrite those
60 in /etc/.
61
62 After installing new generators or updating the configuration,
63 systemctl daemon-reload may be executed. This will delete the previous
64 configuration created by generators, re-run all generators, and cause
65 systemd to reload units from disk. See systemctl(1) for more
66 information.
67
69 Generators are invoked with three arguments: paths to directories where
70 generators can place their generated unit files or symlinks. By default
71 those paths are runtime directories that are included in the search
72 path of systemd, but a generator may be called with different paths for
73 debugging purposes. If only one argument is provided, the generator
74 should use the same directory as the three output paths.
75
76 1. normal-dir
77
78 In normal use this is /run/systemd/generator in case of the system
79 generators and $XDG_RUNTIME_DIR/systemd/generator in case of the
80 user generators. Unit files placed in this directory take
81 precedence over vendor unit configuration but not over native
82 user/administrator unit configuration.
83
84 2. early-dir
85
86 In normal use this is /run/systemd/generator.early in case of the
87 system generators and $XDG_RUNTIME_DIR/systemd/generator.early in
88 case of the user generators. Unit files placed in this directory
89 override unit files in /usr/, /run/ and /etc/. This means that unit
90 files placed in this directory take precedence over all normal
91 configuration, both vendor and user/administrator.
92
93 3. late-dir
94
95 In normal use this is /run/systemd/generator.late in case of the
96 system generators and $XDG_RUNTIME_DIR/systemd/generator.late in
97 case of the user generators. This directory may be used to extend
98 the unit file tree without overriding any other unit files. Any
99 native configuration files supplied by the vendor or
100 user/administrator take precedence.
101
102 Note: generators must not write to other locations or otherwise make
103 changes to system state. Generator output is supposed to last only
104 until the next daemon-reload or daemon-reexec; if the generator is
105 replaced or masked, its effects should vanish.
106
108 The service manager sets a number of environment variables when
109 invoking generator executables. They carry information about the
110 execution context of the generator, in order to simplify
111 conditionalizing generators to specific environments. The following
112 environment variables are set:
113
114 $SYSTEMD_SCOPE
115 If the generator is invoked from the system service manager this
116 variable is set to "system"; if invoked from the per-user service
117 manager it is set to "user".
118
119 $SYSTEMD_IN_INITRD
120 If the generator is run as part of an initrd this is set to "1". If
121 it is run from the regular host (i.e. after the transition from
122 initrd to host) it is set to "0". This environment variable is only
123 set for system generators.
124
125 $SYSTEMD_FIRST_BOOT
126 If this boot-up cycle is considered a "first boot", this is set to
127 "1"; if it is a subsequent, regular boot it is set to "0". For
128 details see the documentation of ConditionFirstBoot= in
129 systemd.unit(5). This environment variable is only set for system
130 generators.
131
132 $SYSTEMD_VIRTUALIZATION
133 If the service manager is run in a virtualized environment,
134 $SYSTEMD_VIRTUALIZATION is set to a pair of strings, separated by a
135 colon. The first string is either "vm" or "container", categorizing
136 the type of virtualization. The second string identifies the
137 implementation of the virtualization technology. If no
138 virtualization is detected this variable will not be set. This data
139 is identical to what systemd-detect-virt(1) detects and reports,
140 and uses the same vocabulary of virtualization implementation
141 identifiers.
142
143 $SYSTEMD_ARCHITECTURE
144 This variable is set to a short identifier of the reported
145 architecture of the system. For details about defined values, see
146 documentation of ConditionArchitecture= in systemd.unit(5).
147
148 $CREDENTIALS_DIRECTORY, $ENCRYPTED_CREDENTIALS_DIRECTORY
149 If set, refers to the directory system credentials have been placed
150 in. Credentials passed into the system in plaintext form will be
151 placed in $CREDENTIALS_DIRECTORY, and those passed in in encrypted
152 form will be placed in $ENCRYPTED_CREDENTIALS_DIRECTORY. Use the
153 systemd-creds(1) command to automatically decrypt/authenticate
154 credentials passed in, if needed. Specifically, use the
155 systemd-creds --system cat command.
156
157 $SYSTEMD_CONFIDENTIAL_VIRTUALIZATION
158 If the service manager is run in a confidential virtualized
159 environment, $SYSTEMD_CONFIDENTIAL_VIRTUALIZATION is set to a
160 string that identifies the confidential virtualization hardware
161 technology. If no confidential virtualization is detected this
162 variable will not be set. This data is identical to what systemd-
163 detect-virt(1) detects and reports, and uses the same vocabulary of
164 confidential virtualization technology identifiers.
165
167 • All generators are executed in parallel. That means all executables
168 are started at the very same time and need to be able to cope with
169 this parallelism.
170
171 • Generators are run very early at boot and cannot rely on any
172 external services. They may not talk to any other process. That
173 includes simple things such as logging to syslog(3), or systemd
174 itself (this means: no systemctl(1))! Non-essential file systems
175 like /var/ and /home/ are mounted after generators have run.
176 Generators can however rely on the most basic kernel functionality
177 to be available, as well as mounted /sys/, /proc/, /dev/, /usr/ and
178 /run/ file systems.
179
180 • Units written by generators are removed when the configuration is
181 reloaded. That means the lifetime of the generated units is closely
182 bound to the reload cycles of systemd itself.
183
184 • Generators should only be used to generate unit files, .d/*.conf
185 drop-ins for them and symlinks to them, not any other kind of
186 non-unit related configuration. Due to the lifecycle logic
187 mentioned above, generators are not a good fit to generate dynamic
188 configuration for other services. If you need to generate dynamic
189 configuration for other services, do so in normal services you
190 order before the service in question.
191
192 Note that using the StandardInputData=/StandardInputText= settings
193 of service unit files (see systemd.exec(5)), it is possible to make
194 arbitrary input data (including daemon-specific configuration) part
195 of the unit definitions, which often might be sufficient to embed
196 data or configuration for other programs into unit files in a
197 native fashion.
198
199 • Since syslog(3) is not available (see above), log messages have to
200 be written to /dev/kmsg instead.
201
202 • The generator should always include its own name in a comment at
203 the top of the generated file, so that the user can easily figure
204 out which component created or amended a particular unit.
205
206 The SourcePath= directive should be used in generated files to
207 specify the source configuration file they are generated from. This
208 makes things more easily understood by the user and also has the
209 benefit that systemd can warn the user about configuration files
210 that changed on disk but have not been read yet by systemd. The
211 SourcePath= value does not have to be a file in a physical
212 filesystem. For example, in the common case of the generator
213 looking at the kernel command line, SourcePath=/proc/cmdline should
214 be used.
215
216 • Generators may write out dynamic unit files or just hook unit files
217 into other units with the usual .wants/ or .requires/ symlinks.
218 Often, it is nicer to simply instantiate a template unit file from
219 /usr/ with a generator instead of writing out entirely dynamic unit
220 files. Of course, this works only if a single parameter is to be
221 used.
222
223 • If you are careful, you can implement generators in shell scripts.
224 We do recommend C code however, since generators are executed
225 synchronously and hence delay the entire boot if they are slow.
226
227 • Regarding overriding semantics: there are two rules we try to
228 follow when thinking about the overriding semantics:
229
230 1. User configuration should override vendor configuration. This
231 (mostly) means that stuff from /etc/ should override stuff from
232 /usr/.
233
234 2. Native configuration should override non-native configuration.
235 This (mostly) means that stuff you generate should never
236 override native unit files for the same purpose.
237
238 Of these two rules the first rule is probably the more important
239 one and breaks the second one sometimes. Hence, when deciding
240 whether to use argv[1], argv[2], or argv[3], your default choice
241 should probably be argv[1].
242
243 • Instead of heading off now and writing all kind of generators for
244 legacy configuration file formats, please think twice! It is often
245 a better idea to just deprecate old stuff instead of keeping it
246 artificially alive.
247
249 Example 1. systemd-fstab-generator
250
251 systemd-fstab-generator(8) converts /etc/fstab into native mount units.
252 It uses argv[1] as location to place the generated unit files in order
253 to allow the user to override /etc/fstab with their own native unit
254 files, but also to ensure that /etc/fstab overrides any vendor default
255 from /usr/.
256
257 After editing /etc/fstab, the user should invoke systemctl
258 daemon-reload. This will re-run all generators and cause systemd to
259 reload units from disk. To actually mount new directories added to
260 fstab, systemctl start /path/to/mountpoint or systemctl start
261 local-fs.target may be used.
262
263 Example 2. systemd-system-update-generator
264
265 systemd-system-update-generator(8) temporarily redirects default.target
266 to system-update.target, if a system update is scheduled. Since this
267 needs to override the default user configuration for default.target, it
268 uses argv[2]. For details about this logic, see systemd.offline-
269 updates(7).
270
271 Example 3. Debugging a generator
272
273 dir=$(mktemp -d)
274 SYSTEMD_LOG_LEVEL=debug /usr/lib/systemd/system-generators/systemd-fstab-generator \
275 "$dir" "$dir" "$dir"
276 find $dir
277
279 systemd(1), systemd-cryptsetup-generator(8), systemd-debug-
280 generator(8), systemd-fstab-generator(8), fstab(5), systemd-getty-
281 generator(8), systemd-gpt-auto-generator(8), systemd-hibernate-resume-
282 generator(8), systemd-rc-local-generator(8), systemd-system-update-
283 generator(8), systemd-sysv-generator(8), systemd-xdg-autostart-
284 generator(8), systemd.unit(5), systemctl(1), systemd.environment-
285 generator(7)
286
287
288
289systemd 254 SYSTEMD.GENERATOR(7)