Linux Kernel 2.6.20[Documentation/early-userspace/Readme]
The kernel has currently 3 ways to mount the root filesystem:
a) all required device and filesystem drivers compiled into the kernel, no initrd. init/main.c:init() will call prepare_namespace() to mount the final root filesystem, based on the root= option and optional init= to run some other init binary than listed at the end of init/main.c:init().
b) some device and filesystem drivers built as modules and stored in an initrd. The initrd must contain a binary '/linuxrc' which is supposed to load these driver modules. It is also possible to mount the final root filesystem via linuxrc and use the pivot_root syscall. The initrd is mounted and executed via repare_namespace().
c) using initramfs. The call to prepare_namespace() must be skipped. This means that a binary must do all the work. Said binary can be stored into initramfs either via modifying usr/gen_init_cpio.c or via the new initrd format, an cpio archive. It must be called "/init". This binary is responsible to do all the things prepare_namespace() would do. To remain backwards compatibility, the /init binary will only run if it comes via an initramfs cpio archive. If this is not the case, init/main.c:init() will run prepare_namespace() to mount the final root and exec one of the predefined init binaries.
其中,第一种方式已经用过了,比如直接在mtdblock3上挂载jffs2根文件系统。第二种方式要分为两种情况,一种是image-initrd,一种是cpio-initrd。在host上普遍采用cpio-initrd了,但是在嵌入式系统上还是采用image-initrd多。本来想探讨一下如何使用cpio-initrd,没有结果,暂时放弃。第三种方式实际上就是把fs作到kernel中,实际的映像就是kernel+fs了。不过第三种方式对小系统来说合适,一旦这个映像比较大,那么更多的时间会消耗在解压缩上,反而得不偿失了。
下面介绍一下第三种方式。
One method of doing this is to use the initramfs function in the kernel. The kernel expects the image to be a gzipped CPIO archive. In the kernel source there are tools to create the CPIO archive. First we must create a file list from our file system (presumedly this is just the root of your current NFS mount).
/!!Remember that you need a 'init' file in /. You can just link to /sbin/init
·进入内核源代码
$ cd /path_to_linux_source
·产生cpio的对应文件里表
# scripts/gen_initramfs_list.sh /path_to_fs_root/ > cpio_list
·Next create the CPIO archive
# usr/gen_init_cpio cpio_list > initramfs.cpio
·Gzip the CPIO archive
$ gzip initramfs.cpio
·Now copy initramfs.cpio.gz to usr/initramfs_data.cpio.gz, and run the normal kernel make. You will notice that uImage is much larger (size = kernel + fs).
最后把uImage烧写到对应区域,引导启动即可。这时候就不需要烧写root filesystem了。