Debian Codegraph @

doc/source/concept_and_workflow/shell_scripts.rst

raw
1 .. _working-with-kiwi-user-defined-scripts:
2
3 User-Defined Scripts
4 ====================
5
6 .. note:: **Abstract**
7
8 This chapter describes the purpose of the user-defined scripts
9 :file:`config.sh`, :file:`image.sh`, :file:`pre_disk_sync.sh`, and
10 :file:`disk.sh`. These scripts can be used to further customize an image in
11 ways that are not possible via the image description alone.
12
13 {kiwi} supports the following optional scripts that it runs in a
14 root environment (chroot) containing an appliance:
15
16 post_bootstrap.sh
17 Runs at the end of the `bootstrap` phase as part of the :ref:`prepare step
18 <prepare-step>`. The script can be used to configure the package manager with
19 additional settings that apply in the following chroot-based installation step,
20 which completes the installation. The script can also be used for other tasks.
21
22 config.sh
23 Runs at the end of the :ref:`prepare step <prepare-step>`
24 and after users have been set and the *overlay tree directory*
25 has been applied. It is usually used to apply a permanent and final
26 change of data in the root tree, such as modifying a package-specific
27 config file.
28
29 config-overlay.sh
30 Available only if `delta_root="true"` is set on a `docker` or `oci` build type
31 that also uses `derived_from`. In this case, the script runs at
32 the end of the :ref:`prepare step <prepare-step>` prior to the `umount` of the
33 overlay root tree. It runs after `config.sh` (if specified), and it is the
34 last entry point to change the delta root tree.
35
36 config-host-overlay.sh
37 Available only if `delta_root="true"` is set on a `docker` or `oci` build type
38 that also uses `derived_from`. In this case, the script runs at
39 the end of the :ref:`prepare step <prepare-step>` prior to the `umount` of the
40 overlay root tree. The script is called **NOT CHROOTED** from the host with
41 the image root directory as its working directory. It runs after `config.sh`
42 and `config-overlay.sh` (if any or both are specified), and it is the last
43 entry point to change the delta root tree.
44
45 images.sh
46 Executed at the beginning of the :ref:`image creation process <create-step>`.
47 It runs in the same image root tree created by the prepare step,
48 but it is invoked whenever an image needs to be created from that root tree.
49 It is normally used to apply image-type-specific changes to the root tree, such
50 as a modification to a config file that must be done when building a live
51 ISO but not when building a virtual disk image.
52
53 pre_disk_sync.sh
54 Executed for the disk image type `oem` only, and it runs
55 right before the synchronization of the root tree into the disk image
56 loop file. The :file:`pre_disk_sync.sh` can be used to change the
57 content of the root tree as a last action before the sync to
58 the disk image is performed. This is useful, for example, for deleting
59 components from the system that were needed earlier or cannot
60 be modified afterward when syncing into a read-only filesystem.
61
62 disk.sh
63 Executed for the disk image type `oem` only, and it runs after the
64 synchronization of the root tree to the disk image loop file. The `chroot`
65 environment for this script call is the virtual disk itself and not the root
66 tree. The script :file:`disk.sh` is normally used to apply changes to parts of
67 the system that are not an element of the file-based root tree, such as the
68 partition table, the contents of the final initrd, the bootloader, filesystem
69 attributes, etc.
70
71 Two further hooks have variable file names. Their paths come from the
72 `editbootconfig` and `editbootinstall` attributes of the `type` element, and
73 both are called **NOT CHROOTED** from the host with the image root directory as
74 the working directory:
75
76 editbootconfig
77 Called after the bootloader configuration has been written into the system
78 image and before the root tree is synced into the disk. Two arguments are
79 passed: the boot filesystem name and the boot partition number. This hook is
80 also invoked for the live ISO build type.
81
82 editbootinstall
83 Called as the very last step of the disk build, after the bootloader has been
84 installed. Two arguments are passed: the disk image file name and the device
85 node of the boot partition. Only invoked for the `oem` disk build type.
86
87 {kiwi} executes scripts via the operating system if their executable
88 bit is set (in that case, a shebang is mandatory); otherwise, they are
89 invoked via the Bash shell. If a script exits with a non-zero exit code,
90 {kiwi} reports the failure and aborts the image creation.
91
92 Developing/Debugging Scripts
93 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95 Creating a custom script may require some experimenting and testing. To help
96 developers with this task, {kiwi} calls scripts associated with a `screen`
97 session. The connection to `screen` is only done if {kiwi} is called with the
98 `--debug-run-scripts-in-screen` option.
99
100 In this mode, a script can be started using the following template:
101
102 .. code:: bash
103
104 # The magic bits are still not set
105
106 echo "break"
107 /bin/bash
108
109 Calling the script executes a `screen` session, which gives you access
110 to the break-in shell. You can then implement the desired script code in this
111 environment. Once the shell is closed, the {kiwi} process continues.
112
113 In addition to providing a fully featured terminal throughout the execution of
114 the script code, you also have control of the session during the process of
115 the image creation. Listing the active sessions for script execution can be done
116 as follows:
117
118 .. code:: bash
119
120 $ sudo screen -list
121
122 There is a screen on:
123 19699.pts-4.asterix (Attached)
124 1 Socket in /run/screens/S-root.
125
126 .. note::
127
128 As shown above, the screen session for executing the script code provides
129 extended control, which can be considered a security risk. Because of that,
130 {kiwi} only runs scripts through `screen` when explicitly enabled via the
131 `--debug-run-scripts-in-screen` switch. In production, all scripts must run
132 natively and must not require a terminal to operate correctly.
133
134 Script Template for config.sh / images.sh
135 -----------------------------------------
136
137 {kiwi} provides a collection of methods and variables that offer custom
138 actions. For details, see :ref:`image-customization-methods`. The following
139 template shows how to import this information into a script:
140
141 .. code:: bash
142
143 #======================================
144 # Include functions & variables
145 #--------------------------------------
146 test -f /.kconfig && . /.kconfig
147 test -f /.profile && . /.profile
148
149 ...
150
151 .. warning:: Modifications of the unpacked root tree
152
153 Keep in mind that there is only one unpacked root tree the
154 script operates in. This means that all changes are permanent
155 and are not automatically restored.
156
157
158 .. _image-customization-methods:
159
160 Functions and Variables Provided by {kiwi}
161 -------------------------------------------
162
163 {kiwi} creates the :file:`.kconfig` and :file:`.profile` files to be sourced
164 by the shell scripts :file:`config.sh` and :file:`images.sh`.
165 :file:`.kconfig` contains several helper functions that can be used to
166 simplify image configuration, while :file:`.profile` contains environment
167 variables populated from the settings provided in the image
168 description.
169
170 Functions
171 ^^^^^^^^^
172
173 The :file:`.kconfig` file provides a common set of functions. Functions
174 specific to SUSE Linux Enterprise and openSUSE start with `suse`; functions
175 applicable to all Linux distributions start with `base`.
176
177 The following list describes the functions provided by :file:`.kconfig` that are
178 supported for use in image scripts. Additional functions are defined in
179 :file:`.kconfig`, these are {kiwi} internals and may change without notice.
180
181 baseSetRunlevel {value}
182 Set the default run level.
183
184 baseStripAndKeep {list of info-files to keep}
185 A helper function for the `baseStrip*` functions that reads a list of files to
186 check from stdin for removing params: files that should be kept.
187
188 baseStripLocales {list of locales}
189 Removes all locales except for the ones given as the parameter.
190
191 baseStripTranslations {list of translations}
192 Removes all translations except those given as the parameter.
193
194 baseStripUnusedLibs
195 Removes libraries that are not directly linked against applications
196 in the bin directories.
197
198 baseUpdateSysConfig {filename} {variable} {value}
199 Updates the contents of a sysconfig variable.
200
201 baseSystemdServiceInstalled {service}
202 Prints the path of the first found systemd unit or mount with the name passed
203 as the first parameter.
204
205 baseSysVServiceInstalled {service}
206 Prints the name `${service}` if a SysV init service with the same name is
207 found; otherwise, it prints nothing.
208
209 baseSystemdCall {service_name} {args}
210 Calls `systemctl ${args} ${service_name}` if a systemd unit, a systemd
211 mount, or a SysV init service with the `${service_name}` exists.
212
213 baseInsertService {servicename}
214 Activates the specified service via :command:`systemctl`.
215
216 baseRemoveService {servicename}
217 Deactivates the specified service via :command:`systemctl`.
218
219 baseService {servicename} {on|off}
220 Activates or deactivates a service via :command:`systemctl`.
221 The function requires the service name and the value `on` or `off` as
222 parameters.
223
224 The following example enables the sshd service on boot:
225
226 .. code:: bash
227
228 baseService sshd on
229
230 suseInsertService {servicename}
231 Calls `baseInsertService`. It exists only for
232 compatibility reasons.
233
234 suseRemoveService {servicename}
235 Calls `baseRemoveService`. It exists only for
236 compatibility reasons.
237
238 suseService {servicename} {on|off}
239 Calls `baseService`. It exists only for compatibility
240 reasons.
241
242 suseSetupProduct
243 Creates the :file:`/etc/products.d/baseproduct` link
244 pointing to the product referenced either by :file:`/etc/SuSE-brand` or
245 :file:`/etc/os-release` or the latest `.prod` file available in
246 :file:`/etc/products.d`.
247
248 baseVagrantSetup
249 Configures the image to work as a vagrant box by performing the following
250 changes:
251
252 - add the `vagrant` user to :file:`/etc/sudoers`
253 or :file:`/etc/sudoers.d/vagrant`.
254 - insert the insecure vagrant ssh key, apply recommended
255 ssh settings, and start the ssh daemon.
256 - create the default shared folder :file:`/vagrant`.
257
258 Debug {message}
259 A helper function to print the supplied message if the variable DEBUG is
260 set to 1 (disabled by default).
261
262 Echo {echo commandline}
263 A helper function to print a message to the controlling terminal.
264
265 Rm {list of files}
266 A helper function to delete files and log the deletion.
267
268 Profile Environment Variables
269 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
270
271 The :file:`.profile` environment file is created by {kiwi} and contains the
272 variables listed below, among others. The full list can be obtained by running:
273
274 .. code:: shell-session
275
276 $ kiwi-ng image info --description=<directory> --print-kiwi-env
277
278 $kiwi_compressed
279 A value of the `compressed` attribute set in the `type` element in
280 :file:`config.xml`.
281
282 $kiwi_delete
283 A list of all packages which are children of the `packages` element
284 with `type="delete"` in :file:`config.xml`.
285
286 $kiwi_drivers
287 A comma-separated list of driver entries as listed in the
288 `drivers` section of :file:`config.xml`.
289
290 $kiwi_iname
291 The name of the image as listed in :file:`config.xml`.
292
293 $kiwi_iversion
294 The image version as a string.
295
296 $kiwi_keytable
297 The contents of the keytable setup as specified in :file:`config.xml`.
298
299 $kiwi_language
300 The contents of the locale setup as specified in :file:`config.xml`.
301
302 $kiwi_profiles
303 A comma-separated list of profiles used to build this image.
304
305 $kiwi_timezone
306 The contents of the timezone setup as specified in :file:`config.xml`.
307
308 $kiwi_type
309 The image type as extracted from the `type` element in
310 :file:`config.xml`.
311
312 .. note:: **.profile.extra**
313
314 If the file :file:`/.profile.extra` is available in the initrd, {kiwi}
315 imports the file importing :file:`/.profile`.
316
317 Configuration Tips
318 ------------------
319
320 #. **Locale configuration:**
321
322 To set the locale, {kiwi} relies on `systemd-firstboot`, which writes the
323 locale configuration file, :file:`/etc/locale.conf`. The values for the locale
324 settings are taken from the description XML file in the `<locale>` element
325 under `<preferences>`.
326
327 Keep in mind that if the build distribution does not use `/etc/locale.conf`,
328 `systemd-firstboot` does not have any effect on the locale
329 settings. For example, in the SLE12 distribution,
330 `systemd-firstboot` is only effective when locales in
331 :file:`/etc/sysconfig/language` are not set or when the file does not exist
332 at all. For compatibility reasons, the file :file:`/etc/sysconfig/language`
333 in SLE12 has precedence over :file:`/etc/locale.conf`, and management tools
334 can still use `sysconfig` files for locale settings.
335
336 In any case, it is possible to configure the locale setting inside the
337 :file:`config.sh` script in {kiwi} in a distribution-specific way or by
338 adding any additional configuration file as part of the overlay root tree.
339
340 #. **Stateless systemd UUIDs:**
341
342 Machine ID files (:file:`/etc/machine-id`, :file:`/var/lib/dbus/machine-id`)
343 may be created and set during the image package installation, depending on
344 the distribution. Those UUIDs must be unique and must be set only once
345 in each deployment.
346
347 If :file:`/etc/machine-id` does not exist or contains the string
348 `uninitialized` (systemd v249 and later), this triggers a firstboot action
349 in systemd, and the services are run with `ConditionFirstBoot=yes`. Unless the
350 file already contains a valid machine ID, systemd generates a machine ID and
351 writes it into the file, creating it if necessary. See the `machine-id man
352 page <https://www.freedesktop.org/software/systemd/man/machine-id.html>`_
353 for more details.
354
355 Depending on whether the firstboot action should be triggered or not,
356 :file:`/etc/machine-id` can be created, removed, or set to
357 `uninitialized` by :file:`config.sh`.
358
359 To prevent images from including a generated machine ID, {kiwi} clears
360 :file:`/etc/machine-id` if it exists and does not contain the string
361 `uninitialized`. This only applies to images based on a dracut initrd.
362
363 .. note:: `rw` is necessary if :file:`/etc/machine-id` does not exist.
364
365 For systemd to be able to write :file:`/etc/machine-id` on boot,
366 either the file must exist (so that a bind mount can be created) or
367 :file:`/etc` must be writable.
368
369 By default, the root filesystem is mounted read-only by dracut/systemd,
370 so a missing :file:`/etc/machine-id` will lead to an error on boot.
371 To force the initial mount to be read-write, add the `rw` option to the kernel command line.
372
373 .. note:: Avoid inconsistent :file:`/var/lib/dbus/machine-id`.
374
375 :file:`/etc/machine-id` and :file:`/var/lib/dbus/machine-id`
376 **must** contain the same unique ID. On modern systems,
377 :file:`/var/lib/dbus/machine-id` is already a symlink to
378 :file:`/etc/machine-id`. However, on older systems, there might be two
379 different files. This is the case for SLE-12-based images. If you are
380 targeting older operating systems, it is recommended to add the
381 symlink creation into :file:`config.sh`:
382
383 .. code:: bash
384
385 #======================================
386 # Make machine-id consistent with dbus
387 #--------------------------------------
388 if [ -e /var/lib/dbus/machine-id ]; then
389 rm /var/lib/dbus/machine-id
390 fi
391 ln -s /etc/machine-id /var/lib/dbus/machine-id