Scripts
Scripts in the image description directory run at defined points of the build, see the diagram in The Build Process. Most of them run inside of the root tree (chroot):
Script |
Runs |
Purpose |
|---|---|---|
|
Prepare step, after the bootstrap |
Configure the package manager for the following installation, or other early tasks. |
|
Prepare step, after the overlay tree and the users are set up |
Final changes to the root tree, for example to configuration files of packages. |
|
Prepare step, after |
Last change to the delta root tree of a |
|
Prepare step, after |
Like |
|
Create step, at the beginning |
Changes for the image type being built, for example only for a live ISO. |
|
Create step of |
Last changes to the root tree, for example to remove tools or to change files which become read-only. |
|
Create step of |
Changes outside of the root tree, such as the partition table, the initrd or the bootloader. |
Two further scripts are set with the editbootconfig and
editbootinstall attributes of the type element. Both are called on
the host, not chrooted, in the image root directory:
editbootconfigRuns after the bootloader configuration was written, before the root tree is synced into the disk, for disk and live ISO images. Its arguments are the name of the boot filesystem and the number of the boot partition.
editbootinstallRuns as the last step of an
oemdisk build, after the bootloader was installed. Its arguments are the disk image file name and the device of the boot partition.
A script with the executable bit set is run directly and needs a shebang line, otherwise it is run with Bash. Scripts which are not chrooted always run with Bash. If a script exits with a non-zero code, KIWI NG stops the build.
Warning
All scripts change the one root tree of the build. Their changes are permanent and are not undone.
Script Template
KIWI NG provides functions and variables to scripts in the file
/.kconfig. Source it at the start of config.sh or
images.sh:
#!/bin/bash
test -f /.kconfig && . /.kconfig
Functions and Variables Provided by KIWI NG
Functions
Functions starting with base work on all distributions, functions
starting with suse are specific to SUSE distributions. Other functions
in /.kconfig are internal to KIWI NG and may change without
notice.
- baseInsertService {servicename}
Enables the service with
systemctl.- baseRemoveService {servicename}
Disables the service with
systemctl.- baseService {servicename} {on|off}
Enables or disables the service, for example
baseService sshd on.- baseSystemdCall {service_name} {args}
Calls
systemctl ${args} ${service_name}if a systemd unit, mount or SysV init service with this name exists.- baseSystemdServiceInstalled {service}
Prints the path of the first systemd unit or mount with this name.
- baseSysVServiceInstalled {service}
Prints the service name if a SysV init service with this name exists.
- baseSetRunlevel {value}
Sets the default run level.
- baseUpdateSysConfig {filename} {variable} {value}
Sets a variable in a sysconfig file.
- baseStripLocales {list of locales}
Removes all locales except the given ones.
- baseStripTranslations {list of translations}
Removes all translations except the given ones.
- baseStripUnusedLibs
Removes libraries which no program in the bin directories links against.
- baseStripAndKeep {list of info-files to keep}
Helper of the
baseStrip*functions: removes the files read from stdin, except the given ones.- baseVagrantSetup
Sets up the image as a Vagrant box: adds the
vagrantuser to the sudoers, installs the insecure Vagrant ssh key with the recommended ssh settings, starts the ssh daemon and creates the shared folder/vagrant.- suseSetupProduct
Creates the link
/etc/products.d/baseproductto the product referenced by/etc/SuSE-brand,/etc/os-releaseor the latestprodfile in/etc/products.d.- suseInsertService, suseRemoveService, suseService
Call the respective
basefunction, for compatibility only.- Debug {message}
Prints the message if the variable
DEBUGis set to 1.- Echo {echo commandline}
Prints a message to the controlling terminal.
- Rm {list of files}
Deletes the files and logs the deletion.
Environment Variables
Each script runs with the environment of the build, which contains the following variables, among others. To list all of them, run:
$ kiwi-ng image info --description=<directory> --print-kiwi-env
Variable |
Value |
|---|---|
|
The name of the image. |
|
The version of the image. |
|
The image type of the |
|
The profiles used for the build, comma-separated. |
|
The |
|
The packages of the |
|
The entries of the |
|
The keytable setting. |
|
The locale setting. |
|
The timezone setting. |
Debugging Scripts
With the option --debug-run-scripts-in-screen, KIWI NG runs the scripts
in a screen session. A script can then open a shell to try out
commands while the build waits:
echo "break"
/bin/bash
The build continues when the shell is closed. List the sessions with:
$ sudo screen -list
Note
The screen session gives extended control over the build and is
therefore only used with the option. In production, scripts must run
without a terminal.
Configuration Tips
- Locale
KIWI NG sets the locale from the
localeelement withsystemd-firstboot, which writes/etc/locale.conf. On distributions which do not use this file, set the locale inconfig.shor with a file in the overlay tree instead.- Machine ID
/etc/machine-idmust be unique for each deployment of an image. For images with a dracut initrd, KIWI NG clears the file if it exists and does not containuninitialized. On first boot, systemd then generates a new ID and runs the services withConditionFirstBoot=yes, see machine-id(5).config.shcan create, remove or set the file touninitializedto control this.If
/etc/machine-iddoes not exist, systemd can only create it if/etcis writable at boot. Addrwto the kernel command line in this case, as dracut mounts the root filesystem read-only by default./var/lib/dbus/machine-idmust contain the same ID. On older distributions, such as SLE 12, where it is a separate file, replace it with a link inconfig.sh:rm -f /var/lib/dbus/machine-id ln -s /etc/machine-id /var/lib/dbus/machine-id