Pocket Linux Guide David Horton

Дата канвертавання20.04.2016
Памер238.31 Kb.
  1   2   3   4   5   6
Pocket Linux Guide

David Horton

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. Copyright and License

2. Disclaimer


1. About Pocket Linux

2. Prerequisite Skills

3. Project Format

4. Help & Support

5. Feedback

1. Project Initiation

1.1. A Brief History of GNU/Linux

1.2. The Goal of Pocket Linux

1.3. Working Within The Constraints

2. A Simple Prototype

2.1. Analysis

2.2. Design

2.3. Construction

2.4. Implementation

3. Saving Space

3.1. Analysis

3.2. Design

3.3. Construction

3.4. Implementation

4. Some Basic Utilities

4.1. Analysis

4.2. Design

4.3. Construction

4.4. Implementation

5. Checking and Mounting Disks

5.1. Analysis

5.2. Design

5.3. Construction

5.4. Implementation

6. Automating Startup & Shutdown

6.1. Analysis

6.2. Design

6.3. Construction

6.4. Implementation

7. Enabling Multiple Users

7.1. Analysis

7.2. Design

7.3. Construction

7.4. Implementation

8. Filling in the Gaps

8.1. Analysis

8.2. Design

8.3. Construction

8.4. Implementation

9. Project Wrap Up

9.1. Celebrating Accomplishments

9.2. Planning Next Steps

A. Hosting Applications

A.1. Analysis

A.2. Design

A.3. Construction

A.4. Implementation

B. GNU Free Documentation License












B.12. ADDENDUM: How to use this License for your documents

Legal Information

1. Copyright and License

This document, Pocket Linux Guide, is copyrighted (c) 2003 - 2004 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.
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://ragib.hypermart.net/linux/
2.3.1. Prepare the boot disk floppy

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 kernel

The steps for building the kernel were tested using Linux kernel version 2.4.18 and should work any 2.4.x kernel. The latest version of the kernel source code may be downloaded from http://www.kernel.org or one of its mirrors.

bash# cd /usr/src/linux

bash# make menuconfig

Be sure to configure support for the following:

  • 386 processor

  • Floppy disk

  • RAM disk

  • Second extended (ext2) filesystem

  • Console on virtual terminal

bash# make dep

bash# make clean

bash# make bzImage

2.3.3. Copy the kernel to diskette

bash# mkdir /mnt/boot

bash# cp /usr/src/linux/arch/i386/boot/bzImage /mnt/boot/vmlinuz

2.3.4. Copy the LILO boot loader

bash# cp /boot/boot.b /mnt/boot/boot.b

2.3.5. Create device files that LILO needs

bash# mkdir /mnt/dev

bash# cd /mnt/dev

bash# mknod fd0 b 2 0

bash# mknod console c 5 1

2.3.6. Write a simple lilo.conf

bash# mkdir /mnt/etc

bash# cd /mnt/etc

Use an editor like vi, emacs or pico to create the following lilo.conf file:

# /etc/lilo.conf - boot loader configuration file









append="load_ramdisk=1 prompt_ramdisk=1"



# end of /etc/lilo.conf

2.3.7. Install the LILO boot loader

bash# lilo -r /mnt

2.3.8. Unmount the boot disk

bash# cd /

bash# umount /mnt

2.3.9. Prepare the root disk floppy

Insert a blank diskette labeled "root disk".

bash# mke2fs -m0 /dev/fd0

bash# mount /dev/fd0 /mnt

2.3.10. Build BASH

Get the bash-2.05 source code package from ftp://ftp.gnu.org/gnu/bash/ and untar it into the /usr/src directory.

BASH version 2.05b, the latest version at the time of this writing, will not build successfully when using the --enable-minimal-config option. This leaves two choices. We can either fix 2.05b by applying the patch posted on news://gnu.bash.bug under the subject, "Compile error in execute_cmd.c with --enable-minimal-config" or we can simply use the 2.05a version.

Build BASH for an i386 CPU with the following commands:

bash# cd /usr/src/bash-2.05a

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.11. 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.12. Create device files that BASH needs

bash# mkdir /mnt/dev

bash# mknod /mnt/dev/console c 5 1

2.3.13. Unmount the root disk

bash# cd /

bash# umount /mnt

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 LILO prompt appears, type bootdisk init=/bin/sh and press Enter.

  • Insert the root disk when prompted.

If all goes well the screen should look something like the example shown below.

boot: bootdisk init=/bin/sh

Loading bootdisk

Uncompressing Linux... Ok, booting kernel.


.. [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.
2.4.4. System shutdown

Remove the diskette from fd0 and restart the system using CTRL-ALT-DELETE.

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 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 kernel is configured with ramdisk support and a default size of 4,096K. If the ramdisk size is something other than 4096K this can be fixed by adding the line "ramdisk=4096" to the development system's lilo.conf file. The lilo.conf(5) man page provides additional information.

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

bash# mount /dev/ram7 /mnt

3.3.2. Rebuild the BASH shell

bash# cd /usr/src/bash-2.05a

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

Note 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)

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

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/*

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 on all of the libraries on the rootdisk.

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

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

3.4.1. System startup

Follow these steps to boot:

  • Restart the PC using the lilo boot disk from the previous chapter.

  • At the LILO prompt, type bootdisk init=/bin/sh and press Enter.

  • Insert the new, compressed root disk when prompted.

The screen output should be similar to the following example:

boot: bootdisk init=/bin/sh

Loading bootdisk

Uncompressing Linux... Ok, booting kernel.


.. [various kernel messages]


VFS: Insert root floppy to be loaded into RAM disk and press ENTER

RAMDISK: Compressed image found at block 0

VFS: Mounted root (ext2 filesystem) read-write.

Freeing unused kernel memory: 178k freed

# _

  1   2   3   4   5   6

База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка