OS TUTORIALS: WHAT IS INITRD AND HOW TO CREATE INITRD (INITIAL RAMDISK)

0 komentar

INITRD (Initial Ramdisk) is a way to get the Linux kernel mount required rootfs, other filesystem partitions and also to load appropriate kernel modules as required during kernel initialization.
 
Normally, when the Linux kernel is distributed by various distributors like that of RedHat Linux, SUSE Linux, Ubuntu etc., the distributors would provide us with a standard Linux kernel that would contain minimalistic standard PC drivers required for booting the machines. However, since the PC configurations vary from system to system, it is required to load the appropriate drivers to obtain all the PC functionalities. However, building a single Linux kernel to include all these drivers would unnecessarily bloat the kernel image size, which can become an issue with PCs having reasonable amount of RAM.
 
In order to overcome this issue, the distributors would provide with a standard Linux kernel containing standard drivers applicable across all the systems while keeping other drivers as loadable kernel modules. This would result in appropriate drivers to be loaded based upon the hardwares detected during system boot-up. As a result, when the drives containing rootfs and other data partitions gets detected, appropriate block drivers are loaded for proper functioning of the drives and the file systems are mounted over it. Sometimes, these drives may not be ordinary pass through drives but containing RAID volumes on top of it. As a result, the RAID driver is also required to be loaded, which again can be optional and hence needs to be loaded as a kernel driver module.
 
At the lower level, the initrd is a minimalistic file system containing all the necessary modules and executable files required to bring up the system and to mount the actual root file system. Some of the components of the initrd includes (but not limited to) init process, simple shell to accept and execute commands (bash, sh, ksh etc) and various Loadable Kernel Modules (including light weight file systems and device drivers). The initrd will also contain some of the dev files, sys files, proc files etc. that could be seen as part of the original rootfs directory.
 
Sometimes, it is advantageous to include busybox binary as the rootfs files. Busybox has the advantage of being a single binary sufficient to support most of the Linux commands and executables.
 
One can create a simple initd image using the following steps:
 
1. Create an empty binary file using the /dev/zero device and call it ramdisk.img
  • dd if=/dev/zero of=/tmp/ramdisk.img bs=1024 count=4000
2. Create a ext2 file system over this ramdisk.img image
 
  • mke2fs -F -m 0 -b 1024 /tmp/ramdisk.img 4000
3. Next, mount this filesystem over a temporary mount point
  •  mkdir /mnt/initrd 
  • mount /tmp/ramdisk.img /mnt/initrd -t ext2 -o /dev/loop0 
4. Create basic directories as seen in standard rootfs filesystem
  • mkdir /mnt/initrd/dev
  • mkdir /mnt/initrd/sys
  • mkdir /mnt/initrd/bin
  • mkdir /mnt/initrd/proc 
5. Copy a busybox binary which can then be used for various command execution
  • cp /home/busybox . 
6. Create symbolic links for various generic commands to this busybox file
  • ln -s busybox cat 
  • ln -s busybox vi 
  • ln -s busybox ash 
  • ln -s busybox mount 
  • ln -s busybox ps 
  • ln -s busybox dmesg 
  • ln -s busybox sysctl 
  • ln -s busybox grep
 7. Create an init file using the linuxrc 
  • cat >> /mnt/initrd/linuxrc << EOF
 
#!/bin/ash
 
echo "initrd will now be active"
 
mount -t proc /proc /proc
 
mount -t sysfs none /sys
 
EOF
  • chmod +x /mnt/initrd/linuxrc 
8. Finally create a gzip of this ramdisk.img
  •  umount /mnt/initrd 
  • gzip /tmp/ramdisk.img 
 
9. Finally place this ramdisk.img under /boot directory
  •  cp /tmp/ramdisk.img.gz /boot/ramdisk.img.gz 
10. Reboot the system and the system should now be booted with this new ramdisk.img
 
 
Share this article :

Post a Comment

 
Support : animeindo
Copyright © 2011. sakurateateamofficialpage - All Rights Reserved
Template Created by Creating Website Proudly powered by Blogger