自制小型Linux系统
前期准备
组成部分:grub,内核,根系统运行程序以及依赖库
可用的centos6 系统和一块新硬盘(C:\VMs\CentOS6.8.2\CentOS6.8.1-0.vmdk)
操作步骤
1 分区并创建文件系统
注意:分两个必要分区,/dev/sdb1 对应/boot ;/dev/sdb2 对应根 /
[root@centos6 ~]# echo -e 'n\np\n1\n\n+1G\nw\n' |fdisk /dev/sdb
[root@centos6 ~]# echo -e 'n\np\n2\n\n\n\nw\n' |fdisk /dev/sdb
[root@centos6 ~]# mkfs.ext4 /dev/sdb1
[root@centos6 ~]# mkfs.ext4 /dev/sdb2
2 挂载boot
注意:子目录必须为boot
[root@centos6 ~]# mkdir /mnt/boot
[root@centos6 ~]# mount /dev/sdb1 /mnt/boot
3 安装grub
必须加参数 --root-directory,否则就安装到当前/boot 中了
[root@centos6 ~]# grub-install --root-directory=/mnt/ /dev/sdb
4 准备内核和initramfs 文件
[root@centos6 ~]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz
[root@centos6 ~]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img
5 创建grub.conf 配置文件
[root@centos6 ~]# vim /mnt/boot/grub/grub.conf
[root@centos6 ~]# cat /mnt/boot/grub/grub.conf
default=0
timeout=8
title dawn linux
root (hd0,0)
kernel /vmlinuz root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
#查看当前准备的文件
[root@centos6 ~]# tree /mnt/boot
/mnt/boot
├── grub
│ ├── device.map
│ ├── e2fs_stage1_5
│ ├── fat_stage1_5
│ ├── ffs_stage1_5
│ ├── grub.conf
│ ├── iso9660_stage1_5
│ ├── jfs_stage1_5
│ ├── minix_stage1_5
│ ├── reiserfs_stage1_5
│ ├── stage1
│ ├── stage2
│ ├── ufs2_stage1_5
│ ├── vstafs_stage1_5
│ └── xfs_stage1_5
├── initramfs.img
├── lost+found
└── vmlinuz
2 directories, 16 files
[root@centos6 ~]#
6 准备根目录中的相关程序和库
[root@centos6 ~]# mkdir /mnt/sysroot
[root@centos6 ~]# mount /dev/sdb2 /mnt/sysroot
[root@centos6 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 97.7G 0 part /
├─sda3 8:3 0 29.3G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1G 0 part /mnt/boot
└─sdb2 8:18 0 19G 0 part /mnt/sysroot
#创建根目录中相关的库
[root@centos6 ~]# mkdir -pv /mnt/sysroot/{bin,boot,dev,etc,home,lib,lib64,media,mnt,opt,proc,root,sbin,selinux,srv,sys,tmp,urs,var}
#创建根目录中相关的程序,如:bash,ps,pstree,ifconfig,insmod,ping,mount,ls,cat,df,lsblk,blkid,tree,fdisk
[root@centos6 ~]# bash /data/scripts/copycmd.sh
Please input a command: bash
Please input a command or quit: ps
Please input a command or quit: pstree
Please input a command or quit: ifconfig
Please input a command or quit: insmod
Please input a command or quit: ping
Please input a command or quit: mount
Please input a command or quit: ls
Please input a command or quit: cat
Please input a command or quit: df
Please input a command or quit: lsblk
Please input a command or quit: blkid
Please input a command or quit: tree
Please input a command or quit: fdisk
Please input a command or quit: quit
#将数据同步到磁盘中
[root@centos6 ~]# sync
[root@centos6 ~]#
# 准备网卡驱动
[root@centos6 ~]# ethtool -i eth0
driver: e1000
version: 7.3.21-k8-NAPI
firmware-version:
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
# 获取网卡驱动的路径
[root@centos6 ~]# modinfo e1000
filename: /lib/modules/2.6.32-642.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
version: 7.3.21-k8-NAPI
......省略部分内容......
# 将网络驱动文件拷贝到自制linux的根目录中
[root@centos6 ~]# cp /lib/modules/2.6.32-642.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/
6.1 拷贝命令的脚本
#!/bin/bash
ch_root="/mnt/sysroot"
[ ! -d $ch_root ] && mkdir $ch_root
bincopy() {
if which $1 &>/dev/null; then
local cmd_path=`which --skip-alias $1`
local bin_dir=`dirname $cmd_path`
[ -d ${ch_root}${bin_dir} ] || mkdir -p ${ch_root}${bin_dir}
[ -f ${ch_root}${cmd_path} ] || cp $cmd_path ${ch_root}${bin_dir}
return 0
else
echo "Command not found."
return 1
fi
}
libcopy() {
local lib_list=$(ldd `which --skip-alias $1` | grep -Eo '/[^[:space:]]+')
for loop in $lib_list;do
local lib_dir=`dirname $loop`
[ -d ${ch_root}${lib_dir} ] || mkdir -p ${ch_root}${lib_dir}
[ -f ${ch_root}${loop} ] || cp $loop ${ch_root}${lib_dir}
done
}
read -p "Please input a command: " command
while [ "$command" != "quit" ];do
if bincopy $command ;then
libcopy $command
fi
read -p "Please input a command or quit: " command
done
7 准备新的虚拟机
将原虚拟机中的sdb硬盘(C:\VMs\CentOS6.8.2\CentOS6.8.1-0.vmdk) 文件增加到新虚拟机中,删除新虚拟机原有磁盘,开机启动
8 配置网卡驱动,并设置ip
# 配置网卡驱动
insmod /lib64/e1000.ko
# 查看网卡
ifconfig -a
# 设置ip
ifconfig eth0 10.0.0.166/24
9 测试,是否跟其他主机能够连接
其他主机ping 该主机