Help answer threads with 0 replies.
Go Back > Blogs > morningboy
User Name


Rate this Entry

Build rootfs for i386/x86

Posted 05-06-2010 at 10:51 PM by morningboy
Updated 05-06-2010 at 11:11 PM by morningboy

Make medium linux for testing and some interests purposes such as:

1. linux that was built is small enough to be inserted into the flash disk or another block device.
2. Can be booted from a laptop, PC, or other x86 standard devices.
3. Software can be installed for testing purposes, such as iptables, ebtables, brctl (bridge controller), vlan, pppoe server / client, pptp server / client, web server, database, nfs, etc..
4. Easy to configure, either to configure libraries or kernels.

We can build this root_fs based on common Distro root_fs structure, for example this root_fs is built based on the Debian distro, with some modifications in the files, in the library or a binary executable or configuration settings etc. in other words, do not used that not important to build root_fs, and put the files the important one.

Make a loop device
Command to create a loop device

* dd if=/dev/zero of=root_fs_i386 bs=1k count=30,000

the point is: create a file with root_fs_i386 contents of /dev/zero (empty) with a block size of 1kb (1kilo Bytes), then copy with the amount of `count * bs BYTES (1kb * 30,000 = 30 MB)

root_fs_i386 files will be generated with file size 30MB (30,720,000 KB)

format root_fs_i386 with reiserfs filesystem:

* mkreiserfs -q -f root_fs_i386

root_fs_i386 mount loop to an empty directory, then copy the contents of root_fs we made into a loop device.

* mount -o loop root_fs_i386 /mnt/disk
* cp -dpR myroot_fs/* /mnt/disk/

Checking whether our root_fs is right with chroot:

* chroot /mnt/disk

After OK, the unmounted

* umount /mnt/disk

Compress root_fs

* gzip root_fs_i386

root_fs_i386.gz is generated

The root_fs that is created here is medium-sized (about 18M inserted into the loop device with 30M size). It is rather large if we call it mini root_fs, so we should say it medium root_fs.

Block Device Interface that we want to be used is the USB Flash Disk and CF (Compact Flash / Block Devices used in the Embedded System). USB FD and CF have limitations, they should not be written too often, because the FD USB and CF has a maximum age of how many times they can be written, so if the age had reached the limit, the CF can not be used again. Therefore, we have to make a system that doesn't write CF too often.

Solution of those problems is fairly easy, we simply load an existing root_fs on CF into the ramdisk. we can make ramdisk_size per block for 30M at booting process with GRUB or through PXE, such as the example below:

* append initrd = root_fs_i386.gz root=/dev/ram0 ramdisk_size=30000


We cannot use a common kernel, we need to configure kernel also, so the kernel can load the root_fs till system login prompt appears.

Fundamental difference of this root_fs is the absence of a kernel module which we usually find in /lib/modules on linux distributions. That means the kernel works must be handled by 1 kernel file (bzImage). That makes the kernel size we used slightly swollen. We also need to include some basic configurations such as filesystems (ext2, ext3, reiserfs, etc.), ethernet, etc into the kernel image.


To configure the kernel itself is a bit confusing. But as mentioned above, we do not have the kernel module, so with this condition, we have to bundle it in one file kernel image.

* To boot, we clearly need a filesystem module so that the system knows the file system in root_fs we use, and then we have to configure the kernel to load our filesystem.
* For iptables capability we need Netfilter, for NAT tables we need CONNTRACK family, so in the KERNEL CONFIG, Netfilter Core section must be loaded.

I already configure the kernel, so it is already support filesystem, iptables, ppp issue.

FORMAT filesystem on a USB or CF FD
We must first determine what file system will be used. Like the example below.
Reiserfs format FileSystem

assume FD USB or CF at /dev/sda1 (check with the command fdisk -l)

* /sbin/mkreiserfs -q -f /dev/sda1

to boot we clearly need a boot loader. We can use GRUB, LILO, or the other according to our wishes. If you use GRUB, follow the example below:
to install grub on a USB FD or CF we can use command below:
we assume CF or USB FD is at /dev/sda1, then we need to mount it first

* mount /dev/sda1 /mnt/disk
/usr/sbin/grub-install --no-floppy --root-directory=/mnt/disk /dev/sdb

then we can see in /mnt/disk/ the "boot" directory.
Open the file "" using a text editor in /mnt/disk/boot/grub directory, then replace the device with the appropriate devices. Example file:

* (hd0) /dev/sda
(hd1) /dev/sdd

Open a text editor using the menu.lst in the /mnt/disk/boot/grub/menu.lst directory ..
About the Contents, adjust it to your conditions:

* timeout 1
title test_root_fs
kernel (hd0, 0)/bzImage
initrd (hd0, 0)/root_fs_i386.gzroot=/dev/ram0 ramdisk_size=30000

Copy kernel and root fs image to CF mount point (/mnt/disk)

* cp root_fs_i386.gz bzImage /mnt/disk

Ready to Rumble ... ..

root_fs image, kernel configuration and can be found at

Example Implementation of this root_fs_i386:
I use it to boot on a VIA board with 4 ethernet ports.
Posted in linux
Views 2179 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 03:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration