Linux Pocket Linux Guide User manual

Pocket Linux Guide
David Horton
<dhorton<AT>NOSPAM.member.fsf.org>
Revision History
Revision 3.1 2005−04−09 Revised by: DH
Minor clarifications and spelling corrections.
Revision 3.0 2004−11−02 Revised by: DH
Changed bootloader to GRUB rather than LILO. Updated versions on all source code packages. Made minor
clarifications to some shell commands and scripts.
Revision 2.1 2004−02−18 Revised by: DH
Corrected typos. Changed resource site hosting to SourceForge. Added appendix B to include the GNU Free
Documentation License as part of this document.
Revision 2.0 2003−11−08 Revised by: DH
Updated to use GNU coreutils in place of fileutils, sh−utils and textutils. Updated version numbers on many
source code packages. Introduced Freshmeat as a resource for finding source code. Changed /etc/mtab to a
real file rather than using a symlink to /proc/mounts. Corrected local_fs script errors. Updated email address.
Revision 1.2 2003−05−31 Revised by: DH
Corrected errors in "strip −o library" commands.
Revision 1.1 2003−05−21 Revised by: DH
Bug fixes, typo corrections and improved XML markup.
Revision 1.0 2003−02−17 Revised by: DH
Initial Release, reviewed by LDP.
The Pocket Linux Guide is for anyone interested in learning the techniques of building a GNU/Linux system
from source code. The guide is structured as a project that builds a small diskette−based GNU/Linux system
called Pocket Linux. Each chapter explores a small piece of the overall system explaining how it works, why
it is needed and how to build it. After completing the Pocket Linux project, readers should possess an
enhanced knowledge of what makes GNU/Linux systems work as well as the confidence to explore larger,
more complex source−code−only projects.

Table of Contents
Legal Information...............................................................................................................................................1
1. Copyright and License.........................................................................................................................1
2. Disclaimer............................................................................................................................................1
Introduction.........................................................................................................................................................2
1. About Pocket Linux.............................................................................................................................2
2. Prerequisite Skills................................................................................................................................2
3. Project Format......................................................................................................................................2
4. Help & Support....................................................................................................................................2
5. Feedback..............................................................................................................................................3
Chapter 1. Project Initiation..............................................................................................................................4
1.1. A Brief History of GNU/Linux.........................................................................................................4
1.2. The Goal of Pocket Linux.................................................................................................................4
1.3. Working Within The Constraints......................................................................................................4
Chapter 2. A Simple Prototype..........................................................................................................................6
2.1. Analysis............................................................................................................................................6
2.2. Design...............................................................................................................................................6
2.2.1. Simplification..........................................................................................................................6
2.2.2. Boot Disk.................................................................................................................................6
2.2.3. Root Disk.................................................................................................................................7
2.2.4. CPU Compatibility..................................................................................................................7
2.3. Construction......................................................................................................................................7
2.3.1. Prepare the boot disk media....................................................................................................7
2.3.2. Build the GRUB bootloader....................................................................................................7
2.3.3. Copy the bootloader files to diskette.......................................................................................8
2.3.4. Finish bootloader installation..................................................................................................8
2.3.5. Build the Linux kernel.............................................................................................................8
2.3.6. Copy the kernel to diskette......................................................................................................9
2.3.7. Unmount the boot disk............................................................................................................9
2.3.8. Prepare the root disk media.....................................................................................................9
2.3.9. Build BASH............................................................................................................................9
2.3.10. Copy BASH to the root disk..................................................................................................9
2.3.11. Create device files that BASH needs....................................................................................9
2.3.12. Unmount the root disk...........................................................................................................9
2.4. Implementation...............................................................................................................................10
2.4.1. System startup.......................................................................................................................10
2.4.2. Testing what works...............................................................................................................10
2.4.3. Noting what does not work...................................................................................................10
2.4.4. System shutdown...................................................................................................................11
Chapter 3. Saving Space...................................................................................................................................12
3.1. Analysis..........................................................................................................................................12
3.2. Design.............................................................................................................................................12
3.2.1. Shared Libraries....................................................................................................................12
3.2.2. Stripped Binaries...................................................................................................................12
3.2.3. Compressed Root Filesystem................................................................................................12
Pocket Linux Guide
i

Table of Contents
Chapter 3. Saving Space
3.3. Construction....................................................................................................................................13
3.3.1. Create a ramdisk....................................................................................................................13
3.3.2. Rebuild the BASH shell........................................................................................................13
3.3.3. Determine which libraries are required.................................................................................13
3.3.4. Copy BASH and its libraries to the ramdisk.........................................................................13
3.3.5. Create a console device.........................................................................................................14
3.3.6. Compress the ramdisk image................................................................................................14
3.3.7. Copy the compressed image to diskette................................................................................14
3.4. Implementation...............................................................................................................................14
3.4.1. System startup.......................................................................................................................14
3.4.2. Verify results.........................................................................................................................15
3.4.3. System shutdown...................................................................................................................15
Chapter 4. Some Basic Utilities........................................................................................................................16
4.1. Analysis..........................................................................................................................................16
4.2. Design.............................................................................................................................................16
4.2.1. Determining Required Commands........................................................................................16
4.2.2. Locating Source Code...........................................................................................................16
4.2.3. Leveraging FHS....................................................................................................................16
4.2.4. Downloading Source Code....................................................................................................17
4.3. Construction....................................................................................................................................17
4.3.1. Create a staging area..............................................................................................................17
4.3.2. Copy contents of phase 2 rootdisk........................................................................................17
4.3.3. Install binaries from GNU coreutils......................................................................................18
4.3.4. Copy additional libraries.......................................................................................................18
4.3.5. Strip binaries and libraries.....................................................................................................18
4.3.6. Create a compressed root disk image....................................................................................18
4.3.7. Write the root disk image to floppy.......................................................................................18
4.4. Implementation...............................................................................................................................19
4.4.1. System startup.......................................................................................................................19
4.4.2. Testing new commands.........................................................................................................19
4.4.3. System shutdown...................................................................................................................20
Chapter 5. Checking and Mounting Disks.....................................................................................................21
5.1. Analysis..........................................................................................................................................21
5.2. Design.............................................................................................................................................21
5.2.1. Determining necessary utilities.............................................................................................21
5.2.2. Finding source code...............................................................................................................21
5.2.3. Automating fsck and mount..................................................................................................22
5.2.4. File dependencies..................................................................................................................22
5.3. Construction....................................................................................................................................23
5.3.1. Install utilities from e2fsprogs...............................................................................................23
5.3.2. Install utilities from util−linux..............................................................................................23
5.3.3. Check library requirements...................................................................................................24
5.3.4. Strip binaries to save space...................................................................................................24
5.3.5. Create additional device files................................................................................................24
5.3.6. Create the fstab and mtab files..............................................................................................24
Pocket Linux Guide
ii

Table of Contents
Chapter 5. Checking and Mounting Disks
5.3.7. Write a script to check and mount local filesystems.............................................................24
5.3.8. Create a compressed root disk image....................................................................................25
5.3.9. Write the root disk image to floppy.......................................................................................25
5.4. Implementation...............................................................................................................................25
5.4.1. System startup.......................................................................................................................25
5.4.2. Test the local_fs script...........................................................................................................26
5.4.3. Create and mount additional filesystems...............................................................................26
5.4.4. System shutdown...................................................................................................................26
Chapter 6. Automating Startup & Shutdown................................................................................................28
6.1. Analysis..........................................................................................................................................28
6.2. Design.............................................................................................................................................28
6.2.1. Determining necessary utilities.............................................................................................28
6.2.2. Obtaining source code...........................................................................................................28
6.2.3. Checking dependencies.........................................................................................................29
6.2.4. Designing a simple GRUB configuration file.......................................................................29
6.2.5. Outlining start−up scripts......................................................................................................29
6.3. Construction....................................................................................................................................30
6.3.1. Create a GRUB configuration file.........................................................................................30
6.3.2. Install sysvinit utilities...........................................................................................................30
6.3.3. Create /etc/inittab file............................................................................................................30
6.3.4. Create /etc/init.d/rc script......................................................................................................31
6.3.5. Modify /etc/init.d/local_fs script...........................................................................................31
6.3.6. Create a hostname script........................................................................................................32
6.3.7. Create halt & reboot scripts...................................................................................................32
6.3.8. Create rcN.d directories and links.........................................................................................33
6.3.9. Create the root disk image.....................................................................................................33
6.3.10. Copy the image to diskette..................................................................................................33
6.4. Implementation...............................................................................................................................33
6.4.1. System Startup.......................................................................................................................33
6.4.2. Verify success of startup scripts............................................................................................34
6.4.3. System shutdown...................................................................................................................34
Chapter 7. Enabling Multiple Users................................................................................................................35
7.1. Analysis..........................................................................................................................................35
7.2. Design.............................................................................................................................................35
7.2.1. The login process...................................................................................................................35
7.2.2. Obtaining source code...........................................................................................................35
7.2.3. Creating support files............................................................................................................35
7.2.4. Dependencies.........................................................................................................................36
7.2.5. Assigning ownership and permissions..................................................................................36
7.3. Construction....................................................................................................................................37
7.3.1. Verify presence of getty and login........................................................................................37
7.3.2. Modify inittab for multi−user mode......................................................................................37
7.3.3. Create tty devices..................................................................................................................38
7.3.4. Create support files in /etc.....................................................................................................38
7.3.5. Copy required libraries..........................................................................................................39
Pocket Linux Guide
iii

Table of Contents
Chapter 7. Enabling Multiple Users
7.3.6. Set directory and file permissions.........................................................................................39
7.3.7. Create the root disk image.....................................................................................................40
7.3.8. Copy the image to diskette....................................................................................................40
7.4. Implementation...............................................................................................................................40
7.4.1. System Startup.......................................................................................................................40
7.4.2. Add a new user to the system................................................................................................40
7.4.3. Test the new user's ability to use the system.........................................................................40
7.4.4. System shutdown...................................................................................................................41
Chapter 8. Filling in the Gaps..........................................................................................................................42
8.1. Analysis..........................................................................................................................................42
8.2. Design.............................................................................................................................................42
8.2.1. more.......................................................................................................................................42
8.2.2. More device files...................................................................................................................43
8.2.3. ps, sed & ed...........................................................................................................................43
8.3. Construction....................................................................................................................................43
8.3.1. Write a "more" script.............................................................................................................43
8.3.2. Create additional device files................................................................................................44
8.3.3. Install ps................................................................................................................................44
8.3.4. Install sed...............................................................................................................................44
8.3.5. Install ed................................................................................................................................45
8.3.6. Strip binaries to save space...................................................................................................45
8.3.7. Ensure proper permissions....................................................................................................45
8.3.8. Create the root disk image.....................................................................................................45
8.3.9. Copy the image to diskette....................................................................................................45
8.4. Implementation...............................................................................................................................45
8.4.1. System startup.......................................................................................................................45
8.4.2. Test the "more" script............................................................................................................45
8.4.3. Use ps to show running processes.........................................................................................46
8.4.4. Run a simple sed script..........................................................................................................46
8.4.5. Test the "ed" editor................................................................................................................46
8.4.6. System shutdown...................................................................................................................46
Chapter 9. Project Wrap Up............................................................................................................................47
9.1. Celebrating Accomplishments........................................................................................................47
9.2. Planning Next Steps........................................................................................................................47
Appendix A. Hosting Applications..................................................................................................................48
A.1. Analysis..........................................................................................................................................48
A.2. Design............................................................................................................................................48
A.2.1. Support for audio hardware..................................................................................................48
A.2.2. Creating space for the program............................................................................................49
A.2.3. Accessing audio files............................................................................................................49
A.2.4. Other required files...............................................................................................................50
A.2.5. Summary of tasks.................................................................................................................50
A.3. Construction...................................................................................................................................50
A.3.1. Create an enhanced boot disk...............................................................................................51
Pocket Linux Guide
iv

Table of Contents
Appendix A. Hosting Applications
A.3.2. Create an enhanced root disk................................................................................................51
A.3.3. Create a compressed /usr disk for mp3blaster......................................................................53
A.3.4. Create a data diskette for testing..........................................................................................54
A.4. Implementation..............................................................................................................................54
A.4.1. System Startup......................................................................................................................54
A.4.2. Verify that the /usr diskette loaded properly........................................................................54
A.4.3. Check the audio device initialization...................................................................................54
A.4.4. Test audio output..................................................................................................................54
A.4.5. Play a sample file..................................................................................................................54
A.4.6. System shutdown..................................................................................................................55
Appendix B. GNU Free Documentation License...........................................................................................56
B.1. PREAMBLE..................................................................................................................................56
B.2. APPLICABILITY AND DEFINITIONS.......................................................................................56
B.3. VERBATIM COPYING................................................................................................................57
B.4. COPYING IN QUANTITY...........................................................................................................58
B.5. MODIFICATIONS........................................................................................................................58
B.6. COMBINING DOCUMENTS.......................................................................................................59
B.7. COLLECTIONS OF DOCUMENTS............................................................................................60
B.8. AGGREGATION WITH INDEPENDENT WORKS...................................................................60
B.9. TRANSLATION............................................................................................................................60
B.10. TERMINATION..........................................................................................................................61
B.11. FUTURE REVISIONS OF THIS LICENSE...............................................................................61
B.12. ADDENDUM: How to use this License for your documents......................................................61
Pocket Linux Guide
v

Legal Information
1. Copyright and License
This document, Pocket Linux Guide, is copyright (c) 2003 − 2005 by David Horton. 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, with
no Front−Cover Texts, and with no Back−Cover Texts. A copy of the license is available at the end of this
document.
Linux is a registered trademark of Linus Torvalds.
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.
Legal Information 1

Introduction
1. About Pocket Linux
The Pocket Linux Guide demonstrates how to build a small console−based GNU/Linux system using only
source code and a couple of diskettes. It is intended for Linux users who would like to gain a deeper
understanding about how their system works beneath the shroud of distribution specific features and tools.
2. Prerequisite Skills
This guide is intended for intermediate to advanced Linux users. It is not intentionally obscure, but certain
assumptions about the readers skill level are made. Success with this guide depends in part on being able to
perform the following tasks:
Use basic shell commands• Reference man and info pages• Build a custom Linux kernel• Compile source code using make and related tools•
3. Project Format
The Pocket Linux Guide takes a hands−on approach to learning. The guide is written with each chapter
building a piece of an overall project. Chapters are further broken into sections of Analysis, Design,
Construction and Implementation. This format is derived from Rapid Application Development (RAD)
methodology. Without going into detail about design methodologies, the sections may be summed up as
follows.
The Analysis section gives a high−level overview of what is to be accomplished in each chapter. It
will introduce the tasks that need to be completed and why they are important to the overall system.
•
The Design section defines the source code packages, files and configuration necessary to address the
requirements set forth in the Analysis section. Much of the theory of why certain system files exist
and what their purpose is can be found here.
•
The Construction section is where all the hands−on action takes place. This section goes into detail
about building source code and configuring the system files.
•
The Implementation section will test the proper operation of the project at the end of each chapter.
Often there are a few shell commands to perform and samples of expected screen outputs are given.
•
Readers interested in learning more about RAD may want to consult a textbook covering systems analysis and
design or visit the following University of California, Davis website on the subject:
http://sysdev.ucdavis.edu/WEBADM/document/rad−stages.htm.
4. Help & Support
Readers are encouraged to visit the Pocket Linux Resource Site at http://pocket−linux.sourceforge.net/. The
resource site is home to:
Information about the Pocket Linux mailing list.•
Introduction 2

A web−based troubleshooting forum where readers can ask questions and give tips to others.• A collection of diskette images for various chapters.• Additional projects that may be of interest to Pocket Linux Guide readers.•
5. Feedback
For technical questions about Pocket Linux please use the mailing list or the troubleshooting forum on the
resource site. General comments and suggestions may be sent to the mailing list or emailed to the author
directly.
Pocket Linux Guide
Introduction 3

Chapter 1. Project Initiation
1.1. A Brief History of GNU/Linux
In the early 90's GNU/Linux systems consisted of little more than a beta−quality Linux kernel and a small
collection of software ported from the GNU project. It was a true hacker's operating system. There were no
CD−ROM's or GUI installation tools; everything had to be compiled and configured by the end user. Being a
Linux Expert meant knowing your system inside and out.
Toward the middle of the decade several GNU/Linux distributions began appearing. One of the first was
Slackware in 1993 and since then there have been many others. Even though there are many "flavors" of
Linux today, the main purpose of the distribution remains the same. The distribution automates many of the
tasks involved in GNU/Linux installation and configuration taking the burden off of the system administrator.
Being a Linux Expert now means knowing which button to click in the GUI administration tool.
Recently there has been a yearn for a return to the "good old days" of Linux when men were men, sysadmins
were hardcore geeks and everything was compiled from source code. A notable indication of this movement
was the publication of the Linux−From−Scratch−HOWTO version 1.0 by Gerard Beekmans in 1999. Being a
Linux Expert once again means knowing how to do it yourself.
For more historical information, see Ragib Hasan's "History of Linux" at http://netfiles.uiuc.edu/rhasan/linux
1.2. The Goal of Pocket Linux
The purpose of Pocket Linux is to support and encourage people who wish to explore Linux by building a
GNU/Linux system from nothing but source code. Pocket Linux is not intended to be a full featured system,
but rather to give the reader a taste of what is involved in building an operating system from source code.
After completing the Pocket Linux system the reader should have enough knowledge to confidently build
almost any project using only source code. Given this direction we can put a few constraints on the project.
The main focus should be learning. The project should not just describe how to do something, it
should also describe why it should be done.
•
The required time commitment should be minimal and manageable.• The project should not require any investment in additional hardware or reconfiguration of existing
hardware to set up a lab environment.
•
Readers should not need to know any programming languages in order to complete the project.• To remain true to the spirit of GNU/Linux, all software used in the project should be covered under
the GNU/GPL or another, similarly liberal, open−source license.
•
1.3. Working Within The Constraints
The Pocket Linux project gets its name from the fact that the bulk of the project fits onto two diskettes making
it possible to carry the entire, working system around in one's pocket. This has the advantage of not requiring
any additional hardware since any PC can be booted from the diskettes without disrupting any OS that exists
on the hard drive. Using diskettes also partially addresses the aspect of time commitment, because the project
size and complexity is necessarily limited by the 1.44 Megabyte size of the installation media.
Chapter 1. Project Initiation 4

To further reduce the time commitment, the Pocket Linux project is divided into several phases, each one
chapter in length. Each phase builds only a small piece of the overall project, but at the same time the
conclusion of each chapter results in a self−contained, working system. This step−by−step approach should
allow readers to pace themselves and not feel the need to rush to see results.
Chapters are further subdivided into four sections. The first two sections, analysis and design, focus on the
theory of what is to be accomplished in each phase and why. The last two sections, construction and
implementation, detail the steps needed to do the actual building. Advanced readers, who may be familiar
with the theories laid out in a particular chapter are encouraged to gloss over the analysis and design sections
in the interest of time. The separation of theory from hands−on exercises should allow readers of all skill
levels to complete the project without feeling either completely lost or mired in too much detail.
Finally, the Pocket Linux project will strive to use GNU/GPL software when possible and other open−source
licensed software when there is no GNU/GPL alternative. Also, Pocket Linux will never require any
programming more complex than a BASH shell script.
Pocket Linux Guide
Chapter 1. Project Initiation 5

Chapter 2. A Simple Prototype
2.1. Analysis
Since this is the first phase of the project it will be kept very simple. The goal here is not to create the ultimate
GNU/Linux system on the first try. Instead, we will be building a very minimal, working system to be used as
a building block in subsequent phases of the project. Keeping this in mind, we can list a few goals for phase
one.
Keep it simple to avoid stressing out.• Build something that works for instant gratification.• Make something that it is useful in later phases of the project.•
2.2. Design
2.2.1. Simplification
Take a moment to skim through the Bootdisk−HOWTO or the
From−PowerUp−to−BASH−Prompt−HOWTO. These HOWTO documents can be found online at
http://www.tldp.org/docs.html#howto. Both documents offer an excellent view of what it takes to get a
GNU/Linux system up and running. There is also a lot of information to digest. Remember that one of our
goals is, "keep it simple to avoid stressing out," so we want to ignore everything but the absolutely critical
pieces of a boot / root diskset.
Basically it boils down to the following required items:
A boot loader• The Linux kernel• A shell• Some /dev files•
We don't even need an init daemon. The kernel can be told to run the shell directly by passing it an option
through the boot loader.
For easy construction we will build a two−disk boot / root set rather than trying to get everything onto a single
diskette. The boot loader and kernel will go on the boot disk and the shell will reside on the root disk.
2.2.2. Boot Disk
For the boot disk we simply need to install the GRUB bootloader and a Linux kernel. We will need to use a
kernel that does not require modules for the hardware we need to access. Mainly, it should have compiled−in
support for the floppy drive, ram disk, second extended filesystem, proc filesystem, ELF binaries, and a
text−based console. If such a kernel is not available, it will need to be built from source code. Kwan Lowe's
Kernel Rebuild Guide is a good reference for this task, however we can ignore the sections that deal with
modules and the initial ramdisk.
Chapter 2. A Simple Prototype 6

2.2.3. Root Disk
For the root disk we will need a floppy that has been prepared with a filesystem. We will also need a BASH
shell that is statically−linked so we can avoid the additional complexities of shared libraries. The configure
program in the BASH source code recognizes the −−enable−static−link option for this feature. We
will also be using the −−enable−minimal−config option to keep the BASH binary down to a
manageable size. Additional requirements for the root disk are a /dev directory and a device file for the
console. The console device is required for BASH to be able to communicate with the keyboard and video
display.
2.2.4. CPU Compatibility
There is one other, less obvious requirement to keep in mind and that is CPU compatibility. Each generation
of CPU features a more complex architecture than its predecessor. Late generation chips have additional
registers and instructions when compared to an older 486 or 386. So a kernel optimized for a new, fast 6x86
machine will not run on an older box. (See the README file in the Linux kernel source code for details.) A
BASH shell built for a 6x86 will probably not run on an older processor either. To avoid this problem, we can
choose the 386 as a lowest common denominator CPU and build all the code for that architecture.
2.3. Construction
In this section, we will be building the actual boot disk and root disk floppies. Lines preceded by bash#
indicate a shell command and lines starting with grub> indicate a command typed within the grub shell.
2.3.1. Prepare the boot disk media
Insert a blank diskette labeled "boot disk".
It may be necessary to erase the "blank" diskette if it comes factory pre−formatted for another,
non−Linux operating system. This can be done using the command dd if=/dev/zero of=/dev/fd0 bs=1k
count=1440
bash# mke2fs −m0 /dev/fd0
bash# mount /dev/fd0 /mnt
2.3.2. Build the GRUB bootloader
Get the GRUB source code from ftp://alpha.gnu.org/gnu/grub/ and unpack it into the /usr/src directory.
Configure and build the GRUB source code for an i386 processor by using the following commands:
bash# cd /usr/src/grub−0.95
bash# export CC="gcc −mcpu=i386"
bash# ./configure −−host=i386−pc−linux−gnu −−without−curses
bash# make
Pocket Linux Guide
Chapter 2. A Simple Prototype 7

2.3.3. Copy the bootloader files to diskette
Normally, after compiling source code, one would use the command make install to copy the finished files to
their proper destinations in the filesystem. However, using make install does not work well with small media
like the floppy disks we are using. The problem is that there are many files in a package besides the actual
binaries that get the job done. For example, there are often man or info pages that provide documentation.
These extra files can take up more space than we can spare on the diskette. We can work around this
limitation by copying essential files manually rather than using make install.
For GRUB to boot we will need to copy the stage1 and stage2 bootloader files to the /boot/grub directory
on the boot floppy.
bash# mkdir −p /mnt/boot/grub
bash# cp /usr/src/grub−0.95/stage1/stage1 /mnt/boot/grub
bash# cp /usr/src/grub−0.95/stage2/stage2 /mnt/boot/grub
2.3.4. Finish bootloader installation
Once the bootloader's files are copied to the boot disk we can enter the grub shell to finish the installation.
bash# /usr/src/grub−0.95/grub/grub
grub> root (fd0)
grub> setup (fd0)
grub> quit
2.3.5. Build the Linux kernel
The steps for building the kernel were tested using Linux kernel version 2.4.26 and should work any 2.4.x or
2.6.x kernel. The latest version of the kernel source code may be downloaded from http://www.kernel.org/ or
one of its mirrors.
The instructions below are very brief and are intended for someone who has previous experience
building custom kernels. A more detailed explanation of the kernel building process can be found in the
Kernel Rebuild Guide by Kwan Lowe.
bash# cd /usr/src/linux
bash# make menuconfig
Be sure to configure support for the following:
386 processor• Console on virtual terminal (2.4.x kernels only)• ELF binaries• Floppy disk• proc filesystem• RAM disk with a default size of 4096K• Second extended (ext2) filesystem• VGA console•
bash# make dep
bash# make clean
bash# make bzImage
Pocket Linux Guide
Chapter 2. A Simple Prototype 8

2.3.6. Copy the kernel to diskette
bash# cp /usr/src/linux/arch/i386/boot/bzImage /mnt/boot/vmlinuz
2.3.7. Unmount the boot disk
bash# cd /
bash# umount /mnt
2.3.8. Prepare the root disk media
Insert a blank diskette labeled "root disk".
bash# mke2fs −m0 /dev/fd0
bash# mount /dev/fd0 /mnt
2.3.9. Build BASH
Get the bash−3.0 source code package from ftp://ftp.gnu.org/gnu/bash/ and untar it into the /usr/src
directory.
Build BASH for an i386 CPU with the following commands:
bash# cd /usr/src/bash−3.0
bash# export CC="gcc −mcpu=i386"
bash# ./configure −−enable−static−link \
−−enable−minimal−config −−host=i386−pc−linux−gnu
bash# make
bash# strip bash
2.3.10. Copy BASH to the root disk
bash# mkdir /mnt/bin
bash# cp bash /mnt/bin/bash
bash# ln −s bash /mnt/bin/sh
2.3.11. Create device files that BASH needs
bash# mkdir /mnt/dev
bash# mknod /mnt/dev/console c 5 1
2.3.12. Unmount the root disk
bash# cd /
bash# umount /mnt
Pocket Linux Guide
Chapter 2. A Simple Prototype 9

2.4. Implementation
2.4.1. System startup
Follow these steps to boot the system:
Restart the PC with the boot disk in the floppy drive.• When the grub> prompt appears, type kernel (fd0)/boot/vmlinuz init=/bin/sh
root=/dev/fd0 load_ramdisk=1 prompt_ramdisk=1 and press Enter.
•
After the kernel loads, type boot and press Enter.• Insert the root disk when prompted.•
If all goes well the screen should look something like the example shown below.
GNU GRUB version 0.95
grub> kernel (fd0)/boot/vmlinuz init=/bin/sh root=/dev/fd0 load_ramdisk=1 prompt_ramdisk=1
[Linux−bzImage, setup=0xc00, size=0xce29b]
grub> boot
Linux version 2.4.26
..
.. [various kernel messages]
..
VFS: Insert root floppy disk to be loaded into RAM disk and press ENTER
RAMDISK: ext2 filesystem found at block 0
RAMDISK: Loading 1440 blocks [1 disk] into ram disk... done.
VFS: Mounted root (ext2 filesystem) readonly.
Freeing unused kernel memory: 178k freed
# _
2.4.2. Testing what works
Try out a few of BASH's built−in commands to see if things are working properly.
bash# echo "Hello World"
bash# cd /
bash# pwd
bash# echo *
2.4.3. Noting what does not work
Try out a few other familiar commands.
bash# ls /var
bash# mkdir /var/tmp
Notice that only commands internal to BASH actually work and that external commands like ls and mkdir do
not work at all. This shortcoming is something that can be addressed in a future phase of the project. For now
we should just enjoy the fact that our prototype boot / root diskset works and that it was not all that hard to
build.
Pocket Linux Guide
Chapter 2. A Simple Prototype 10

2.4.4. System shutdown
Remove the diskette from fd0 and restart the system using CTRL−ALT−DELETE.
Pocket Linux Guide
Chapter 2. A Simple Prototype 11

Chapter 3. Saving Space
3.1. Analysis
One of the drawbacks in the prototype phase of the project was that the diskset was not all that useful. The
only commands that worked were the ones built into the BASH shell. We could improve our root disk by
installing commands like cat,ls,mv,rm and so on. Unfortunately, we are short on space. The current root
disk has no shared libraries so each utility would have to be statically−linked just like the BASH shell. A lot
of big binaries together with a static shell will rapidly exceed the tiny 1.44M of available disk space. So our
main goal in this phase should be to maximize space savings on the root disk and pave the way for expanded
functionality in the next phase.
3.2. Design
Take another look at the Bootdisk−HOWTO and notice how many utilities can be squeezed onto a 1.44M
floppy. There are three things that make this possible. One is the use of shared libraries. The second is
stripped binaries. And the third is the use of a compressed filesystem. We can use all of these techniques to
save space on our root disk.
3.2.1. Shared Libraries
First, in order to use shared libraries we will need to rebuild the BASH shell. This time we will configure it
without using the −−enable−static−link option. Once BASH is rebuilt we need to figure out which
libraries it is linked with and be sure to include them on the root disk. The ldd command makes this job easy.
By typing ldd bash on the command−line we can see a list of all the shared libraries that BASH uses. As long
as all these libraries are copied to the root disk, the new BASH build should work fine.
3.2.2. Stripped Binaries
Next, we should strip any binaries that get copied to the root disk. The manpage for strip does not give much
description of what it does other than to say, "strip discards all symbols from the object files." It seems like
removing pieces of a binary would render it useless, but this is not the case. The reason it works is because a
large number of these discarded symbols are used for debugging. While debugging symbols are very helpful
to programmers working to improve the code, they do not do much for the average end−user other than take
up more disk space. And since space is at a premium, we should definitely remove as many symbols as
possible from BASH and any other binaries before we copy over them to the ramdisk.
The process of stripping files to save space also works with shared library files. But when stripping libraries it
is important to use the −−strip−unneeded option so as not to break them. Using −−strip−unneeded
shrinks the file size, but leaves the symbols needed for relocation intact which is something that shared
libraries need to function properly.
3.2.3. Compressed Root Filesystem
Finally, we can tackle the problem of how to build a compressed root filesystem. The Bootdisk−HOWTO
suggests three ways of constructing a compressed root filesystem using either a ramdisk, a spare hard drive
partition or a loopback device. This project will concentrate on using the ramdisk approach. It seems logical
Chapter 3. Saving Space 12

that if the root filesystem is going to be run from a ramdisk, it may as well be built on a ramdisk. All we have
to do is create a second extended filesystem on a ramdisk device, mount it and copy files to it. Once the
filesystem is populated with all the files that the root disk needs, we simply unmount it, compress it and write
it out to floppy.
For this to work, we need to make sure the system used for building has ramdisk support. If ramdisk is
not available it is also possible to use a loopback device. See the Bootdisk−HOWTO for more
information on using loopback devices.
3.3. Construction
This section is written using ramdisk seven (/dev/ram7) to build the root image. There is nothing
particularly special about ramdisk seven and it is possible to use any of the other available ramdisks provided
they are not already in use.
3.3.1. Create a ramdisk
bash# dd if=/dev/zero of=/dev/ram7 bs=1k count=4096
bash# mke2fs −m0 /dev/ram7 4096
bash# mount /dev/ram7 /mnt
3.3.2. Rebuild the BASH shell
bash# cd /usr/src/bash−3.0
bash# make distclean
bash# export CC="gcc −mcpu=i386"
bash# ./configure −−enable−minimal−config −−host=i386−pc−linux−gnu
bash# make
bash# strip bash
3.3.3. Determine which libraries are required
bash# ldd bash
View the output from the ldd command. It should look similar to the example below.
bash# ldd bash
libdl.so.2 => /lib/libdl.so.2 (0x4001d000)
libc.so.6 => /lib/libc.so.6 (0x40020000)
/lib/ld−linux.so.2 => /lib/ld−linux.so.2 (0x40000000)
Some systems may have a slightly different library set up. For example, you may see libc.so.6 =>
/lib/tls/libc.so.6 rather than libc.so.6 => /lib/libc.so.6 as shown in the example.
If your ldd output does not match the example then use the path given by your ldd command when
completing the next step.
3.3.4. Copy BASH and its libraries to the ramdisk
bash# mkdir /mnt/bin
bash# cp bash /mnt/bin
bash# ln −s bash /mnt/bin/sh
Pocket Linux Guide
Chapter 3. Saving Space 13

bash# mkdir /mnt/lib
bash# strip −−strip−unneeded −o /mnt/lib/libdl.so.2 /lib/libdl.so.2
bash# strip −−strip−unneeded −o /mnt/lib/libc.so.6 /lib/libc.so.6
bash# strip −−strip−unneeded −o /mnt/lib/ld−linux.so.2 /lib/ld−linux.so.2
bash# chmod +x /mnt/lib/ld−linux.so.2
Using strip −o might seem an odd way to copy library files from the development system to the ramdisk.
What it does is strip the symbols while the file is in transit from the source location to the destination.
This has the effect of stripping symbols from the library on the ramdisk without altering the libraries on
the development system. Unfortunately file permissions are lost when copying libraries this way which
is why the chmod +x command is then used to set the execute flag for the rootdisk's dynamic loader.
3.3.5. Create a console device
bash# mkdir /mnt/dev
bash# mknod /mnt/dev/console c 5 1
3.3.6. Compress the ramdisk image
bash# cd /
bash# umount /dev/ram7
bash# dd if=/dev/ram7 of=~/phase2−image bs=1k count=4096
bash# gzip −9 ~/phase2−image
3.3.7. Copy the compressed image to diskette
Insert the floppy labeled "root disk" into drive fd0.
bash# dd if=~/phase2−image.gz of=/dev/fd0 bs=1k
3.4. Implementation
Successful implementation of this phase is probably the most difficult part of the Pocket Linux Guide. If you
need help getting things to work please visit the Pocket Linux Guide Resource Site to browse the
troubleshooting forum and subscribe to the mailing list.
3.4.1. System startup
Follow these steps to boot:
Restart the PC using the boot disk from the previous chapter.• At the grub> prompt, type kernel (fd0)/boot/vmlinuz init=/bin/sh
root=/dev/fd0 load_ramdisk=1 prompt_ramdisk=1 and press Enter.
•
Type boot at the grub> prompt and press Enter.• Insert the new, compressed root disk when prompted.•
The screen output should be similar to the following example:
GNU GRUB version 0.95
Pocket Linux Guide
Chapter 3. Saving Space 14
Table of contents
Other Linux Software manuals