搭建虚拟机环境:

 配置服务器:192.168.1.64


一、搭建环境

1、关闭配置服务器,添加一块硬盘,用来存放mini Linux系统(大小建议10G左右,名字为mini.vmdk),启动配置服务器。

2、安装编译环境所需的软件

[root@localhost ~]# yum groupinstall "Development Tools" "Server Platform development" -y


二、配置Mini Linux

1、基本配置

[root@localhost ~]# tar xvf linux-3.10.10.tar.xz -C /usr/src

[root@localhost ~]# cd /usr/src/

[root@localhost src]# ln -sv linux-3.10.10 linux

"linux" -> "linux-3.10.10"

[root@localhost src]# cd linux

[root@localhost linux]# make allnoconfig


查看硬盘接口型号:

[root@localhost ~]# 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

[root@localhost linux]# make menuconfig

2.1选择内核

[*] 64-bit kernel

wKioL1ehemTwxMdAAAFQfZW1qJo857.jpg

2.2支持模块装载

[*] Enable loadable module support --->

wKiom1ehekvykAwXAAFTkCl72cM795.jpg

2.2.1、支持模块动态卸载

[*] Module unloading

wKiom1eheybhQQPxAAGD_PvMyt4232.jpg

2.3 选择处理器类型和特点

Processor type and features    --->

wKioL1ehe_KCkT2vAAF59T41-TA081.jpg

2.3.1    选择处理器家族

Processor family (Generic-x86-64) --->

wKiom1ehfXSwHCNtAAGuzNrWrWk037.jpg

(X)Generic-x86-64

wKiom1ehhq7CWvlFAAEMCU-6nKQ978.jpg

2.3.2 支持多处理器

[*] Symmetric multi-processing support

wKioL1ehh3-R1QCgAAF5VqiZtY8009.jpg

2.4 配置总线

    Bus options (PCI etc.) --->

wKioL1ehiEyBB3ZuAAGVUv5H7Xw256.jpg

2.4.1 支持PCI总线

[*] PCI support

wKioL1ehiP3jF_yIAAFztB5ILng610.jpg

2.5 设备驱动

Device Drivers

wKiom1ehiZmhazFjAAFQJiU9dFs471.jpg


2.5.1 支持SCSI设备

SCSI  device support  --->

wKiom1ehis6DCR42AAGkRx1GX9c353.jpg

2.5.1.1 支持SCSI device support

<*> SCSI device support

wKioL1eht5iQYMvDAAGc3acD-p8191.jpg

2.5.1.2 支持SCSI磁盘

<*> SCSI disk support

wKiom1ehuDXBRGDFAAF1KlCosfg552.jpg

2.5.2 存储设备驱动

[*] Fusion MPTdevice support    --->

wKioL1ehjWuh5j7gAAGl6y9xVeg293.jpg

2.5.2.1

<*> Fusion MTP ScsiHost drivers for SPI

wKioL1ehui3hHNgXAAGi_smv3lY102.jpg

2.5.2.2

<*> Fusion MPT misc device (ioctl) driver

wKioL1ehut7iDf9wAAFZH3EdjA4554.jpg

2.5.2.3

为了确保万无一失,把剩下的也选上

wKioL1ehu3Oheg-OAAF2fOUokXo043.jpg

2.6 保存配置文件

wKiom1ehvC6CpU8EAAB4DYYdauY871.jpg

2.7、编译内核

[root@localhost 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

分区过程略

[root@localhost linux]# mke2fs -t ext4 /dev/sdb1

[root@localhost linux]# mke2fs -t ext4 /dev/sdb2

[root@localhost ~]# mkdir /mnt/{boot,sysroot}

[root@localhost ~]# mount /dev/sdb1 /mnt/boot/

[root@localhost ~]# 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

[root@localhost ~]# ls /mnt/boot/

grub  lost+found

[root@localhost ~]# 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

[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/

[root@localhost ~]# cd /usr/src/linux

[root@localhost linux]# cp arch/x86/boot/bzImage /mnt/boot/bzImage

[root@localhost linux]# file /mnt/boot/bzImage

/mnt/boot/bzImage: Linux kernel x86 boot executable bzImage, version 3.10.10 (root@localhost.localdo, RO-rootFS, swap_dev 0x1, Normal VGA

[root@localhost 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

保存退出


[root@localhost ~]# sync        

[root@localhost ~]# sync        #同步硬盘信息

挂起配置服务器


4、配置测试服务器

新建一个测试服务器:

    配置:RAM:256M,硬盘:配置服务器的第二块硬盘(sdb),启动

错误信息如下:

No filesystem could Mount root,tried

Kernel panic

错误原因:没有加载文件系统模块

wKiom1eiiWHxM0fMAAHihS0Gjxc782.jpg

关闭测试服务器,启用配置服务器。

5、重新编辑内核

5.1、配置文件系统模块

[root@localhost ~]# cd /usr/src/linux

[root@localhost linux]# make menuconfig

File systems    --->

wKioL1eijHjwp_dcAAFfBmgEhgE513.jpg

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

wKiom1eijTzztmaNAAHMOxw-XY8137.jpg

5.2、配置文件格式

Executable file formats / Emulations    --->

wKiom1eijtnDRT2-AAGWqnxz8m4973.jpg

5.2.1、支持的文件格式

[*] Kernel support for ELF binaries

[*] Kernel support for scripts starting with #!

wKiom1eij27hSYxUAAE-vElCeK4581.jpg

5.3、编译内核

[root@localhost linux]# make -j 4 bzImage

Kernel: arch/x86/boot/bzImage is ready  (#2)

[root@localhost linux]# cp arch/x86/boot/bzImage /mnt/boot/

cp:是否覆盖"/mnt/boot/bzImage"? y

[root@localhost linux]# sync

[root@localhost linux]# sync

6、启动测试机,测试配置

挂起配置服务器,启动测试机,错误信息如下

error:could't Mount because of unsupported optional features

Kernel panic - not syncing: No init found.

错误原因:找不到根文件系统

wKioL1eik6LRCEXnAAIzqpQbWN0354.jpg

关闭测试机启动,启动配置服务器

7、配置根文件系统

7.1、创建根文件目录

查看main.c文件,了解init程序启动顺序

[root@localhost 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.");

}

[root@localhost linux]# cd /mnt/sysroot/

[root@localhost sysroot]# ls

lost+found

[root@localhost 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、复制根文件系统程序及其依赖的库文件脚本

[root@localhost sysroot]# cd

[root@localhost ~]# 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

[root@localhost ~]# bash bincp.sh 

A command: bash

Another command(quit): ls

Another command(quit): quit

[root@localhost ~]# tree /mnt/sysroot/

/mnt/sysroot/

├── bin

│?? ├── bash

│?? └── ls


测试bash

[root@localhost ~]# 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

[root@localhost ~]# 

7.3、编辑grub.conf文件

[root@localhost ~]# 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

保存退出

[root@localhost ~]# sync

[root@localhost ~]# sync

8、挂起配置服务器,启动测试机

错误信息:

mousedev:PS/2 mouse device common for all Mice

错误原因:

没有键盘、鼠标驱动,无法输入

wKiom1eiwjeT9vVKAAIOpqJQgWA889.jpg

关闭测试机,启动配置服务器

9、重新配置内核

9.1、编辑内核

[root@localhost ~]# cd /usr/src/linux

[root@localhost linux]# make menuconfig

9.1.1、编辑设备驱动

Device Drivers

wKioL1eixA2R3OBBAAGQb9L1Aeg320.jpg

9.1.1.1、输入设备支持

Input device support

wKioL1eixMaBXUP0AAFfatnnNmk561.jpg

9.1.1.1.1、键盘

Keyboards

wKioL1eixTPx0SOoAAGFhFKuYrA519.jpg

wKiom1eixZ-ToAGxAAEwtPXkGdI790.jpg

9.1.1.1.2、鼠标

Mice

wKiom1eixf_g_1W_AAFCJwL2mmg541.jpg

9.1.1.2、USB支持

[*] USB support

wKiom1eix2DC658CAAFbDu2rHqQ997.jpg

查看系统USB类型

[root@localhost 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

wKiom1eiySjiH6XHAAGrJqf70r0359.jpg

保存配置退出

9.2、编译内核

[root@localhost linux]# cd /usr/src/linux

[root@localhost 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)

[root@localhost linux]# cp arch/x86/boot/bzImage /mnt/boot/

cp:是否覆盖"/mnt/boot/bzImage"? y

[root@localhost linux]# sync

[root@localhost linux]# sync

挂起配置服务器,开启测试机

10、编辑内核成功,如下图所示

wKiom1eizObjB_Y_AAGpM4H23tw547.jpg

三、用户init启动系统

1、编辑配置

[root@localhost ~]# cd /mnt/sysroot/

[root@localhost sysroot]# ls

bin  dev  etc  home  lib  lib64  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr  var

[root@localhost 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

保存退出


[root@localhost sysroot]# cd

[root@localhost ~]# bash bincp.sh

A command: blkid

Another command(quit): touch

Another command(quit): top

Another command(quit): ps

Another command(quit): quit


2、编辑配置

[root@localhost ~]# cd /usr/src/linux

[root@localhost linux]# make menconfig

2.1、设备驱动

Device Drivers

wKioL1ei1syRx24CAAFOrf5mruE936.jpg

2.1.1、

Generic Driver Options

wKiom1ei1ybi-fIOAAGlKcObAtg248.jpg

2.1.1.1、

[*] Maintain a devtmpfs filesystem to mount at /dev

[*] Automount devtmpfs at /dev, after the kernel mounted the ro

wKioL1ei18viHk6eAAGHuEAs0Yk832.jpg

保存退出

3、编译配置

[root@localhost 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)

[root@localhost linux]# chmod +x /mnt/sysroot/sbin/init

[root@localhost linux]# cp arch/x86/boot/bzImage /mnt/boot/

cp:是否覆盖"/mnt/boot/bzImage"? y

[root@localhost linux]# sync

[root@localhost linux]# sync

关闭配置服务器,启动测试机