自制linux系统


本文主要通过裁剪现有Linux系统,打造一个属于自己的Linux小系统,让其能够装载网卡驱动,并配置IP地址,实现网络功能。

自制linux系统

步骤概述:

1、新建一个硬盘
2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区
3、格式化并且挂载两个分区
4、安装grub至目标磁盘
5、为grub提供配置文件
6、复制内核文件和initrd文件
7、创建目标主机根文件系统
8、移植bash命令和其库文件到根文件系统
9、装载模块,实现网络功能
10、启动测试

特别提醒

如果在vmvare上做此实验,在新建虚拟机创建新磁盘的时候,一定要选“Store virtual disk as a single file”,否则,也会出现内核恐慌kennel panic。

步骤演示详解过程:

1、新建一个硬盘

2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区

首先,我们要在目标磁盘上分两个区,并进行格式化。第一个分区100M,用来装引导程序;第二个分区19G,用来装根文件系统。然后再进行挂载操作,将/dev/sdb1挂载到/mnt/boot下,将/dev/sdb2挂载到/mnt/root下。

sdb                   8:16   0    20G  0 disk 
├─sdb1                8:17   0 109.8M  0 part
└─sdb2                8:18   0  19.9G  0 part
[root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdd8058c2

  Device Boot     Start         End      Blocks  Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15        2610    20852370   83  Linux

3、格式化并且挂载两个分区

注意,其中boot分区的挂载目录一定是boot名字的目录

[root@localhost ~]# mkfs.ext4 /dev/sdb1
[root@localhost ~]# mkfs.ext4 /dev/sdb2
[root@localhost ~]# mkdir -p /mnt/{root,boot}
[root@localhost ~]# ll /mnt/
total 8
drwxr-xr-x 2 root root 4096 Jul 25 08:52 boot
drwxr-xr-x 2 root root 4096 Jul 25 08:52 root
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/root/
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdb1             103M  1.6M   96M   2% /mnt/boot
/dev/sdb2              20G   44M   19G   1% /mnt/root

4、安装grub至目标磁盘

一个系统能启动,就需要引导,所以我们首先要安装一个grub引导程序到我们的新磁盘上,安装grub引导程序主要有两个命令,一个是grub-install,另一个是setup,这里最好使用grub-install来安装。因为:

①grub-install会安装grub引导第二阶段的文件 
②setup不会安装第二阶段的引导程序,是安装引导信息到MBR
//第二个需要注意的地方就是--root-directory=后面接的路径应该是boot目录所在的地方,而不是/mnt/boot,因为boot目录在mnt下;目标磁盘是/dev/sdb
[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 ~]# cd /mnt/boot/
[root@localhost boot]# ll
total 13
drwxr-xr-x 2 root root  1024 Jul 25 08:58 grub
drwx------ 2 root root 12288 Jul 25 08:51 lost+found

安装完grub后,进入grub目录,会发现没有grub.conf配置文件,这样就导致我们的引导程序是不健全的,所以我们需要手动写一个配置文件在里边。

5、为grub提供配置文件

上面移植了内核和initrd文件,我们就可以根据内核版本和initrd版本来编写grub.conf配置文件了:

default=0
timeout=5
title CentOS to ZhangHe Soft-Linux
root (hd0,0)
kernel /vmlinz-soft ro root=/dev/sda2 quiet selinux=0 init=/bin/bash
initrd /initramfs-soft.img

quiet是静默安装,不再显示安装时的一大堆信息。后面要把selinux关掉,而且init要使用/bin/bash,告诉内核不要再去找init程序了。如果不指定这一步,在启动过程中就会报kernel panic(内核恐慌),以为系统就它一个了,没有init进程,恐慌的不行。

6、复制内核文件和initrd文件

init是系统中用来产生其它所有进程的程序。它以守护进程的方式存在,其进程号为1,init是所有进程的父进程,老祖宗,所以不移植是不行的。它通过调用/etc/inittab这个配置文件,然后再去执行/etc/rc.d/rc.sysinit的系统初始化脚本。 
将内核文件和initrd文件复制到/dev/sdb下的boot目录中。

[root@localhost grub]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz-soft
[root@localhost grub]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs-soft.img

7、创建目标主机根文件系统

使用命令行展开创建文件系统

[root@localhost grub]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}
mkdir:
created directory `/mnt/sysroot'
mkdir:
created directory `/mnt/sysroot/etc'
mkdir:
created directory `/mnt/sysroot/etc/rc.d'
mkdir:
created directory `/mnt/sysroot/usr'
mkdir:
created directory `/mnt/sysroot/var'
mkdir:
created directory `/mnt/sysroot/proc'
mkdir:
created directory `/mnt/sysroot/sys'
mkdir:
created directory `/mnt/sysroot/dev'
mkdir:
created directory `/mnt/sysroot/lib'
mkdir:
created directory `/mnt/sysroot/lib64'
mkdir:
created directory `/mnt/sysroot/bin'
mkdir:
created directory `/mnt/sysroot/sbin'
mkdir:
created directory `/mnt/sysroot/boot'
mkdir:
created directory `/mnt/sysroot/srv'
mkdir:
created directory `/mnt/sysroot/mnt'
mkdir:
created directory `/mnt/sysroot/media'
mkdir:
created directory `/mnt/sysroot/home'
mkdir:
created directory `/mnt/sysroot/root'
[root@localhost mnt]# ls root/
bin  boot dev etc home lib lib64 lost+found media mnt proc root sbin srv sys usr var

8、移植bash命令和其库文件到根文件系统

[root@localhost scripts]# bash cporder.sh 
Enter a command: bash
Enter a command: shutdown
Enter a command: vim
Enter a command: reboot
Enter a command: touch
Enter a command: mkdir
Enter a command: ls
Enter a command: rm
Enter a command: cat
Enter a command: less
Enter a command: tree
Enter a command: ifconfig
Enter a command: ip
Enter a command: route
Enter a command: ping
Enter a command: quit
quit

附:命令移植脚本

#!/bin/bash  
#  
target=/mnt/root  
clearCmd() {  
if which $cmd &> /dev/null; then  
cmdPath=`which --skip-alias $cmd`  
else  
echo "No such command"  
return 5  
fi  
}  
cmdCopy() {  
cmdDir=`dirname $1`  
[ -d ${target}${cmdDir} ] || mkdir -p ${target}${cmdDir}  
[ -f ${target}${1} ] || cp $1 ${target}${cmdDir}  
}  
libCopy() {  
for lib in `ldd $1 | grep -o "/[^[:space:]]\{1,\}"`; do  
libDir=`dirname $lib`  
[ -d ${target}${libDir} ] || mkdir -p ${target}${libDir}  
[ -f ${target}${lib} ] || cp $lib ${target}${libDir}  
done  
}  
while true; do  
read -p "Enter a command: " cmd  
if [ "$cmd" == 'quit' ] ;then  
echo "quit"  
exit 0  
fi  
clearCmd $cmd  
[ $? -eq 5 ] && continue  
cmdCopy $cmdPath  
libCopy $cmdPath  
done  

9、装载模块,实现网络功能

1、查看宿主机的网卡模块信息

[root@localhost ~]# lsmod | grep e1000
e1000                 134863  0

2、查看网卡的详细信息

[root@localhost ~]# modinfo e1000
filename:       /lib/modules/2.6.32-642.el6.x86_64/kernel/drivers/net/e1000/e1000.ko
version:        7.3.21-k8-NAPI
license:        GPL
description:    Intel(R) PRO/1000 Network Driver
author:         Intel Corporation, <linux.nics@intel.com>
srcversion:     A911791C4EFC2A93BCFCF6A
alias:          pci:v00008086d00002E6Esv*sd*bc*sc*i*
alias:          pci:v00008086d000010B5sv*sd*bc*sc*i*
alias:          pci:v00008086d00001099sv*sd*bc*sc*i*
alias:          pci:v00008086d0000108Asv*sd*bc*sc*i*
这里查询到了网卡模块的路径,把它复制到/dev/sdb的库文件下:
[root@localhost ~]# mkdir /mnt/root/lib64/modules
[root@localhost ~]# cp /lib/modules/2.6.32-642.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/root/lib64/modules/e1000.ko

3、init程序 
现在虽然是模块复制过去了,但是还不能用,而且现在也不满足我们的流程需要,因为连最起码的init程序都没有,如果我们想要这个init,有两个选择,第一,移植宿主系统的,但是格式会复杂一些;所以我们还是先自己动手写脚本吧,把脚本当作init来用,能够让小系统跑起来。init一般在sbin目录下,所以我们要在/dev/sdb2这个分区上编写一个init脚本。

[root@localhost ~]# cd /mnt/root/sbin
[root@localhost sbin]# cd /mnt/root/sbin/
#!/bin/bash
#print Welcome info
echo -e "Welcome to \033[34m nmshuishui soft-Linux\033[0m"
#mount wei wenjian system when the system is running.
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
#mount ethernet driver autl when the system is start.
insmod /lib64/modules/e1000.ko
[ $? -eq 0 ] && echo -e "Load e1000 module succeeded                    [\033[32m0K\033[0m]"
ifconfig lo 172.0.0.1/8
ifconfig eth0 172.16.251.235/16
#mount the /dev/sda2 to make it can be write and read.
mount -n -o remount,rw /dev/sda2 /
#run /bin/bash
/bin/bash

写完这个init脚本后,我们要把我们要给其一个执行权限,让其能够被执行;此脚本中还用到mount,insmod这些命令,所以要用上一个脚本把这些命令移植过去。最后还需要把/mnt/boot/grub/grub.conf中的init=/bin/bash换成init=/sbin/init,因为我现在要用这个init脚本来执行系统启动了,再也不需让/bin/bash来替换了。

10、启动测试

上面的步骤完成后,就可以把/dev/sdb挂到另一台主机上体验我们的私人订制小系统了。

GIF.gif

原创文章,作者:M20_he,如若转载,请注明出处:http://www.178linux.com/46018

(1)
上一篇 2016-09-13 09:50
下一篇 2016-09-13 11:06

相关推荐

  • 磁盘分区和文件系统管理

    磁盘分区 两种分区方式:MBR,GPT  MBR: Master Boot Record,1982年,使用32位表示扇区 数,分区不超过2T  如何分区:按柱面      0磁道0扇区:512bytes      446bytes: boo…

    Linux干货 2016-08-29
  • Tomcat性能优化-2

    修改Connector运行模式 启用NIO 或 APR bio介绍 bio(blocking I/O,阻塞式I/O操作),表示Tomcat使用的是传统的Java I/O操作 默认模式,性能最差,没有经过任何优化处理和支持 nio模式 nio(non-blocking I/O),Java SE 1.4…

    Linux干货 2016-09-19
  • 网络通信安全基础OpenSSL

    OpenSSL: NIST: 保密性: 数据保密性 隐私性 完整性: 数据完整性 系统完整性 可用性  安全攻击: 被动攻击:窃听 主动攻击:伪装、重放、消息篡改、拒绝服  安全机制: 加密、数字签名、访问控制、数据完整性、认证交换、流量填充、路由控制、公证 安全服务: 认证 访问控制 数据保密性 连接保密性 无连接保密性 选择域保密性 …

    Linux干货 2015-09-06
  • Linux系统发展史

    引言:     Linux是一类Unix计算机操作系统的统称。Linux操作系统的内核的名字也是“Linux”。Linux操作系统也是自由软件和开放源代码发展中最著名的例子。严格来讲,Linux这个词本身只表示Linux内核,但在实际上人们已经习惯了用Linux来形容整个基于Linux内核,并且使用GNU 工程各种工具…

    Linux干货 2016-10-19
  • CentOS系统安装

    centos系统安装        安装程序:anaconda,Anaconda是RedHat、CentOS、Fedora等Linux的安装管理程序。它可以提供文本、图形等安装管理方式,并支持Kickstart等脚本提供自动安装的功能。此外,其还支持许多启动参数,熟悉这些参数可为安装带来很多方便。该程序的功能是把位于光…

    Linux干货 2016-09-23
  • HA Cluste 的配置:keepalived:vrrp协议 主备模型

    keepalived:vrrp协议 主备模型 第一步配置出始环境: 准备两节点: 主节点:172.18.57.7 long1 备节点:172.18.57.8 long2 (1) 各节点时间必须同步; 同步时间: ~]# yum -y install chrony ~]# vim /etc/chrony.conf ~]# systemctl start chr…

    Linux干货 2017-05-15