source: postlfs/filesystems/initramfs.xml@ 50f410f

10.0 10.1 11.0 11.1 11.2 11.3 12.0 12.1 7.10 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 50f410f was 50f410f, checked in by Pierre Labastie <pieere@…>, 10 years ago

Update initramfs for new LVM versions

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

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