Click here to learn
about this Sponsor:
Home  |  News  |  Articles  |  Polls  |  Forum

Keywords: Match:
ELJonline: Building Tiny Linux Systems with Busybox, Part 2: Building the Kernel
Bruce Perens   (January, 2001)

Let Bruce help you put the BusyBox to work.

For this example I use Linux kernel version 2.2.17. The 2.4.0-test8 kernel that I tried did not size the RAM disk for the root file system properly, leading to a "not enough memory" message at boot time. That bug will probably be repaired in the 2.4 series of kernels by the time you read this.

We will build our example to run on an i386-architecture PC-compatible system with PC keyboard and VGA display, booting from a floppy disk and running the root file system entirely in RAM once the system is booted. This example should also boot from IDE disks and from FLASH EEPROM devices that masquerade as IDE disks. It can also be configured to boot from a CD-ROM.

Build a bzImage-style kernel with all of the facilities needed for the application, plus these three:
  • RAM disk support (in the Block Devices menu)
  • Initial RAM disk (initrd) support (also in the Block Devices menu)
  • ROM file system support (in the File Systems menu)
Don't use kernel modules, because this example system doesn't support them. Don't put any facilities in the kernel that you don't need, as they will use up space that you need on the floppy disk. A kernel with the facilities you need should be around half a megabyte in size and should fit easily on a floppy along with the ROM root file system. A kernel with many unnecessary bells and whistles will be a megabyte or more and won't leave sufficient room for your ROM root file system.

If you're not familiar with building and installing kernels on a normal Linux PC, you'll need to study up on that. In short, I placed the kernel sources in /usr/src/linux and ran:


xhost +localhost
su
make xconfig
make dep
make bzImage

This created a compiled Linux kernel in /usr/src/linux/arch/i386/boot/bzImage.

Building a Static-Linked Busybox

In the busybox source directory, edit the Makefile, changing the variable DOSTATIC from false to true. Then run make. That will create a static-linked version of busybox. Confirm that it is static-linked by running this command:


ldd busybox

This should print something like:


statically linked (ELF)

It's important to get this right; if you install a dynamic-linked version of Busybox, your system won't run because we aren't installing the runtime dynamic linker and its libraries on the floppy disk for this example.

Creating a ROM Root File System

We're going to go through all of the steps for creating a minimal root file system by hand so that you will understand just how little is necessary to boot your system rather than copying all of the files from the root of your Linux distribution and then being afraid to remove anything because you don't know whether it's necessary. You will need to become root (the superuser) to perform the following steps because the mknod command requires superuser privilege.

Create the tiny-linux directory and change directory into it:


mkdir tiny-linux
cd tiny-linux

Create the standard directories in it:


mkdir dev etc etc/init.d bin proc mnt tmp var var/shm
chmod 755 . dev etc etc/init.d bin proc mnt tmp var var/shm

Enter the tiny-linux/dev directory:


cd dev

Create the generic terminal devices:


mknod tty c 5 0
mknod console c 5 1
chmod 666 tty console
#Allow anyone to open and write terminals.

Create the virtual terminal device for the VGA display:


mknod tty0 c 4 0
chmod 666 tty0

Create the RAM disk device:


mknod ram0 b 1 0
chmod 600 ram0

Create the null device, used to discard unwanted output:


mknod null c 1 3
chmod 666 null

Change directory to tiny-linux/etc/init.d, where startup scripts are stored:


cd ../etc/init.d

Use an editor to create this shell script in tiny-linux/etc/init.d/rcS. It will be executed when the system boots:


#! /bin/sh
mount -a # Mount the default file systems mentioned in /etc/fstab.

Make the script executable:


chmod 744 rcS

Change directory to tiny-linux/etc:


cd ..

Use an editor to create the file tiny-linux/etc/fstab, which says what file systems should be mounted at boot time:


proc /proc proc defaults 0 0
none /var/shm shm defaults 0 0

Set the mode of tiny-linx/etc/fstab:


chmod 644 fstab

Use an editor to create the file tiny-linux/etc/inittab, which tells /bin/init, the system startup program, what processes to start:


::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh

The above example runs the script /etc/init.d/rcS at boot time and runs an interactive shell on the console device.

Set the modes of tiny-linux/etc/inittab:


chmod 644 inittab

That's everything necessary to create your root file system, except for the installation of the programs. Change directory to tiny-linux/bin:


cd ../bin

Copy your static-linked version of Busybox from wherever you built it into tiny-linux/bin/busybox with a command similar to this one:


cp ~/busybox-0.46/busybox busybox

Add another command name ls to Busybox using the ln command:


ln busybox ls

Run ls, and the result should look like this:


-rwxr-xr-x 2 root root 580424 Sep 12 15:17 busybox
-rwxr-xr-x 2 root root 580424 Sep 12 15:17 ls

Repeat the above ln command for all of these names:


[, ar, basename, cat, chgrp, chmod, chown,
chroot, chvt, clear, cp, cut, date, dc, dd,
deallocvtdf, dirname, dmesg, du, dumpkmap dutmp,
echo, false, fbset, fdflush, find, free,
freeramdisk, fsck.minix, grep, gunzip, gzip, halt,
head, hostid, hostname, id, init, insmod, kill,
killall, length, linuxrc, ln, loadacm, loadfont,
loadkmap, logger, logname, lsmod, makedevs, md5sum,
mkdir, mkfifo, mkfs.minix, mknod, mkswap, mktemp,
more, mount, mt, mv, nc, nslookup, ping, poweroff,
printf, ps, pwd, reboot, rm, rmdir, rmmod, sed,
setkeycodes, sh, sleep, sort, swapoff, swapon, syn, c
syslogd, tail, tar, tee, telnet, test, touch, tri,
true, tty, umount, uname, uniq, update, uptime,
usleep, uudecode, uuencode, wc, which, whoami, yes,
zcat

Are you tired yet? Well, now is a good time to take a break -- you've finished creating your ROM root file system.

Generate the ROM Root File System Image

You'll need the genromfs program to generate the ROM file system image. If you are using Debian or Red Hat, it's already packaged for you as part of the standard system; you'll just need to run the command to install it. If your Linux distribution doesn't include a prepackaged version, at this writing the program can be found at ftp://ibiblio.org/pub/Linux/system/recovery/genromfs-0.3.tar.gz. Install the program, change directory to the directory that contains tiny-linux and run these commands:


genromfs -d tiny-linux -f fs

This creates the ROM file system image in the file fs. Now, compress the file system image using the gzip command:


gzip -9 fs

That will create the file fs.gz, which is about half the size of the uncompressed version.

Build the Floppy

We'll need one more program to build our floppy: syslinux. This is an i386 bootstrap program that will load a kernel and a compressed root file system image from a floppy, hard disk or CD. Again, it's prepackaged with Debian or Red Hat and can be found, at this writing, at ftp://ibiblio.org/pub/Linux/system/boot/loaders/syslinux-1.48.tar.gz.

Create an MS-DOS file system on a floppy by using the Linux mformat command (or by another means). Put the floppy in your drive but don't mount it, and install the syslinux bootstrap with this command:


syslinux /dev/fd0

That will copy a first-stage bootstrap onto the first block of the floppy and a second-stage bootstrap into the file LDLINUX.SYS in the MS-DOS file system of the floppy. I'm assuming you have a directory called /mnt; substitute whatever directory you usually use for mounting floppy disks in the shell commands below. Now, it's time to copy our kernel and root file system onto the floppy:


mount -t msdos /dev/fd0 /mnt
cp fs.gz /mnt
cp /usr/src/linux/arch/i386/boot/bzImage /mnt/linux

Create the configuration file /mnt/syslinux.cfg with an editor:


TIMEOUT 20
DEFAULT linux

LABEL linux
KERNEL linux
APPEND root=/dev/ram0 initrd=fs.gz

This tells syslinux to wait for two seconds and then boot the default system. You can interrupt the default boot during those two seconds by pressing the shift key. Typing linux at the prompt will do the same thing as the default.

The kernel is booted with the arguments root=/dev/ram0 and initrd=fs.gz. These arguments tell the kernel that the root is a RAM disk, and that the RAM disk is loaded from the compressed ROM file system image fs.gz. Although the "ROM" root file system is actually in RAM, it will not be writable because the Linux ROM file system driver used in this example is meant to work with real ROMs and thus doesn't support writing.

The Smoke Test

Configure an i386 PC to boot from the floppy. Note that this is a setting in the BIOS preferences of most new PCs, and they are often configured to boot from the hard disk without first looking for a bootstrap on the floppy. Place the floppy in the first floppy drive, and restart the system. You should see something like the following:


SYSLINUX 1.48 1999-09-26
Copyright (C) 1994-1999 H. Peter Anvin

loading fs.gz.........
loading linux...............
Uncompressing linux, OK, booting the kernel

Tons of cybercrud about every device driver and facility in the kernel race by too rapidly to read...


RAMDISK: Compressed image found at block 0.
VFS: Mounted root (romfs file system)

Please press Enter to activate this console.

You've done it! Press Enter and you should see something like this:


Busybox v0.46 (2000-09-12-22:16+0000) built-in shell
Enter 'help' for a list of built-in commands.

/ # _

Pop out the floppy disk; it's not being used any longer. The root file system is entirely in RAM. You should be able to look around the system and try out commands, but you won't have any writable storage. This is the simplest, bootable-system, running busybox that I could think of, and thus I've left out the files in /dev that you'd need to mount writable RAM disks, floppies and hard disks. You now should be able to figure out what those devices are, add them and build a tiny Linux system specialized to your application.



About the author: Bruce Perens (bruce@perens.com) is president of Linux Captial Group and chairman of Progeny Linux Systems and Known Safe. He was project leader of the Debian Project for two and a half years.



Copyright © 2001 Specialized Systems Consultants, Inc. All rights reserved. Embedded Linux Journal Online is a cooperative project of Embedded Linux Journal and LinuxDevices.com.


(Click here for further information)


FUEL Database on MontaVista Linux
Whether building a mobile handset, a car navigation system, a package tracking device, or a home entertainment console, developers need capable software systems, including an operating system, development tools, and supporting libraries, to gain maximum benefit from their hardware platform and to meet aggressive time-to-market goals.

Breaking New Ground: The Evolution of Linux Clustering
With a platform comprising a complete Linux distribution, enhanced for clustering, and tailored for HPC, Penguin Computing¿s Scyld Software provides the building blocks for organizations from enterprises to workgroups to deploy, manage, and maintain Linux clusters, regardless of their size.

Data Monitoring with NightStar LX
Unlike ordinary debuggers, NightStar LX doesn¿t leave you stranded in the dark. It¿s more than just a debugger, it¿s a whole suite of integrated diagnostic tools designed for time-critical Linux applications to reduce test time, increase productivity and lower costs. You can debug, monitor, analyze and tune with minimal intrusion, so you see real execution behavior. And that¿s positively illuminating.

Virtualizing Service Provider Networks with Vyatta
This paper highlights Vyatta's unique ability to virtualize networking functions using Vyatta's secure routing software in service provider environments.

High Availability Messaging Solution Using AXIGEN, Heartbeat and DRBD
This white paper discusses a high-availability messaging solution relying on the AXIGEN Mail Server, Heartbeat and DRBD. Solution architecture and implementation, as well as benefits of using AXIGEN for this setup are all presented in detail.

Understanding the Financial Benefits of Open Source
Will open source pay off? Open source is becoming standard within enterprises, often because of cost savings. Find out how much of a financial impact it can have on your organization. Get this methodology and calculator now, compliments of JBoss.

Embedded Hardware and OS Technology Empower PC-Based Platforms
The modern embedded computer is the jack of all trades appearing in many forms.

Data Management for Real-Time Distributed Systems
This paper provides an overview of the network-centric computing model, data distribution services, and distributed data management. It then describes how the SkyBoard integration and synchronization service, coupled with an implementation of the OMG¿s Data Distribution Service (DDS) standard, can be used to create an efficient data distribution, storage, and retrieval system.

7 Advantages of D2D Backup
For decades, tape has been the backup medium of choice. But, now, disk-to-disk (D2D) backup is gaining in favor. Learn why you should make the move in this whitepaper.

 


Got a HOT tip?   please tell us!
Free weekly newsletter
Enter your email...
Click here for a profile of each sponsor:
PLATINUM SPONSORS
(Become a sponsor)
GOLD SPONSORS
(Become a sponsor)
(Become a sponsor)

ADVERTISEMENT
(Advertise here)

Check out the latest Linux powered...

Mobile phones!

MIDs, UMPCs
& tablets

Mobile devices

Other cool
gadgets



Resource Library

• Unix, Linux Uptime and Reliability Increase: Patch Management Woes Plague Windows Yankee Group survey finds IBM AIX Unix is highest in ...
• Scalable, Fault-Tolerant NAS for Oracle - The Next Generation For several years NAS has been evolving as a storage ...
• Managing Software Intellectual Property in an Open Source World This whitepaper draws on the experiences of the Black Duck ...
• Open Source Security Myths Dispelled Is it risky to trust mission-critical infrastructure to open source ...
• Bringing IT Operations Management to Open Source & Beyond Download this IDC analyst report to learn how open source ...


BREAKING NEWS

• Superscalar ARM SoC runs Linux
• "Zubuntu" keeps Zaurus spirit alive
• i.MX515 targets Linux netbooks
• Palm "Nova" Linux set for CES debut?
• German Linux integrator launches workshops
• In memorium: Thiemo Seufer
• Browser for Linux devices hits second alpha
• OpenSUSE changes licenses
• "...and I'm Linux" contest nears
• COM Express module sports Atom
• Half-U net appliance runs Linux
• Targeting virtual hardware
• Samsung to ship Android phone in Q2?
• ARM, x86 duel in MID warfare
• Development bounties offered for open source STB


Most popular stories -- past 90 days:
• Linux boots in 2.97 seconds
• Tiniest Linux system, yet?
• Linux powers "cloud" gaming console
• Report: T-Mobile sells out first 1.5 million G1s
• Open set-top box ships
• E17 adapted to Linux devices, demo'd on Treo650
• Android debuts
• First ALP Linux smartphone?
• Cortex-A8 gaming handheld runs Linux
• Ubuntu announces ARM port


DesktopLinux headlines:
• Linux desktop gains kid-friendly browser
• OpenSUSE Community Manager discusses 11.1 release
• "...and I'm Linux" video contest approaches
• OpenSUSE rev's license, build system
• Linux gains fresh "AIR"
• Video-call software boasts HD quality
• Sun rev's "open source" desktop VM manager
• Open source music player rev's up
• Fedora 10 dubbed a "solid" chapeau
• HP preinstalls Linux on SMB desktop


Also visit our sister site:


Sign up for LinuxDevices.com's...

news feed

Home  |  News  |  Articles  |  Polls  |  Forum  |  About  |  Contact
 

Ziff Davis Enterprise Home | Contact Us | Advertise | Link to Us | Reprints | Magazine Subscriptions | Newsletters
Tech RSS Feeds | White Papers | ROI Calculators | Tech Podcasts | Tech Video | VARs | Channel News

Baseline | Careers | Channel Insider | CIO Insight | DesktopLinux | DeviceForge | DevSource | eSeminars |
eWEEK | Enterprise Network Security | LinuxDevices | Linux Watch | Microsoft Watch | Mid-market | Networking | PDF Zone |
Publish | Security IT Hub | Strategic Partner | Web Buyer's Guide | Windows for Devices

Developer Shed | Dev Shed | ASP Free | Dev Articles | Dev Hardware | SEO Chat | Tutorialized | Scripts |
Code Walkers | Web Hosters | Dev Mechanic | Dev Archives | igrep

Use of this site is governed by our Terms of Service and Privacy Policy. Except where otherwise specified, the contents of this site are copyright © 1999-2008 Ziff Davis Enterprise Holdings Inc. All Rights Reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff Davis Enterprise is prohibited. Linux is a registered trademark of Linus Torvalds. All other marks are the property of their respective owners.