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