这里会显示出您选择的修订版和当前版本之间的差别。
jaysnote:ramfs_rootfs_initramfs [2018/11/16 21:48] 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. | ||
- | This means the pages are never marked clean, so they can't be freed by th M when it's looking to recycle memory. | ||
- | |||
- | The amount of code required to implement ramfs is tiny, because all th ork is done by the existing Linux caching infrastructure. | ||
- | you're mounting the disk cache as a filesystem. | ||
- | |||
- | ## ramfs and ramdisk: | ||
- | |||
- | |||
- | The older "ram disk" mechanism created a synthetic block device out o n area of RAM and used it as backing store for a filesystem. | ||
- | (such as ext2) to format and interpret this data. | ||
- | |||
- | Compared to ramfs, this wastes memory (and memory bus bandwidth), create nnecessary 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 o oopback devices offered a more flexible and convenient way to creat ynthetic 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 fil p all memory, and the VM can't free it because the VM thinks that file hould get written to backing store (rather than swap space), but ramfs hasn' ot any backing store. | ||
- | |||
- | A ramfs derivative called tmpfs was created to add size limits, and the abilit o write the data to swap space. | ||
- | |||
- | ## What is rootfs? | ||
- | |||
- | |||
- | Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which i lways present in 2.6 systems. | ||
- | |||
- | Most systems just mount another filesystem over rootfs and ignore it. Th mount 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 initramf rchive and links it into the resulting kernel binary. | ||
- | |||
- | The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig, | ||
- | and living in usr/ | ||
- | |||
- | | ||
- | 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 t et permissions or create device nodes in the new archive. | ||
- | |||
- | The kernel does not depend on external cpio tools. | ||
- | (obviously) self-contained. | ||
- | |||
- | The one thing you might need external cpio utilities installed for is creatin r 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 scrip r 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 ca se 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 initramf rchive if you follow it. It says "A typical way to generate the lis f filenames is with the find command; you should give find the -depth optio o minimize problems with permissions on directories that are unwritable or no earchable." | ||
- | |||
- | ## External initramfs images: | ||
- | |||
- | |||
- | If the kernel has initrd support enabled, an external cpio.gz archive can als e passed into a 2.6 kernel in place of an initrd. | ||
- | |||
- | This has the memory efficiency advantages of initramfs (no ramdisk bloc evice) but the separate packaging of initrd (which is nice if you hav on-GPL code you'd like to run from initramfs, without conflating it wit he 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 ou need to get a minimal root filesystem up and running, here are som eferences: | ||
- | |||
- | * http:// | ||
- | |||
- | * http:// | ||
- | |||
- | * http:// | ||
- | |||
- | The " | ||
- | |||
- | I use uClibc (http:// | ||
- | myself. | ||
- | |||
- | In theory you could use glibc, but that's not well suited for small embedde ses like this. (A "hello world" program statically linked against glibc i ver 400k. With uClibc it's 7k. Also note that glibc dlopens libnss to d ame 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) o ser 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. | ||
- | |||
- | | ||
- | |||
- | It's not as popular as tar because the traditional cpio command line tools require _truly_hideous_ command line arguments. | ||
- | 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. Th ernel falls back to legacy boot code that is reached only if initramfs doe ot contain an /init program. | ||
- | "early userspace" | ||
- | |||
- | The move to early userspace is necessary because finding and mounting the rea oot device is complex. | ||
- | and so on. | ||
- | |||
- | This kind of complexity (which inevitably includes policy) is rightly handle n userspace. | ||
- | |||
- | The klibc package has now been accepted into Andrew Morton' | ||
- | The kernel' | ||
- | |||
- | </ |