⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ramfs-rootfs-initramfs.txt

📁 linux 内核源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
ramfs, rootfs and initramfsOctober 17, 2005Rob Landley <rob@landley.net>=============================What is ramfs?--------------Ramfs is a very simple filesystem that exports Linux's disk cachingmechanisms (the page cache and dentry cache) as a dynamically resizableRAM-based filesystem.Normally all files are cached in memory by Linux.  Pages of data read frombacking store (usually the block device the filesystem is mounted on) are keptaround in case it's needed again, but marked as clean (freeable) in case theVirtual Memory system needs the memory for something else.  Similarly, datawritten to files is marked clean as soon as it has been written to backingstore, but kept around for caching purposes until the VM reallocates thememory.  A similar mechanism (the dentry cache) greatly speeds up access todirectories.With ramfs, there is no backing store.  Files written into ramfs allocatedentries and page cache as usual, but there's nowhere to write them to.This means the pages are never marked clean, so they can't be freed by theVM when it's looking to recycle memory.The amount of code required to implement ramfs is tiny, because all thework is done by the existing Linux caching infrastructure.  Basically,you're mounting the disk cache as a filesystem.  Because of this, ramfs is notan optional component removable via menuconfig, since there would be negligiblespace savings.ramfs and ramdisk:------------------The older "ram disk" mechanism created a synthetic block device out ofan area of RAM and used it as backing store for a filesystem.  This blockdevice was of fixed size, so the filesystem mounted on it was of fixedsize.  Using a ram disk also required unnecessarily copying memory from thefake block device into the page cache (and copying changes back out), as wellas creating and destroying dentries.  Plus it needed a filesystem driver(such as ext2) to format and interpret this data.Compared to ramfs, this wastes memory (and memory bus bandwidth), createsunnecessary work for the CPU, and pollutes the CPU caches.  (There are tricksto avoid this copying by playing with the page tables, but they're unpleasantlycomplicated and turn out to be about as expensive as the copying anyway.)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.  The RAMdisk is simply unnecessary; ramfs is internally much simpler.Another reason ramdisks are semi-obsolete is that the introduction ofloopback devices offered a more flexible and convenient way to createsynthetic 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 fillup all memory, and the VM can't free it because the VM thinks that filesshould get written to backing store (rather than swap space), but ramfs hasn'tgot any backing store.  Because of this, only root (or a trusted user) shouldbe allowed write access to a ramfs mount.A ramfs derivative called tmpfs was created to add size limits, and the abilityto write the data to swap space.  Normal users can be allowed write access totmpfs mounts.  See Documentation/filesystems/tmpfs.txt for more information.What is rootfs?---------------Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which isalways present in 2.6 systems.  You can't unmount rootfs for approximately thesame reason you can't kill the init process; rather than having special codeto check for and handle an empty list, it's smaller and simpler for the kernelto just make sure certain lists can't become empty.Most systems just mount another filesystem over rootfs and ignore it.  Theamount of space an empty instance of ramfs takes up is tiny.What is initramfs?------------------All 2.6 Linux kernels contain a gzipped "cpio" format archive, which isextracted into rootfs when the kernel boots up.  After extracting, the kernelchecks to see if rootfs contains a file "init", and if so it executes it as PID1.  If found, this init process is responsible for bringing the system therest of the way up, including locating and mounting the real root device (ifany).  If rootfs does not contain an init program after the embedded cpioarchive is extracted into it, the kernel will fall through to the older codeto locate and mount a root partition, then exec some variant of /sbin/initout of that.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 directory linux-*/usr is devoted    to generating this archive during the build.)  - 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/early-userspace/buffer-format.txt).  The    kernel's cpio extraction code is not only extremely small, it's also    __init text and data that can be discarded during the boot process.  - 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.  (If /init needs to hand    off control it can overmount / with a new root device and exec another init    program.  See the switch_root utility, below.)  - When switching another root device, initrd would pivot_root and then    umount the ramdisk.  But initramfs is rootfs: you can neither pivot_root    rootfs, nor unmount it.  Instead delete everything out of rootfs to    free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs    with the new root (cd /newmount; mount --move . /; chroot .), attach    stdin/stdout/stderr to the new /dev/console, and exec the new init.    Since this is a remarkably persnickity process (and involves deleting    commands before you can run them), the klibc package introduced a helper    program (utils/run_init.c) to do all this for you.  Most other packages    (such as busybox) have named this command "switch_root".Populating initramfs:---------------------The 2.6 kernel build process always creates a gzipped cpio format initramfsarchive and links it into the resulting kernel binary.  By default, thisarchive is empty (consuming 134 bytes on x86).The config option CONFIG_INITRAMFS_SOURCE (for some reason buried underdevices->block devices in menuconfig, and living in usr/Kconfig) can be usedto specify a source for the initramfs archive, which will automatically beincorporated into the resulting binary.  This option can point to an existinggzipped cpio archive, a directory containing files to be archived, or a textfile specification such as the following example:  dir /dev 755 0 0  nod /dev/console 644 0 0 c 5 1  nod /dev/loop0 644 0 0 b 7 0  dir /bin 755 1000 1000  slink /bin/sh busybox 777 0 0  file /bin/busybox initramfs/busybox 755 0 0  dir /proc 755 0 0  dir /sys 755 0 0  dir /mnt 755 0 0  file /init initramfs/init.sh 755 0 0Run "usr/gen_init_cpio" (after the kernel build) to get a usage messagedocumenting the above file format.One advantage of the configuration file is that root access is not required toset permissions or create device nodes in the new archive.  (Note that thosetwo example "file" entries expect to find files named "init.sh" and "busybox" ina directory called "initramfs", under the linux-2.6.* directory.  SeeDocumentation/early-userspace/README for more details.)The kernel does not depend on external cpio tools.  If you specify adirectory instead of a configuration file, the kernel's build infrastructurecreates a configuration file from that directory (usr/Makefile callsscripts/gen_initramfs_list.sh), and proceeds to package up that directoryusing the config file (by feeding it to usr/gen_init_cpio, which is createdfrom usr/gen_init_cpio.c).  The kernel's build-time cpio creation code isentirely self-contained, and the kernel's boot-time extractor is also(obviously) self-contained.The one thing you might need external cpio utilities installed for is creatingor 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 scriptor by the kernel build) back into its component files:  cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenamesThe following shell script can create a prebuilt cpio archive you can

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -