source: postlfs/filesystems/initramfs.xml@ 8253f9fc

10.0 10.1 11.0 11.1 11.2 11.3 12.0 12.1 7.10 7.4 7.5 7.6 7.6-blfs 7.6-systemd 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 9.0 9.1 basic bdubbs/svn elogind gnome kde5-13430 kde5-14269 kde5-14686 kea ken/TL2024 ken/inkscape-core-mods ken/tuningfonts krejzi/svn lazarus lxqt nosym perl-modules plabs/newcss plabs/python-mods python3.11 qt5new rahul/power-profiles-daemon renodr/vulkan-addition systemd-11177 systemd-13485 trunk upgradedb xry111/intltool xry111/llvm18 xry111/soup3 xry111/test-20220226 xry111/xf86-video-removal
Last change on this file since 8253f9fc was 8253f9fc, checked in by Bruce Dubbs <bdubbs@…>, 12 years ago

Updates to initramfs scripts

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

  • Property mode set to 100644
File size: 11.7 KB
Line 
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
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
27 significantly longer, if an initramfs is used.</para>
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
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
47 LVM logical volume. They do not support an encrypted root file system
48 or mounting the rootfs over a network card. For a more complete
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"
57#!/bin/bash
58# This file based in part on the mkinitrafms script for the LFS LiveCD
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
94binfiles="sh cat cp dd killall ls lsmod mkdir mknod mount "
95binfiles="$binfiles umount sed sleep ln rm uname"
96
97#Optional files and locations
98for f in mdadm udevd; do
99 if [ -x /sbin/$f ] ; then sbinfiles="$sbinfiles $f"; fi
100done
101
102unsorted=$(mktemp /tmp/unsorted.XXXXXXXXXX)
103
104DATADIR=/usr/share/mkinitramfs
105INITIN=init.in
106
107# Create a temporrary working directory
108WDIR=$(mktemp -d /tmp/initrd-work.XXXXXXXXXX)
109
110# Create base directory structure
111mkdir -p $WDIR/{bin,dev,lib/firmware,run,sbin,sys,proc}
112mkdir -p $WDIR/etc/{modprobe.d,udev/rules.d}
113touch $WDIR/etc/modprobe.d/modprobe.conf
114ln -s lib $WDIR/lib64
115
116# Create necessary device nodes
117mknod -m 640 $WDIR/dev/console c 5 1
118mknod -m 664 $WDIR/dev/null c 1 3
119
120# Install the udev configuration files
121cp /etc/udev/udev.conf $WDIR/etc/udev/udev.conf
122
123for file in $(find /etc/udev/rules.d/ -type f) ; do
124 cp $file $WDIR/etc/udev/rules.d
125done
126
127if [ -f /etc/mdadm.conf ] ; then
128 cp /etc/mdadm.conf $WDIR/etc
129fi
130
131# Install the init file
132install -m0755 $DATADIR/$INITIN $WDIR/init
133
134if [ -n "$KERNEL_VERSION" ] ; then
135 if [ -x /bin/kmod ] ; then
136 binfiles="$binfiles kmod"
137 else
138 binfiles="$binfiles lsmod"
139 sbinfiles="$sbinfiles insmod"
140 fi
141fi
142
143# Install basic binaries
144for f in $binfiles ; do
145 ldd /bin/$f | sed "s/\t//" | cut -d " " -f1 &gt;&gt; $unsorted
146 copy $f bin
147done
148
149# Add lvm if present
150if [ -x /sbin/lvm ] ; then sbinfiles="$sbinfiles lvm"; fi
151
152for f in $sbinfiles ; do
153 ldd /sbin/$f | sed "s/\t//" | cut -d " " -f1 &gt;&gt; $unsorted
154 copy $f sbin
155done
156
157# Add udevd libraries if not in /sbin
158if [ -x /lib/udev/udevd ] ; then
159 ldd /lib/udev/udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
160fi
161
162# Add module symlinks if appropriate
163if [ -n "$KERNEL_VERSION" ] &amp;&amp; [ -x /bin/kmod ] ; then
164 ln -s kmod $WDIR/bin/lsmod
165 ln -s kmod $WDIR/bin/insmod
166fi
167
168# Add lvm symlinks if appropriate
169if [ -x /sbin/lvm ] ; then
170 ln -s lvm $WDIR/sbin/lvchange
171 ln -s lvm $WDIR/sbin/lvrename
172 ln -s lvm $WDIR/sbin/lvextend
173 ln -s lvm $WDIR/sbin/lvcreate
174 ln -s lvm $WDIR/sbin/lvdisplay
175 ln -s lvm $WDIR/sbin/lvscan
176
177 ln -s lvm $WDIR/sbin/pvchange
178 ln -s lvm $WDIR/sbin/pvck
179 ln -s lvm $WDIR/sbin/pvcreate
180 ln -s lvm $WDIR/sbin/pvdisplay
181 ln -s lvm $WDIR/sbin/pvscan
182
183 ln -s lvm $WDIR/sbin/vgcreate
184 ln -s lvm $WDIR/sbin/vgscan
185 ln -s lvm $WDIR/sbin/vgrename
186 ln -s lvm $WDIR/sbin/vgck
187fi
188
189# Install libraries
190sort $unsorted | uniq | while read library ; do
191 if [ "$library" == "linux-vdso.so.1" ] ||
192 [ "$library" == "linux-gate.so.1" ]; then
193 continue
194 fi
195
196 copy $library lib
197done
198
199cp -a /lib/udev $WDIR/lib
200
201# Install the kernel modules if requested
202if [ -n "$KERNEL_VERSION" ]; then
203 find \
204 /lib/modules/$KERNEL_VERSION/kernel/{crypto,fs,lib} \
205 /lib/modules/$KERNEL_VERSION/kernel/drivers/{block,ata,md,firewire} \
206 /lib/modules/$KERNEL_VERSION/kernel/drivers/{scsi,message,pcmcia,virtio} \
207 /lib/modules/$KERNEL_VERSION/kernel/drivers/usb/{host,storage} \
208 -type f 2&gt; /dev/null | cpio --make-directories -p --quiet $WDIR
209
210 cp /lib/modules/$KERNEL_VERSION/modules.{builtin,order} \
211 $WDIR/lib/modules/$KERNEL_VERSION
212
213 depmod -b $WDIR $KERNEL_VERSION
214fi
215
216( cd $WDIR ; find . | cpio -o -H newc --quiet | gzip -9 ) &gt; $INITRAMFS_FILE
217
218# Remove the temporary directory and file
219rm -rf $WDIR $unsorted
220printf "done.\n"
221
222EOF</userinput></screen>
223
224 <screen role="root"><userinput>mkdir -p /usr/share/mkinitramfs &amp;&amp;
225cat &gt; /usr/share/mkinitramfs/init.in &lt;&lt; "EOF"
226#!/bin/sh
227
228PATH=/bin:/usr/bin:/sbin:/usr/sbin
229export PATH
230
231problem()
232{
233 printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
234 sh
235}
236
237no_device()
238{
239 printf "The device %s, which is supposed to contain the\n" $1
240 printf "root file system, does not exist.\n"
241 printf "Please fix this problem and exit this shell.\n\n"
242}
243
244no_mount()
245{
246 printf "Could not mount device %s\n" $1
247 printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
248 printf "Maybe the device is formatted with an unsupported file system?\n\n"
249 printf "Or maybe filesystem type autodetection went wrong, in which case\n"
250 printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
251 printf "Available partitions:\n"
252}
253
254do_mount_root()
255{
256 mkdir /.root
257 [ -n "$rootflags" ] &amp;&amp; rootflags="$rootflags,"
258 rootflags="$rootflags$ro"
259
260 case "$root" in
261 /dev/* ) device=$root ;;
262 UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;;
263 LABEL=*) eval $root; device="/dev/disk/by-uuid/$LABEL" ;;
264 "" ) echo "No root device specified." ; problem ;;
265 esac
266
267 while [ ! -b "$device" ] ; do
268 no_device $device
269 problem
270 done
271
272 if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" /.root ; then
273 no_mount $device
274 cat /proc/partitions
275 while true ; do sleep 10000 ; done
276 else
277 echo "Successfully mounted device $root"
278 fi
279}
280
281init=/sbin/init
282root=
283rootdelay=
284rootfstype=auto
285ro="ro"
286rootflags=
287device=
288
289mount -n -t devtmpfs devtmpfs /dev
290mknod -m 640 /dev/console c 5 1
291mknod -m 664 /dev/null c 1 3
292mount -n -t proc proc /proc
293mount -n -t sysfs sysfs /sys
294mount -n -t tmpfs tmpfs /run
295
296read -r cmdline &lt; /proc/cmdline
297
298for param in $cmdline ; do
299 case $param in
300 init=* ) init=${param#init=} ;;
301 root=* ) root=${param#root=} ;;
302 rootdelay=* ) rootdelay=${param#rootdelay=} ;;
303 rootfstype=*) rootfstype=${param#rootfstype=} ;;
304 rootflags=* ) rootflags=${param#rootflags=} ;;
305 ro ) ro="ro" ;;
306 rw ) ro="rw" ;;
307 esac
308done
309
310# udevd location depends on version
311if [ -x /sbin/udevd ]; then
312 UDEV_PATH=/sbin
313else
314 UDEV_PATH=/lib/udev
315fi
316
317${UDEV_PATH}/udevd --daemon --resolve-names=never
318udevadm trigger
319udevadm settle
320
321if [ -f /etc/mdadm.conf ] ; then mdadm -As ; fi
322if [ -x /sbin/vgchange ] ; then /sbin/vgchange -a y &gt; /dev/null ; fi
323if [ -n "$rootdelay" ] ; then sleep "$rootdelay" ; fi
324
325do_mount_root
326
327killall -w ${UDEV_PATH}/udevd
328
329exec switch_root /.root "$init" "$@"
330
331EOF</userinput></screen>
332
333 </sect2>
334
335 <sect2 id="initramfs-install">
336 <title>Using an initramfs</title>
337
338 <bridgehead renderas="sect3">Required Runtime Dependency</bridgehead>
339
340 <para role="required">
341 <xref linkend="cpio"/>
342 </para>
343
344 <para condition="html" role="usernotes">User Notes:
345 <ulink url="&blfs-wiki;/initramfs"/>
346 </para>
347
348
349 <para>To build an initramfs, run the following as the <systemitem
350 class="username">root</systemitem> user:</para>
351
352 <screen><userinput>mkinitramfs [KERNEL VERSION]</userinput></screen>
353
354 <para>The optional argument is the directory where the appropriate kernel
355 modules are located. This must be a subdirectory of <filename
356 class='directory'> /lib/modules</filename>. If no modules are specified,
357 then the initramfs is named <emphasis>initrd.img-no-kmods</emphasis>. If a
358 kernel version is specified, the initrd is named
359 <emphasis>initrd.img-$KERNEL_VERSION</emphasis> and is only appropriate for
360 the specific kernel specified. The output file will be placed in the
361 current directory.</para>
362
363 <para>After generating the initrd, copy it to the <filename
364 class='directory'>/boot</filename> directory.</para>
365
366 <para>Now edit <filename>/boot/grub/grub.cfg</filename> and add a new
367 menuentry. Below are several examples.</para>
368
369 <screen><userinput># Generic initramfs and root fs identified by UUID
370menuentry "LFS Dev (LFS-7.0-Feb14) initrd, Linux 3.0.4"
371{
372 linux /vmlinuz-3.0.4-lfs-20120214 root=UUID=54b934a9-302d-415e-ac11-4988408eb0a8 ro
373 initrd /initrd.img-no-kmods
374}</userinput></screen>
375
376 <screen><userinput># Generic initramfs and root fs on LVM partition
377menuentry "LFS Dev (LFS-7.0-Feb18) initrd lvm, Linux 3.0.4"
378{
379 linux /vmlinuz-3.0.4-lfs-20120218 root=/dev/mapper/myroot ro
380 initrd /initrd.img-no-kmods
381}</userinput></screen>
382
383 <screen><userinput># Specific initramfs and root fs identified by LABEL
384menuentry "LFS Dev (LFS-7.1-Feb20) initrd label, Linux 3.2.6"
385{
386 linux /vmlinuz-3.2.6-lfs71-120220 root=LABEL=lfs71 ro
387 initrd /initrd.img-3.2.6-lfs71-120220
388}</userinput></screen>
389
390 <para>Finally, reboot the system and select the desired system.</para>
391
392 </sect2>
393
394</sect1>
Note: See TracBrowser for help on using the repository browser.