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
145 #--------------------------------------
146 test -f /.kconfig && . /.kconfig
147
148 ...
149
150 .. warning:: Modifications of the unpacked root tree
151
152 Keep in mind that there is only one unpacked root tree the
153 script operates in. This means that all changes are permanent
154 and are not automatically restored.
155
156
157 .. _image-customization-methods:
158
159 Functions and Variables Provided by {kiwi}
160 -------------------------------------------
161
162 {kiwi} creates the :file:`.kconfig` file to be sourced
163 by shell scripts. :file:`.kconfig` contains several helper functions
164 that can be used to simplify image configuration.
165
166 Functions
167 ^^^^^^^^^
168
169 The :file:`.kconfig` file provides a common set of functions. Functions
170 specific to SUSE Linux Enterprise and openSUSE start with `suse`; functions
171 applicable to all Linux distributions start with `base`.
172
173 The following list describes the functions provided by :file:`.kconfig` that are
174 supported for use in image scripts. Additional functions are defined in
175 :file:`.kconfig`, these are {kiwi} internals and may change without notice.
176
177 baseSetRunlevel {value}
178 Set the default run level.
179
180 baseStripAndKeep {list of info-files to keep}
181 A helper function for the `baseStrip*` functions that reads a list of files to
182 check from stdin for removing params: files that should be kept.
183
184 baseStripLocales {list of locales}
185 Removes all locales except for the ones given as the parameter.
186
187 baseStripTranslations {list of translations}
188 Removes all translations except those given as the parameter.
189
190 baseStripUnusedLibs
191 Removes libraries that are not directly linked against applications
192 in the bin directories.
193
194 baseUpdateSysConfig {filename} {variable} {value}
195 Updates the contents of a sysconfig variable.
196
197 baseSystemdServiceInstalled {service}
198 Prints the path of the first found systemd unit or mount with the name passed
199 as the first parameter.
200
201 baseSysVServiceInstalled {service}
202 Prints the name `${service}` if a SysV init service with the same name is
203 found; otherwise, it prints nothing.
204
205 baseSystemdCall {service_name} {args}
206 Calls `systemctl ${args} ${service_name}` if a systemd unit, a systemd
207 mount, or a SysV init service with the `${service_name}` exists.
208
209 baseInsertService {servicename}
210 Activates the specified service via :command:`systemctl`.
211
212 baseRemoveService {servicename}
213 Deactivates the specified service via :command:`systemctl`.
214
215 baseService {servicename} {on|off}
216 Activates or deactivates a service via :command:`systemctl`.
217 The function requires the service name and the value `on` or `off` as
218 parameters.
219
220 The following example enables the sshd service on boot:
221
222 .. code:: bash
223
224 baseService sshd on
225
226 suseInsertService {servicename}
227 Calls `baseInsertService`. It exists only for
228 compatibility reasons.
229
230 suseRemoveService {servicename}
231 Calls `baseRemoveService`. It exists only for
232 compatibility reasons.
233
234 suseService {servicename} {on|off}
235 Calls `baseService`. It exists only for compatibility
236 reasons.
237
238 suseSetupProduct
239 Creates the :file:`/etc/products.d/baseproduct` link
240 pointing to the product referenced either by :file:`/etc/SuSE-brand` or
241 :file:`/etc/os-release` or the latest `.prod` file available in
242 :file:`/etc/products.d`.
243
244 baseVagrantSetup
245 Configures the image to work as a vagrant box by performing the following
246 changes:
247
248 - add the `vagrant` user to :file:`/etc/sudoers`
249 or :file:`/etc/sudoers.d/vagrant`.
250 - insert the insecure vagrant ssh key, apply recommended
251 ssh settings, and start the ssh daemon.
252 - create the default shared folder :file:`/vagrant`.
253
254 Debug {message}
255 A helper function to print the supplied message if the variable DEBUG is
256 set to 1 (disabled by default).
257
258 Echo {echo commandline}
259 A helper function to print a message to the controlling terminal.
260
261 Rm {list of files}
262 A helper function to delete files and log the deletion.
263
264 Profile Environment Variables
265 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
266
267 kiwi exports its environment file for each shell script call.
268 The environment contains the variables listed below, among others.
269 The full list can be obtained by running:
270
271 .. code:: shell-session
272
273 $ kiwi-ng image info --description=<directory> --print-kiwi-env
274
275 $kiwi_compressed
276 A value of the `compressed` attribute set in the `type` element in
277 :file:`config.xml`.
278
279 $kiwi_delete
280 A list of all packages which are children of the `packages` element
281 with `type="delete"` in :file:`config.xml`.
282
283 $kiwi_drivers
284 A comma-separated list of driver entries as listed in the
285 `drivers` section of :file:`config.xml`.
286
287 $kiwi_iname
288 The name of the image as listed in :file:`config.xml`.
289
290 $kiwi_iversion
291 The image version as a string.
292
293 $kiwi_keytable
294 The contents of the keytable setup as specified in :file:`config.xml`.
295
296 $kiwi_language
297 The contents of the locale setup as specified in :file:`config.xml`.
298
299 $kiwi_profiles
300 A comma-separated list of profiles used to build this image.
301
302 $kiwi_timezone
303 The contents of the timezone setup as specified in :file:`config.xml`.
304
305 $kiwi_type
306 The image type as extracted from the `type` element in
307 :file:`config.xml`.
308
309 Configuration Tips
310 ------------------
311
312 #. **Locale configuration:**
313
314 To set the locale, {kiwi} relies on `systemd-firstboot`, which writes the
315 locale configuration file, :file:`/etc/locale.conf`. The values for the locale
316 settings are taken from the description XML file in the `<locale>` element
317 under `<preferences>`.
318
319 Keep in mind that if the build distribution does not use `/etc/locale.conf`,
320 `systemd-firstboot` does not have any effect on the locale
321 settings. For example, in the SLE12 distribution,
322 `systemd-firstboot` is only effective when locales in
323 :file:`/etc/sysconfig/language` are not set or when the file does not exist
324 at all. For compatibility reasons, the file :file:`/etc/sysconfig/language`
325 in SLE12 has precedence over :file:`/etc/locale.conf`, and management tools
326 can still use `sysconfig` files for locale settings.
327
328 In any case, it is possible to configure the locale setting inside the
329 :file:`config.sh` script in {kiwi} in a distribution-specific way or by
330 adding any additional configuration file as part of the overlay root tree.
331
332 #. **Stateless systemd UUIDs:**
333
334 Machine ID files (:file:`/etc/machine-id`, :file:`/var/lib/dbus/machine-id`)
335 may be created and set during the image package installation, depending on
336 the distribution. Those UUIDs must be unique and must be set only once
337 in each deployment.
338
339 If :file:`/etc/machine-id` does not exist or contains the string
340 `uninitialized` (systemd v249 and later), this triggers a firstboot action
341 in systemd, and the services are run with `ConditionFirstBoot=yes`. Unless the
342 file already contains a valid machine ID, systemd generates a machine ID and
343 writes it into the file, creating it if necessary. See the `machine-id man
344 page <https://www.freedesktop.org/software/systemd/man/machine-id.html>`_
345 for more details.
346
347 Depending on whether the firstboot action should be triggered or not,
348 :file:`/etc/machine-id` can be created, removed, or set to
349 `uninitialized` by :file:`config.sh`.
350
351 To prevent images from including a generated machine ID, {kiwi} clears
352 :file:`/etc/machine-id` if it exists and does not contain the string
353 `uninitialized`. This only applies to images based on a dracut initrd.
354
355 .. note:: `rw` is necessary if :file:`/etc/machine-id` does not exist.
356
357 For systemd to be able to write :file:`/etc/machine-id` on boot,
358 either the file must exist (so that a bind mount can be created) or
359 :file:`/etc` must be writable.
360
361 By default, the root filesystem is mounted read-only by dracut/systemd,
362 so a missing :file:`/etc/machine-id` will lead to an error on boot.
363 To force the initial mount to be read-write, add the `rw` option to the kernel command line.
364
365 .. note:: Avoid inconsistent :file:`/var/lib/dbus/machine-id`.
366
367 :file:`/etc/machine-id` and :file:`/var/lib/dbus/machine-id`
368 **must** contain the same unique ID. On modern systems,
369 :file:`/var/lib/dbus/machine-id` is already a symlink to
370 :file:`/etc/machine-id`. However, on older systems, there might be two
371 different files. This is the case for SLE-12-based images. If you are
372 targeting older operating systems, it is recommended to add the
373 symlink creation into :file:`config.sh`:
374
375 .. code:: bash
376
377 #======================================
378 # Make machine-id consistent with dbus
379 #--------------------------------------
380 if [ -e /var/lib/dbus/machine-id ]; then
381 rm /var/lib/dbus/machine-id
382 fi
383 ln -s /etc/machine-id /var/lib/dbus/machine-id