Create Your Own Initial Ram Disk (Initrd) : Uditha Atukorala
Create Your Own Initial Ram Disk (Initrd) : Uditha Atukorala
Uditha Atukorala
Revision History
2
Chapter 0. Legal information
0. Legal information
0.1 Copyright and License
This document, Create your own initial ram disk (initrd), is copyright (c) 2008 by Uditha Atukorala.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free
Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the
section entitled "GNU Free Documentation License".
0.2. Disclaimer
This documentation is provided as−is with no warranty of any kind, either expressed or implied, including,
but not limited to, the implied warranties of merchantability and fitness for a particular purpose. Use the
concepts, examples and information at your own risk. The author(s) do not take any responsibility for
damages that may arise from the use of this document.
All copyrights are held by their respective owners, unless specifically noted otherwise. Use of a term in this
document should not be regarded as affecting the validity of any trademark or service mark. Naming of
particular products or brands should not be seen as endorsements.
3
Chapter 1. What is initrd?
1. What is initrd?
Initial Ram Disk or initrd is a compressed file system which is uncompressed and loaded to the memory by
the boot loader. When the kernel is booting it treats this initial file system as the root file system and
executes the first program /linuxrc. (Note: for Debian kernels the default is /init) This program then mounts
the actual file system and passes the control to it and ideally un-mounting the initrd and freeing memory.
Initial ram disk is a way of resolving the chicken and egg dilemma for the kernel. Imagine you have the
actual root file system in a device which requires the kernel to use additional modules, but in order to load
these modules the kernel has to access the file system. Thus the initrd could contain all the required modules
and mount the actual file system and then pass over the control.
4
Chapter 2. Why create an initrd?
5
Chapter 3. Modifying the Debian initrd
It is a good idea to have a clean working directory so you know what you are doing at the moment and it is
easier to manage. I setup my working directory as $HOME/WorkBench
Now let’s copy the initrd from Debian iso image. But first we need to find where the initrd image is. You
could find the initrd image in /boot in a Linux System so hoping that the iso image has the same structure
we’ll look in the mounted image structure.
$ ls –l /mnt/isoimage/
Unfortunately the boot folder is not there, but we could find a folder named ‘isolinux’. Since we find this
folder we could conclude the iso image uses the ISOLINUX2 boot system.
$ ls –l /mnt/isoimage/isolinux
Now we have the isolinux.cfg file which is used to store ISOLINUX configuration.
$ less /mnt/isoimage/isolinux/isolinux.cfg
LABEL install
kernel /install.386/vmlinuz
append vga=normal initrd=/install.386/initrd.gz –
1
URL: http://www.debian.org/CD/netinst/#businesscard-stable
2
URL: http://syslinux.zytor.com/iso.php
6
Chapter 3. Modifying the Debian initrd
There we are; the initrd image is in install.386 folder. Copy it to your work bench.
$ cp /mnt/isoimage/isolinux/install.386/initrd.gz $HOME/WorkBench
$ cd $HOME/WorkBench
Now you should have a directory structure which is very similar to a root file system extracted to your
working directory. Browse through it and try to find something interesting.
#!/bin/sh -e
# used for initramfs
export PATH
. /lib/debian-installer/init-debug
debugshell "just booted"
mount /proc
if [ -x /sbin/udevd ]; then
/lib/debian-installer/init-udev-devices
else
mount /dev
mount /dev/pts
fi
init='busybox init'
for i in $(cat /proc/cmdline); do
case $i in
init=*)
init=${i#init=}
;;
esac
done
debugshell "before init"
exec $init
Let’s create our own init script, so we’ll be able to execute basic shell commands.
Debian initrd comes with BusyBox packed into it, so let’s use advantage of it and it’s build in ash shell. First
we need to mount the /proc and /sys file systems and then give the ash prompt. Edit the init script so that it’ll
be similar to the one below and don’t hesitate to add your own code and your own greeting.
7
Chapter 3. Modifying the Debian initrd
init script
#!/bin/ash
mount -t proc /proc /proc
mount -t sysfs none /sys
echo
echo "initrd is running"
echo "Using BusyBox..."
echo
exec /bin/ash --login
Well, that’s all for our simple init script. Now it’s time to make our custom initrd.
$ rm initrd.gz
# cp $HOME/initrd.img /boot
Note: You might have to be in root mode.
Edit your boot loader to load the new initrd and it’s always a good idea to add a new entry with the new
initrd rather than editing the existing entries.
# nano /boot/grub/menu.lst
Do a reboot and select our new entry while booting. After booting you should get the BusyBox ash prompt
and should be able to enter simple commands.
3.6. Troubleshooting
If you are not using a Debian system then you might face some problems. In this section I try to highlight
some of the things that you might face.
First thing would be having problems with extracting the stock initrd image. The initrd image I used and all
the other Debian initrd images (I haven’t tested old Debian initrds so this could not be the case with the old
8
Chapter 3. Modifying the Debian initrd
ones) are compressed using cpio3 archive. But other initrd images could have some other compression such
as gzip.
# mkdir -p /mnt/initrd
# gunzip initrd.img.gz
# mount -t ext -o loop initrd.img /mnt/initrd
Note that the initrd image had the extension .img.gz this could give a hint that it’s compressed using gzip.
If you uncompress a gzip image then it’s probably best to compress it back using gzip because you kernel
might not recognize cramfs4 file system (file system used in cpio archives).
# umount /mnt/initrd
# gzip –c initrd.img
For any reason if you need to recreate the stock initrd you could use this command on a Debian system.
# mkdir -p /mnt/initrd
# mount -t cramfs initrd.img /mnt/initrd -o loop
3
URL: http://en.wikipedia.org/wiki/Cpio
4
URL: http://en.wikipedia.org/wiki/Cramfs
5
Reference: http://kernel-handbook.alioth.debian.org/ch-initramfs.html
9
Chapter 4. Create a new initrd from scratch
$ mkdir $HOME/WorkBench/initfs
$ cd $HOME/WorkBench/initfs
I’m going to use the BusyBox which shipped with my Debian Linux System. If you are having thoughts of
compiling your own selection of programs it would be a good idea to use a static compile so you don’t have
to worry about the dynamic libraries. But if you really insist on using dynamic libraries you could try
installing them to your initfs directory.
Anyway as I’ve mentioned earlier the choice is up to you and don’t hesitate to try new things.
# BusyBox
$ cp /bin/busybox $HOME/WorkBench/initfs/bin
# Dynamic Libraries
$ pushd $HOME/WorkBench/initfs/lib
$ cp /lib/libc.so.6 .
$ cp /lib/libdl.so.2 .
$ cp /lib/ld-linux.so.2 .
$ cp /lib/libcrypt.so.1 .
$ cp /lib/libm.so.6 .
$ popd
Note: lib files required by BusyBox may vary depending on your BusyBox compilation. You can use $ ldd
/bin/busybux to find out the library dependencies for your BusyBox.
And we need to create some sym-links to get the BusyBox working as we need.
$ pushd $HOME/WorkBench/initfs/bin
$ ln -s busybox ash
6
URLs: http://www.pocket-linux.org/, http://tldp.org/LDP/Pocket-Linux-Guide/html/
7
URL: http://www.busybox.net/
Chapter 4. Create a new initrd from scratch
$ ln -s busybox mount
$ ln -s busybox echo
$ ln -s busybox ls
$ ln -s busybox cat
$ ln -s busybox ps
$ ln -s busybox dmesg
$ ln -s busybox sysctl
$ ln -s busybox sh
$ popd
I found out that these links are really not necessary when you are using the BusyBox built-in ash shell. You
can just enter the commands and they’ll work. But if you are using a different shell like bash, you’ll have to
have these links to get things working.
4.5. Finalise
If you have noticed we didn’t create the sbin directory when we were population the file system earlier. That
is simply because we are only going to have root login and everything will be executed as root. And that’s
why we didn’t even consider setting the appropriate privileges to the device nodes as well. So it is safe to just
link the bin and sbin directories.
$ ln -s bin sbin
We are almost there now. All there is left is to create the init (or linuxrc) script which will be executed by the
kernel as the first program. We will use the same init script as before so we could test our initrd against the
stock initrd.
init script
#!/bin/ash
mount -t proc /proc /proc
mount -t sysfs none /sys
echo
echo "initrd is running"
echo "Using BusyBox..."
echo
exec /bin/ash –login
11
Chapter 4. Create a new initrd from scratch
Have a look at the contents in our initfs folder now. It should be similar to this.
total 24
drwxr-xr-x 2 root root 4096 2008-03-17 14:44 bin
drwxr-xr-x 2 root root 4096 2008-03-17 14:44 dev
-rwxr-xr-x 1 root root 142 2008-03-17 14:44 init
drwxr-xr-x 2 root root 4096 2008-03-17 14:44 lib
drwxr-xr-x 2 root root 4096 2008-03-17 14:44 proc
lrwxrwxrwx 1 root root 3 2008-03-17 14:44 sbin -> bin
drwxr-xr-x 2 root root 4096 2008-03-17 14:44 sys
# cp $HOME/WorkBench/initrd-2.img /boot
# nano /boot/grub/menu.lst
Reboot and select the ‘Linux [Custom initrd]’ from the boot menu and you should get to the BusyBox built-
in ash shell prompt. Key in some commands like ls, ls –l to make sure our initrd is working properly.
If you didn’t make any sym-links to BusyBox or you only made some of them, you can find out what are the
available commands by typing help on the ash prompt. Try experimenting with some of them just to make
sure that everything is working as planned.
12
Appendix A. GNU Free Documentation License
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not
allowed.
A.0. PREAMBLE
The purpose of this License is to make a manual, textbook, or other functional and useful document "free" in
the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without
modifying it, either commercially or noncommercially.
Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not
being considered responsible for modifications made by others.
This License is a kind of "copyleft", which means that derivative works of the document must themselves be
free in the same sense. It complements the GNU General Public License, which is a copyleft license
designed for free software.
We have designed this License in order to use it for manuals for free software, because free software needs
free documentation: a free program should come with manuals providing the same freedoms that the
software does. But this License is not limited to software manuals;
it can be used for any textual work, regardless of subject matter or whether it is published as a printed book.
We recommend this License principally for works whose purpose is instruction or reference.
A "Modified Version" of the Document means any work containing the Document or a portion of it, either
copied verbatim, or with modifications and/or translated into another language.
A "Secondary Section" is a named appendix or a front-matter section of the Document that deals exclusively
with the relationship of the publishers or authors of the Document to the Document's overall subject (or to
related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the
Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.) The
relationship could be a matter of historical connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding them.
The "Invariant Sections" are certain Secondary Sections whose titles are designated, as being those of
Invariant Sections, in the notice that says that the Document is released under this License. If a section does
not fit the above definition of Secondary then it is not allowed to be designated as Invariant. The Document
may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are
none.
13
Appendix A. GNU Free Documentation License
The "Cover Texts" are certain short passages of text that are listed, as Front-Cover Texts or Back-Cover
Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be
at most 5 words, and a Back-Cover Text may be at most 25 words.
A "Transparent" copy of the Document means a machine-readable copy, represented in a format whose
specification is available to the general public, that is suitable for revising the document straightforwardly
with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some
widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to
a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent file
format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent
modification by readers is not Transparent. An image format is not Transparent if used for any substantial
amount of text. A copy that is not "Transparent" is called "Opaque".
Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input
format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming
simple HTML, PostScript or PDF designed for human modification. Examples of transparent image formats
include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only
by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally
available, and the machine-generated HTML, PostScript or PDF produced by some word processors for
output purposes only.
The "Title Page" means, for a printed book, the title page itself, plus such following pages as are needed to
hold, legibly, the material this License requires to appear in the title page. For works in formats which do
not have any title page as such, "Title Page" means the text near the most prominent appearance of the
work's title, preceding the beginning of the body of the text.
A section "Entitled XYZ" means a named subunit of the Document whose title either is precisely XYZ or
contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for
a specific section name mentioned below, such as "Acknowledgements", "Dedications", "Endorsements", or
"History".) To "Preserve the Title" of such a section when you modify the Document means that it remains a
section "Entitled XYZ" according to this definition.
The Document may include Warranty Disclaimers next to the notice which states that this License applies to
the Document. These Warranty Disclaimers are considered to be included by reference in this License, but
only as regards disclaiming warranties: any other implication that these Warranty Disclaimers may have is
void and has no effect on the meaning of this License.
You may also lend copies, under the same conditions stated above, and you may publicly display copies.
14
Appendix A. GNU Free Documentation License
to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated
as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as
many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either
include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque
copy a computer-network location from which the general network-using public has access to download
using public-standard network protocols a complete Transparent copy of the Document, free of added
material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of
Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated
location until at least one year after the last time you distribute an Opaque copy (directly or through your
agents or retailers) of that edition to the public.
It is requested, but not required, that you contact the authors of the Document well before redistributing any
large number of copies, to give them a chance to provide you with an updated version of the Document.
A.4 MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3
above, provided that you release the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution and modification of the Modified
Version to whoever possesses a copy of it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from
those of previous versions (which should, if there were any, be listed in the History section of the
Document). You may use the same title as a previous version if the original publisher of that version
gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the
modifications in the Modified Version, together with at least five of the principal authors of the
Document (all of its principal authors, if it has fewer than five), unless they release you from this
requirement.
C. State on the Title page the name of the publisher of the Modified Version, as the publisher.
E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices.
F. Include, immediately after the copyright notices, a license notice giving the public permission to use
the Modified Version under the terms of this License, in the form shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in
the Document's license notice.
I. Preserve the section Entitled "History", Preserve its Title, and add to it an item stating at least the
title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is
no section Entitled "History" in the Document, create one stating the title, year, authors, and
publisher of the Document as given on its Title Page, then add an item describing the Modified
Version as stated in the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to a Transparent copy
of the Document, and likewise the network locations given in the Document for previous versions it
15
Appendix A. GNU Free Documentation License
was based on. These may be placed in the "History" section. You may omit a network location for a
work that was published at least four years before the Document itself, or if the original publisher of
the version it refers to gives permission.
K. For any section Entitled "Acknowledgements" or "Dedications", Preserve the Title of the section,
and preserve in the section all the substance and tone of each of the contributor acknowledgements
and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section
numbers or the equivalent are not considered part of the section titles.
M. Delete any section Entitled "Endorsements". Such a section may not be included in the Modified
Version.
N. Do not retitle any existing section to be Entitled "Endorsements" or to conflict in title with any
Invariant Section.
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections
and contain no material copied from the Document, you may at your option designate some or all of these
sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version's
license notice. These titles must be distinct from any other section titles.
You may add a section Entitled "Endorsements", provided it contains nothing but endorsements of your
Modified Version by various parties--for example, statements of peer review or that the text has been
approved by an organization as the authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a
Back-Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-
Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity.
If the Document already includes a cover text for the same cover, previously added by you or by
arrangement made by the same entity you are acting on behalf of, you may not add another; but you may
replace the old one, on explicit permission from the previous publisher that added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names for
publicity for or to assert or imply endorsement of any Modified Version.
The combined work need only contain one copy of this License, and multiple identical Invariant Sections
may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different
contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the
original author or publisher of that section if known, or else a unique number. Make the same adjustment to
the section titles in the list of Invariant Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled "History" in the various original documents,
forming one section Entitled "History"; likewise combine any sections Entitled "Acknowledgements", and
any sections Entitled "Dedications". You must delete all sections Entitled "Endorsements".
16
Appendix A. GNU Free Documentation License
You may extract a single document from such a collection, and distribute it individually under this License,
provided you insert a copy of this License into the extracted document, and follow this License in all other
respects regarding verbatim copying of that document.
If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the
Document is less than one half of the entire aggregate, the Document's Cover Texts may be placed on covers
that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in
electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate.
A.8. TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under
the terms of section 4. Replacing Invariant Sections with translations requires special permission from their
copyright holders, but you may include translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections. You may include a translation of this License, and all the
license notices in the Document, and any Warranty Disclaimers, provided that you also include the original
English version of this License and the original versions of those notices and disclaimers. In case of a
disagreement between the translation and the original version of this License or a notice or disclaimer, the
original version will prevail.
A.9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under
this License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will
automatically terminate your rights under this License. However, parties who have received copies, or rights,
from you under this License will not have their licenses terminated so long as such parties remain in full
compliance.
Each version of the License is given a distinguishing version number. If the Document specifies that a
particular numbered version of this License "or any later version" applies to it, you have the option of
following the terms and conditions either of that specified version or of any later version that has been
published (not as a draft) by the Free Software Foundation. If the Document does not specify a version
17
Appendix A. GNU Free Documentation License
number of this License, you may choose any version ever published (not as a draft) by the Free Software
Foundation.
If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the "with...Texts." line
with this:
with the Invariant Sections being LIST THEIR TITLES, with the Front-Cover
Texts being LIST, and with the Back-Cover Texts being LIST.
If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those two
alternatives to suit the situation.
If your document contains nontrivial examples of program code, we recommend releasing these examples in
parallel under your choice of free software license, such as the GNU General Public License, to permit their
use in free software.
18