Aim is to create my own Linux distribution which runs completely runs in-memory. The Linux should be minimal at the same time functional.
In order to achieve this, you need 2 things: 1. Kernel of your choice 2. Initrd image
Kernel
1) Download the kernel of your choice. using
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.tar.bz2
2) unzip the bz2 image
''tar -jxvf linux-2.6.23.tar.bz2''
3) make sure the following is enabled in kernel configuration
General Setup --->
[* ] Initial RAM filesystem and RAM disk (initramfs/initrd) support
() Initramfs source file(s)
4) just make the kernel. Need not do make install. The bzImage is available in ''linux-2.6.23/arch/i386/boot/bzImage''
Prepare Initrd image
Initrd image is nothing but a small filesystem that contains mini linux system - shell and all supported commands etc. You can put your own programs also into this.
Most lightweight linux systems use busybox as their all-in-one utility program. busybox is a single binary that acts as common programs like ls,pwd,time,date etc. So, download busybox from www.busybox.net and compile it using make menuconfig & make commands. you will find busybox executable in the busybox folder itself. Make sure to configure the busybox to link statically all "applets". Applets are the individual programs.
1) in your work directory, create a directory called initramfs
2) create bin sbin etc proc sys newroot directories under initramfs directory.
''$ mkdir -p initramfs/{bin,sbin,etc,proc,sys,newroot}
$ touch initramfs/etc/mdev.conf ''
3) copy the busybox executable into ''initramfs/bin/''
4) create softlink called sh in bin.
'' $ ln -s busybox initramfs/bin/sh''
5) in initramfs directory create a ''init'' script file. This is the script that is run as pid=1 by Kernel.
~~~~~~~
''#!/bin/sh
#Mount things needed by this script
mount -t proc proc /proc
mount -t sysfs sysfs /sys
#Disable kernel messages from popping onto the screen
echo 0 > /proc/sys/kernel/printk
#Clear the screen
clear
#Create all the symlinks to /bin/busybox
busybox --install -s
#Create device nodes
mknod /dev/null c 1 3
mknod /dev/tty c 5 0
mdev -s
#Function for parsing command line options with "=" in them
# get_opt("init=/sbin/init") will return "/sbin/init"
get_opt() {
echo "$@" | cut -d "=" -f 2
}
#Defaults
init="/sbin/init"
root="/dev/hda1"
#Process command line options
for i in $(cat /proc/cmdline); do
case $i in
root=*)
root=$(get_opt $i)
;;
init=*)
init=$(get_opt $i)
;;
esac
done
#Mount the root device
mount "${root}" /newroot
#Check if $init exists and is executable
if [[[ -x "/newroot/${init}" ]] ; then
#Unmount all other mounts so that the ram used by
#the initramfs can be cleared after switch_root
umount /sys /proc
#Switch to the new root and execute init
exec switch_root /newroot "${init}"
fi
#This will only be run if the exec above failed
echo "Failed to switch_root, dropping to a shell"
exec sh''
~~~~~~~~~
6) create softlinks for mount,echo and clear in initramfs/sbin
''$ln -s initramfs/bin/busybox initramfs/sbin/mount
$ln -s initramfs/bin/busybox initramfs/sbin/echo
$ln -s initramfs/bin/busybox initramfs/sbin/clear''
7) inside initramfs, execute following command to create a cpio archive
''$ cd initramfs
$ find . | cpio -H newc -o > ../initramfs.cpio
$ cd ..
$ cat initramfs.cpio | gzip > initramfs.igz
''
Now, Initrd image is ready.
Testing
I. Using a emulator - QEMU
qemu is a PC emulator. You can download the qemu from internet. Once installed, You can test your images using following command.
''qemu -kernel /boot/bzImage -initrd /boot/initramfs.igz -append "root=/dev/ram" my_iso/rawimage''
II. Booting your PC with newly created images
Copy newly built kernel image and initramfs.igz image into /boot directory
Create new entry in grub/menu.lst
Restart your PC and choose the new kernel to boot with.
The PC should start and provide you a prompt. Now the PC runs completely on RAM. Go ahead explore the system.
In a typical Server/Desktop environment, Initrd contains specific programs that will further load necessary applications even do second stage of booting. In embedded
systems initrd IS the filesystem. Initrd provides simple "container" of your own programs that boot loader iteslf loads as part of bootprocess.
Happy Exploring.
Sunday, March 7, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment