initramfs-tools.7 16.6 KB
Newer Older
1
.TH INITRAMFS-TOOLS 7  "2018/07/18" "initramfs\-tools" "Linux Programmer's Manual"
2 3 4 5 6

.SH NAME
initramfs-tools \- an introduction to writing scripts for mkinitramfs

.SH DESCRIPTION
7
initramfs-tools has one main script and two different sets of subscripts which
8 9 10 11
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.

12 13 14 15 16 17 18
.SH Kernel Command Line
The root filesystem used by the kernel is specified by the boot loader as
always. The traditional \fBroot=/dev/sda1\fR style device specification is
allowed. If a label is used, as in \fBroot=LABEL=rootPart\fR the initrd will
search all available devices for a filesystem with the appropriate label, and
mount that device as the root filesystem.  \fBroot=UUID=uuidnumber\fR will
mount the partition with that UUID as the root filesystem.
maximilian attems's avatar
maximilian attems committed
19

20
.SS Standard
21

22
.TP
23
\fB\fI init= "<path to real init>"
24 25
the binary to hand over execution to on the root fs after the initramfs scripts are done.

26 27 28 29 30 31 32 33 34 35 36 37
.TP
\fB\fI initramfs.clear
clear screen at the beginning

.TP
\fB\fI initramfs.runsize
The size of the \fI/run\fP tmpfs mount point in bytes (suffixes are supported)
or as percentage of your physical RAM. This parameter is used as the value of
the size mount option to tmpfs. See
\fBhttps://www.kernel.org/doc/Documentation/filesystems/tmpfs.txt\fR for
details. The default is 10%.

38
.TP
39
\fB\fI root= "<path to blockdevice>"
40
the device node to mount as the root file system.
41
The recommended usage is to specify the UUID as followed "root=UUID=xxx".
42 43 44 45

.TP
\fB\fI rootfstype
set the root file system type.
46 47

.TP
48
\fB\fI rootdelay
49
set delay in seconds. Determines how long mountroot waits for root to appear.
50
The default is 180 seconds.
51 52

.TP
53
\fB\fI rootflags
54 55
set the file system mount option string.

56
.TP
57
\fB\fI nfsroot
58
can be either "auto" to try to get the relevant information from DHCP or a
59
string of the form NFSSERVER:NFSPATH or NFSSERVER:NFSPATH:NFSOPTS.
60 61
Use root=/dev/nfs for NFS to kick to in. NFSOPTS can be looked up in
\fInfs(5)\fP.
62 63

.TP
64
\fB\fI ip
65
tells how to configure the ip address. Allows one to specify an different
66
NFS server than the DHCP server. See Documentation/filesystems/nfsroot.txt
67
in any recent Linux source for details. Optional parameter for NFS root.
68

69 70
.TP
\fB\fI BOOTIF
71 72
is a mac address in pxelinux format with leading "01-" and "-" as separations.
pxelinux passes mac address of network card used to PXE boot on with this
73 74
bootarg.

75
.TP
76
\fB\fI boot
maximilian attems's avatar
maximilian attems committed
77
either local or NFS (affects which initramfs scripts are run, see the "Subdirectories" section under boot scripts).
78 79

.TP
80
\fB\fI resume
81 82 83
The resume hook tries to autodetect the resume partition and uses the first
swap partition as valid guess. It is possible to set the RESUME variable in
/etc/initramfs-tools/conf.d/resume.
84
The boot variable noresume overrides it.
85

86
.TP
87
\fB\fI resume_offset
88 89 90
Specify the offset from the partition given by "resume=" at which the swap
header of the swap file is located.

91
.TP
92
\fB\fI quiet
93
reduces the amount of text output to the console during boot.
94 95

.TP
96
\fB\fI ro
97
mounts the rootfs read-only.
98 99

.TP
100
\fB\fI rw
101 102
mounts the rootfs read-write.

103
.TP
104
\fB\fI blacklist
105 106 107
disables load of specific modules.
Use blacklist=module1,module2,module3 bootparameter.

108
.SS Debug
109
.TP
110
\fB\fI panic
111
sets an timeout on panic.
112
panic=<sec> is a documented security feature: it disables the debug shell.
113 114

.TP
115
\fB\fI debug
116
generates lots of output. It writes a log to /run/initramfs/initramfs.debug.
117 118
Instead when invoked with an arbitrary argument output is written to console.
Use for example "debug=vc".
119 120

.TP
121
\fB\fI break
122
spawns a shell in the initramfs image at the chosen phase
123 124
(top, modules, premount, mount, mountroot, bottom, init)
before actually executing the corresponding scripts
125 126
(see the "Boot scripts" section) or action.  Multiple
phases may be specified, delimited by commas.
127
The default, if no phase is specified, is "premount".
128 129
Beware that if both "panic" and "break" are present,
initramfs will not spawn any shells but reboot instead.
130

131 132 133 134
.TP
\fB\fI netconsole
loads netconsole linux modules with the chosen args.

135
.TP
136
\fB\fI all_generic_ide
137 138
loads generic IDE/ATA chipset support on boot.

139

140
.SH SCRIPTS
141

142 143 144
Valid boot and hook scripts names consist solely of alphabetics, numerics,
dashes and underscores. Other scripts are discarded.

145 146 147 148
.SS Configuration hook scripts
These are used to override the user configuration where necessary, for
example to force use of busybox instead of klibc utilities.

149 150 151 152 153 154 155 156 157 158 159 160
.SS Hook scripts
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).

.SS Boot scripts
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.


161 162 163 164 165 166 167 168 169 170 171 172
.SH CONFIGURATION HOOK SCRIPTS

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 \fIinitramfs.conf\fR(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 \fBBUSYBOX=y\fR.


173 174
.SH HOOK SCRIPTS

175
Hooks can be found in two places: /usr/share/initramfs-tools/hooks and
176
/etc/initramfs-tools/hooks. They are executed during generation of the
177 178
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
179
different scripts are executed unless the prereqs are setup in the script.
180 181 182 183 184 185
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 \fBtheir\fR 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).
186

187 188 189 190 191
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 \fImust not\fR
read initramfs-tools configuration files directly.

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
.SS Header
In order to support prereqs, each script should begin with the following lines:

.RS
.nf
#!/bin/sh
PREREQ=""
prereqs()
{
	echo "$PREREQ"
}

case $1 in
prereqs)
	prereqs
	exit 0
	;;
esac

211
\fR. /usr/share/initramfs-tools/hook-functions
212 213 214 215 216 217 218 219 220 221 222 223
# Begin real processing below this line
.fi
.RE

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.

.SS Help functions
/usr/share/initramfs-tools/hook-functions contains a number of functions which
deal with some common tasks in a hook script:
.TP
224
\fB\fI
225 226 227 228 229
manual_add_modules
adds a module (and any modules which it depends on) to the initramfs image.
.RS
.PP
.B Example:
maximilian attems's avatar
maximilian attems committed
230
manual_add_modules isofs
231 232 233
.RE

.TP
234
\fB\fI
235 236 237 238 239 240 241 242 243 244 245 246
add_modules_from_file
reads a file containing a list of modules (one per line) to be added to the
initramfs image. The file can contain comments (lines starting with #) and
arguments to the modules by writing the arguments on the same line as the name
of the module.
.RS
.PP
.B Example:
add_modules_from_file /tmp/modlist
.RE

.TP
247
\fB\fI
248 249 250 251 252 253 254 255 256 257 258
force_load
adds a module (and its dependencies) to the initramfs image and also
unconditionally loads the module during boot. Also supports passing arguments
to the module by listing them after the module name.
.RS
.PP
.B Example:
force_load cdrom debug=1
.RE

.TP
259
\fB\fI
260 261 262 263 264 265
copy_modules_dir
copies an entire module directory from /lib/modules/KERNELVERSION/ into the
initramfs image.
.RS
.PP
.B Example:
266
copy_modules_dir kernel/drivers/ata
267 268 269 270 271 272 273
.RE

.SS Including binaries
If you need to copy binaries to the initramfs module, a command like this
should be used:
.PP
.RS
274
copy_exec /sbin/mdadm /sbin
275 276 277 278 279 280 281
.RE

mkinitramfs will automatically detect which libraries the executable 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.

282 283 284 285 286 287 288 289 290 291 292 293 294 295
.SS Including a system firmware preimage (early initramfs)
If you need to prepend data to the initramfs image, you need to prepare it
in a file, and call the \fB\fIprepend_earlyinitramfs\fR function.  The file
can be disposed of as soon as the function returns.

.B Example:
.nf
TEMP_FILE=$(mktemp ...)
  ...
prepend_earlyinitramfs ${TEMP_FILE}
rm -f ${TEMP_FILE}

.RE

296 297 298 299 300
.SS Exported variables
mkinitramfs sets several variables for the hook scripts environment.

.TP
\fB\fI MODULESDIR
301
corresponds to the linux modules dir.
302 303
.TP
\fB\fI version
304
is the $(uname \-r) linux version against mkinitramfs is run.
305 306 307 308 309 310 311 312 313 314 315 316 317
.TP
\fB\fI CONFDIR
is the path of the used initramfs-tools configurations.
.TP
\fB\fI DESTDIR
is the root path of the newly build initramfs.
.TP
\fB\fI DPKG_ARCH
allows arch specific hook additions.
.TP
\fB\fI verbose
corresponds to the verbosity of the update-initramfs run.
.TP
318 319
\fB\fI BUSYBOX, KEYMAP, MODULES
are as described in \fIinitramfs.conf\fR(5).
320
.TP
321 322 323
\fB\fI BUSYBOXDIR
is the directory where busybox utilities should be installed from, or
empty if busybox is not being used.
324

325 326 327 328

.SH BOOT SCRIPTS

Similarly to hook scripts, boot scripts can be found in two places
329 330 331
/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.
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

.SS Header
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:

.RS
.nf
#!/bin/sh
PREREQ=""
prereqs()
{
	echo "$PREREQ"
}

case $1 in
prereqs)
	prereqs
	exit 0
	;;
esac
.fi
.RE

Where PREREQ is modified to list other scripts in the same subdirectory if necessary.

.SS Help functions
360 361 362
A number of functions (mostly dealing with output) are provided to boot scripts in
.I /scripts/functions
:
363 364

.TP
365
\fB\fI
366 367 368 369 370 371 372 373 374
log_success_msg
Logs a success message
.RS
.PP
.B Example:
log_success_msg "Frobnication successful"
.RE

.TP
375
\fB\fI
376 377 378 379 380 381 382 383 384
log_failure_msg
Logs a failure message
.RS
.PP
.B Example:
log_failure_msg "Frobnication component froobz missing"
.RE

.TP
385
\fB\fI
386 387 388 389 390 391 392 393 394
log_warning_msg
Logs a warning message
.RS
.PP
.B Example:
log_warning_msg "Only partial frobnication possible"
.RE

.TP
395
\fB\fI
396 397 398 399
log_begin_msg
Logs a message that some processing step has begun

.TP
400
\fB\fI
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
log_end_msg
Logs a message that some processing step is finished
.RS
.PP
.B Example:
.PP
.RS
.nf
log_begin_msg "Frobnication begun"
# Do something
log_end_msg
.fi
.RE
.RE

.TP
417
\fB\fI
418 419 420 421 422 423 424 425 426 427
panic
Logs an error message and executes a shell in the initramfs image to allow the
user to investigate the situation.
.RS
.PP
.B Example:
panic "Frobnication failed"
.RE

.SS Subdirectories
428 429
Both /usr/share/initramfs-tools/scripts and /etc/initramfs-tools/scripts
contains the following subdirectories.
430 431

.TP
432
\fB\fI
433 434
init-top
the scripts in this directory are the first scripts to be executed after sysfs
435 436 437
and procfs have been mounted.
It also runs the udev hook for populating the /dev tree (udev will keep
running until init-bottom).
438 439

.TP
440
\fB\fI
441
init-premount
442
happens after modules specified by hooks and /etc/initramfs-tools/modules
443
have been loaded.
444 445

.TP
446
\fB\fI
447 448
local-top OR nfs-top
After these scripts have been executed, the root device node is expected to be
maximilian attems's avatar
maximilian attems committed
449
present (local) or the network interface is expected to be usable (NFS).
450

451 452 453
.TP
\fB\fI
local-block
454 455 456 457 458
These scripts are called with the name of a local block device.  After
these scripts have been executed, that device node should be present.
If the local-top or local-block scripts fail to create the wanted
device node, the local-block scripts will be called periodically to
try again.
459

460
.TP
461
\fB\fI
462 463
local-premount OR nfs-premount
are run after the sanity of the root device has been verified (local) or the
maximilian attems's avatar
maximilian attems committed
464
network interface has been brought up (NFS), but before the actual root fs has
465 466 467
been mounted.

.TP
468
\fB\fI
469
local-bottom OR nfs-bottom
maximilian attems's avatar
maximilian attems committed
470
are run after the rootfs has been mounted (local) or the NFS root share has
471
been mounted.
472 473

.TP
474
\fB\fI
475 476 477
init-bottom
are the last scripts to be executed before procfs and sysfs are moved to the
real rootfs and execution is turned over to the init binary which should now be
478
found in the mounted rootfs. udev is stopped.
479

maximilian attems's avatar
maximilian attems committed
480 481
.SS Boot parameters
.TP
482
\fB\fI
maximilian attems's avatar
maximilian attems committed
483 484 485 486
/conf/param.conf
allows boot scripts to change exported variables that are listed on top of init. Write the new values to it. It will be sourced after an boot script run if it exists.


487 488 489 490
.SH EXAMPLES

.SS Hook script
An example hook script would look something like this (and would usually be
491
placed in /etc/initramfs-tools/hooks/frobnicate):
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

.RS
.nf
#!/bin/sh
# Example frobnication hook script

PREREQ="lvm"
prereqs()
{
	echo "$PREREQ"
}

case $1 in
prereqs)
	prereqs
	exit 0
	;;
esac

511
\fR. /usr/share/initramfs-tools/hook-functions
512 513
# Begin real processing below this line

514
if [ ! \-x "/sbin/frobnicate" ]; then
515 516 517 518 519 520 521 522 523 524
	exit 0
fi

force_load frobnicator interval=10
cp /sbin/frobnicate "${DESTDIR}/sbin"
exit 0
.fi
.RE

.SS Boot script
525
An example boot script would look something like this (and would usually be placed in /etc/initramfs-tools/scripts/local-top/frobnicate):
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

.RS
.nf
#!/bin/sh
# Example frobnication boot script

PREREQ="lvm"
prereqs()
{
	echo "$PREREQ"
}

case $1 in
prereqs)
	prereqs
	exit 0
	;;
esac

545
\fR. /scripts/functions
546
# Begin real processing below this line
547
if [ ! \-x "/sbin/frobnicate" ]; then
548 549 550
	panic "Frobnication executable not found"
fi

551
if [ ! \-e "/dev/mapper/frobb" ]; then
552 553 554
	panic "Frobnication device not found"
fi

555
log_begin_msg "Starting frobnication"
556 557 558 559 560 561 562
/sbin/frobnicate "/dev/mapper/frobb" || panic "Frobnication failed"
log_end_msg

exit 0
.fi
.RE

563 564 565 566 567
.SS Exported variables
init sets several variables for the boot scripts environment.

.TP
\fB\fI ROOT
568
corresponds to the root boot option.
569 570 571
Advanced boot scripts like cryptsetup or live-initramfs need to play tricks.
Otherwise keep it alone.
.TP
572
\fB\fI ROOTDELAY, ROOTFLAGS, ROOTFSTYPE, IP
573
corresponds to the rootdelay, rootflags, rootfstype or ip boot option.
574 575
Use of ROOTDELAY is deprecated; you should implement a \fIlocal-block\fR
boot script rather than delaying or polling.
576 577 578 579 580 581 582 583 584 585
.TP
\fB\fI DPKG_ARCH
allows arch specific boot actions.
.TP
\fB\fI blacklist, panic, quiet, resume, noresume, resume_offset
set according relevant boot option.
.TP
\fB\fI break
Useful for manual intervention during setup and coding an boot script.
.TP
586 587 588 589
\fB\fI REASON
Argument passed to the \fIpanic\fP helper function.  Use to find out why
you landed in the initramfs shell.
.TP
590 591 592 593 594
\fB\fI init
passes the path to init(8) usually /sbin/init.
.TP
\fB\fI readonly
is the default for mounting the root corresponds to the ro bootarg.
595
Overridden by rw bootarg.
596 597
.TP
\fB\fI rootmnt
598
is the path where root gets mounted usually /root.
599 600 601 602 603
.TP
\fB\fI debug
indicates that a debug log is captured for further investigation.


604 605 606 607 608 609 610 611 612 613 614
.SH KERNEL HOOKS
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 \fBINITRD\fR is
set to \fBNo\fR.  This will be the case for kernel packages
built with \fBmake deb-pkg\fR and with \fBCONFIG_BLK_DEV_INITRD\fR
not set in the kernel config, or built with \fBmake-kpkg\fR and not
using the \fB--initrd\fR option.


615
.SH DEBUG
616
It is easy to check the generated initramfs for its content. One may need
617 618 619
to double-check if it contains the relevant binaries, libs or modules:
.RS
.nf
620
lsinitramfs /boot/initrd.img\-3.16\-3\-amd64
621 622 623 624
.fi
.RE


625 626 627 628
.SH FILES
.TP
.I /run/initramfs/fsck.log
Log of fsck commands run within the initramfs, with their output.
629 630 631 632 633 634
.TP
.I /run/initramfs/fsck-root
Exists only if fsck ran successfully for the root filesystem.
.TP
.I /run/initramfs/fsck-usr
Exists only if fsck ran successfully for the \fI/usr\fR filesystem.
635 636


637
.SH AUTHOR
638
The initramfs-tools are written by Maximilian Attems <maks@debian.org>,
639
Jeff Bailey <jbailey@raspberryginger.com> and numerous others.
640
.PP
David Härdeman's avatar
David Härdeman committed
641
This manual was written by David  H\[:a]rdeman <david@hardeman.nu>,
642
updated by Maximilian Attems <maks@debian.org>.
643 644 645

.SH SEE ALSO
.BR
646 647
.IR initramfs.conf (5),
.IR mkinitramfs (8),
648 649
.IR update-initramfs (8),
.IR lsinitramfs (8).