Wednesday, August 11, 2010

Initrd and initramfs

Initrd Creation

To create an (initially empty) initrd use the following steps:
Note: Change count to your required filesystem size. E.g. with count=8192 you will get a 8MB ramdisk.

host > dd if=/dev/zero of=/dev/ram0 bs=1k count=<count>
host > mke2fs -vm0 /dev/ram0 <count>
host > tune2fs -c 0 /dev/ram0
host > dd if=/dev/ram0 bs=1k count=<count>  | gzip -v9 > ramdisk.gz

Now, we have a (empty) gzipped ramdisk image with (extracted) size of <count>.

Filling

To fill empty ramdisk created above with all files needed for ramdisk, mount the image and fill it. Content would be e.g. BusyBox and/or other applications and/or libraries.

host > mkdir mnt
host > gunzip ramdisk.gz
host > mount -o loop ramdisk mnt/
host > ... copy stuff you want to have in ramdisk to mnt...
host > umount mnt
host > gzip -v9 ramdisk

The resulting ramdisk.gz is now ready for usage. Note its size is smaller than <count> cause of compression.

Note: Don't forget to create/copy some basic /dev/xxx nodes to ramdisk.

Note: If BusyBox or applications in ramdisk are linked dynamically, don't forget to copy dynamic libraries (*.so) to ramdisk (to correct directory) as well.

Kernel options

To make initrd work, you have to configure kernel properly:

#
# General setup
#
...
CONFIG_BLK_DEV_INITRD=y
CONFIG_INITRAMFS_SOURCE=""
...
#
# UBI - Unsorted block images
#
...
CONFIG_BLK_DEV_RAM=y
CONFIG_BLK_DEV_RAM_COUNT=1
CONFIG_BLK_DEV_RAM_SIZE=8192
CONFIG_BLK_DEV_RAM_BLOCKSIZE=1024
...
Note: The ramdisk size e.g. 8192 above has to be configured for your individual setup.

Installation

Now, you can install the ramdisk via u-boot e.g. in NOR flash. For this copy filled ramdisk created above to your tftpboot directory on host (e.g. /tftpboot/ramdisk.gz). Then start target and copy the data into RAM and flash:

UBOOT # tftp 0x87000000 ramdisk.gz
UBOOT # erase 0x2200000 +0x<filesize>
UBOOT # cp.b 0x87000000 0x2200000 0x<filesize>

Note: Replace filesize above by the value the tftp download command gives you as Bytes transferred.
Now, last step is to update kernel boot parameters and save them

UBOOT # setenv bootargs ... root=/dev/ram0 rw initrd=0x87000000,8M
UBOOT # setenv bootcmd cp.b 0x2200000 0x87000000 0x<filesize>; bootm
UBOOT # saveenv

Note: In example above with "8M" we assume that your ramdisk is 8MBytes. Adapt this to your needs.
Note: Your ramdisk filled above should have a /dev/ram0 node to make this work properly.

brw-rw---- 1 root disk 1, 0 Sep 11 1999 /dev/ram0

Now you should be able to start your kernel and it should find and mount the initrd:

Linux version 2.6.23-davinci1 ...
...
checking if image is initramfs...it isn't (no cpio magic); looks like an initrd
Freeing initrd memory: 8192K
...
RAMDISK driver initialized: 1 RAM disks of 8192K size 1024 blocksize
...
RAMDISK: Compressed image found at block 0
VFS: Mounted root (ext2 filesystem).
Freeing init memory: ...
...

Initramfs

To use initramfs a cpio archive is embedded directly into the kernel. I.e. you don't create an additional (ramdisk) image. Instead, the initial file system is directly incorporated into the kernel. With this, the kernel size increases by the file system size. It's like you embed above ramdisk directly into the kernel.

Creation

Cause initramfs is directly embedded in the the kernel, its creation is simpler. No dd & mount & gzip stuff like with ramdisk above. You simply have to fill a directory on your host with the target filesystem you like and then pass the path to this directory to the kernel build process.

Create target file system

host > mkdir target_fs
host > ... copy stuff you want to have in initramfs to target_fs...

Note: cpio system used for initramfs can't handle hard links. If you e.g. created your BusyBox using hard links, you will get a quite large initramfs cause each command is taken with its size and not as hard link. In cpio initramfs use symbolic/soft links instead.

Note: To be able to detect initramfs by kernel properly, the top level directory has to contain a program called init. This can be done by e.g. using a soft link from top level init to /bin/busybox

/init -> /bin/busybox

if you use BusyBox in your initramfs.

Kernel options

The only difference from creating an initrd is to give the kernel the path to the target file system you like to embed:

#
# General setup
#
...
CONFIG_BLK_DEV_INITRD=y
CONFIG_INITRAMFS_SOURCE="<path_to>/target_fs>"
...
#
# UBI - Unsorted block images
#
...
CONFIG_BLK_DEV_RAM=y
CONFIG_BLK_DEV_RAM_COUNT=1
CONFIG_BLK_DEV_RAM_SIZE=8192
CONFIG_BLK_DEV_RAM_BLOCKSIZE=1024
...

Then, if you compile the kernel, e.g. by make uImage, the cpio archive is generated and embedded into the kernel:

...
CHK include/linux/compile.h
GEN usr/initramfs_data.cpio.gz
AS usr/initramfs_data.o
LD usr/built-in.o
...

Installation

No special installation like above with initrd is necessary. The initramfs is already in the kernel. If you start the kernel, the initramfs is already there. Therefore, there is no root=/dev/ram0 rw initrd=0x87000000,8M bootargs option necessary. Remove this if you still have it!

initrd vs. initramfs
  1. Using initrd, kernel and initial file system are splitted. Making changes to kernel or filesystem doesn't touch the other one. The download size (e.g. while development) of one component is smaller.
  2. Creating and modifying an initramfs is easier than with initrd (unzip & mount & unmount & zip)
  3. Having one big image (kernel & initramfs) is easier to handle (e.g. download or flashing) than having two splitted images.

2 comments:

  1. Thanks a lot. Your idea of making a init link to busybox solved mine light years old problem.
    Regards,
    Muhammad Ali

    ReplyDelete
  2. Initramfs is going into '.init' section of kernel, and when kernel frees the init memory , what happens to the initramfs.
    If I want to use the initramfs as the final rootfs what are the options I should give.

    ReplyDelete