📄 ramfs-rootfs-initramfs.txt
字号:
use in place of the above config file: #!/bin/sh # Copyright 2006 Rob Landley <rob@landley.net> and TimeSys Corporation. # Licensed under GPL version 2 if [ $# -ne 2 ] then echo "usage: mkinitramfs directory imagename.cpio.gz" exit 1 fi if [ -d "$1" ] then echo "creating $2 from $1" (cd "$1"; find . | cpio -o -H newc | gzip) > "$2" else echo "First argument must be a directory" exit 1 fiNote: The cpio man page contains some bad advice that will break your initramfsarchive if you follow it. It says "A typical way to generate the listof filenames is with the find command; you should give find the -depth optionto minimize problems with permissions on directories that are unwritable or notsearchable." Don't do this when creating initramfs.cpio.gz images, it won'twork. The Linux kernel cpio extractor won't create files in a directory thatdoesn't exist, so the directory entries must go before the files that go inthose directories. The above script gets them in the right order.External initramfs images:--------------------------If the kernel has initrd support enabled, an external cpio.gz archive can alsobe passed into a 2.6 kernel in place of an initrd. In this case, the kernelwill autodetect the type (initramfs, not initrd) and extract the external cpioarchive into rootfs before trying to run /init.This has the memory efficiency advantages of initramfs (no ramdisk blockdevice) but the separate packaging of initrd (which is nice if you havenon-GPL code you'd like to run from initramfs, without conflating it withthe GPL licensed Linux kernel binary).It can also be used to supplement the kernel's built-in initramfs image. Thefiles in the external archive will overwrite any conflicting files inthe built-in initramfs archive. Some distributors also prefer to customizea single kernel image with task-specific initramfs images, without recompiling.Contents of initramfs:----------------------An initramfs archive is a complete self-contained root filesystem for Linux.If you don't already understand what shared libraries, devices, and pathsyou need to get a minimal root filesystem up and running, here are somereferences:http://www.tldp.org/HOWTO/Bootdisk-HOWTO/http://www.tldp.org/HOWTO/From-PowerUp-To-Bash-Prompt-HOWTO.htmlhttp://www.linuxfromscratch.org/lfs/view/stable/The "klibc" package (http://www.kernel.org/pub/linux/libs/klibc) isdesigned to be a tiny C library to statically link early userspacecode against, along with some related utilities. It is BSD licensed.I use uClibc (http://www.uclibc.org) and busybox (http://www.busybox.net)myself. These are LGPL and GPL, respectively. (A self-contained initramfspackage is planned for the busybox 1.3 release.)In theory you could use glibc, but that's not well suited for small embeddeduses like this. (A "hello world" program statically linked against glibc isover 400k. With uClibc it's 7k. Also note that glibc dlopens libnss to doname lookups, even when otherwise statically linked.)A good first step is to get initramfs to run a statically linked "hello world"program as init, and test it under an emulator like qemu (www.qemu.org) orUser Mode Linux, like so: cat > hello.c << EOF #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); sleep(999999999); } EOF gcc -static hello2.c -o init echo init | cpio -o -H newc | gzip > test.cpio.gz # Testing external initramfs using the initrd loading mechanism. qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zeroWhen debugging a normal root filesystem, it's nice to be able to boot with"init=/bin/sh". The initramfs equivalent is "rdinit=/bin/sh", and it'sjust as useful.Why cpio rather than tar?-------------------------This decision was made back in December, 2001. The discussion started here: http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1538.htmlAnd spawned a second thread (specifically on tar vs cpio), starting here: http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1587.htmlThe quick and dirty summary version (which is no substitute for readingthe above threads) is:1) cpio is a standard. It's decades old (from the AT&T days), and already widely used on Linux (inside RPM, Red Hat's device driver disks). Here's a Linux Journal article about it from 1996: http://www.linuxjournal.com/article/1213 It's not as popular as tar because the traditional cpio command line tools require _truly_hideous_ command line arguments. But that says nothing either way about the archive format, and there are alternative tools, such as: http://freshmeat.net/projects/afio/2) The cpio archive format chosen by the kernel is simpler and cleaner (and thus easier to create and parse) than any of the (literally dozens of) various tar archive formats. The complete initramfs archive format is explained in buffer-format.txt, created in usr/gen_init_cpio.c, and extracted in init/initramfs.c. All three together come to less than 26k total of human-readable text.3) The GNU project standardizing on tar is approximately as relevant as Windows standardizing on zip. Linux is not part of either, and is free to make its own technical decisions.4) Since this is a kernel internal format, it could easily have been something brand new. The kernel provides its own tools to create and extract this format anyway. Using an existing standard was preferable, but not essential.5) Al Viro made the decision (quote: "tar is ugly as hell and not going to be supported on the kernel side"): http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1540.html explained his reasoning: http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1550.html http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1638.html and, most importantly, designed and implemented the initramfs code.Future directions:------------------Today (2.6.16), initramfs is always compiled in, but not always used. Thekernel falls back to legacy boot code that is reached only if initramfs doesnot contain an /init program. The fallback is legacy code, there to ensure asmooth transition and allowing early boot functionality to gradually move to"early userspace" (I.E. initramfs).The move to early userspace is necessary because finding and mounting the realroot device is complex. Root partitions can span multiple devices (raid orseparate journal). They can be out on the network (requiring dhcp, setting aspecific MAC address, logging into a server, etc). They can live on removablemedia, with dynamically allocated major/minor numbers and persistent namingissues requiring a full udev implementation to sort out. They can becompressed, encrypted, copy-on-write, loopback mounted, strangely partitioned,and so on.This kind of complexity (which inevitably includes policy) is rightly handledin userspace. Both klibc and busybox/uClibc are working on simple initramfspackages to drop into a kernel build.The klibc package has now been accepted into Andrew Morton's 2.6.17-mm tree.The kernel's current early boot code (partition detection, etc) will probablybe migrated into a default initramfs, automatically created and used by thekernel build.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -