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