一. 最小系统要求
1. 接受键盘输入,能显示字符信息
2. 能处理普通linux发行版中的大部分基本命令,如:ls, mv…
3. 具有网络功能
4. 不使用ramdisk, 所需驱动全部编译进内核
二. 主机环境
1. VMware 7.0 / windows 7
2. 主机系统Ubuntu 10.04 x86
3. 最小系统内核版本 linux-2.6.38
三. 详细构建过程
1. 下载linux-2.6.38内核
- #cd /usr/src
- #wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.38.tar.gz
- #tar xvf linux-2.6.38.tar.gz
- #cd linux-2.6.38
2.编译内核
这一步主要就是内核的配置,如果配置错了可能导致系统无法启动,因此需要非常仔细。
- #make mrproper
- #make allnoconfig
- #make menuconfig
首先利用lspci查看硬件信息:
- #lspci
00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX – 82443BX/ZX/DX Host bridge (rev 01)
00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX – 82443BX/ZX/DX AGP bridge (rev 01)
00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10)
00:0f.0 VGA compatible controller: VMware SVGA II Adapter
00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)
00:11.0 PCI bridge: VMware PCI bridge (rev 02)
00:15.0 PCI bridge: VMware PCI Express Root Port (rev 01)
………………. ……………………. …………………..
00:18.6 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01)
02:00.0 USB Controller: Intel Corporation 82371AB/EB/MB PIIX4 USB
02:01.0 Ethernet controller: Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE] (rev 10)
02:02.0 Multimedia audio controller: Ensoniq ES1371 [AudioPCI-97] (rev 02)
02:03.0 USB Controller: VMware USB2 EHCI Controller
根据查到的硬件型号配置.config文件
以下详细介绍内核的配置
General setup —>
[*] System V IPC
[*] Enable loadable module support —>
Processor type and features —>
Processor family (Pentium Pro) —>
[*] Generuc x86 support
Bus options (PCI etc.) —>
[*] PCI support
PCI access mode (Any) —>
Executable file formats / Emulations —>
[*] Kernel support for ELF binaries
[*] Write ELF core dumps with partial segments
[*] Networking support —>
Networking options —>
<*> Packet socket
<*> Unix domain sockets (NEW)
[*] TCP/IP networking
<*> IP: IPsec transport mode (NEW)
<*> IP: IPsec tunnel mode (NEW)
<*> IP: IPsec BEET mode (NEW)
<*> Large Receive Offload (ipv4/tcp) (NEW)
<*> INET: socket monitoring interface (NEW)
Device Drivers —>
[*] Block devices —>
<*> Loopback device support
SCSI device support —>
<*> SCSI device support
[*] legacy /proc/scsi/ support (NEW)
<*> SCSI disk support
[*] SCSI low-level drivers (NEW) —>
<*> BusLogic SCSI support
[*] Fusion MPT device support —>
<*> Fusion MPT ScsiHost drivers for SPI
<*> Fusion MPT ScsiHost drivers for FC
<*> Fusion MPT ScsiHost drivers for SAS
<*> Fusion MPT misc device (ioctl) driver
[*] Network device support —>
[*] Ethernet (10 or 100Mbit) —>
[*] EISA, VLB, PCI and on board controllers
<*> AMD PCnet32 PCI support
Graphics support —>
<*> Support for frame buffer devices —>
File systems —>
<*> Second extended fs support
[*] Ext2 extended attributes
[*] Ext2 POSIX Access Control Lists
[*] Ext2 Security Labels
[*] Ext2 execute in place support
<*> Ext3 journalling file system support
[*] Default to ‘data=ordered’ in ext3 (NEW)
[*] Ext3 extended attributes (NEW)
[*] Ext3 POSIX Access Control Lists
[*] Ext3 Security Labels
CD-ROM/DVD Filesystems —>
<*> ISO 9660 CDROM file system support
<*> Native language support —>
<*> Codepage 437 (United States, Canada)
<*> Simplified Chinese charset (CP936, GB2312)
配置完成后就可以开始编译了:
- #make bzImage
注意:在make menuconfig的时候一定要选对磁盘驱动,否则会出现kernel panic — VFS error:
这里我用的是SCSI的磁盘,IDE的要做相应修改。3. 制作根文件系统
为简单起见,我们利用busybox制用根文件系统。
- #wget http://busybox.net/downloads/busybox-1.18.4.tar.bz2
- #tar xvf busybox-1.18.4.tar.bz2
- #cd busybox-1.18.4
- #make menuconfig
下面是需要编译进busybox的功能选项。
General Configuration应该选的选项
Don’t use /usr
这个选项一定要选,否则make install 后busybox将安装在原系统的/usr下,这将覆盖掉系统原有的命令。
Build Options
Build BusyBox as a static binary (no shared libs)
这个选项也是一定要选择的,这样才能把busybox编译成静态链接的可执行文件,运行时才独立于其他函数库.否则必需要其他库文件才能运行,在单一个linux内核不能使它正常工作。
其它选项都是一些linux基本命令选项,自己需要哪些命令就编译进去,一般用默认的就可以了,配置好后退出并保存。
编译并安装busybox
- #make
- #make install
make install后会在busybox目录下生成一个叫_install的目录,里面有busybox和指向它的链接。
接下来给虚拟机添加一块SCSI磁盘并分区,假设根分区为sdb1
- #mount /dev/sdb1 /mnt 将根分区挂载到/mnt
- #cp _install/* /mnt/ 将_install下的文件全复制到sdb1
- #rm -f linuxrc
- #cp -r ./examples/bootfloppy/etc /mnt 将etc下的配置文件拷到sdb1下
- #cd /mnt/
- #mkdir proc mnt usr var tmp dev sys 创建目录
- #cp -a /dev/{console,tty,tty2} dev/
最后重启一下
- #reboot
如果一切顺利,就会出现下图的信息, —– 启动成功。
在上一节中,我们制作的内核已经具有网络功能,但是需要经过手动配置。
配置过程如下:
首先,我的VMware用 nat方式上网,并且开启了dhcp功能。nat网段为192.168.130.0/255.255.255.0 网关为192.168.130.2
于是在my linux-2.6.38中输入以下命令
- #ifconfig eth0 192.168.130.22 netmask 255.255.255.0 broadcast 192.168.130.255 up 设置IP地址
- #route add default gw 192.168.130.2 添加到网关的默认路由
- #ping 74.125.71.99 测试外网地址(www.linuxidc.com)
如果能ping通说明网络设置成功。
注意,这里ping的时候不能ping www.linuxidc.com,因为还没有设置nameseaver
如果每次启动系统都要手动配置网络上很不方便的。因此,我们考虑做一个网络初始化脚本。当然,直接在脚本中执行上面手动配置的命令也是可以的,但我不想那么做。
参考CentOS 5.4的网络初始化过程,我们可以写一个简化版的:
- #!/bin/sh
- #
- # network Bring up/down networking
- #
- # config: /etc/network.conf
- # /etc/network.d/ifcfg-[devname]
- if [ ! -f /etc/network.conf ]; then
- exit 0
- fi
- . /etc/network.conf
- [ “${NETWORKING}” = “no” ] && exit 0
- case “$1” in
- start)
- for i in /etc/network.d/ifcfg-*
- do
- if [ -r “$i” ]; then
- . $i
- if [ “$DHCP” = “yes” ]; then
- echo -n “Starting DHCP for interface $INTERFACE: “
- udhcpc -b -i “$INTERFACE” \
- -p “/var/run/udhcpc.$INTERFACE.pid” \
- > /dev/null
- else
- echo -n “Setting up interface $INTERFACE: “
- ifconfig “$INTERFACE” “$IPADDRESS” \
- netmask “$NETMASK” \
- broadcast “$BROADCAST” up
- fi
- fi
- done
- if [ “$USE_GATEWAY” = “yes” -a -n “$GATEWAY” ]; then
- echo -n “Setting default route: “
- route add default gw $GATEWAY
- fi
- ;;
- stop)
- if [ “$USE_GATEWAY” = “yes” -a -n “$GATEWAY” ]; then
- echo -n “Removing default route: “
- route del -net 0.0.0.0
- fi
- for i in /etc/network.d/ifcfg-*
- do
- if [ -r “$i” ]; then
- . $i
- echo -n “Shutting down interface $INTERFACE: “
- ifconfig $INTERFACE down
- if [ “$DHCP” = “yes” ]; then
- kill `cat “/var/run/udhcpc.$INTERFACE.pid”`
- sleep 1
- fi
- fi
- done
- ;;
- restart)
- $0 stop
- $0 start
- ;;
- status)
- ifconfig
- ;;
- *)
- echo “Usage: $0 {start|stop|restart|status}”
- exit 1
- esac
以上脚本需要:
1. /etc/network.conf配置文件
- NETWORKING=yes
- USE_GATEWAY=yes
- GATEWAY=192.168.130.2
2. /etc/network.d/ifcfg-eth0 eth0接口的配置文件
- DHCP=no
- INTERFACE=eth0
- IPADDRESS=192.168.130.22
- NETMASK=255.255.255.0
- BROADCAST=192.168.130.255
3. 新建/var/run/目录
注意:如果你是在windows编辑以上文件,请注意完成之后将windows下的回车换行符(\r\n)替换为linux下的换行符(\n),否则脚本执行可能会出错。
好了,需要的文件都编写好了,现在将文件拷到相应的目录下,在/etc/init.d/rcS文件中加入一行
- /etc/network start
就可以了。
重启
- #reboot
测试网络
- #ifconfig
- #ping 74.125.71.99