作者:Jim Zheng。接触Linux 4年,但是一直处于自学状态,没有机会参与Linux平台的产品开发。很希望能用上Linux平台作实际开发。联系方式:zhengweishang@google.com
日期:2010.1.1
内容提要:
1。最小系统的意义
2。最小系统的要素
3。主机环境介绍
4。软件版本选择
5。构建完成之文件
6。最小系统启动信息
7。详细构建过程
8。下一步工作
1。最小系统的意义
在我的开发经历中,每当要使用新的硬件或软件平台时,我都会从他们的最小系统开始。比如新使用一款单片机,我会首先研究其电源、复位、时钟、JTAG、UART。根据这些可以搭建一个开发的最小硬件系统。我在维护一套3W行的C语言软件时,也曾将起其精简到1千行的最小系统,仅包含消息机制、在屏幕上显示一个小图标、队列服务等,而其他的功能都是一些添加上去的模块。吃透这个最小系统后,增加功能或修正bug就简单很多。最小系统有些像建房子打的地基,对它理解透了,会发现那些眼花缭乱的功能通过添砖加瓦的工作就可以完成了。现今的软硬件系统设计时模块化都做得很好,增减一个功能模块已经是个比较简单的事情。
我想比较深入的学习Linux,于是想到:Linux的最小系统是什么样?
2。最小系统的要素
Linux的发布版本都提供了大量完善的功能和服务,而我想建立的最小系统之仅包含以下要素:
A. 有字符显示。
B。能通过键盘输入字符
C。能执行ls等简单命令
根据以上需求,并分析Linux平台的架构。该最小系统特征如下
A。kernel+rootfs, 不使用initrd.
B. ls等命令放在rootfs中,elf格式。静态编译,不支持动态链接库。
C。 kernel支持elf可执行文件格式
D。 kernel不支持可加载模块功能。必要的驱动直接编译到kernel中。
E。kernel应包含字符的显示驱动、键盘驱动。
F。rootfs放在IDE硬盘上,所以kernel应包含PATA驱动
G。rootfs为EXT3文件系统,kernel应包含EXT2、EXT3文件系统驱动。
3。主机环境介绍
[root@goldway ~]# cat /proc/version
Linux version 2.6.18-1.2798.fc6 (brewbuilder@hs20-bc2-4.build.redhat.com) (gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)) #1 SMP Mon Oct 16 14:37:32 EDT 2006
[root@goldway ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Celeron(R) CPU 2.60GHz
stepping : 9
cpu MHz : 2625.987
cache size : 128 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe up cid xtpr
bogomips : 5257.90
[root@goldway ~]# lspci
00:00.0 Host bridge: VIA Technologies, Inc. P4M266 Host Bridge
00:01.0 PCI bridge: VIA Technologies, Inc. VT8633 [Apollo Pro266 AGP]
00:10.0 USB Controller: VIA Technologies, Inc. VT82xxxxx UHCI USB 1.1 Controller (rev 80)
00:10.1 USB Controller: VIA Technologies, Inc. VT82xxxxx UHCI USB 1.1 Controller (rev 80)
00:10.2 USB Controller: VIA Technologies, Inc. VT82xxxxx UHCI USB 1.1 Controller (rev 80)
00:10.3 USB Controller: VIA Technologies, Inc. USB 2.0 (rev 82)
00:11.0 ISA bridge: VIA Technologies, Inc. VT8235 ISA Bridge
00:11.1 IDE inteRFace: VIA Technologies, Inc. VT82C586A/B/VT82C686/A/B/VT823x/A/C PIPC Bus Master IDE (rev 06)
00:11.5 ** audio controller: VIA Technologies, Inc. VT8233/A/8235/8237 AC97 Audio Controller (rev 50)
00:12.0 Ethernet controller: VIA Technologies, Inc. VT6102 [Rhine-II] (rev 74)
01:00.0 VGA compatible controller: nVidia Corporation NV34 [GeForce FX 5200] (rev a1)
[root@goldway ~]# fdisk -l
Disk /dev/hda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 * 1 1001 8040501 7 HPFS/NTFS
/dev/hda2 1002 19457 148247820 f W95 Ext'd (LBA)
/dev/hda5 1002 5001 32129968+ 7 HPFS/NTFS
/dev/hda6 5002 9001 32129968+ 7 HPFS/NTFS
/dev/hda7 9002 13001 32129968+ c W95 fat32 (LBA)
/dev/hda8 13002 17001 32129968+ c W95 FAT32 (LBA)
/dev/hda9 18213 19457 10000431 83 Linux
/dev/hda10 17002 18081 8675068+ 83 Linux
/dev/hda11 18082 18212 1052226 82 Linux swap / Solaris
Partition table entries are not in disk order
[root@goldway ~]# mount
/dev/hda10 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/hda7 on /mnt/windows/f type vfat (rw)
/dev/hda8 on /mnt/windows/g type vfat (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hda9 on /mnt/cdrom type ext3 (rw)
请注意以下几点:
A。硬盘是IDE接口,所以这篇文档描述的操作不适合于使用SATA硬盘的朋友。
B。我使用的linux dist是fedora core 6. gcc version 4.1.1
C。/dev/hda10是fc6的/, 即将构建的最小系统的文件将放在/dev/hda9中。
4。软件版本选择
A. 选择busybox-1.15.3构建rootfs.
B. 内核版本选择linux-2.6.26.5 。gcc4.1.1不支持最新版的kernel.
5。构建完成之文件
[root@goldway ~]# ll /mnt/cdrom
总计 728
drwxr-xr-x 2 root root 4096 12-31 19:20 bin
-rw-r--r-- 1 root root 701872 01-01 00:28 bzImage
drwxr-xr-x 2 root root 4096 12-31 19:27 dev
-rw-r--r-- 1 root root 3900 2010-01-01 dmesg.txt
drwxr-xr-x 3 root root 4096 01-01 13:03 etc
drwxr-xr-x 2 root root 4096 01-01 13:07 mnt
drwxr-xr-x 2 root root 4096 12-31 19:25 proc
drwxr-xr-x 2 root root 4096 12-31 19:20 sbin
drwxr-xr-x 2 root root 4096 12-31 19:25 sys
drwxr-xr-x 4 root root 4096 12-31 19:20 usr
以上文件夹打包给大家
[root@goldway cdrom]# tar jcf ../rootfs.tar.bz2 *
[root@goldway cdrom]# ll ..
总计 1508
drwxr-xr-x 10 root root 4096 2010-01-01 cdrom
-rw-r--r-- 1 root root 1516709 01-01 13:14 rootfs.tar.bz2
drwxr-xr-x 2 root root 4096 12-11 19:14 usb
drwxr-xr-x 6 root root 4096 11-07 17:38 windows
以下为grub引导设置:
[root@goldway ~]# cat /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You do not have a /boot partition. This means that
# all kernel and initrd paths are relative to /, eg.
# root (hd0,9)
# kernel /boot/vmlinuz-version ro root=/dev/hda10
# initrd /boot/initrd-version.img
#boot=/dev/hda
default=2
timeout=5
splashimage=(hd0,9)/boot/grub/splash.xpm.gz
hiddenmenu
title kernel test
root (hd0,8)
kernel /bzImage ro root=/dev/hda9
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,9)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=LABEL=/1 rhgb quiet
initrd /boot/initrd-2.6.18-1.2798.fc6.img
title Other
rootnoverify (hd0,0)
chainloader +1