Linux系统裁剪和定制

操作系统运行情况:

SSH客户端访问:

HTTP浏览器访问页面:

2 资源下载
2.1 操作系统内核kernel:linux-3.12.26
https://download.csdn.net/download/shenlongqing/12387854
2.2 busybox:busybox-1.22.1
https://download.csdn.net/download/shenlongqing/12387864
2.3 dropbear:dropbear-2014.65
https://download.csdn.net/download/shenlongqing/12387847
2.4 nginx:nginx-1.4.5
https://download.csdn.net/download/shenlongqing/12387856
3 工具准备
3.1 VMware Workstation
产品:VMware Workstation 14 Pro
版本:14.1.3 build-9474260

3.2 MobaXterm
产品:免费的SSH客户端工具
版本:Personal Edition v11.1 Build 3860

3.3 CenterOS 6.5 64位
版本号:Linux hostname.localhost 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

4 移植步骤
[注]启动流程参考:
linux系统启动流程
POST-->BIOS(BOOT Sequence)-->MBR(bootloader,446个字节)-->kernel-->initrd(initramfs)-->(ROORFS)/sbin/init(/etc/inittab)
以上linux启动流程只是针对于centos6系列基于upstart流程的2.6内核,centos7使用了stytemd和此稍有不同。
可参考网页:
https://www.cnblogs.com/sysk/p/4778976.html
4.1 在VMware Workstation中安装CenterOS6.5 64位

版本号:Linux hostname.localhost 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
4.2 准备硬盘
为虚拟机准备一块1G容量(大小也可自定)硬盘,并为硬盘分区。磁盘文件:C:\VirtualMachines\CentOS64\CentOS64_Portable.vmdk (路径和名字可自定)

(1)为硬盘分区,在我的虚拟机中新加的硬盘为/dev/sdb,将此硬盘分区,其中/dev/sdb1=50M, 剩余的容量全部给 /dev/sdb2,分区操作命令:
echo -e "n \n p \n 1 \n \n +50M \n n \n p \n 2 \n \n \n w \n" | fdisk /dev/sdb
(2)将分区格式化,格式化操作命令:
mke2fs -t ext2 /dev/sdb1
mke2fs -t ext2 /dev/sdb2
(3)挂载磁盘并安装grub引导程序:
mkdir /mnt/boot
mount /dev/sdb1 /mnt/boot
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
4.3 编译内核,添加到硬盘中
(1)安装必要的工具和依赖包:
yum install ncurses-devel
[注]必要的工具:perl、bc、gcc
若有必要:
yum groupinstall "Development Libraries" " Development Tools"
(2)下载并解压源码包,操作命令:
cp linux-3.12.26.tar.xz /usr/src
xz -d linux-3.12.26.tar.xz
tar -xvf linux-3.12.26.tar
ln -sv /usr/src/linux-3.12.26/ /usr/src/linux
make allnoconfig
make menuconfig
(3)进行内核编译配置:

I)首页:

II)进入 General setup 选项

III)返回首页,进入Processor type and features

IV)进入 Processor family (Generic-x86-64) 选择 cpu 类型 默认即可
V)返回首页,进入 Bus options (PCI etc.)

VI)返回首页,返回首界面进入 Executable file formats / Emulations

VII)返回首页,进入Networking support--->Networking options

VIII)返回首页,进入 Device Drivers --->Generic Driver Options

IX)返回上级页面


X)进入SCSI device support

XI)进入SCSI low-level dirvers --->


XII)返回上级页面,进入 Network device support---> Ethernet driver support (NEW)

XIII)返回到 Network device support 页面,选择 Input device support

XIV)返回上级添加 usb 支持 USB support

XV)返回首页 File systems

XVI)退出保存配置
(4)编译并存入硬盘,操作命令:
make
make bzImage
cp ./arch/x86/boot/bzImage /mnt/boot
4.4 添加 rootfs 及二进制程序(由 busybox 提供)
(1)下载解压源码包,操作命令:
tar -xf busybox-1.22.1.tar.bz2 -C /usr/src/
cd /usr/src/busybox-1.22.1/
make menuconfig
(2)进行busybox编译配置:

进入 Busybox Settings ---->Build Options
此处需要选择 :
Busybox Settings --> Build Options --> Build BusyBox as a static binary (no shared libs)
(3)安装glibc静态支持,操作命令:
yum install glibc-static
(4)编译并存入硬盘,操作命令:
make
make install
mount /dev/sdb2 /mnt/sysroot/
cp -a ./_install/* /mnt/sysroot/
mkdir -pv /mnt/sysroot/{etc,boot,root,mnt,media,proc,sys,dev,home,lib,lib64,var,tmp}
[注] 由于 busybox 启动时默认的 init 程序为 /etc/init.d/rcS
mkdir -pv /mnt/sysroot/etc/init.d
vim /mnt/sysroot/etc/init.d/rcS
输入如下内容并保存:
#!/bin/sh
mount -n -t ext2 -o remount,rw /dev/sda2 /
mount -n -t ext2 /dev/sda1 /boot
mount -n -t tmpfs none /dev
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
mkdir /dev/pts/
mount -n -t devpts devpts /dev/pts
[ -r /etc/sysconfig/network ]&&source /etc/sysconfig/network
if [ ! -z $HOSTNAME ];then
/bin/hostname $HOSTNAME
else
/bin/hostname localhost
fi
# 设置网卡IP地址
#ifconfig eth0 192.168.147.22 netmask 255.255.255.0 up
#ifconfig lo 127.0.0.0 netmask 255.0.0.0
#sleep 3
# 启动ssh服务
#/usr/local/sbin/dropbear -E &
# 启动nginx服务
#/usr/local/nginx/sbin/nginx &
chmod +x /mnt/sysroot/etc/init.d/rcS
vim /mnt/sysroot/etc/profile
输入如下内容并保存:
export PS1="[\u@\h \w]\$"
mkdir -pv /mnt/sysroot/etc/sysconfig
vim /mnt/sysroot/etc/sysconfig/network
输入如下内容并保存:
HOSTNAME=www.autolinux.com
4.5 编辑grub配置文件
vim /mnt/boot/grub/grub.conf
输入如下内容并保存:
default=0
timeout=5
title auto-busy-box-1.0
root(hd0,0)
kernel /bzImage ro root=/dev/sda2
4.6 测试是否能够正常启动
新建虚拟机,设置硬盘为4.2步配置的磁盘文件C:\VirtualMachines\CentOS64\CentOS64_Portable.vmdk (路径和名字按实际配置):

上电后显示grub界面:

进入auto-busy-box-1.0系统,参考界面如下:

4.7 进一步,使其能够以用户名和密码登录
(1)关闭4.6步测试时打开的虚拟机
(2)回到4.1步创建的虚拟机,打开虚拟机,登录进入CenterOS6.5 64位系统
(3)设置登录终端,操作命令:
mount /dev/sdb2 /mnt/sysroot
vim /mnt/sysroot/etc/inittab
输入如下内容并保存:
::sysinit:/etc/init.d/rcS
tty1::respawn:/sbin/getty 19200 tty1
tty2::respawn:/sbin/getty 19200 tty2
(4)设置登录用户名和密码,此处直接复制CenterOS6.5 64位系统中所有用户和密码,操作命令:
cp -p /etc/passwd /mnt/sysroot/etc/
cp -p /etc/shadow /mnt/sysroot/etc/
cp -p /etc/group /mnt/sysroot/etc/
cp -p /etc/gshadow /mnt/sysroot/etc/
(5)编辑 /mnt/sysroot/etc/passwd 将登录 shell 改为 /bin/sh
(6)测试是否成功
4.8 增加SSH远程连接功能
(1)下载解压dropbear源码包,操作命令:
mount /dev/sdb2 /mnt/sysroot
cd /usr/src
tar –zxvf dropbear_2014.65.tar.gz
(2)编译安装 dropbear ( ssh ),操作命令:
cd dropbear-2014.65
./configure
make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp"
make install
[注] 默认安装在 /usr/local/{bin,sbin}
I)将此应用移植到目标系统,移植脚本如下,port.sh
#!/bin/bash
#
MntDIR1="/mnt/boot"
MntDIR2="/mnt/sysroot"
PORAPP ()
{
read -p "Plz input the app: " APPLI
until [ "$APPLI" == quit ];do
while true;do
if which --skip-alias $APPLI ;then
WHERE=$(which --skip-alias $APPLI)
DIRAPP=$(dirname $WHERE)
[ -d ${MntDIR2}${DIRAPP} ]||mkdir -p ${MntDIR2}${DIRAPP}
[ -e ${MntDIR2}${WHERE} ]||cp $WHERE ${MntDIR2}${DIRAPP}
LIBS=$(ldd $WHERE|grep -oE "(/[[:alnum:]]*/[^[:space:]]*[[:space:]])")
for i in $LIBS;do
DIRLIB=$(dirname $i)
[ -d ${MntDIR2}$DIRLIB ]||mkdir -p ${MntDIR2}$DIRLIB
cp $i ${MntDIR2}$i
done
read -p "Do you want to another app? or quit: " APPLI
continue 2
else
read -p "Input is not app!! Plz input again or quit: " APPLI
continue 2
fi
done
done
}
##################MAIN##################
MAIN ()
{
PORAPP
}
MAIN
运行脚本:bash port.sh
Plz input the app: /usr/local/bin/dbclient
/usr/local/bin/dbclient
Do you want to another app? or quit: /usr/local/bin/dropbearkey
/usr/local/bin/dropbearkey
Do you want to another app? or quit: /usr/local/bin/dropbearconvert
/usr/local/bin/dropbearconvert
Do you want to another app? or quit: /usr/local/sbin/dropbear
/usr/local/sbin/dropbear
Do you want to another app? or quit: quit
II)查看移植结果,操作命令:
ls /mnt/sysroot/usr/local/{bin,sbin}
/mnt/sysroot/usr/local/bin:
dbclient dropbearconvert dropbearkey
/mnt/sysroot/usr/local/sbin:
dropbear
ls /mnt/sysroot/lib64/
ld-linux-x86-64.so.2 libcrypt.so.1 libc.so.6 libdl.so.2 libfreebl3.so libutil.so.1 libz.so.1
III)创建主机密钥,操作命令:
mkdir /mnt/sysroot/etc/dropbear
dropbearkey-t dss -f /mnt/sysroot/etc/dropbear/dropbear_dss_host_key
dropbearkey -t rsa -f /mnt/sysroot/etc/dropbear/dropbear_rsa_host_key
(3)增加 libnss 库文件,操作命令:
find /lib64/libnss* -exec cp {} /mnt/sysroot/lib64/ \;
find /usr/lib64/libnss* -exec cp {} /mnt/sysroot/usr/lib64/ \;
(4)增加/etc/nsswitch.conf文件
vim /mnt/sysroot/etc/nsswitch.conf
输入如下内容并保存:
passwd: files
shadow: files
group: files
(5)启用网络功能,操作命令:
vim /mnt/sysroot/etc/init.d/rcS
增加如下内容并保存:
ifconfig eth0 192.168.253.22 netmask 255.255.255.0 up
ifconfig lo 127.0.0.0 netmask 255.0.0.0
[注]IP地址根据虚拟机中地址修改,确保客户机和宿主机位于同一网段,可正常通信。
(6)测试是否成功
进入auto-busy-box-1.0系统,登录。
输入命令:
/usr/local/sbin/dropbear -E

4.9 增加nginx服务
(1)下载解压源码包,操作命令:
mount /dev/sdb2 /mnt/sysroot
cd /usr/src
tar –zxvf nginx-1.4.5.tar.gz
(2)编译安装,操作命令:
cd nginx-1.4.5
./confgiure --prefix=/usr/local/nginx --without-http_rewrite_module
make
make install
(3)运行4.8中第二步的移植脚本
输入
/usr/local/nginx/sbin/nginx
[注]将nginx及其依赖库文件全部拷贝出来
cp -a -r /usr/local/nginx/* /mnt/sysroot/usr/local/nginx/
[注]将nginx运行相关配置文件、临时文件和临时目录全部拷贝出来
(4)测试是否成功
进入auto-busy-box-1.0系统。
客户端测试结果:
