8.3. 构建

8.3.1. 编写 "more" 脚本

使用文本编辑器创建以下脚本,并将其保存为~/staging/bin/more.sh

#!/bin/sh
#
# more.sh - emulates the basic functions of the "more" binary without
#           requiring ncurses or termcap libraries.
#
# Assume input is coming from STDIN unless a valid file is given as
# a command-line argument. 
if [ -f $1 ]; then
  INPUT="$1"
else
  INPUT="/dev/stdin"
fi
#
# Set IFS to newline only. See BASH(1) manpage for details on IFS.
IFS=$'\n'
#
# If terminal dimensions are not already set as shell variables, take
# a guess of 80x25.
if [ "$COLUMNS" = "" ]; then
  let COLUMNS=80;
fi
if [ "$LINES" = "" ]; then
  let LINES=25;
fi
#
# Initialize line counter variable
let LINE_COUNTER=$LINES
#
# Read the input file one line at a time and display on STDOUT until
# the page fills up. Display "Press <Enter>" message on STDERR and wait
# for keypress from STDERR.  Continue until the end of the input file.
# Any input line greater than $COLUMNS characters in length is wrapped
# and counts as multiple lines.
#
while read -n $COLUMNS LINE_BUFFER; do
  echo "$LINE_BUFFER"
  let LINE_COUNTER=$LINE_COUNTER-1
  if [ $LINE_COUNTER -le 1 ]; then
    echo "Press <ENTER> for next page or <CTRL>+C to quit.">/dev/stderr
    read</dev/stderr
    let LINE_COUNTER=$LINES
  fi
done<$INPUT
#
# end of more.sh

为以下内容创建符号链接:more

bash# ln -s more.sh ~/staging/bin/more

8.3.2. 创建额外的设备文件

bash# ln -s /proc/self/fd ~/staging/dev/fd
bash# ln -s fd/0 ~/staging/dev/stdin
bash# ln -s fd/1 ~/staging/dev/stdout
bash# ln -s fd/2 ~/staging/dev/stderr
bash# mknod -m644 ~/staging/dev/zero c 1 5

8.3.3. 安装 ps

http://procps.sourceforge.net/ 获取最新的 procps 源代码包。

bash# cd /usr/src/procps-3.2.3
bash# make SHARED=0 CC="gcc -mcpu=i386"
bash# cd ps
bash# cp ps ~/staging/bin

8.3.4. 安装 sed

ftp://ftp.gnu.org/gnu/sed/ 下载 GNU 的 sed。

bash# cd /usr/src/sed-4.1.2
bash# export CC="gcc -mcpu=i386"
bash# ./configure --host=i386-pc-linux-gnu
bash# make
bash# cd sed
bash# cp sed ~/staging/bin

8.3.5. 安装 ed

ed 包也来自 GNU,地址为 ftp://ftp.gnu.org/gnu/ed/

bash# cd /usr/src/ed-0.2
bash# ./configure --host=i386-pc-linux-gnu
bash# make
bash# cp ed ~/staging/bin

8.3.6. 剥离二进制文件以节省空间

bash# strip ~/staging/bin/*

8.3.7. 确保正确的权限

bash# chown 0:0 ~/staging/bin/*
bash# chmod -R 755 ~/staging/bin
bash# chmod 4750 ~/staging/bin/su

8.3.8. 创建根磁盘镜像

bash# cd /
bash# dd if=/dev/zero of=/dev/ram7 bs=1k count=4096
bash# mke2fs -m0 /dev/ram7 4096
bash# mount /dev/ram7 /mnt
bash# cp -dpR ~/staging/* /mnt
bash# umount /dev/ram7
bash# dd if=/dev/ram7 of=~/phase7-image bs=1k
bash# gzip -9 ~/phase7-image

8.3.9. 将镜像复制到软盘

将标记为 "root disk" 的软盘插入驱动器 fd0。

bash# dd if=~/phase7-image.gz of=/dev/fd0 bs=1k