这里会显示出您选择的修订版和当前版本之间的差别。
jaysnote:ramfs_rootfs_initramfs [2018/11/19 18:35] jaylee |
jaysnote:ramfs_rootfs_initramfs [2021/06/22 23:14] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | < | ||
- | # ramfs, rootfs and initramfs | ||
- | |||
- | october 17, 2005 | ||
- | Rob Landley < | ||
- | |||
- | |||
- | ## What is ramfs? | ||
- | |||
- | Ramfs is a very simple filesystem that exports Linux' | ||
- | |||
- | Normally all files are cached in memory by Linux. | ||
- | |||
- | With ramfs, there is no backing store. | ||
- | |||
- | The amount of code required to implement ramfs is tiny, because all the work is done by the existing Linux caching infrastructure. | ||
- | you're mounting the disk cache as a filesystem. | ||
- | |||
- | ## ramfs and ramdisk: | ||
- | |||
- | (such as ext2) to format and interpret this data. | ||
- | |||
- | Compared to ramfs, this wastes memory (and memory bus bandwidth), creates unnecessary work for the CPU, and pollutes the CPU caches. | ||
- | More to the point, all the work ramfs is doing has to happen _anyway_, | ||
- | since all file access goes through the page and dentry caches. | ||
- | |||
- | Another reason ramdisks are semi-obsolete is that the introduction of loopback devices offered a more flexible and convenient way to create synthetic block devices, now from files instead of from chunks of memory. | ||
- | See losetup (8) for details. | ||
- | |||
- | ## ramfs and tmpfs: | ||
- | |||
- | One downside of ramfs is you can keep writing data into it until you fill up all memory, and the VM can't free it because the VM thinks that files should get written to backing store (rather than swap space), but ramfs hasn't got any backing store. | ||
- | |||
- | A ramfs derivative called tmpfs was created to add size limits, and the ability to write the data to swap space. | ||
- | |||
- | ## What is rootfs? | ||
- | |||
- | Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which is always present in 2.6 systems. | ||
- | |||
- | Most systems just mount another filesystem over rootfs and ignore it. The amount of space an empty instance of ramfs takes up is tiny. | ||
- | |||
- | ## What is initramfs? | ||
- | |||
- | All 2.6 Linux kernels contain a gzipped " | ||
- | |||
- | All this differs from the old initrd in several ways: | ||
- | |||
- | - The old initrd was always a separate file, while the initramfs archive is linked into the linux kernel image. | ||
- | |||
- | - The old initrd file was a gzipped filesystem image (in some file format,such as ext2, that needed a driver built into the kernel), while the new initramfs archive is a gzipped cpio archive (like tar only simpler,see cpio(1) and `Documentation/ | ||
- | |||
- | - The program run by the old initrd (which was called /initrd, not /init) did some setup and then returned to the kernel, while the init program from initramfs is not expected to return to the kernel. | ||
- | |||
- | - When switching another root device, initrd would pivot_root and then umount the ramdisk. | ||
- | |||
- | Since this is a remarkably persnickety process (and involves deleting commands before you can run them), the klibc package introduced a helper program (utils/ | ||
- | |||
- | ## Populating initramfs: | ||
- | |||
- | The 2.6 kernel build process always creates a gzipped cpio format initramfs archive and links it into the resulting kernel binary. | ||
- | |||
- | The config option **CONFIG_INITRAMFS_SOURCE** (in General Setup in menuconfig, | ||
- | |||
- | ```bash | ||
- | dir /dev 755 0 0 | ||
- | nod / | ||
- | nod /dev/loop0 644 0 0 b 7 0 | ||
- | dir /bin 755 1000 1000 | ||
- | slink /bin/sh busybox 777 0 0 | ||
- | file / | ||
- | dir /proc 755 0 0 | ||
- | dir /sys 755 0 0 | ||
- | dir /mnt 755 0 0 | ||
- | file /init initramfs/ | ||
- | ``` | ||
- | |||
- | Run `" | ||
- | |||
- | One advantage of the configuration file is that root access is not required to set permissions or create device nodes in the new archive. | ||
- | |||
- | The kernel does not depend on external cpio tools. | ||
- | |||
- | The one thing you might need external cpio utilities installed for is creating or extracting your own preprepared cpio files to feed to the kernel build (instead of a config file or directory). | ||
- | |||
- | The following command line can extract a cpio image (either by the above script or by the kernel build) back into its component files: | ||
- | |||
- | ```bash | ||
- | cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenames | ||
- | ``` | ||
- | |||
- | The following shell script can create a prebuilt cpio archive you can use in place of the above config file: | ||
- | |||
- | ```bash | ||
- | #!/bin/sh | ||
- | #Copyright 2006 Rob Landley rob@landley.net and TimeSys Corporation.Licensed under GPL version 2 | ||
- | |||
- | if [ $# -ne 2 ] | ||
- | then | ||
- | echo " | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | if [ -d " | ||
- | then | ||
- | echo " | ||
- | (cd " | ||
- | else | ||
- | echo "First argument must be a directory" | ||
- | exit 1 | ||
- | fi | ||
- | ``` | ||
- | |||
- | > **Note:** The cpio man page contains some bad advice that will break your initramfs archive if you follow it. It says "A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unwritable or not searchable." | ||
- | |||
- | External initramfs images: | ||
- | -------------------------- | ||
- | |||
- | If the kernel has initrd support enabled, an external cpio.gz archive can also be passed into a 2.6 kernel in place of an initrd. | ||
- | |||
- | This has the memory efficiency advantages of initramfs (no ramdisk block device) but the separate packaging of initrd (which is nice if you have non-GPL code you'd like to run from initramfs, without conflating it with the GPL licensed Linux kernel binary). | ||
- | |||
- | It can also be used to supplement the kernel' | ||
- | |||
- | ## 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 path you need to get a minimal root filesystem up and running, here are som references: | ||
- | |||
- | * http:// | ||
- | |||
- | * http:// | ||
- | |||
- | * http:// | ||
- | |||
- | The " | ||
- | |||
- | I use uClibc (http:// | ||
- | |||
- | In theory you could use glibc, but that's not well suited for small embedde uses like this. (A "hello world" program statically linked against glibc is over400k. | ||
- | |||
- | 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) or User Mode Linux, like so: | ||
- | |||
- | ```c | ||
- | cat > hello.c << EOF | ||
- | |||
- | include < | ||
- | include < | ||
- | |||
- | int main(int argc, char *argv[]) | ||
- | { | ||
- | printf(" | ||
- | sleep(999999999); | ||
- | } | ||
- | EOF | ||
- | gcc -static hello.c -o init | ||
- | echo init | cpio -o -H newc | gzip > test.cpio.gz | ||
- | # Testing external initramfs using the initrd loading mechanism. | ||
- | qemu -kernel / | ||
- | ``` | ||
- | |||
- | When debugging a normal root filesystem, it's nice to be able to boot with " | ||
- | |||
- | ## Why cpio rather than tar? | ||
- | |||
- | This decision was made back in December, 2001. The discussion started here: | ||
- | |||
- | http:// | ||
- | |||
- | And spawned a second thread (specifically on tar vs cpio), starting here: | ||
- | |||
- | http:// | ||
- | |||
- | The quick and dirty summary version (which is no substitute for readin he above threads) is: | ||
- | |||
- | 1. cpio is a standard. | ||
- | |||
- | | ||
- | |||
- | | ||
- | such as: | ||
- | |||
- | 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. | ||
- | |||
- | 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. | ||
- | |||
- | 5. Al Viro made the decision (quote: "tar is ugly as hell and not going to be supported on the kernel side" | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | | ||
- | |||
- | and, most importantly, | ||
- | |||
- | ## Future directions: | ||
- | |||
- | Today (2.6.16), initramfs is always compiled in, but not always used. The kernel falls back to legacy boot code that is reached only if initramfs does not contain an /init program. | ||
- | "early userspace" | ||
- | |||
- | The move to early userspace is necessary because finding and mounting the real root device is complex. | ||
- | |||
- | This kind of complexity (which inevitably includes policy) is rightly handled in userspace. | ||
- | |||
- | The klibc package has now been accepted into Andrew Morton' | ||
- | The kernel' | ||
- | |||
- | </ |