source: postlfs/filesystems/initramfs.xml@ eb9d3f2

elogind
Last change on this file since eb9d3f2 was eb9d3f2, checked in by Pierre Labastie <pieere@…>, 5 years ago

Add a few missing <literal>

git-svn-id: svn://svn.linuxfromscratch.org/BLFS/branches/BOOK-elogind@21724 af4574ff-66df-0310-9fd7-8a98e5e911e0

  • Property mode set to 100644
File size: 12.7 KB
RevLine 
[a32a241]1<?xml version="1.0" encoding="ISO-8859-1"?>
[5632009]2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
[a42ba00]3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
[5632009]4 <!ENTITY % general-entities SYSTEM "../../general.ent">
5 %general-entities;
6]>
7
8<sect1 id="initramfs">
9 <?dbhtml filename="initramfs.html"?>
10
11 <sect1info>
12 <othername>$LastChangedBy$</othername>
13 <date>$Date$</date>
14 </sect1info>
15
16 <title>About initramfs</title>
17
18 <para>The only purpose of an initramfs is to mount the root filesystem. The
19 initramfs is a complete set of directories that you would find on a normal
20 root filesystem. It is bundled into a single cpio archive and compressed
21 with one of several compression algorithms.</para>
22
23 <para>At boot time, the boot loader loads the kernel and the initramfs image
24 into memory and starts the kernel. The kernel checks for the presence of the
25 initramfs and, if found, mounts it as / and runs /init. The init program is
26 typically a shell script. Note that the boot process takes longer, possibly
[64cb61a]27 significantly longer, if an initramfs is used.</para>
[5632009]28
29 <para>For most distributions, kernel modules are the biggest reason to have an
30 initramfs. In a general distribution, there are many unknowns such as file
31 system types and disk layouts. In a way, this is the opposite of LFS where
32 the system capabilities and layout are known and a custom kernel is normally
33 built. In this situation, an initramfs is rarely needed.</para>
34
35 <para>There are only four primary reasons to have an initramfs in the LFS
36 environment: loading the rootfs from a network, loading it from an LVM
37 logical volume, having an encrypted rootfs where a password is required, or
38 for the convenience of specifying the rootfs as a LABEL or UUID. Anything
39 else usually means that the kernel was not configured properly.</para>
40
41 <sect2 id="initramfs-build">
42 <title>Building an initramfs</title>
43
[0d7900a]44 <para>If you do decide to build an initramfs, the following scripts
45 will provide a basis to do it. The scripts will allow specifying a
46 rootfs via partition UUID or partition LABEL or a rootfs on an
[5632009]47 LVM logical volume. They do not support an encrypted root file system
[0d7900a]48 or mounting the rootfs over a network card. For a more complete
[5632009]49 capability see <ulink url="http://www.linuxfromscratch.org/hints/read.html">
50 the LFS Hints</ulink> or <ulink url="http://fedoraproject.org/wiki/Dracut">
51 dracut</ulink>.</para>
52
53 <para>To install these scripts, run the following commands as the
54 <systemitem class="username">root</systemitem> user:</para>
55
56 <screen role="root"><userinput>cat &gt; /sbin/mkinitramfs &lt;&lt; "EOF"
[eb9d3f2]57<literal>#!/bin/bash
[fe58aa0]58# This file based in part on the mkinitramfs script for the LFS LiveCD
[5632009]59# written by Alexander E. Patrakov and Jeremy Huntwork.
60
61copy()
62{
63 local file
64
65 if [ "$2" == "lib" ]; then
66 file=$(PATH=/lib:/usr/lib type -p $1)
67 else
68 file=$(type -p $1)
69 fi
70
71 if [ -n $file ] ; then
72 cp $file $WDIR/$2
73 else
74 echo "Missing required file: $1 for directory $2"
75 rm -rf $WDIR
76 exit 1
77 fi
78}
79
80if [ -z $1 ] ; then
81 INITRAMFS_FILE=initrd.img-no-kmods
82else
83 KERNEL_VERSION=$1
84 INITRAMFS_FILE=initrd.img-$KERNEL_VERSION
85fi
86
87if [ -n "$KERNEL_VERSION" ] &amp;&amp; [ ! -d "/lib/modules/$1" ] ; then
88 echo "No modules directory named $1"
89 exit 1
90fi
91
92printf "Creating $INITRAMFS_FILE... "
93
[90c6607]94binfiles="sh cat cp dd killall ls mkdir mknod mount "
[5632009]95binfiles="$binfiles umount sed sleep ln rm uname"
[8b6d8b6]96binfiles="$binfiles readlink basename"
[5632009]97
[f350508]98# Systemd installs udevadm in /bin. Other udev implementations have it in /sbin
99if [ -x /bin/udevadm ] ; then binfiles="$binfiles udevadm"; fi
100
101sbinfiles="modprobe blkid switch_root"
[90c6607]102
[8253f9fc]103#Optional files and locations
[f8f7442]104for f in mdadm mdmon udevd udevadm; do
[8253f9fc]105 if [ -x /sbin/$f ] ; then sbinfiles="$sbinfiles $f"; fi
106done
[5632009]107
108unsorted=$(mktemp /tmp/unsorted.XXXXXXXXXX)
109
110DATADIR=/usr/share/mkinitramfs
111INITIN=init.in
112
[f8f7442]113# Create a temporary working directory
[5632009]114WDIR=$(mktemp -d /tmp/initrd-work.XXXXXXXXXX)
115
116# Create base directory structure
[8b6d8b6]117mkdir -p $WDIR/{bin,dev,lib/firmware,run,sbin,sys,proc,usr}
[5632009]118mkdir -p $WDIR/etc/{modprobe.d,udev/rules.d}
119touch $WDIR/etc/modprobe.d/modprobe.conf
120ln -s lib $WDIR/lib64
[8b6d8b6]121ln -s ../bin $WDIR/usr/bin
[5632009]122
123# Create necessary device nodes
124mknod -m 640 $WDIR/dev/console c 5 1
125mknod -m 664 $WDIR/dev/null c 1 3
126
127# Install the udev configuration files
[50f410f]128if [ -f /etc/udev/udev.conf ]; then
129 cp /etc/udev/udev.conf $WDIR/etc/udev/udev.conf
130fi
[5632009]131
132for file in $(find /etc/udev/rules.d/ -type f) ; do
133 cp $file $WDIR/etc/udev/rules.d
134done
135
[a32a241]136# Install any firmware present
137cp -a /lib/firmware $WDIR/lib
138
[f350508]139# Copy the RAID configuration file if present
[5632009]140if [ -f /etc/mdadm.conf ] ; then
141 cp /etc/mdadm.conf $WDIR/etc
142fi
143
144# Install the init file
145install -m0755 $DATADIR/$INITIN $WDIR/init
146
[ea8a884]147if [ -n "$KERNEL_VERSION" ] ; then
148 if [ -x /bin/kmod ] ; then
149 binfiles="$binfiles kmod"
150 else
151 binfiles="$binfiles lsmod"
[64cb61a]152 sbinfiles="$sbinfiles insmod"
[ea8a884]153 fi
154fi
155
[5632009]156# Install basic binaries
157for f in $binfiles ; do
[8b6d8b6]158 if [ -e /bin/$f ]; then d="/bin"; else d="/usr/bin"; fi
159 ldd $d/$f | sed "s/\t//" | cut -d " " -f1 &gt;&gt; $unsorted
160 copy $d/$f bin
[5632009]161done
162
163# Add lvm if present
[50f410f]164if [ -x /sbin/lvm ] ; then sbinfiles="$sbinfiles lvm dmsetup"; fi
[5632009]165
166for f in $sbinfiles ; do
167 ldd /sbin/$f | sed "s/\t//" | cut -d " " -f1 &gt;&gt; $unsorted
168 copy $f sbin
169done
170
[8253f9fc]171# Add udevd libraries if not in /sbin
172if [ -x /lib/udev/udevd ] ; then
[f350508]173 ldd /lib/udev/udevd | sed "s/\t//" | cut -d " " -f1 &gt;&gt; $unsorted
174elif [ -x /lib/systemd/systemd-udevd ] ; then
175 ldd /lib/systemd/systemd-udevd | sed "s/\t//" | cut -d " " -f1 &gt;&gt; $unsorted
[8253f9fc]176fi
177
[ea8a884]178# Add module symlinks if appropriate
179if [ -n "$KERNEL_VERSION" ] &amp;&amp; [ -x /bin/kmod ] ; then
180 ln -s kmod $WDIR/bin/lsmod
181 ln -s kmod $WDIR/bin/insmod
182fi
183
[5632009]184# Add lvm symlinks if appropriate
[50f410f]185# Also copy the lvm.conf file
[5632009]186if [ -x /sbin/lvm ] ; then
187 ln -s lvm $WDIR/sbin/lvchange
[8253f9fc]188 ln -s lvm $WDIR/sbin/lvrename
189 ln -s lvm $WDIR/sbin/lvextend
[5632009]190 ln -s lvm $WDIR/sbin/lvcreate
191 ln -s lvm $WDIR/sbin/lvdisplay
192 ln -s lvm $WDIR/sbin/lvscan
193
194 ln -s lvm $WDIR/sbin/pvchange
195 ln -s lvm $WDIR/sbin/pvck
196 ln -s lvm $WDIR/sbin/pvcreate
197 ln -s lvm $WDIR/sbin/pvdisplay
198 ln -s lvm $WDIR/sbin/pvscan
199
[068c8df]200 ln -s lvm $WDIR/sbin/vgchange
[5632009]201 ln -s lvm $WDIR/sbin/vgcreate
202 ln -s lvm $WDIR/sbin/vgscan
203 ln -s lvm $WDIR/sbin/vgrename
204 ln -s lvm $WDIR/sbin/vgck
[50f410f]205 # Conf file(s)
206 cp -a /etc/lvm $WDIR/etc
[5632009]207fi
208
209# Install libraries
[8253f9fc]210sort $unsorted | uniq | while read library ; do
211 if [ "$library" == "linux-vdso.so.1" ] ||
212 [ "$library" == "linux-gate.so.1" ]; then
[5632009]213 continue
214 fi
215
216 copy $library lib
217done
218
[f350508]219if [ -d /lib/udev ]; then
220 cp -a /lib/udev $WDIR/lib
221fi
222if [ -d /lib/systemd ]; then
223 cp -a /lib/systemd $WDIR/lib
224fi
[5632009]225
226# Install the kernel modules if requested
227if [ -n "$KERNEL_VERSION" ]; then
228 find \
229 /lib/modules/$KERNEL_VERSION/kernel/{crypto,fs,lib} \
230 /lib/modules/$KERNEL_VERSION/kernel/drivers/{block,ata,md,firewire} \
231 /lib/modules/$KERNEL_VERSION/kernel/drivers/{scsi,message,pcmcia,virtio} \
232 /lib/modules/$KERNEL_VERSION/kernel/drivers/usb/{host,storage} \
233 -type f 2&gt; /dev/null | cpio --make-directories -p --quiet $WDIR
234
[8253f9fc]235 cp /lib/modules/$KERNEL_VERSION/modules.{builtin,order} \
236 $WDIR/lib/modules/$KERNEL_VERSION
237
[5632009]238 depmod -b $WDIR $KERNEL_VERSION
239fi
240
241( cd $WDIR ; find . | cpio -o -H newc --quiet | gzip -9 ) &gt; $INITRAMFS_FILE
242
243# Remove the temporary directory and file
244rm -rf $WDIR $unsorted
245printf "done.\n"
[eb9d3f2]246</literal>
[5712a7d]247EOF
248
[e7d893b]249chmod 0755 /sbin/mkinitramfs</userinput></screen>
[0d7900a]250
[5632009]251 <screen role="root"><userinput>mkdir -p /usr/share/mkinitramfs &amp;&amp;
252cat &gt; /usr/share/mkinitramfs/init.in &lt;&lt; "EOF"
[eb9d3f2]253<literal>#!/bin/sh
[5632009]254
255PATH=/bin:/usr/bin:/sbin:/usr/sbin
256export PATH
257
258problem()
259{
260 printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
261 sh
262}
263
264no_device()
265{
266 printf "The device %s, which is supposed to contain the\n" $1
267 printf "root file system, does not exist.\n"
268 printf "Please fix this problem and exit this shell.\n\n"
269}
270
271no_mount()
272{
273 printf "Could not mount device %s\n" $1
274 printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
275 printf "Maybe the device is formatted with an unsupported file system?\n\n"
276 printf "Or maybe filesystem type autodetection went wrong, in which case\n"
277 printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
278 printf "Available partitions:\n"
279}
280
281do_mount_root()
282{
283 mkdir /.root
284 [ -n "$rootflags" ] &amp;&amp; rootflags="$rootflags,"
285 rootflags="$rootflags$ro"
286
287 case "$root" in
288 /dev/* ) device=$root ;;
289 UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;;
[4a20b84]290 LABEL=*) eval $root; device="/dev/disk/by-label/$LABEL" ;;
[5632009]291 "" ) echo "No root device specified." ; problem ;;
292 esac
293
294 while [ ! -b "$device" ] ; do
295 no_device $device
296 problem
297 done
298
299 if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" /.root ; then
300 no_mount $device
301 cat /proc/partitions
302 while true ; do sleep 10000 ; done
303 else
304 echo "Successfully mounted device $root"
305 fi
306}
307
308init=/sbin/init
309root=
310rootdelay=
311rootfstype=auto
312ro="ro"
313rootflags=
314device=
315
316mount -n -t devtmpfs devtmpfs /dev
[a32a241]317mount -n -t proc proc /proc
318mount -n -t sysfs sysfs /sys
319mount -n -t tmpfs tmpfs /run
[5632009]320
321read -r cmdline &lt; /proc/cmdline
322
323for param in $cmdline ; do
324 case $param in
325 init=* ) init=${param#init=} ;;
326 root=* ) root=${param#root=} ;;
327 rootdelay=* ) rootdelay=${param#rootdelay=} ;;
328 rootfstype=*) rootfstype=${param#rootfstype=} ;;
329 rootflags=* ) rootflags=${param#rootflags=} ;;
330 ro ) ro="ro" ;;
331 rw ) ro="rw" ;;
332 esac
333done
334
[8253f9fc]335# udevd location depends on version
336if [ -x /sbin/udevd ]; then
[f350508]337 UDEVD=/sbin/udevd
338elif [ -x /lib/udev/udevd ]; then
339 UDEVD=/lib/udev/udevd
340elif [ -x /lib/systemd/systemd-udevd ]; then
341 UDEVD=/lib/systemd/systemd-udevd
[8253f9fc]342else
[f350508]343 echo "Cannot find udevd nor systemd-udevd"
344 problem
[8253f9fc]345fi
346
[f350508]347${UDEVD} --daemon --resolve-names=never
[5632009]348udevadm trigger
349udevadm settle
350
[50f410f]351if [ -f /etc/mdadm.conf ] ; then mdadm -As ; fi
352if [ -x /sbin/vgchange ] ; then /sbin/vgchange -a y &gt; /dev/null ; fi
353if [ -n "$rootdelay" ] ; then sleep "$rootdelay" ; fi
[5632009]354
355do_mount_root
356
[f350508]357killall -w ${UDEVD##*/}
[5632009]358
359exec switch_root /.root "$init" "$@"
[eb9d3f2]360</literal>
[5632009]361EOF</userinput></screen>
[0d7900a]362
[5632009]363 </sect2>
364
365 <sect2 id="initramfs-install">
366 <title>Using an initramfs</title>
367
[ea8a884]368 <bridgehead renderas="sect3">Required Runtime Dependency</bridgehead>
369
370 <para role="required">
[96e9478]371 <xref role="runtime" linkend="cpio"/>
[ea8a884]372 </para>
373
374 <para condition="html" role="usernotes">User Notes:
375 <ulink url="&blfs-wiki;/initramfs"/>
376 </para>
377
378
[5632009]379 <para>To build an initramfs, run the following as the <systemitem
380 class="username">root</systemitem> user:</para>
381
[e7d893b]382 <screen role="nodump"><userinput>mkinitramfs [KERNEL VERSION]</userinput></screen>
[5632009]383
384 <para>The optional argument is the directory where the appropriate kernel
385 modules are located. This must be a subdirectory of <filename
386 class='directory'> /lib/modules</filename>. If no modules are specified,
387 then the initramfs is named <emphasis>initrd.img-no-kmods</emphasis>. If a
388 kernel version is specified, the initrd is named
389 <emphasis>initrd.img-$KERNEL_VERSION</emphasis> and is only appropriate for
390 the specific kernel specified. The output file will be placed in the
391 current directory.</para>
392
393 <para>After generating the initrd, copy it to the <filename
394 class='directory'>/boot</filename> directory.</para>
395
[0d7900a]396 <para>Now edit <filename>/boot/grub/grub.cfg</filename> and add a new
[5632009]397 menuentry. Below are several examples.</para>
398
[e7d893b]399 <screen role="nodump"><userinput># Generic initramfs and root fs identified by UUID
[0d7900a]400menuentry "LFS Dev (LFS-7.0-Feb14) initrd, Linux 3.0.4"
[5632009]401{
402 linux /vmlinuz-3.0.4-lfs-20120214 root=UUID=54b934a9-302d-415e-ac11-4988408eb0a8 ro
403 initrd /initrd.img-no-kmods
404}</userinput></screen>
405
[e7d893b]406 <screen role="nodump"><userinput># Generic initramfs and root fs on LVM partition
[0d7900a]407menuentry "LFS Dev (LFS-7.0-Feb18) initrd lvm, Linux 3.0.4"
[5632009]408{
409 linux /vmlinuz-3.0.4-lfs-20120218 root=/dev/mapper/myroot ro
410 initrd /initrd.img-no-kmods
411}</userinput></screen>
412
[e7d893b]413 <screen role="nodump"><userinput># Specific initramfs and root fs identified by LABEL
[0d7900a]414menuentry "LFS Dev (LFS-7.1-Feb20) initrd label, Linux 3.2.6"
[5632009]415{
416 linux /vmlinuz-3.2.6-lfs71-120220 root=LABEL=lfs71 ro
417 initrd /initrd.img-3.2.6-lfs71-120220
418}</userinput></screen>
419
420 <para>Finally, reboot the system and select the desired system.</para>
421
422 </sect2>
423
424</sect1>
Note: See TracBrowser for help on using the repository browser.