Linux仍是親生的好,實戰自制Linux操做系統


實戰自制Linux操做系統
linux


本文主要經過裁剪現有Linux系統,打造一個屬於本身的Linux小系統,讓其可以裝載網卡驅動,並配置IP地址,實現網絡功能。sql

自制linux系統

步驟概述:vim

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。bash

wKioL1fXXb7CIt1PAABHjngGLbU781.png

步驟演示詳解過程:網絡

一、新建一個硬盤

二、在該新硬盤上新建兩個分區,一個當boot分區,一個當/分區

首先,咱們要在目標磁盤上分兩個區,並進行格式化。第一個分區100M,用來裝引導程序;第二個分區19G,用來裝根文件系統。而後再進行掛載操做,將/dev/sdb1掛載到/mnt/boot下,將/dev/sdb2掛載到/mnt/root下。less

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

三、格式化而且掛載兩個分區

注意,其中boot分區的掛載目錄必定是boot名字的目錄ide

[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

四、安裝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配置文件,這樣就致使咱們的引導程序是不健全的,因此咱們須要手動寫一個配置文件在裏邊。工具

五、爲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進程,恐慌的不行。

六、複製內核文件和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

七、建立目標主機根文件系統

使用命令行展開建立文件系統

[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

八、移植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  

九、裝載模塊,實現網絡功能

Linux是一個模塊化的操做系統,好多功能組件都是經過模塊化的工具來實現的,並且支持動態裝載和卸載,咱們要是想實現某種功能,只需加載相應的模塊便可,就能夠實現咱們的Linux操做系統大瘦身了。


一、查看宿主機的網卡模塊信息

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

二、查看網卡的詳細信息

[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

三、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來替換了。

十、啓動測試

上面的步驟完成後,就能夠把/dev/sdb掛到另外一臺主機上體驗咱們的私人訂製小系統了。

wKioL1fXXdDhCCPuAACVa6zac2Y467.gif


到此,一個迷你版的Linux誕生了,能夠裝在本身移動U盤等設備上面,這裏不作過多解釋

相關文章
相關標籤/搜索