1. init
Module providing functions to build an init script
The mkinit() function will generate a /init script.
do_foo() functions write a string performing the foo action into a
stream. This stream should be the init script.
_fun_foo() functions write a string defining the foo function into a
stream. This stream should be the init script.
Helper functions available in the init script that can be used by
cmkinitramfs.data.Data classes:
_fun_die(): Fatal error handler (does not return).
_fun_log(): Logging functions (always successful).
Special helper function available in the init script:
_fun_rescue_shell(), _fun_panic().
Init environment variables:
HOME
PATH
Init global variables:
PRINTK: Initial kernel log level.
INIT: Program to run as init process after the initramfs.
RD_XXX: If set, feature XXX is enabled.
RD_BREAK_XXX: If set, breakpoint XXX is enabled.
The init script should never rely on global variables being set or unset, it should always assign a default value if not set.
- class cmkinitramfs.init.Breakpoint(value)[source]
Bases:
EnumBreakpoint in the boot process
Breakpoints can be enabled by adding rd.break to the kernel command-line (e.g.
./kernel.img foo rd.break=init). Settingrd.break=foo,barwill enable bothfooandbar. Environment variables can also be set to enable them (e.g../kernel.img foo RD_BREAK_EARLY=true).- EARLY = 1
Early break: break before any action, including command-line parsing. Can be set with the
RD_BREAK_EARLYenvironment variable.
- INIT = 2
init: Break after initramfs initialization. Can also be set with theRD_BREAK_INITenvironment variable.
- MODULE = 3
module: Break after loading kernel modules. Can also be set with theRD_BREAK_MODULEenvironment variable. Alias:modules.
- MOUNT = 5
mount: Break after mounting all filesystems. Can also be set with theRD_BREAK_MOUNTenvironment variable. Alias:mounts.
- ROOTFS = 4
rootfs: Break after mounting the root filesystem. Can also be set with theRD_BREAK_ROOTFSenvironment variable.
- cmkinitramfs.init._fun_rescue_shell(out)[source]
Define the rescue_shell function
rescue_shelldrop the user to/bin/sh.Arguments: none.
This function should not be called from a subshell.
This function does not return.
- cmkinitramfs.init._fun_panic(out)[source]
Define the panic function
paniccauses a kernel panic by exiting/init.Arguments: none.
This function should not be called from a subshell.
This function does not return.
- cmkinitramfs.init._fun_die(out)[source]
Define the die function
diewill either start a rescue shell or cause a kernel panic, wetherRD_PANICis set or not.Arguments: error message.
This function should not be called from a subshell.
This function does not return.
- cmkinitramfs.init._fun_log(out)[source]
Define the logging functions
log: log a message.Argument 1: syslog level number, from 0 to 7.
Additionnal arguments: message to log.
Logs printed to stderr:
Level ≤ 4: always
5 ≤ level ≤ 6: if debug enabled or quiet disabled
Level = 7: if debug enabled
Helper functions:
emerg: log a message for a panic condition. The message is prepended by ‘FATAL:’.alert: log a critical error message requiring immediate action. The message is prepended by ‘ERROR:’.crit: log a critical error message. The message is prepended by ‘ERROR:’.err: log an error message. The message is prepended by ‘ERROR:’.warn: log a warning message. The message is prepended by ‘WARNING:’.notice: log a significant/unusual informational message.info: log an informational message.debug: log a debug-level message.
Helper functions will call
logwith the coresponding syslog level.Logging functions always return successfully.
- cmkinitramfs.init.do_header(out, home='/root', path='/bin:/sbin')[source]
Create the /init header
Create the shebang
/bin/shConfigure environment variables
Define global functions (
panic, logging, …)
- cmkinitramfs.init.do_init(out)[source]
Initialize the init environment
Check the current PID is 1
Mount
/proc,/sys,/devSet the kernel log level to 4 (
KERN_ERRand higher priority)
- cmkinitramfs.init.do_cmdline(out)[source]
Parse the kernel command line for known parameters
Note: the command line is parsed up to “–”, arguments after this are passed through to the final init process.
Parsed parameters:
init=<path to init>: Set the program to run as init process after the initramfs.debug: Enable debugging, seerd.debug.quiet: Enable quiet mode, seerd.quiet.rd.break={init|rootfs|mount}: Stops the boot process, defaults torootfs. SeeBreakpoint.rd.debug: Enable debugging mode: output verbose informations. If quiet mode is disabled, enable shell trace (withset -x).rd.panic: On fatal error: cause a kernel panic rather than dropping into a shell.rd.quiet: Enable quiet mode: reduce verbosity.
- cmkinitramfs.init.do_break(out, breakpoint_, scripts=())[source]
Drop into a shell if rd.break is set
- Parameters
breakpoint – Which breakpoint to check
scripts (Iterable[str]) – User commands to run before the breakpoint
breakpoint_ (Breakpoint) –
- Return type
None
- cmkinitramfs.init.do_switch_root(out, newroot, init='/sbin/init')[source]
Cleanup and switch root
Print debugging information
Restore kernel log level (set to boot-time default if not possible)
Kill all processes
Unmount
/dev,/sys,/procSwitch root
- cmkinitramfs.init.mkinit(out, root, mounts=(), keymap=None, modules=None, scripts=None)[source]
Create the init script
- Parameters
root (Data) –
Datato use as rootfskeymap (Optional[str]) – Path of the keymap to load,
Nonemeans no keymapmodules (Optional[Mapping[str, Iterable[str]]]) – Kernel modules to be loaded in the initramfs:
{module: (arg, ...)}.moduleis the module name string, and(arg, ...)`is the iterable with the module parameters.scripts (Optional[Mapping[Breakpoint, Iterable[str]]]) – User commands to run.
{breakpoint: commands}:breakpointis theBreakpointwhere the commands will be run.commandsis the iterable with the commands.
- Return type
None