INITRAMFS-TOOLS(7) | Linux Programmer's Manual | INITRAMFS-TOOLS(7) |
initramfs-tools - an introduction to writing scripts for mkinitramfs
initramfs-tools has one main script and two different sets of subscripts which will be used during different phases of execution. Each of these will be discussed separately below with the help of an imaginary tool which performs a frobnication of a lvm partition prior to mounting the root partition.
The root filesystem used by the kernel is specified by the boot loader as always. The traditional root=/dev/sda1 style device specification is allowed. If a label is used, as in root=LABEL=rootPart the initrd will search all available devices for a filesystem with the appropriate label, and mount that device as the root filesystem. root=UUID=uuidnumber will mount the partition with that UUID as the root filesystem.
Valid boot and hook scripts names consist solely of alphabetics, numerics, dashes and underscores. Other scripts are discarded.
These are used to override the user configuration where necessary, for example to force use of busybox instead of klibc utilities.
These are used when an initramfs image is created and not included in the image itself. They can however cause files to be included in the image. Hook scripts are executed under errexit. Thus a hook script can abort the mkinitramfs build on possible errors (exitcode != 0).
These are included in the initramfs image and normally executed during kernel boot in the early user-space before the root partition has been mounted.
Configuration hook scripts can be found in /usr/share/initramfs-tools/conf-hooks.d. They are sourced by mkinitramfs after the configuration files in /etc and before running any hook scripts. They can override any of the variables documented in initramfs.conf(5), but this should be done only if absolutely necessary. For example, if a package's boot script requires commands not provided by klibc-utils, it should also install a configuration hook that sets BUSYBOX=y.
Hooks can be found in two places: /usr/share/initramfs-tools/hooks and /etc/initramfs-tools/hooks. They are executed during generation of the initramfs-image and are responsible for including all the necessary components in the image itself. No guarantees are made as to the order in which the different scripts are executed unless the prereqs are setup in the script. Please notice that PREREQ is only honored inside a single directory. So first the scripts in /usr/share/initramfs-tools are ordered according to their PREREQ values and executed. Then all scripts in /etc/initramfs-tools are ordered according to their PREREQ values and executed. This mean that currently there is no possibility to have a local script (/etc/initramfs-tools) get executed before one from the package (/usr/share/initramfs-tools).
If a hook script requires configuration beyond the exported variables listed below, it should read a private configuration file that is separate from the /etc/initramfs-tools directory. It must not read initramfs-tools configuration files directly.
In order to support prereqs, each script should begin with the following lines:
#!/bin/sh PREREQ="" prereqs() { echo "$PREREQ" } case $1 in prereqs) prereqs exit 0 ;; esac . /usr/share/initramfs-tools/hook-functions # Begin real processing below this line
For example, if you are writing a new hook script which relies on lvm, the line starting with PREREQ should be changed to PREREQ="lvm" which will ensure that the lvm hook script is run before your custom script.
/usr/share/initramfs-tools/hook-functions contains a number of functions which deal with some common tasks in a hook script:
Example: manual_add_modules isofs
Example: add_modules_from_file /tmp/modlist
Example: force_load cdrom debug=1
Example: copy_modules_dir kernel/drivers/ata
If you need to copy an executable or shared library to the initramfs module, use a command like this:
mkinitramfs will automatically detect which libraries it depends on and copy them to the initramfs. This means that most executables, unless compiled with klibc, will automatically include glibc in the image which will increase its size by several hundred kilobytes.
If you need to prepend data to the initramfs image, you need to prepare it in a file, and call the prepend_earlyinitramfs function. The file can be disposed of as soon as the function returns.
Example:
TEMP_FILE=$(mktemp ...)
... prepend_earlyinitramfs ${TEMP_FILE} rm -f ${TEMP_FILE}
mkinitramfs sets several variables for the hook scripts environment.
Similarly to hook scripts, boot scripts can be found in two places /usr/share/initramfs-tools/scripts/ and /etc/initramfs-tools/scripts/. There are a number of subdirectories to these two directories which control the boot stage at which the scripts are executed.
Like for hook scripts, there are no guarantees as to the order in which the different scripts in one subdirectory (see "Subdirectories" below) are executed. In order to define a certain order, a similar header as for hook scripts should be used:
#!/bin/sh PREREQ="" prereqs() { echo "$PREREQ" } case $1 in prereqs) prereqs exit 0 ;; esac
Where PREREQ is modified to list other scripts in the same subdirectory if necessary.
A number of functions (mostly dealing with output) are provided to boot scripts in /scripts/functions :
Example: log_success_msg "Frobnication successful"
Example: log_failure_msg "Frobnication component froobz missing"
Example: log_warning_msg "Only partial frobnication possible"
Example:
log_begin_msg "Frobnication begun" # Do something log_end_msg
Example: panic "Frobnication failed"
Both /usr/share/initramfs-tools/scripts and /etc/initramfs-tools/scripts contains the following subdirectories.
An example hook script would look something like this (and would usually be placed in /etc/initramfs-tools/hooks/frobnicate):
#!/bin/sh # Example frobnication hook script PREREQ="lvm" prereqs() { echo "$PREREQ" } case $1 in prereqs) prereqs exit 0 ;; esac . /usr/share/initramfs-tools/hook-functions # Begin real processing below this line if [ ! -x "/sbin/frobnicate" ]; then exit 0 fi force_load frobnicator interval=10 copy_exec /sbin/frobnicate /sbin exit 0
An example boot script would look something like this (and would usually be placed in /etc/initramfs-tools/scripts/local-top/frobnicate):
#!/bin/sh # Example frobnication boot script PREREQ="lvm" prereqs() { echo "$PREREQ" } case $1 in prereqs) prereqs exit 0 ;; esac . /scripts/functions # Begin real processing below this line if [ ! -x "/sbin/frobnicate" ]; then panic "Frobnication executable not found" fi if [ ! -e "/dev/mapper/frobb" ]; then panic "Frobnication device not found" fi log_begin_msg "Starting frobnication" /sbin/frobnicate "/dev/mapper/frobb" || panic "Frobnication failed" log_end_msg exit 0
init sets several variables for the boot scripts environment.
Package maintainer scripts should not run update-initramfs directly. A package that installs hooks for initramfs-tools should include a triggers file containing:
activate-noawait update-initramfs
Kernel packages must call the kernel hooks as documented in the Debian Kernel Handbook.
A package that requires an initramfs to function, but is not a kernel package, should include a triggers file containing:
activate-await update-initramfs
initramfs-tools includes hook scripts that are called by kernel packages on installation and removal, so that an initramfs is automatically created, updated or deleted as necessary. The hook scripts do nothing if the environment variable INITRD is set to No. This will be the case for kernel packages built with make deb-pkg and with CONFIG_BLK_DEV_INITRD not set in the kernel config, or built with make-kpkg and not using the --initrd option.
It is easy to check the generated initramfs for its content. One may need to double-check if it contains the relevant binaries, libs or modules:
lsinitramfs /boot/initrd.img-3.16-3-amd64
The initramfs-tools are written by Maximilian Attems <maks@debian.org>, Jeff Bailey <jbailey@raspberryginger.com> and numerous others.
This manual was written by David Härdeman <david@hardeman.nu>, updated by Maximilian Attems <maks@debian.org>.
initramfs.conf(5), mkinitramfs(8), update-initramfs(8), lsinitramfs(8).
2018/07/18 | initramfs-tools |