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