Typo
[linux_from_scratch_hints.git] / OLD / initrd.txt
blob344c3697d0a7c45d8ccdae2a5371fd5c68177de3
1 TITLE:          initrd for LFS
2 LFS VERSION:    any
3 AUTHOR:         Jim Gifford <giffordj@linkline.com>
5 SYNOPSIS:
6         How to setup initrd for LFS.
8 HINT:
9 $Revision: 1.1 $
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
15 into the kernel.
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
22 ---
23 Assumptions Made in this document
25         I have made the following assumptions in this document.
26         Files have been downloaded.
28 ---
29 Kernel Configuration
31         You will need to make sure the following items are configured
32         in your kernel. With out these, the initrd will not work.
34         Block Devices
35         
36         Select Loopback Device Support this can be a module
37         or built-in.
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.
46 ---
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
56         in busybox itself.
58         You will need to create a symlink to init and call it
59         linuxrc
61         cd /sbin
62         ln -sf init linuxrc
64 ---
65 Needed Static Modules
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.
72         busybox
73         ----
75         Busybox has a Config.h file that needs the following options
76         enabled to enable them remove the //
78         #define BB_INSMOD
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.
83         
84         #define BB_ASH
85         #define BB_CHROOT
86         #define BB_ECHO
87         #define BB_INSMOD
88         #define BB_MKDIR
89         #define BB_MODPROBE
90         #define BB_MOUNT
91         #define BB_PIVOT_ROOT
92         #define BB_UMOUNT
94         To create a static version of bash needed for initrd use
95         the following commands.
96         
97         cd /usr/src
98         tar zxvf /usr/src/busybox-*.tar.gz
99         cd busy*
100         make LDFLAGS=-static
101         cp busybox /bin/busybox
103         Busybox must be in the /bin directory or the links created 
104         during the initrid will fail.
107 mkinitrd
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
112         
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
118 #!/bin/bash
120 # mkinitrd for LFS by Jim Gifford <giffordj@linkline.com>
121 # $Revision: 1.1 $
123 # Variables
124 TEMP="$1"
126 KERNEL_VERSION=""
127 CONFIG_FILE="/etc/modules.conf"
129 FSTAB="/etc/fstab"
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 }'`"
136 NEEDED_USB="usbcore"
138 MODULES="$NEEDED_SCSI $SCSI_MODULES $NEEDED_USB $USB_MODULES"
140 IMAGE_SIZE=3000
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
148 if ! [ -e /initrd ]
149         then
150                 mkdir /initrd
152                 
153 # Check for RAM Disk Device
155 if [ -e /dev/.devfsd ]
156         then
157                 RAM_DEVICE="rd"
158         else
159                 RAM_DEVICE="ram0"
162 # Check for input
164 if [ "$TEMP" == "" ]
165         then
166                 KERNEL_VERSION="`uname -r`"
167         else 
168                 KERNEL_VERSION="$TEMP"
171 INITRD="/boot/initrd-$KERNEL_VERSION.img"
173 if [ "$TEMP" == "-h" ] || [ "$TEMP" == "--h" ] || [ "$TEMP" == "-help" ] || [ "$TEMP" == "--help" ]
174         then
175                 echo "usage: mkinitrd kernel_version"
176                 echo "     : mkinitrd will automatically determin kernel version"
177                 exit 1
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
185         do
186                 if losetup /dev/loop$device_number $IMAGE 2>/dev/null
187                         then
188                                 break
189                 fi
190 done
192 if [ "$device_number" = "8" ]
193         then
194                 rm -rf $MOUNT_POINT $IMAGE
195                 echo "All of your loopback devices are in use!" >&2
196                 exit 1
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"
208                                              exit 1
209                                            }
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
236 # Copying Modules
238 for MODULE in $MODULES
239         do
240                 echo "$MODULE" | {
241                 IFS=':' read module options
242                 module=$module
243                 options=$options
244                 DIR_SEARCH1="`ls -1 /lib/modules/$KERNEL_VERSION/kernel/drivers`"
245                 for DIR_1SEARCH in $DIR_SEARCH1
246                 do
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
250                                 do
251                                         cp /lib/modules/$KERNEL_VERSION/kernel/drivers/$DIR_1SEARCH/$DIR_2SEARCH/$module.o $MOUNT_IMAGE/lib > /dev/null 2>&1
252                                 done
253                 done
254                                    }
255         done
257 for i in console null $RAM_DEVICE tty[1234]
258         do
259                 cp -a /dev/$i $MOUNT_IMAGE/dev
260 done
262 # Creating linuxrc File
264 echo "#!/bin/sh" > $LINUXRC
265 echo "" >> $LINUXRC
267 echo "echo \"Initial RAMDISK Loading Starting...\"" >> $LINUXRC
269 for MODULE in $MODULES
270         do
271                 echo "$MODULE" | {
272                 IFS=':' read module
273                 module=$module
275                 echo "Loading module $module"
276                 echo "insmod /lib/$module.o" >> $LINUXRC
277                 }
278 done
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
305 chmod +x $LINUXRC
307 (cd $MOUNT_IMAGE; tar cf - .) | (cd $MOUNT_POINT; tar xf -)
309 umount $MOUNT_POINT
310 losetup -d $LOOP_DEVICE
312 gzip -9 < $IMAGE > $INITRD
313 rm -rf $MOUNT_IMAGE $MOUNT_POINT $IMAGE
315 lilo -v
318 initrd script
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
325         mountfs.
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
332 #!/bin/bash
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
339 source $rc_functions
341 echo "Clearing Initial RAM Disk..."
342         if [ -e /initrd/dev/.devfsd ]
343                 then
344                 umount /initrd/dev
345         fi
346         umount /initrd
347         /sbin/blockdev --flushbufs /dev/ram0
349 # End $rc_base/init.d/initrd
350                 
352 For Lilo
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
362         label=test
363         initrd=/boot/initrd-2.4.18.img
364         read-only
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.  
371 For Grub
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.
380         title test
381         root (hd0,1)
382         kernel /boot/vmlinuz-2.4.18
383         initrd /boot/initrd-2.4.18.img
386 For Syslinux
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.
395         label test
396         kernel vmlinuz
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