Linux init. RAMdisk and root filesystem
Introduction
This page outlines the basics of creating a root filesystem image for use with the OpenRISC Linux kernel port.
The resulting RAMdisk image, and its contained filesystem, are intended to be included in the Linux kernel image and loaded during boot up.
Below is a list of steps to building the image. It is assumed the user is on a system with the ability to run tools such as mount,dd and mke2fs.
For a good overview of what's contained in a root filesystem (and an example of building one for a PC) please see this page.
Creating the image
Allocate some space to a file
First we need to create a file which is sized the same as our RAMdisk image. We'll use the dd command, specifying the number and size of each block in bytes. We'll fill the file with zeros, making any unused area in the image easier to compress later on. The name of the image file we'll make is initrd.ext2.
user@host:$ dd if=/dev/zero of=initrd.ext2 bs=1k count=2048
Here we've made a file with a block size of 1 kilobytes, a total of 2 megabytes (2048*1024 bytes) in size, named initrd.ext2. Why .ext2? This is the format of the filesystem the image will contain.
Format with the ext2 filesystem
This RAMdisk will contain a filesystem that Linux can read at boot time. There are some alternatives, but the most common filesystem used is ext2.
The mke2fs program will format our file, turning it into a valid ext2 image. The options here specify the blocksize (-b), number of inodes (-N), bytes per inode (-i) and amount of reserved space (-m). The option -F forces the creation of a filesystem, because typically mke2fs expects a partition or block device to work on.
user@host:$ mke2fs -b 1024 -N 2048 -i 1024 -m 0 -F initrd.ext2
mke2fs automatically detects the size of the image and adjusts the rest of its parameters itself.
Mount the new image
We will now mount the image to gain access to it.
Create a directory, here we'll use mountdir/ to mount the image on.
user@host:$ mkdir mountdir
user@host:$ sudo mount -o loop initrd.ext2 mountdir
Setup the root filesystem
A number of directories are going to be needed when we're using the filesystem, these will be setup now.
Go into the newly mounted filesystem and do an ls. You should see just a lost+found directory. We will now create a set of basic directories.
Note: all of the commands inside the mounted image require the use of the sudo command as they are usually root user owned files. The required sudo will be omitted from each command.
Create directories
user@host:mountdir$ mkdir bin dev etc home lib mnt proc root sbin tmp usr var
Setup /dev
The /dev path contains a set of special files that provide a way for programs to interact with the kernel via the filesystem. The directory is setup with the use of the mknod command which creates a special file, of a given type and configuration.
We use mknod here to configure some memory and character devices. We also create some symbolic links to the /proc directory.
user@host:mountdir/dev$ mknod ttyS0 c 4 64
user@host:mountdir/dev$ mknod console c 5 1
user@host:mountdir/dev$ mknod tty c 5 0
user@host:mountdir/dev$ mknod ram0 b 1 0
user@host:mountdir/dev$ mknod ram1 b 1 1
user@host:mountdir/dev$ mknod mem c 1 1
user@host:mountdir/dev$ mknod kmem c 1 2
user@host:mountdir/dev$ mknod null c 1 3
user@host:mountdir/dev$ mknod zero c 1 5
user@host:mountdir/dev$ mknod fb0 c 29 0
user@host:mountdir/dev$ mknod loop0 b 7 0
user@host:mountdir/dev$ ln -s fb0 fb
user@host:mountdir/dev$ ln -s ram1 ram
user@host:mountdir/dev$ ln -s ram0 ramdisk
user@host:mountdir/dev$ ln -s ../proc/self/fd/2 stderr
user@host:mountdir/dev$ ln -s ../proc/self/fd/0 stdin
user@host:mountdir/dev$ ln -s ../proc/self/fd/1 stdout
user@host:mountdir/dev$ ln -s ../proc/kcore kcore
user@host:mountdir/dev$ chmod a+rwx *
The last command sets the appropriate privileges for these devices (some things cannot be set, ignore the errors.)
Setup /etc
There are some basic files required when the kernel and BusyBox are initialising.
Put the following into the file /etc/fstab
proc /proc proc defaults 0 0
/dev/ramdisk / ext2 defaults 0 0
Put the following into the file /etc/inittab
::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
# Stuff to do when restarting the init process
::restart:/sbin/init
# Stuff to do before rebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/etc/shutdown
Create the /etc/init.d directory and put the following into the file /etc/init.d/rcS
#! /bin/sh
mount -a
# Remount / as writable
mount -o remount,rw /
#Any other things to do at boot go in here
Miscellaneous directories and permissions
It's a good idea to make the /tmp directory writable by anyone.
user@host:mountdir$ chmod a+rw tmp
Some applications may also need the paths /var/log, /var/run, and /var/tmp to exist.
user@host:mountdir$ cd var
user@host:mountdir/var$ mkdir log
user@host:mountdir/var$ mkdir run
user@host:mountdir/var$ mkdir tmp
user@host:mountdir/var$ chmod a+rwx tmp
Unmount the image
user@host:mountdir$ cd ..
user@host:$ sudo umount mountdir
The initrd.ext2 image is now hopefully configured and ready to have BusyBox installed in it.
Note that this is just the basics and still lacks most of what is required for other features like networking.
See the OpenRISC project's Linux and BusyBox setup guide for information on building and installing BusyBox, and compiling this image into the Linux kernel image.
Developer(s)
The team working on Linux port and hardware verification:
Top