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:`images.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. Scripts that are called **NOT CHROOTED** are
90 always invoked via the Bash shell. If a script exits with a non-zero exit code,
91 {kiwi} reports the failure and aborts the image creation.
92
93 Developing/Debugging Scripts
94 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
96 Creating a custom script may require some experimenting and testing. To help
97 developers with this task, {kiwi} calls scripts associated with a `screen`
98 session. The connection to `screen` is only done if {kiwi} is called with the
99 `--debug-run-scripts-in-screen` option.
100
101 In this mode, a script can be started using the following template:
102
103 .. code:: bash
104
105 # The magic bits are still not set
106
107 echo "break"
108 /bin/bash
109
110 Calling the script executes a `screen` session, which gives you access
111 to the break-in shell. You can then implement the desired script code in this
112 environment. Once the shell is closed, the {kiwi} process continues.
113
114 In addition to providing a fully featured terminal throughout the execution of
115 the script code, you also have control of the session during the process of
116 the image creation. Listing the active sessions for script execution can be done
117 as follows:
118
119 .. code:: bash
120
121 $ sudo screen -list
122
123 There is a screen on:
124 19699.pts-4.asterix (Attached)
125 1 Socket in /run/screens/S-root.
126
127 .. note::
128
129 As shown above, the screen session for executing the script code provides
130 extended control, which can be considered a security risk. Because of that,
131 {kiwi} only runs scripts through `screen` when explicitly enabled via the
132 `--debug-run-scripts-in-screen` switch. In production, all scripts must run
133 natively and must not require a terminal to operate correctly.
134
135 Script Template for config.sh / images.sh
136 -----------------------------------------
137
138 {kiwi} provides a collection of methods and variables that offer custom
139 actions. For details, see :ref:`image-customization-methods`. The following
140 template shows how to import this information into a script:
141
142 .. code:: bash
143
144 #======================================
145 # Include functions
146 #--------------------------------------
147 test -f /.kconfig && . /.kconfig
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` file to be sourced
164 by shell scripts. :file:`.kconfig` contains several helper functions
165 that can be used to simplify image configuration.
166
167 Functions
168 ^^^^^^^^^
169
170 The :file:`.kconfig` file provides a common set of functions. Functions
171 specific to SUSE Linux Enterprise and openSUSE start with `suse`; functions
172 applicable to all Linux distributions start with `base`.
173
174 The following list describes the functions provided by :file:`.kconfig` that are
175 supported for use in image scripts. Additional functions are defined in
176 :file:`.kconfig`, these are {kiwi} internals and may change without notice.
177
178 baseSetRunlevel {value}
179 Set the default run level.
180
181 baseStripAndKeep {list of info-files to keep}
182 A helper function for the `baseStrip*` functions that reads a list of files to
183 check from stdin for removing params: files that should be kept.
184
185 baseStripLocales {list of locales}
186 Removes all locales except for the ones given as the parameter.
187
188 baseStripTranslations {list of translations}
189 Removes all translations except those given as the parameter.
190
191 baseStripUnusedLibs
192 Removes libraries that are not directly linked against applications
193 in the bin directories.
194
195 baseUpdateSysConfig {filename} {variable} {value}
196 Updates the contents of a sysconfig variable.
197
198 baseSystemdServiceInstalled {service}
199 Prints the path of the first found systemd unit or mount with the name passed
200 as the first parameter.
201
202 baseSysVServiceInstalled {service}
203 Prints the name `${service}` if a SysV init service with the same name is
204 found; otherwise, it prints nothing.
205
206 baseSystemdCall {service_name} {args}
207 Calls `systemctl ${args} ${service_name}` if a systemd unit, a systemd
208 mount, or a SysV init service with the `${service_name}` exists.
209
210 baseInsertService {servicename}
211 Activates the specified service via :command:`systemctl`.
212
213 baseRemoveService {servicename}
214 Deactivates the specified service via :command:`systemctl`.
215
216 baseService {servicename} {on|off}
217 Activates or deactivates a service via :command:`systemctl`.
218 The function requires the service name and the value `on` or `off` as
219 parameters.
220
221 The following example enables the sshd service on boot:
222
223 .. code:: bash
224
225 baseService sshd on
226
227 suseInsertService {servicename}
228 Calls `baseInsertService`. It exists only for
229 compatibility reasons.
230
231 suseRemoveService {servicename}
232 Calls `baseRemoveService`. It exists only for
233 compatibility reasons.
234
235 suseService {servicename} {on|off}
236 Calls `baseService`. It exists only for compatibility
237 reasons.
238
239 suseSetupProduct
240 Creates the :file:`/etc/products.d/baseproduct` link
241 pointing to the product referenced either by :file:`/etc/SuSE-brand` or
242 :file:`/etc/os-release` or the latest `.prod` file available in
243 :file:`/etc/products.d`.
244
245 baseVagrantSetup
246 Configures the image to work as a vagrant box by performing the following
247 changes:
248
249 - add the `vagrant` user to :file:`/etc/sudoers`
250 or :file:`/etc/sudoers.d/vagrant`.
251 - insert the insecure vagrant ssh key, apply recommended
252 ssh settings, and start the ssh daemon.
253 - create the default shared folder :file:`/vagrant`.
254
255 Debug {message}
256 A helper function to print the supplied message if the variable DEBUG is
257 set to 1 (disabled by default).
258
259 Echo {echo commandline}
260 A helper function to print a message to the controlling terminal.
261
262 Rm {list of files}
263 A helper function to delete files and log the deletion.
264
265 Profile Environment Variables
266 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
267
268 kiwi exports its environment file for each shell script call.
269 The environment contains the variables listed below, among others.
270 The full list can be obtained by running:
271
272 .. code:: shell-session
273
274 $ kiwi-ng image info --description=<directory> --print-kiwi-env
275
276 $kiwi_compressed
277 A value of the `compressed` attribute set in the `type` element in
278 :file:`config.xml`.
279
280 $kiwi_delete
281 A list of all packages which are children of the `packages` element
282 with `type="delete"` in :file:`config.xml`.
283
284 $kiwi_drivers
285 A comma-separated list of driver entries as listed in the
286 `drivers` section of :file:`config.xml`.
287
288 $kiwi_iname
289 The name of the image as listed in :file:`config.xml`.
290
291 $kiwi_iversion
292 The image version as a string.
293
294 $kiwi_keytable
295 The contents of the keytable setup as specified in :file:`config.xml`.
296
297 $kiwi_language
298 The contents of the locale setup as specified in :file:`config.xml`.
299
300 $kiwi_profiles
301 A comma-separated list of profiles used to build this image.
302
303 $kiwi_timezone
304 The contents of the timezone setup as specified in :file:`config.xml`.
305
306 $kiwi_type
307 The image type as extracted from the `type` element in
308 :file:`config.xml`.
309
310 Configuration Tips
311 ------------------
312
313 #. **Locale configuration:**
314
315 To set the locale, {kiwi} relies on `systemd-firstboot`, which writes the
316 locale configuration file, :file:`/etc/locale.conf`. The values for the locale
317 settings are taken from the description XML file in the `<locale>` element
318 under `<preferences>`.
319
320 Keep in mind that if the build distribution does not use `/etc/locale.conf`,
321 `systemd-firstboot` does not have any effect on the locale
322 settings. For example, in the SLE12 distribution,
323 `systemd-firstboot` is only effective when locales in
324 :file:`/etc/sysconfig/language` are not set or when the file does not exist
325 at all. For compatibility reasons, the file :file:`/etc/sysconfig/language`
326 in SLE12 has precedence over :file:`/etc/locale.conf`, and management tools
327 can still use `sysconfig` files for locale settings.
328
329 In any case, it is possible to configure the locale setting inside the
330 :file:`config.sh` script in {kiwi} in a distribution-specific way or by
331 adding any additional configuration file as part of the overlay root tree.
332
333 #. **Stateless systemd UUIDs:**
334
335 Machine ID files (:file:`/etc/machine-id`, :file:`/var/lib/dbus/machine-id`)
336 may be created and set during the image package installation, depending on
337 the distribution. Those UUIDs must be unique and must be set only once
338 in each deployment.
339
340 If :file:`/etc/machine-id` does not exist or contains the string
341 `uninitialized` (systemd v249 and later), this triggers a firstboot action
342 in systemd, and the services are run with `ConditionFirstBoot=yes`. Unless the
343 file already contains a valid machine ID, systemd generates a machine ID and
344 writes it into the file, creating it if necessary. See the `machine-id man
345 page <https://www.freedesktop.org/software/systemd/man/machine-id.html>`_
346 for more details.
347
348 Depending on whether the firstboot action should be triggered or not,
349 :file:`/etc/machine-id` can be created, removed, or set to
350 `uninitialized` by :file:`config.sh`.
351
352 To prevent images from including a generated machine ID, {kiwi} clears
353 :file:`/etc/machine-id` if it exists and does not contain the string
354 `uninitialized`. This only applies to images based on a dracut initrd.
355
356 .. note:: `rw` is necessary if :file:`/etc/machine-id` does not exist.
357
358 For systemd to be able to write :file:`/etc/machine-id` on boot,
359 either the file must exist (so that a bind mount can be created) or
360 :file:`/etc` must be writable.
361
362 By default, the root filesystem is mounted read-only by dracut/systemd,
363 so a missing :file:`/etc/machine-id` will lead to an error on boot.
364 To force the initial mount to be read-write, add the `rw` option to the kernel command line.
365
366 .. note:: Avoid inconsistent :file:`/var/lib/dbus/machine-id`.
367
368 :file:`/etc/machine-id` and :file:`/var/lib/dbus/machine-id`
369 **must** contain the same unique ID. On modern systems,
370 :file:`/var/lib/dbus/machine-id` is already a symlink to
371 :file:`/etc/machine-id`. However, on older systems, there might be two
372 different files. This is the case for SLE-12-based images. If you are
373 targeting older operating systems, it is recommended to add the
374 symlink creation into :file:`config.sh`:
375
376 .. code:: bash
377
378 #======================================
379 # Make machine-id consistent with dbus
380 #--------------------------------------
381 if [ -e /var/lib/dbus/machine-id ]; then
382 rm /var/lib/dbus/machine-id
383 fi
384 ln -s /etc/machine-id /var/lib/dbus/machine-id