搭建虚拟机环境:
配置服务器:192.168.1.64
一、搭建环境
1、关闭配置服务器,添加一块硬盘,用来存放mini Linux系统(大小建议10G左右,名字为mini.vmdk),启动配置服务器。
2、安装编译环境所需的软件
[[email protected] ~]# yum groupinstall "Development Tools" "Server Platform development" -y
二、配置Mini Linux
1、基本配置
[[email protected] ~]# tar xvf linux-3.10.10.tar.xz -C /usr/src
[[email protected] ~]# cd /usr/src/
[[email protected] src]# ln -sv linux-3.10.10 linux
"linux" -> "linux-3.10.10"
[[email protected] src]# cd linux
[[email protected] linux]# make allnoconfig
查看硬盘接口型号:
[[email protected] ~]# 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:16.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:17.0 PCI bridge: VMware PCI Express Root Port (rev 01)
00:18.0 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, Inc. [AMD] 79c970 [PCnet32 LANCE] (rev 10)
02:02.0 Multimedia audio controller: Ensoniq ES1371 / Creative Labs CT2518 [AudioPCI-97] (rev 02)
02:03.0 USB controller: VMware USB2 EHCI Controller
2、编辑内核
make menuconfig
[[email protected] linux]# make menuconfig
2.1选择内核
[*] 64-bit kernel
2.2支持模块装载
[*] Enable loadable module support --->
2.2.1、支持模块动态卸载
[*] Module unloading
2.3 选择处理器类型和特点
Processor type and features --->
2.3.1 选择处理器家族
Processor family (Generic-x86-64) --->
(X)Generic-x86-64
2.3.2 支持多处理器
[*] Symmetric multi-processing support
2.4 配置总线
Bus options (PCI etc.) --->
2.4.1 支持PCI总线
[*] PCI support
2.5 设备驱动
Device Drivers
2.5.1 支持SCSI设备
SCSI device support --->
2.5.1.1 支持SCSI device support
<*> SCSI device support
2.5.1.2 支持SCSI磁盘
<*> SCSI disk support
2.5.2 存储设备驱动
[*] Fusion MPTdevice support --->
2.5.2.1
<*> Fusion MTP ScsiHost drivers for SPI
2.5.2.2
<*> Fusion MPT misc device (ioctl) driver
2.5.2.3
为了确保万无一失,把剩下的也选上
2.6 保存配置文件
2.7、编译内核
[[email protected] linux]# make -j 4 bzImage
……
Setup is 13628 bytes (padded to 13824 bytes).
System is 1660 kB
CRC f0025ad7
Kernel: arch/x86/boot/bzImage is ready (#1)
3、对新添硬盘分区、复制内核信息
共两个分区
sdb1 100M
sdb2 2G
分区过程略
[[email protected] linux]# mke2fs -t ext4 /dev/sdb1
[[email protected] linux]# mke2fs -t ext4 /dev/sdb2
[[email protected] ~]# mkdir /mnt/{boot,sysroot}
[[email protected] ~]# mount /dev/sdb1 /mnt/boot/
[[email protected]ocalhost ~]# grub-install --root-directory=/mnt /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install‘.
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
[[email protected] ~]# ls /mnt/boot/
grub lost+found
[[email protected] ~]# ls /mnt/boot/grub/
device.map fat_stage1_5 iso9660_stage1_5 minix_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
e2fs_stage1_5 ffs_stage1_5 jfs_stage1_5 reiserfs_stage1_5 stage2 vstafs_stage1_5
[[email protected] ~]# mount /dev/sdb2 /mnt/sysroot/
[[email protected] ~]# cd /usr/src/linux
[[email protected] linux]# cp arch/x86/boot/bzImage /mnt/boot/bzImage
[[email protected] linux]# file /mnt/boot/bzImage
/mnt/boot/bzImage: Linux kernel x86 boot executable bzImage, version 3.10.10 ([email protected], RO-rootFS, swap_dev 0x1, Normal VGA
[[email protected] linux]# vim /mnt/boot/grub/grub.conf
default=0
timeout=3
title Mini Linux (3.10.10)
root (hd0,0)
kernel /bzImage ro root=/dev/sda2
保存退出
[[email protected] ~]# sync
[[email protected] ~]# sync #同步硬盘信息
挂起配置服务器
4、配置测试服务器
新建一个测试服务器:
配置:RAM:256M,硬盘:配置服务器的第二块硬盘(sdb),启动
错误信息如下:
No filesystem could Mount root,tried
Kernel panic
错误原因:没有加载文件系统模块
关闭测试服务器,启用配置服务器。
5、重新编辑内核
5.1、配置文件系统模块
[[email protected] ~]# cd /usr/src/linux
[[email protected] linux]# make menuconfig
File systems --->
5.1.1、支持的文件系统格式
可选:
<*> Second extended fs support #ext2
<*> Ext3 journalling file system support
[] Ext4 POSIX Access Control List (NEW) #ACL
必选:
<*> The Extended 4 (ext4) filesystem
5.2、配置文件格式
Executable file formats / Emulations --->
5.2.1、支持的文件格式
[*] Kernel support for ELF binaries
[*] Kernel support for scripts starting with #!
5.3、编译内核
[[email protected] linux]# make -j 4 bzImage
Kernel: arch/x86/boot/bzImage is ready (#2)
[[email protected] linux]# cp arch/x86/boot/bzImage /mnt/boot/
cp:是否覆盖"/mnt/boot/bzImage"? y
[[email protected] linux]# sync
[[email protected] linux]# sync
6、启动测试机,测试配置
挂起配置服务器,启动测试机,错误信息如下
error:could‘t Mount because of unsupported optional features
Kernel panic - not syncing: No init found.
错误原因:找不到根文件系统
关闭测试机启动,启动配置服务器
7、配置根文件系统
7.1、创建根文件目录
查看main.c文件,了解init程序启动顺序
[[email protected] linux]# less /usr/src/linux/init/main.c
if (execute_command) {
if (!run_init_process(execute_command))
return 0;
pr_err("Failed to execute %s. Attempting defaults...\n",
execute_command);
}
if (!run_init_process("/sbin/init") ||
!run_init_process("/etc/init") ||
!run_init_process("/bin/init") ||
!run_init_process("/bin/sh"))
return 0;
panic("No init found. Try passing init= option to kernel. "
"See Linux Documentation/init.txt for guidance.");
}
[[email protected] linux]# cd /mnt/sysroot/
[[email protected] sysroot]# ls
lost+found
[[email protected] sysroot]# mkdir -pv etc dev proc sys bin sbin usr/{bin,sbin,lib,lib64} lib64 lib/modules home var/{log,run,lock} tmp mnt media root
7.2、复制根文件系统程序及其依赖的库文件脚本
[[email protected] sysroot]# cd
[[email protected] ~]# vim bincp.sh
#!/bin/bash
target=/mnt/sysroot
[ -d $target ] || mkdir /mnt/sysroot
read -p "A command: " command
libcp() {
for lib in $(ldd $1 | grep -o "[^[:space:]]*/lib[^[:space:]]*")
do
libdir=$(dirname $lib)
[ -d $target$libdir ] || mkdir -p $target$libdir
[ -f $target$lib ] || cp $lib $target$lib
done
}
while [ "$command" != ‘quit‘ ];do
if ! which $command &> /dev/null;then
read -p "No such command,enter again: " command
continue
fi
command=$(which --skip-alias $command)
cmnddir=$(dirname $command)
[ -d $cmnddir ] || mkdir -p $target$cmnddir
[ -f $target$command ] || cp $command $target$command
libcp $command
read -p "Another command(quit): " command
done
[[email protected] ~]# bash bincp.sh
A command: bash
Another command(quit): ls
Another command(quit): quit
[[email protected] ~]# tree /mnt/sysroot/
/mnt/sysroot/
├── bin
│?? ├── bash
│?? └── ls
测试bash
[[email protected] ~]# chroot /mnt/sysroot/ /bin/bash
bash-4.1# ls
bin dev etc home lib lib64 media mnt proc root sbin sys tmp usr var
bash-4.1# exit
exit
[[email protected] ~]#
7.3、编辑grub.conf文件
[[email protected] ~]# vim /mnt/boot/grub/grub.conf
default=0
timeout=3
title Mini Linux (3.10.10)
root (hd0,0)
kernel /bzImage ro root=/dev/sda2 init=/sbin/init
保存退出
[[email protected] ~]# sync
[[email protected] ~]# sync
8、挂起配置服务器,启动测试机
错误信息:
mousedev:PS/2 mouse device common for all Mice
错误原因:
没有键盘、鼠标驱动,无法输入
关闭测试机,启动配置服务器
9、重新配置内核
9.1、编辑内核
[[email protected] ~]# cd /usr/src/linux
[[email protected] linux]# make menuconfig
9.1.1、编辑设备驱动
Device Drivers
9.1.1.1、输入设备支持
Input device support
9.1.1.1.1、键盘
Keyboards
9.1.1.1.2、鼠标
Mice
9.1.1.2、USB支持
[*] USB support
查看系统USB类型
[[email protected] linux]# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
USB支持
必选:
<*> Support for Host-side USB
<*> XHCI HCD (USB 3.0) support (NEW)
<*> EHCI HCD (USB 2.0) support
<*> UHCI HCD (most Intel and VIA) support
<*> OHCI HCD support
保存配置退出
9.2、编译内核
[[email protected] linux]# cd /usr/src/linux
[[email protected] linux]# make -j 4 bzImage
Setup is 13628 bytes (padded to 13824 bytes).
System is 2143 kB
CRC 8b205de8
Kernel: arch/x86/boot/bzImage is ready (#3)
[[email protected] linux]# cp arch/x86/boot/bzImage /mnt/boot/
cp:是否覆盖"/mnt/boot/bzImage"? y
[[email protected] linux]# sync
[[email protected] linux]# sync
挂起配置服务器,开启测试机
10、编辑内核成功,如下图所示
三、用户init启动系统
1、编辑配置
[[email protected] ~]# cd /mnt/sysroot/
[[email protected] sysroot]# ls
bin dev etc home lib lib64 lost+found media mnt proc root sbin sys tmp usr var
[[email protected] sysroot]# vim sbin/init
#!/bin/bash
echo -e "\tWelcome to \033[32mMini\033[0m Linux"
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
mount -n -o remount,rw /dev/sda2 /
/bin/bash
保存退出
[[email protected] sysroot]# cd
[[email protected] ~]# bash bincp.sh
A command: blkid
Another command(quit): touch
Another command(quit): top
Another command(quit): ps
Another command(quit): quit
2、编辑配置
[[email protected] ~]# cd /usr/src/linux
[[email protected] linux]# make menconfig
2.1、设备驱动
Device Drivers
2.1.1、
Generic Driver Options
2.1.1.1、
[*] Maintain a devtmpfs filesystem to mount at /dev
[*] Automount devtmpfs at /dev, after the kernel mounted the ro
保存退出
3、编译配置
[[email protected] linux]# make -j 4 bzImage
Setup is 13628 bytes (padded to 13824 bytes).
System is 2142 kB
CRC ac7aa859
Kernel: arch/x86/boot/bzImage is ready (#6)
[[email protected] linux]# chmod +x /mnt/sysroot/sbin/init
[[email protected] linux]# cp arch/x86/boot/bzImage /mnt/boot/
cp:是否覆盖"/mnt/boot/bzImage"? y
[[email protected] linux]# sync
[[email protected] linux]# sync
关闭配置服务器,启动测试机