Today we will look what in initrd image, initramfs in Linux and what are the differences between them.
Initrd :- Initial RAM Disk
Initramfs :- Initial RAM Filesystem
Introduction to Initrd and Initramfs
1. Initrd and/or initramfs is used to load temporary root file system into RAM or system memory hence its named like initial RAM Disk/FS.
To start our Linux OS, we need to have either initrd or initramfs filesystem to load kernel into memory and then finally mounting the root filesystem on hard disk from memory which is done by dracut daemon.
2. initrd and initramfs refers to two types of different methods to achieve this. Both are used to make preparations(to start kernel, and initiate hardware devices) before the real root file system can be mounted(on system hard disk or storage).
3. initrd works as a block device and required a filesystem driver(ext2/ext3, etc) to be compiled into the kernel. The kernel must have at least one built-in module available for detecting filesystem of initrd. The Initrd disk has a fixed size. All of the operations(read/write) on an Initrd Image are buffered unnecessarily into the system main memory.
4. Why initramfs come into picture ?
There are a lot of cpu overhead due to multiple pagein & pageout from initrd block device.
This can be avoided if initrd can be mounted as a filesystem(instead of a block device) to keep the files in cache and never get rid of them until they’re deleted or a system reboot is performed.
5. What is initramfs ?
With initramfs, we create a cpio archive with the necessary boot files/modules which the kernel extracts to a tmpfs during boot.
Initramfs is mounted on Linux system as tmpfs filesystem. tmpfs is an improved version of ramfs. ramfs was initially written by Linus Torvalds, and later the group came up with tmpfs implementation. tmpfs has additional features which enables it to write data to swap space in case of available memory is less.
Initramfs can automatically grow or shrink according to the data it contain. If new files are added to ramfs, it will automatically allocates more memory and hence there is no duplication in the file data.
6. Format of initramfs
initramfs is a cpio archive which is a binary archive format(used in rpm also) and then finally compressed using gzip.
7. How to generate initramfs file in Linux ?
RHEL 5 # mkinitrd -f -v /boot/initrd-$(uname -r).img $(uname -r) RHEL 6 # cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.backup # dracut -f
8. Why the generated file is named as initrd ?
The file generated in above step is usually named as initrd, however its a initramfs file. The naming conventions for live and install CD/DVDs is kept same and hence the name.
/dev/ram device is mounted when using initrd implementation.
9. Difference between ramfs and tmpfs ?
By now, you must get an idea that ramfs was initial implementation by Linus torvalds and tmpfs in an advanced version which can use swap.
Both tmpfs and ramfs are targetted to achieve same work with few differences.
ramfs used to grow dynamically till it occupies total RAM. Once all RAM is occupied, the system will hang(since there is no space available).
In tmpfs, it can use swap space and hence the system doesn’t hang even after total memory is occupied.
Device name of ramfs /dev/ram0 mounted on /tmp Device name of tmpfs /dev/shm [the mount point can be tweaked using /etc/fstab file] mounted on /tmp
tmpfs size and mount point can be modified using /etc/fstab file like below.
tmpfs /dev/shm tmpfs size=4g 0 0
Also one point to note here is that /dev/shm i.e. shm(Shared memory) points to tmpfs device and there is no difference between them. tmpfs and shm are same.