3 AUTHOR: Jim Gifford <giffordj@linkline.com>
6 How to setup initrd for LFS.
11 Introduction to Initial RAMDisk
13 This hint will help you configure an LFS system for Initial RAMDisk.
14 Which will allow you to add modules at start-up instead of compiling them
17 The script that is enclosed works with SCSI and USB modules only. IDE
18 devices are recommened to be built-in the kernel. The script will
19 auto-detect all SCSI and USB modules and add them to the initial ramdisk.
20 It will also detect the root from the fstab file
23 Assumptions Made in this document
25 I have made the following assumptions in this document.
26 Files have been downloaded.
31 You will need to make sure the following items are configured
32 in your kernel. With out these, the initrd will not work.
36 Select Loopback Device Support this can be a module
39 Select RAM Disk Support this needs to be compiled as
40 built-in or the initrd will not show up.
42 Set Default RAM Disk size is 4096 which is the default
44 Select Initial RAM Disk (initrd) support needs be selected.
47 Needed File System Changes
49 You will need to create a directory for initrd to use.
51 The default one that is looked for is /initrd.
53 To Create this directory use mkdir /initrd
55 Another change that needs to be made is due to a bug
58 You will need to create a symlink to init and call it
67 In order for the initrd to work properly during boot up
68 you will need to create to static programs.
70 The first one being bash.
75 Busybox has a Config.h file that needs the following options
76 enabled to enable them remove the //
79 #define BB_FEATURE_SH_STANDALONE_SHELL
81 You can configure the rest as you need, but remember have at
82 least the following enabled to make initrd to work properly.
94 To create a static version of bash needed for initrd use
95 the following commands.
98 tar zxvf /usr/src/busybox-*.tar.gz
101 cp busybox /bin/busybox
103 Busybox must be in the /bin directory or the links created
104 during the initrid will fail.
109 For those who do not want to type out the script. It is
110 available on my CVS server at
111 http://www.jg555.com/cvs/cvsweb.cgi/scripts/mkinitrd-lfs
113 This script will create the initial RAM Disk image file.
114 By default this script creates /boot/initrd.img
116 The default location for this file is /sbin
120 # mkinitrd for LFS by Jim Gifford <giffordj@linkline.com>
127 CONFIG_FILE="/etc/modules.conf"
130 ROOT_DEVICE=$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' $FSTAB)
132 SCSI_MODULES="`grep scsi_hostadapter $CONFIG_FILE | grep -v '^[ ]*#' | awk '{ print $3 }'`"
133 NEEDED_SCSI="scsi_mod sd_mod"
135 USB_MODULES="`grep usb-controller $CONFIG_FILE | grep -v '^[ ]*#' | awk '{ print $3 }'`"
138 MODULES="$NEEDED_SCSI $SCSI_MODULES $NEEDED_USB $USB_MODULES"
141 MOUNT_IMAGE="/tmp/initrd.$$"
142 IMAGE="/tmp/initrd.img-$$"
143 MOUNT_POINT="/tmp/initrd.mnt-$$"
144 LINUXRC="$MOUNT_IMAGE/linuxrc"
146 # Check for initrd Directory
153 # Check for RAM Disk Device
155 if [ -e /dev/.devfsd ]
166 KERNEL_VERSION="`uname -r`"
168 KERNEL_VERSION="$TEMP"
171 INITRD="/boot/initrd-$KERNEL_VERSION.img"
173 if [ "$TEMP" == "-h" ] || [ "$TEMP" == "--h" ] || [ "$TEMP" == "-help" ] || [ "$TEMP" == "--help" ]
175 echo "usage: mkinitrd kernel_version"
176 echo " : mkinitrd will automatically determin kernel version"
180 # Creating LoopBack Device
182 dd if=/dev/zero of=$IMAGE bs=1k count=$IMAGE_SIZE 2> /dev/null
184 for device_number in 0 1 2 3 4 5 6 7 8
186 if losetup /dev/loop$device_number $IMAGE 2>/dev/null
192 if [ "$device_number" = "8" ]
194 rm -rf $MOUNT_POINT $IMAGE
195 echo "All of your loopback devices are in use!" >&2
199 LOOP_DEVICE=/dev/loop$device_number
201 echo y | mke2fs $LOOP_DEVICE $IMAGE_SIZE > /dev/null 2> /dev/null
203 echo "Using loopback device $LOOP_DEVICE"
205 mkdir -p $MOUNT_POINT
206 mount -t ext2 $LOOP_DEVICE $MOUNT_POINT || {
207 echo "Can't get a loopback device"
210 # Creating Directories
212 mkdir -p $MOUNT_IMAGE
213 mkdir -p $MOUNT_IMAGE/lib
214 mkdir -p $MOUNT_IMAGE/bin
215 mkdir -p $MOUNT_IMAGE/etc
216 mkdir -p $MOUNT_IMAGE/dev
217 mkdir -p $MOUNT_IMAGE/proc
218 ln -s /bin $MOUNT_IMAGE/sbin
220 rm -rf $MOUNT_POINT/lost+found
222 # Copying Static Programs
224 cp -a /bin/busybox $MOUNT_IMAGE/bin/busybox
225 ln -s /bin/busybox $MOUNT_IMAGE/bin/echo
226 ln -s /bin/busybox $MOUNT_IMAGE/bin/mount
227 ln -s /bin/busybox $MOUNT_IMAGE/bin/modprobe
228 ln -s /bin/busybox $MOUNT_IMAGE/bin/mkdir
229 ln -s /bin/busybox $MOUNT_IMAGE/bin/sh
230 ln -s /bin/busybox $MOUNT_IMAGE/bin/umount
231 ln -s /bin/busybox $MOUNT_IMAGE/bin/insmod
232 ln -s /bin/busybox $MOUNT_IMAGE/bin/pivot_root
233 cp -a /etc/fstab $MOUNT_IMAGE/etc/fstab
234 cp -a /etc/modules.conf $MOUNT_IMAGE/etc/modules.conf
238 for MODULE in $MODULES
241 IFS=':' read module options
244 DIR_SEARCH1="`ls -1 /lib/modules/$KERNEL_VERSION/kernel/drivers`"
245 for DIR_1SEARCH in $DIR_SEARCH1
247 cp /lib/modules/$KERNEL_VERSION/kernel/drivers/$DIR_1SEARCH/$module.o $MOUNT_IMAGE/lib > /dev/null 2>&1
248 DIR_SEARCH2="`ls -1 /lib/modules/$KERNEL_VERSION/kernel/drivers/$DIR_1SEARCH`"
249 for DIR_2SEARCH in $DIR_SEARCH2
251 cp /lib/modules/$KERNEL_VERSION/kernel/drivers/$DIR_1SEARCH/$DIR_2SEARCH/$module.o $MOUNT_IMAGE/lib > /dev/null 2>&1
257 for i in console null $RAM_DEVICE tty[1234]
259 cp -a /dev/$i $MOUNT_IMAGE/dev
262 # Creating linuxrc File
264 echo "#!/bin/sh" > $LINUXRC
267 echo "echo \"Initial RAMDISK Loading Starting...\"" >> $LINUXRC
269 for MODULE in $MODULES
275 echo "Loading module $module"
276 echo "insmod /lib/$module.o" >> $LINUXRC
280 echo "echo \"Initial RAMDISK Loading Completed...\"" >> $LINUXRC
281 echo "mkdir /new_root" >> $LINUXRC
282 echo "echo \"Mounting proc...\"" >> $LINUXRC
283 echo "mount -n -t proc none /proc" >> $LINUXRC
284 echo "echo 0x0100 > /proc/sys/kernel/real-root-dev" >> $LINUXRC
285 echo "echo \"Mounting real root dev...\"" >> $LINUXRC
286 echo "mount -n -o ro $ROOT_DEVICE /new_root" >> $LINUXRC
287 echo "umount /proc" >> $LINUXRC
288 echo "cd /new_root" >> $LINUXRC
289 echo "echo \"Running pivot_root...\"" >> $LINUXRC
290 echo "pivot_root . initrd" >> $LINUXRC
291 echo "if [ -c initrd/dev/.devfsd ]" >> $LINUXRC
292 echo " then" >> $LINUXRC
293 echo " echo \"Mounting devfs...\"" >> $LINUXRC
294 echo " mount -n -t devfs none dev" >> $LINUXRC
295 echo "fi" >> $LINUXRC
296 echo "if [ \$\$ = 1 ]" >> $LINUXRC
297 echo " then" >> $LINUXRC
298 echo " echo \"Running init...\"" >> $LINUXRC
299 echo " exec chroot . sbin/init dev/console 2>&1" >> $LINUXRC
300 echo " else" >> $LINUXRC
301 echo " echo \"Using bug circumvention for busybox...\"" >> $LINUXRC
302 echo " exec chroot . linuxrc dev/console 2>&1" >> $LINUXRC
303 echo "fi" >> $LINUXRC
307 (cd $MOUNT_IMAGE; tar cf - .) | (cd $MOUNT_POINT; tar xf -)
310 losetup -d $LOOP_DEVICE
312 gzip -9 < $IMAGE > $INITRD
313 rm -rf $MOUNT_IMAGE $MOUNT_POINT $IMAGE
320 The following script needs to placed in /etc/rc.d/init.d.
322 You will then need to link it to rcsysinit.d.
324 It is recommended that this script be run right after
327 To link the script change to the /etc/rc.d/rcsysinit.d
328 directory and issue the following command.
330 ln -sf ../init.d/initrd S41initrd
333 # Begin $rc_base/init.d/initrd
335 # Based on sysklogd script from LFS-3.1 and earlier.
336 # Rewritten by Gerard Beekmans - gerard@linuxfromscratch.org
338 source /etc/sysconfig/rc
341 echo "Clearing Initial RAM Disk..."
342 if [ -e /initrd/dev/.devfsd ]
347 /sbin/blockdev --flushbufs /dev/ram0
349 # End $rc_base/init.d/initrd
354 In order to use the initrd.img file is to add the
355 following entry to you lilo.conf file.
357 initrd=/boot/initrd.img
359 So your lilo.conf should look something like this.
361 image=/boot/vmlinuz-2.4.18
363 initrd=/boot/initrd-2.4.18.img
365 append="root=/dev/ram0 init=/linuxrc rw"
367 If you are just testing. You should make a separate
368 entry in lilo.conf. This will still allow you to boot.
373 In order to use the initrd.img file is to add the
374 following entry to you menu.lst file.
376 initrd /boot/initrd-2.4.18.img
378 So your menu.lst should look something like this.
382 kernel /boot/vmlinuz-2.4.18
383 initrd /boot/initrd-2.4.18.img
388 In order to use the initrd.img file is to add the
389 following to syslinux.cfg file.
391 append root=/dev/ram0 initrd=initrd-2.4.18.img
393 So your syslinux.cfg should look something like this.
397 append root=/dev/ram0 initrd=initrd.img
400 Mail suggestions to giffordj@linkline.com
402 New Version of this document can be viewed from
403 http://www.jg555.com/cvs