對於Linux愛好者來說自己手動定做一個屬於自己的Linux系統是一件多麼酷的事。
那麼接下來我就給大家演示一下怎麼製作一個屬於自己的Linux吧。
爲什麼要製作屬於自己的Linux呢?
我們目前用的Linux系統都是從網上或者官方那裏得到的,是屬於一種通用版,並不是根據自己的硬件特性定製的,不能最大的發揮Linux系統的優越性。因此這是不能滿足我們這些Linux發燒友,製作屬於自己的Linux讓其發揮最大優越性能吧。
準備工作:我們製作的Linux是根據我們的硬件本身量身定製的,因此在製作前我們需要清楚的知道我們的目標主機(我們要安裝微型Linux的主機)的硬件信息,製作系統前我們需要在宿主機(製作Linux的主機)上完成對系統的製作
整理一下心情讓我們開始吧!!!
1、步驟規劃
A、爲內核提供CPU+Memory+Disk
B、提供文件系統
C、提供bash
D、提供init, --> bash
E、提供輸入設備
F、提供TCP/IP協議棧,網絡功能
一、查看設備硬件信息
[root@jsh ~]# cat /proc/cpuinfo 查看CPU信息 processor: 0 vendor_id: GenuineIntel cpu family: 6 model: 42 model name: Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz 酷睿i5-2450M的 stepping: 7 cpu MHz: 2494.379 cache size: 3072 KB physical id: 0 siblings: 2 core id: 0 cpu cores: 2 雙核心 apicid: 0 initial apicid: 0 fpu: yes fpu_exception: yes cpuid level: 13 wp: yes [root@jsh ~]# cat /proc/meminfo 查看內存信息 [root@jsh ~]# lspci 查看橋設備信息 [root@jsh ~]# lsusb 查看usb信息
下載內核源代碼:www.kernel.org
我們此次實驗使用的是3.13.6版本的內核源碼
二、爲目標磁盤創建文件系統
們製作的Linux系統首先是存放在宿主機的一塊硬盤上,製作好後把此硬盤給目標機,因此我們首先需要在宿主機上添加一塊硬盤然後對此硬盤創建文件系統。
添加硬盤步驟
注:添加完要重新啓動虛擬機纔會識別此硬盤
添加完成後接下來就是創建文件系統了。
[root@jsh ~]# fdisk /dev/sdb 看好自己添加的是哪一塊硬盤,此處我添加的是sbd Command (m for help): n 創建分區 Command action e extended p primary partition (1-4) P 創建主分區 Partition number (1-4): 1 創建第一個主分區 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +50M 給第一個主分區分配50M空間用來存放內核 Command (m for help): n 再創建一個分區 Command action e extended p primary partition (1-4) P 創建主分區 Partition number (1-4): 2 創建第二個主分區 First cylinder (8-2610, default 8): Using default value 8 Last cylinder, +cylinders or +size{K,M,G} (8-2610, default 2610): +512M 給第二個主分區分配512M空間來存放根文件系統 Command (m for help): w 保存並退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@jsh ~]# mke2fs -t ext4 /dev/sdb{1,2} 格式化sdb1和sdb2,文件系統爲ext4
創建兩個目錄,讓這兩個分區各自掛載
[root@jsh ~]# mkdir /mnt/{boot,sysroot} 在/mnt下創建boot和sysroot兩個目錄 [root@jsh ~]# mount /dev/sdb1 /mnt/boot/ 把sdb1掛載至/mnt/boot/ [root@jsh ~]# mount /dev/sdb2 /mnt/sysroot/ 把sdb2掛載至/mnt/sysroot/
爲目標機提供根文件系統
[root@jsh ~]# cd /mnt/sysroot/
注:在此目錄下我們有一個腳本,是用來複製程序以及程序所依賴的庫文件用的腳本
給大家提供這個腳本的代碼
[root@jsh sysroot]# vim bincp.sh #!/bin/bash # target=/mnt/sysroot/ [ -d $target ] || mkdir $target #!/bin/bash # target=/mnt/sysroot/ [ -d $target ] || mkdir $target preCommand() { if which $1 &> /dev/null; then commandPath=`which --skip-alias $1` return 0 else echo "No such command." return 1 fi } commandCopy() { commandDir=`dirname $1` [ -d ${target}${commandDir} ] || mkdir -p ${target}${commandDir} [ -f ${target}${commandPath} ] || cp $1 ${target}${commandDir} } libCopy() { for lib in `ldd $1 | egrep -o "/[^[:space:]]+"`; do libDir=`dirname $lib` [ -d ${target}${libDir} ] || mkdir -p ${target}${libDir} [ -f ${target}${lib} ] || cp $lib ${target}${libDir} done } read -p "Plz enter a command: " command until [ "$command" == 'quit' ]; do if preCommand $command ; then commandCopy $commandPath libCopy $commandPath fi read -p "Plz enter a command: " command done
注:若複製此腳本的話那麼之前創建的文件路徑必須與我們之前創建的路徑一樣,否則此腳本執行時會報錯的。
[root@jsh sysroot]# chmod +x bincp.sh 給此腳本一個執行權限 [root@jsh sysroot]# bash bincp.sh 執行此腳本 Plz enter a command: bash 複製bash程序 Plz enter a command: cat 複製cat命令 Plz enter a command: ls 複製ls命令 Plz enter a command: ifconfig 複製ifconfig命令 Plz enter a command: quit 退出 [root@jsh sysroot]#
爲目標機提供根下的目錄
[root@jsh sysroot]# mkdir -pv etc proc sys dev usr var tmp home root boot sbin
三、爲目標磁盤安裝grub
[root@jsh ~]# grub-install --root-directory=/mnt /dev/sdb [root@jsh ~]# ls /mnt/boot/ grub lost+found 此時我們就能看到boot目錄下有一個grub目錄
四、編譯內核
然後我們給grub提供配置文件就行了,配置文件在內核源碼中,因此我們需要解壓內核源碼
此內核是我之前下載好的
[root@jsh ~]# tar xf linux-3.13.6.tar.xz -C /usr/src/ 我們把它解壓到/usr/src/下
[root@jsh linux]# make menuconfig 執行此命令,去選擇我們用到的功能
注:執行此步驟會彈出一個圖形化界面,如果你的系統不支持圖形化操作請自行安裝開發環境
接下來我們就該選磁盤功能了
然後就可以保存退出了
[root@jsh linux]# make bzImage 編譯內核
注:編譯可能要花費一些時間,我們不防這會兒給提供一下grub配置文件
給grub提供配置文件
[root@jsh ~]# cd /mnt/boot/grub [root@jsh grub]# vim grub.conf timeout=5 default=0 title ce shi linux (3.13.6) root (hd0,0) kernel /bzImage ro root=/dev/sda2 init=/bin/bash
然後我們再來看一下已經編譯好了
然後把編譯好的內核放在我們之前創建的專門用於存放內核的目錄下
[root@jsh linux]# cp arch/x86/boot/bzImage /mnt/boot/ [root@jsh linux]# sync 然後同步一下 [root@jsh boot]# chmod +x /mnt/boot/bzImage 給執行權限
然後把宿主機掛起創建目標主機就可以測試我們製作的系統是否能用了。
創建目標虛擬機
此時我們的系統已經能夠正常的啓動了,但是由於我們沒有做好輸入設備的驅動,這裏的鼠標鍵盤是不能用的,接下來我們就開始配置內核的輸入設備驅動吧。
首先把目標機關機,然後再次開啓宿主機,(注:之前做目標機測試時我們要把宿主機掛起,現在我們再啓用宿主機)
在宿主機下:
[root@jsh ~]# cd /usr/src/linux [root@jsh linux]# make menuconfig
然後保存退出
[root@jsh linux]# make -j 3 編譯 [root@jsh linux]# cp arch/x86/boot/bzImage /mnt/boot/ cp: overwrite `/mnt/boot/bzImage'? y 覆蓋原文件 [root@jsh linux]# sync 同步
這就完成了鍵盤鼠標的驅動,如果這樣啓動的話還是有一個問題,我們需要讓其啓動時自動運行init,因此我們需要在/sbin下編輯文件
[root@jsh linux]# cd /mnt/sysroot/sbin [root@jsh sbin]# vim init #!/bin/bash echo -e "Welcom to \033[32mce shi\033[34mlinux." mount -n -t proc proc /proc mount -n -t sysfs sysfs /sys mount -n -o remount,rw /dev/sda2 /bin/bash [root@jsh sbin]# chmod +x init 給一個執行權限 [root@jsh sbin]# vim /mnt/boot/grub/grub.conf
然後我們需要移植mount命令
[root@jsh ~]# bash bincp.sh bincp.sh這個腳本要實現創建 Plz enter a command: mount 移植mount命令 Plz enter a command: umount 移植umount命令 Plz enter a command: quit 退出 [root@jsh ~]#
好了,這樣我們就可以啓動一下試試了,首先掛起宿主機。然後開啓目標機
Ok這樣就啓動了,哈哈只不過字體顏色設置的有點奇怪,嘿嘿。
包括我們移植的命令也可以用哦,是不是有點小激動呢,但是總覺得怪怪的,跟我們的正常的系統還是有很多的不一樣。
但是這樣還有一個問題,我們可以看一下我們的設備文件
我們的dev下是空的,那怎麼識別我們的sda1和sda2呢?
注:此時各設備文件是在內核的內存中的
我們還回到宿主機上
[root@jsh ~]# cd /usr/src/linux [root@jsh linux]# make menuconfig
然後保存退出
[root@jsh linux]# make -j 3 [root@jsh linux]# cp arch/x86/boot/bzImage /mnt/boot/ cp: overwrite `/mnt/boot/bzImage'? y
編輯文件手動掛載
[root@jsh ~]# vim /mnt/sysroot/sbin/init
添加紅色方框內容
好我們再來試一下,看看上邊的設備文件有沒有掛載上,首先掛起宿主機,然後再開啓目標機。
OK!成功了,呵呵怎麼樣是不是小有成就呢。
至此,我們的六個步驟中的前五個步驟都已經完成了,接下來就是最後一步了,配置網絡功能。
在宿主機下
[root@jsh ~]# cd /usr/src/linux [root@jsh linux]# make menuconfig
然後保存退出
[root@jsh linux]# make -j 3 [root@jsh linux]# cp arch/x86/boot/bzImage /mnt/boot/ cp: overwrite `/mnt/boot/bzImage'? y
移植一下網絡相關的命令
[root@jsh ~]# bash bincp.sh Plz enter a command: route Plz enter a command: netstat Plz enter a command: ping Plz enter a command: quit
然後就能掛起宿主機,啓動目標機了
OK是不是很有成就感!!!