1 引言
1.1 编写目的
说明Linux系统裁减的过程及解决的主要技术问题。本文档的编写是为了说明L
iPS的详细设计过程,也可以作为构造一个小型化Linux系统的指南。
本文档内容的组织如下所述:这一部分是引言,第2部分是对项目软、硬件环境
和需求的总体描述,第3部分说明了裁减LINUX的相关基本概念。后面几个部分是具
体实现方法的详细说明,4~6分别详细描述了创建内核、创建根文件系统和引导系
统的方法。最后的内容可以看作附录,其中7说明了一般Linux系统安装光盘的创建
方法,8列出了相关的配置文件和源代码。
1.2 背景
华中科技大学计算机存储系统国家专业实验室,NAS项目(2003.3——2003.8)
。
1.3 定义
O boot loader: 引导器
O kernel: 内核
O root filesystem: 根文件系统
O USB: 通用串行总线
O
1.4 参考资料
[1] DIY:自己动手做一个迷你 Linux 系统. IBM developerWorks 中国网站. htt
p://www-900.ibm.com/developerWorks/cn/linux/embed/diy/index.shtml
[2] Linux From Scratch.
[3] Linux启动过程综述.
i=15750
[4] The Linux Bootdisk HOWTO (中译版).
ootdisk-HOWTO.html
[5] Using the initial RAM disk (initrd). /usr/src/linux-2.4/Documentatio
n/initrd.txt. (译文:
http://zope.slat.org/Project/3ybaby/discuss-area/c
d-boot/cdboot_refer/initrd.htm)
[6] Quickmouse于2003.1发表于白云黄鹤BBS ( Linux讨
论区的帖子“裁减Linux”。
[7] Filesystem Hierarchy Standard.
.2.pdf
[8] LINUX ALLOCATED DEVICES. /usr/src/linux-2.4/Documentation/device.txt
. 或者最新的列表在
http://www.kernel.org/pub/linux/docs/device-list/
[9] Karim Yaghmour. Building Embedded Linux Systems. O'Reilly, April 200
3.
[10] Alessandro Rubini. Take Command: Init.
cs/init/ , November 1998.
[11] 深入 Linux PAM 体系结构. IBM developerWorks 中国网站.
0.ibm.com/developerWorks/cn/linux/l-pam/index.shtml . 2003.7.
[12]
2 总体描述
2.1 硬件环境
开发主机:
Intel 845芯片组主板,P4赛扬1.8GHz CPU,256M内存,60G IDE硬盘,RTL813
9网卡。
目标机:
与主机同构。
2.2 软件环境
开发主机:
Redhat Linux 7.3 (kernel 2.4.18-3),或者Redhat Linux 9 (kernel 2.4.2
0-8)。可以根据需要使用其他版本的内核,比如Redhat 7.3+kernel 2.4.20。
目标机:
LiPS,以及应用程序。
2.3 需求规定和约束
我们目标系统LiPS,即Linux Powered Storage是一个嵌入式Linux操作系统,
主要着眼于提供各种文件服务,不包括X GUI;并且足够小,可以装入32M USB盘或
者DOC/DOM等常用设备引导。
3 总体设计
3.1 基本设计概念
提起“裁减Linux”,最容易想到的方法是以一个已经安装好的系统为基础,删
除掉不需要的文件,以减小整个系统的尺寸。而事实上这个方法几乎是行不通的,
因为通过光盘安装的一个Redhat 9 Linux系统,即便是只选择了比较少的软件包,
其所占空间也可以轻松达到300~500MB,想在这个基础上裁减到30MB左右是比较困
难的。更困难的是确定哪些东西是可以删除的?如果删除了系统必须的文件,可能
导致系统不能引导。
另一个途径是从零开始根据需要构建整个系统。“Linux From Scratch”采用
的就是这种方式,一个个安装需要的软件包。类似的,更简单的方法是,安装一个
完整的Linux系统,然后将必要的软件(可执行程序)、配置文件、库文件、内核等
复制出来构建一个小系统。我们采取后一种方式。
3.2 软件结构
LiPS包括3部分:Linux内核,根文件系统和引导器。
内核(Kernel)提供了一个操作系统的基本功能,如内存管理、进程调度、文
件系统、网络等,以及设备驱动程序。
根文件系统(Root filesystem)是存放运行、维护系统所必须的各种工具软件
、库文件、脚本、配置文件和其他特殊文件(比如设备节点)的地方,也可以安装
各种软件包。通常根文件系统位于某个磁盘分区,而在LiPS中应用了initrd(初始
RAM盘)机制,将根文件系统放在RAM Disk中。
引导器(Boot Loader)的任务是从引导设备装载内核,引导系统运行。Linux
系统常见的引导器包括早期的LILO和近期的GRUB。LiPS用的是GRUB。
4 内核
4.1 创建内核的方法
Linux提供了方便灵活的方法来定制内核,主要步骤包括:下载安装内核源代码
树,配置内核,编译内核。具体方法请参考相关文档。关键指令如下:
cd /usr/src/linux-2.4
make menuconfig
make dep; make clean; make bzImage
编译成功的内核文件为arch/i386/boot/bzImage.
4.2 内核配置
4.2.1 概述
make menuconfig之后可以看到类似如下的内容(编号是为了描述方便而加上的
)。根据内核版本的不同,列出的项目会有差异。而变化主要发生在发行版为标准
内核打的各种补丁上面,其实标准内核是比较稳定的。
如下所示,我们配置的内核版本为2.4.20-8,这个“-8”就是Redhat所打的补
丁版本号。补丁一般是对标准内核错误的修正和新增的功能。与2.4.20内核配置选
项比较会发现,[10,25,30]都属于Redhat的补丁。
一般地,这些补丁选项都不是必须的,因此我们的LiPS内核中都没有包括这些
选项。其实,完全可以通过直接配置一个标准的内核版本(比如2.4.20)来产生我
们所需要的内核。
Linux Kernel v2.4.20-8-R2Lips Configuration
[1] Code maturity level options --->
[2] Loadable module support --->
[3] Processor type and features --->
[4] General setup --->
[5] Memory Technology Devices (MTD) --->
[6] Parallel port support --->
[7] Plug and Play configuration --->
[8] Block devices --->
[9] Multi-device support (RAID and LVM) --->
[10] Cryptography support (CryptoAPI) --->
[11] Networking options --->
[12] Telephony Support --->
[13] ATA/IDE/MFM/RLL support --->
[14] SCSI support --->
[15] Fusion MPT device support --->
[16] I2O device support --->
[17] Network device support --->
[18] Amateur Radio support --->
[19] IrDA (infrared) support --->
[20] ISDN subsystem --->
[21] Old CD-ROM drivers (not SCSI, not IDE) --->
[22] Input core support --->
[23] Character devices --->
[24] Multimedia devices --->
[25] Crypto Hardware support --->
[26] File systems --->
[27] Console drivers --->
[28] Sound --->
[29] USB support --->
[30] Additional device driver support --->
[31] Bluetooth support --->
[32] Kernel hacking --->
[33] Library routines --->
Load an Alternate Configuration File
Save Configuration to an Alternate File
< Exit > < Help >
下面我们以2.4.20内核为例,来详细说明内核的配置,也就是选哪些东西、不
选哪些东西。没有说明的选项,比如“Code maturity level options”,表示不选
。
4.2.2 Loadable module support[*] Enable loadable module support
[ ] Set version information on all module symbols[*] Kernel module loader
可加载模块支持。虽然目前所选项目全部编译进内核,但是内核是支持模块的
。可以考虑将一些硬件设备的驱动程序编译为模块,从而减小内核尺寸,同时增加
适应不同硬件平台的灵活性。
4.2.3 Processor type and features
(Pentium-III/Celeron(Coppermine)) Processor family[*] Machine Check Exception
< > Toshiba Laptop support
< > Dell laptop support
< > /dev/cpu/microcode - Intel IA32 CPU microcode support
< > /dev/cpu/*/msr - Model-specific register support
< > /dev/cpu/*/cpuid - CPU information support
(off) High Memory Support
[ ] Math emulation
[ ] MTRR (Memory Type Range Register) support
[ ] Symmetric multi-processing support
[ ] Local APIC support on uniprocessors
[ ] Unsynced TSC support
处理器特性。选择一下Processor family,其余基本上都没有选。High Memor
y Support也可以打开,不清楚有什么用。
4.2.4 General setup[*] Networking support[*] PCI support
(Any) PCI access mode[*] ISA bus support[*] PCI device name database
[ ] EISA support
[ ] MCA support
[ ] Support for hot-pluggable devices[*] System V IPC[*] BSD Process Accounting[*] Sysctl support
(ELF) Kernel core (/proc/kcore) format
< > Kernel support for a.out binaries
<*> Kernel support for ELF binaries
< > Kernel support for MISC binaries[*] Power Management support
< > Kernel support for MISC binaries[*] Power Management support
<*> Advanced Power Management BIOS support[*] Ignore USER
SUSPEND[*] Enable PM at boot time[*] Make CPU Idle calls when idle[*]
Enable console blanking using APM[*] RTC stores time in GMT[*] Allow
interrupts during APM BIOS calls[*] Use real mode APM BIOS call to
power off
总体设置。这一部分选项比较重要,包括网络、总线、进程、二进制文件格式
、电源管理等。其中APM相关的选项比较搞不清楚,有时候halt –p命令不能关闭系
统电源跟这里有关系。
4.2.5 Plug and Play configuration
<*> Plug and Play support
< > ISA Plug and Play support
即插即用设置。
4.2.6 Block devices
<*> Normal floppy disk support
< > XT hard disk support
< > Compaq SMART2 support
< > Compaq Smart Array 5xxx support
< > Mylex DAC960/DAC1100 PCI RAID Controller support
<*> Loopback device support
< > Network block device support
<*> RAM disk support
(4096) Default RAM disk size[*] Initial RAM disk (initrd) support[*] Per partition statistics in /proc/partitions
块设备。软驱也可以不选。这里的关键选项是Loopback device,RAM disk和i
nitrd,一定要选中。Default RAM disk size多少都没有关系。
4.2.7 Multi-device support (RAID and LVM)[*] Multiple devices driver support (RAID and LVM)
<*> RAID support
<*> Linear (append) mode
<*> RAID-0 (striping) mode
<*> RAID-1 (mirroring) mode
<*> RAID-4/RAID-5 mode
<*> Multipath I/O support
<*> Logical volume manager (LVM) support
MD支持。软RAID和LVM的支持在这里选择,如果不使用,也可以不选。
4.2.8 Networking options
<*> Packet socket[*] Packet socket: mmapped IO
< > Netlink device emulation[*] Network packet filtering (replaces ipchains)
[ ] Network packet filtering debugging[*] Socket Filtering
<*> Unix domain sockets[*] TCP/IP networking[*] IP: multicasting[*] IP: advanced router
[ ] IP: policy routing
[ ] IP: equal cost multipath
[ ] IP: use TOS value as routing key
[ ] IP: verbose route monitoring
[ ] IP: large routing tables[*] IP: kernel level autoconfiguration[*] IP: DHCP support[*] IP: BOOTP support[*] IP: RARP support
<*> IP: tunneling
< > IP: GRE tunnels over IP
[ ] IP: multicast routing
[ ] IP: TCP Explicit Congestion Notification support
[ ] IP: TCP syncookie support (disabled per default)
IP: Netfilter Configuration --->
< > 802.1Q VLAN Support
---
< > The IPX protocol
< > Appletalk protocol support
Appletalk devices --->
< > DECnet Support
< > 802.1d Ethernet Bridging
QoS and/or fair queueing --->
Network testing --->
网络选项。这里选项也比较多,如果没有什么特殊要求,又不是十分理解这些
选项的意义,基本上可以用缺省设置。注意DHCP选项,如果要用到的话要记得选中
这个。
IP: Netfilter Configuration(跟iptables有关)等几个子选项都没有选。
4.2.9 ATA/IDE/MFM/RLL support
<*> ATA/IDE/MFM/RLL support
IDE, ATA and ATAPI Block devices --->
<*> Enhanced IDE/MFM/RLL disk/cdrom/tape/floppy support
--- Please see Documentation/ide.txt for help/info on IDE drives
[ ] Use old disk-only driver on primary interface
<*> Include IDE/ATA-2 DISK support[*] Use multi-mode by default[*] Auto-Geometry Resizing support
<*> Include IDE/ATAPI CDROM support
< > Include IDE/ATAPI TAPE support
< > Include IDE/ATAPI FLOPPY support
<*> SCSI emulation support
[ ] IDE Taskfile Access
--- IDE chipset support/bugfixes
[ ] CMD640 chipset bugfix/support
[ ] RZ1000 chipset bugfix/support[*] Generic PCI IDE chipset support[*]
Sharing PCI IDE interrupts support[*] Generic PCI bus-master DMA support
[ ] Boot off-board chipsets first support
[ ] Force enable legacy 2.0.X HOSTS to use DMA[*] Use PCI DMA by default when available
[ ] Enable DMA only for disks
[ ] AEC62XX chipset support
[ ] ALI M15x3 chipset support
[ ] AMD Viper support
[ ] CMD64X and CMD680 chipset support
[ ] CY82C693 chipset support
[ ] Cyrix CS5530 MediaGX chipset support
[ ] HPT34X chipset support
[ ] HPT366/368/370 chipset support[*] Intel PIIXn chipsets support[*] PIIXn Tuning support
[ ] PROMISE PDC202{46|62|65|67|68|69|70} support
[ ] ServerWorks OSB4/CSB5 chipsets support
[ ] SiS5513 chipset support
[ ] SLC90E66 chipset support
[ ] VIA82CXXX chipset support
[ ] Other IDE chipset support
[ ] IGNORE word93 Validation BITS
IDE设备支持。要用IDE硬盘,当然要选中。CDROM选项看情况也可以不要。SCS
I emulation似乎对使用USB盘有影响。剩下的很多都是跟IDE chipset有关的。
最好是用dmesg命令找出目标系统主板芯片组,然后选中对应的芯片组支持,这
样相当于安装了IDE芯片组的驱动程序。如果没有指定应该也可以,只是性能可能会
受到一些影响。
4.2.10 SCSI support
<*> SCSI support
--- SCSI support type (disk, tape, CD-ROM)
<*> SCSI disk support
(40) Maximum number of SCSI disks that can be loaded as modules
< > SCSI tape support
< > SCSI OnStream SC-x0 tape support
< > SCSI CD-ROM support
<*> SCSI generic support
--- Some SCSI devices (e.g. CD jukebox) support multiple LUNs
[ ] Enable extra checks in new queueing code
[ ] Probe all LUNs on each SCSI device
[ ] Verbose SCSI error reporting (kernel size +=12K)
[ ] SCSI logging facility
SCSI low-level drivers --->
SCSI支持。如果要用到SCSI硬盘等SCSI设备,这里当然要选。目前我们的目标
系统其实没有SCSI硬盘,但是为了使用USB盘,也要选中SCSI支持。
SCSI low-level drivers子选项中是一些SCSI卡,都不用选。
4.2.11 Network device support[*] Network device support
ARCnet devices --->
< > Dummy net driver support
< > Bonding driver support
< > EQL (serial line load balancing) support
< > Universal TUN/TAP device driver support
Ethernet (10 or 100Mbit) --->[*] Ethernet (10 or 100Mbit)[*] EISA, VLB, PCI and on board controllers
<*> EtherExpressPro/100 support (eepro100, original Becker driv
er
<*> RealTek RTL-8139 PCI Fast Ethernet Adapter support
Ethernet (1000 Mbit) --->
[ ] FDDI driver support
< > PPP (point-to-point protocol) support
< > SLIP (serial line) support
Wireless LAN (non-hamradio) --->
Token Ring devices --->
[ ] Fibre Channel driver support
Wan interfaces --->
网络设备支持。主要选择网卡类型,Ethernet (10 or 100Mbit)子选项中列出
了内核可以支持的10/100M以太网卡,这里仅列出了我们选中的2种。其他子选项都
没有选。
4.2.12 Character devices[*] Virtual terminal[*] Support for console on virtual terminal
<*> Standard/generic (8250/16550 and compatible UARTs) serial suppo
rt[*] Support for console on serial port
[ ] Extended dumb serial driver options
[ ] Non-standard serial port support
[ ] Unix98 PTY support
I2C support --->
Mice --->
Joysticks --->
< > QIC-02 tape support
Watchdog Cards --->
< > AMD 768 Random Number Generator support
< > Intel i8x0 Random Number Generator support
< > AMD 76x native power management (Experimental)
< > /dev/nvram support
< > Enhanced Real Time Clock Support
< > Double Talk PC internal speech card support
< > Siemens R3964 line discipline
< > Applicom intelligent fieldbus card support
Ftape, the floppy tape device driver --->
< > /dev/agpgart (AGP Support)
[ ] Direct Rendering Manager (XFree86 DRI support)
< > ACP Modem (Mwave) support
字符设备。这里主要就是选择了最前面4个跟终端和串口有关的选项,以支持虚
拟终端(远程登录要用到),支持标准串口,支持串口控制台。
4.2.13 File systems[*] Quota support
< > Kernel automounter support
<*> Kernel automounter version 4 support (also supports v3)
<*> Reiserfs support
[ ] Enable reiserfs debug mode[*] Stats in /proc/fs/reiserfs
<*> Ext3 journalling file system support
[ ] JBD (ext3) debugging support
<*> DOS FAT fs support
<*> MSDOS fs support
< > UMSDOS: Unix-like file system on top of standard MSDOS fs
<*> VFAT (Windows-95) fs support
< > Compressed ROM file system support[*] Virtual memory file system support (former shm fs)
<*> ISO 9660 CDROM file system support[*] Microsoft Joliet CDROM extensions
[ ] Transparent decompression extension
< > JFS filesystem support
< > Minix fs support
< > FreeVxFS file system support (VERITAS VxFS(TM) compatible)
< > NTFS file system support (read only)
< > OS/2 HPFS file system support[*] /proc file system support
< > QNX4 file system support (read only)
< > ROM file system support
<*> Second extended fs support
< > System V/Xenix/V7/Coherent file system support
< > UDF file system support (read only)
< > UFS file system support (read only)
Network File Systems --->
< > Coda file system support (advanced network fs)
<*> NFS file system support[*] Provide NFSv3 client support
[ ] Root file system on NFS
<*> NFS server support[*] Provide NFSv3 server support
<*> SMB file system support (to mount Windows shares etc.)
[ ] Use a default NLS
< > NCP file system support (to mount NetWare volumes)
Partition Types --->
Native Language Support --->
Default NLS Option: "iso8859-1"
<*> Codepage 437 (United States, Canada)
<*> Simplified Chinese charset (CP936, GB2312)
<*> Traditional Chinese charset (Big5)
文件系统。这部分内容比较多,也很重要。首先是Quota,如果目标系统支持这
个功能,这里就要选中。然后是选择要支持的文件系统,常用的有Reiserfs,Ext3
,FAT,ISO9660,JFS等等,可以根据情况选择。/proc和ext2一定要选中。
还有Network File Systems子选项,可以选择是否支持NFS和SMB。Native Lan
guage Support子选项跟所支持的语言有关,这里没有全部列出来,我们选中了英语
和简体、繁体中文字符集。
4.2.14 Console drivers[*] VGA text console
[ ] Video mode selection support
控制台驱动。调试的时候可以给目标系统接显示器看一下,实际系统应该可以
不选。
4.2.15 USB support
<*> Support for USB
[ ] USB verbose debug messages
--- Miscellaneous USB options[*] Preliminary USB device filesystem
[ ] Long timeout for slow-responding devices (some MGE Ellipse UP
Se
--- USB Host Controller Drivers
<*> UHCI Alternate Driver (JE) support
<*> OHCI (Compaq, iMacs, OPTi, SiS, ALi, ...) support
--- USB Device Class drivers
< > USB MIDI support
<*> USB Mass Storage support[*] USB Mass Storage verbose debug
[ ] Freecom USB/ATAPI Bridge support
[ ] ISD-200 USB/ATA Bridge support
[ ] Microtech CompactFlash/SmartMedia support
< > USB Modem (CDC ACM) support
< > USB Printer support
--- USB Human Interface Devices (HID)
<*> USB Human Interface Device (full HID) support
--- Input core support is needed for USB HID input layer or HID
BP
[ ] /dev/hiddev raw HID device support
--- USB Imaging devices
< > USB Kodak DC-2xx Camera support
< > USB Scanner support
< > Microtek X6USB scanner support
--- USB Multimedia devices
--- Video4Linux support is needed for USB Multimedia device suppo
rt
--- USB Network adaptors
--- USB port drivers
USB Serial Converter support --->
--- USB Miscellaneous drivers
< > Texas Instruments Graph Link USB (aka SilverLink) cable suppo
rt
< > USB LCD device support
USB支持。如果没有使用USB设备,这部分可以不选。要使用USB盘,一定要选中
USB Mass Storage support。其余很多选项我也不清楚是干什么用的。
4.3 提示
4.3.1 EXTRAVERSION
在系统中使用uname可以获得系统版本信息。比如Redhat 9:
[root@lips root]# uname -r
2.4.20-8
[root@lips root]#
返回结果表示了Redhat修订后的内核版本号。前面我们提到过,这个“-8”就
是Redhat附加在标准内核版本号后面的一个信息。
在一个运行LiPS的系统中,我们也需要确定当前使用的内核版本,通过版本信
息来知道这个内核的配置,也可以通过在标准内核版本后面附加特定的编码来实现
。
/usr/src/linux-2.4/Makefile中的变量EXTRAVERSION可以帮助我们达到这个目
标。下面是Makefile文件的最前面几行内容:
VERSION = 2
PATCHLEVEL = 4
SUBLEVEL = 20
EXTRAVERSION = -8
KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
现在KERNELRELEASE的值就是2.4.20-8。这个信息会被编译到内核中,uname工
具会从内核文件中提取出版本号。如果将Makefile做一些修改,比如,
EXTRAVERSION = -8-R2Lips
那么我们可以得到内核版本号2.4.20-8-R2Lips,表示为了创建LiPS而编译的一
个特定配置的内核,这个内核是以2.4.20-8为基础,R2Lips(Release 2 Lips)即
第二个版本。
对Makefile的修改要在编译内核之前完成。
4.3.2 .config
内核配置(make menuconfig)完成之后,会询问:
Do you wish to save your new kernel configuration?
< Yes > < No >
回答Yes,配置信息会保存在/usr/src/linux-2.4/.config文件中。注意以“.
”开头的文件是个隐藏文件,要用ls –a才看的到。.config是个文本文件,内容类
似下面:
#
# Loadable module support
#
CONFIG_MODULES=y
# CONFIG_MODVERSIONS is not set
CONFIG_KMOD=y
所以可以把这个文件保存起来,并且给文件名带上版本号,方便管理、使用。
cp .config ~/config-2.4.20-8-R2Lips