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 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
149 • All generators are executed in parallel. That means all executables
150 are started at the very same time and need to be able to cope with
151 this parallelism.
152
153 • Generators are run very early at boot and cannot rely on any
154 external services. They may not talk to any other process. That
155 includes simple things such as logging to syslog(3), or systemd
156 itself (this means: no systemctl(1))! Non-essential file systems
157 like /var/ and /home/ are mounted after generators have run.
158 Generators can however rely on the most basic kernel functionality
159 to be available, as well as mounted /sys/, /proc/, /dev/, /usr/ and
160 /run/ file systems.
161
162 • Units written by generators are removed when the configuration is
163 reloaded. That means the lifetime of the generated units is closely
164 bound to the reload cycles of systemd itself.
165
166 • Generators should only be used to generate unit files, .d/*.conf
167 drop-ins for them and symlinks to them, not any other kind of
168 non-unit related configuration. Due to the lifecycle logic
169 mentioned above, generators are not a good fit to generate dynamic
170 configuration for other services. If you need to generate dynamic
171 configuration for other services, do so in normal services you
172 order before the service in question.
173
174 Note that using the StandardInputData=/StandardInputText= settings
175 of service unit files (see systemd.exec(5)), it is possible to make
176 arbitrary input data (including daemon-specific configuration) part
177 of the unit definitions, which often might be sufficient to embed
178 data or configuration for other programs into unit files in a
179 native fashion.
180
181 • Since syslog(3) is not available (see above), log messages have to
182 be written to /dev/kmsg instead.
183
184 • The generator should always include its own name in a comment at
185 the top of the generated file, so that the user can easily figure
186 out which component created or amended a particular unit.
187
188 The SourcePath= directive should be used in generated files to
189 specify the source configuration file they are generated from. This
190 makes things more easily understood by the user and also has the
191 benefit that systemd can warn the user about configuration files
192 that changed on disk but have not been read yet by systemd. The
193 SourcePath= value does not have to be a file in a physical
194 filesystem. For example, in the common case of the generator
195 looking at the kernel command line, SourcePath=/proc/cmdline should
196 be used.
197
198 • Generators may write out dynamic unit files or just hook unit files
199 into other units with the usual .wants/ or .requires/ symlinks.
200 Often, it is nicer to simply instantiate a template unit file from
201 /usr/ with a generator instead of writing out entirely dynamic unit
202 files. Of course, this works only if a single parameter is to be
203 used.
204
205 • If you are careful, you can implement generators in shell scripts.
206 We do recommend C code however, since generators are executed
207 synchronously and hence delay the entire boot if they are slow.
208
209 • Regarding overriding semantics: there are two rules we try to
210 follow when thinking about the overriding semantics:
211
212 1. User configuration should override vendor configuration. This
213 (mostly) means that stuff from /etc/ should override stuff from
214 /usr/.
215
216 2. Native configuration should override non-native configuration.
217 This (mostly) means that stuff you generate should never
218 override native unit files for the same purpose.
219
220 Of these two rules the first rule is probably the more important
221 one and breaks the second one sometimes. Hence, when deciding
222 whether to use argv[1], argv[2], or argv[3], your default choice
223 should probably be argv[1].
224
225 • Instead of heading off now and writing all kind of generators for
226 legacy configuration file formats, please think twice! It is often
227 a better idea to just deprecate old stuff instead of keeping it
228 artificially alive.
229
231 Example 1. systemd-fstab-generator
232
233 systemd-fstab-generator(8) converts /etc/fstab into native mount units.
234 It uses argv[1] as location to place the generated unit files in order
235 to allow the user to override /etc/fstab with their own native unit
236 files, but also to ensure that /etc/fstab overrides any vendor default
237 from /usr/.
238
239 After editing /etc/fstab, the user should invoke systemctl
240 daemon-reload. This will re-run all generators and cause systemd to
241 reload units from disk. To actually mount new directories added to
242 fstab, systemctl start /path/to/mountpoint or systemctl start
243 local-fs.target may be used.
244
245 Example 2. systemd-system-update-generator
246
247 systemd-system-update-generator(8) temporarily redirects default.target
248 to system-update.target, if a system update is scheduled. Since this
249 needs to override the default user configuration for default.target, it
250 uses argv[2]. For details about this logic, see systemd.offline-
251 updates(7).
252
253 Example 3. Debugging a generator
254
255 dir=$(mktemp -d)
256 SYSTEMD_LOG_LEVEL=debug /usr/lib/systemd/system-generators/systemd-fstab-generator \
257 "$dir" "$dir" "$dir"
258 find $dir
259
261 systemd(1), systemd-cryptsetup-generator(8), systemd-debug-
262 generator(8), systemd-fstab-generator(8), fstab(5), systemd-getty-
263 generator(8), systemd-gpt-auto-generator(8), systemd-hibernate-resume-
264 generator(8), systemd-rc-local-generator(8), systemd-system-update-
265 generator(8), systemd-sysv-generator(8), systemd-xdg-autostart-
266 generator(8), systemd.unit(5), systemctl(1), systemd.environment-
267 generator(7)
268
269
270
271systemd 253 SYSTEMD.GENERATOR(7)