自己动手制作(DIY)一个Mini-Linux系统
寫在前面:如果此文有幸被某位朋友看見并發現有錯的地方,希望批評指正。如有不明白的地方,愿可一起探討。
本文主要目的
????本文并不是要真正的去構建一個自己的Linux的系統,而是通過制作Linux系統的過程中,更加清晰的理解Linux系統的啟動流程,實踐檢驗真理嘛。
Linux系統啟動的簡要流程
????為了明白制作Linux系統的簡單步驟,下面給出Linux系統啟動的簡要流程圖:
????
????本文主要進行的步驟:
????1.制作grub,編輯grub.conf配置文件;
????2.編譯內核并將其復制到相應目錄;
????3.編輯/sbin/init并配置其執行環境;
準備工作
?在虛擬宿主機上添加一塊大小為10G的SCSI磁盤(Cl7)并將其分區格式化(具體如何添加,這里就不進行說明了,望理解):
#?fdisk?/dev/sdbn?p?1??+50M?n?p?2??+512M?n?p?3??+512M?t?3?82?w #?kpartx?-af?/dev/sdb #?partx?-a?/dev/sdb #?mke2fs?-t?ext4?/dev/sdb1 #?mke2fs?-t?ext4?/dev/sdb2 #?mkswap?/dev/sdb3 #?mkdir?/mnt/boot #?mkdir?/mnt/sysroot #?mount?/dev/sdb1?/mnt/boot #?mount?/dev/sdb2?/mnt/sysroot再添一臺虛擬主機(Cl7),并將虛擬宿主機中所添加的磁盤作為其啟動盤
提示:在新建虛擬主機(Cl7)過程中,當到達"Select a Disk"對話框這一步時,選擇"Use an existing virtual disk"選項;點擊Next后點擊Browse...按鈕找到虛擬宿主機所添加的那塊磁盤即可。
安裝GRUB并編輯grub.conf配置文件
安裝GRUB
[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編輯grub.conf配置文件
[root@localhost ~]# vim /mnt/boot/grub/grub.conf
default=0 timeout=5 title?Mini-Linux?(3.13.6-cl7)root?(hd0,0)kernel?/bzImage?ro?root=/dev/sda2?init=/sbin/init將數據同步到磁盤
#?sync #?sync #?sync掛起虛擬宿主機,啟動Cl7這臺虛擬主機看看效果
看到了嗎?這紅色框里就是我們在grub.conf里所編輯的title那行!
呀,怎么文件沒有找到?找到了才不正常呢!因為我們根本就沒有提供這個文件哦。接下來,我們就來定制、編譯內核并將其添加進來。
定制并編譯內核
#?tar?xf?linux-3.13.6.tar.xz?-C?/usr/src/ #?cd?/usr/src/ #?ln?-sv?linux-3.13.6/?linux #?cd?linux #?make?allnoconfig #?make?menuconfig
接下來就是選擇所需要的內核模塊,*表示將其編譯進內核,除了默認選項外,本文需要添加內容
[*]?64-bit?kernelGeneral?setup?--->()Local?version?-?append?to?kernel?release-cl7[*]?System?V?IPC [*]?Enable?loadable?module?support?--->[*]?Module?unloading? -*-?Enable?the?block?layer?--->[*]?Block?layer?SG?support?v4 Processor?type?and?features?--->[*]?Symmetric?multi-processing?supportProcessor?family?(Generic-x86-64)?--->(X)?Core?2/newer?Xeon Bus?options?(PCI?etc.)?--->[*]?PCI?support? Executable?file?formats?/?Emulations?--->[*]?Kernel?support?for?ELF?banaries[*]?Kernel?support?for?scripts?starting?with?#! [*]?Networking?support?--->[?]?Wireless?----Networking?options?---><*>?Unix?domain?sockets[*]?TCP/IP?networking[*]?IP:?multicasting[*]?IP:?advanced?router[*]?TCP:?advanced?congestion?control?---><?>?The?IPv6?protocol?---- Device?Drivers?--->Generic?Driver?Options?--->[*]?Maintain?a?devtmpfs?filesystem?to?mount?at?/dev[*]?Automount?devtmpts?at?/dev,?after?the?kernel?mounted?the?rootfsSCSI?device?support?---><*>?SCSI?device?support<*>?SCSI?disk?support[*]?Fusion?MPT?device?support?---><*>?Fusion?MPT?ScsiHost?drivers?for?SPI[*]?Fusion?MPT?logging?facility[*]?Network?device?support?--->[*]?Ethernet?driver?support?--->[*]?Intel?devices<*>?Intel?(R)?PRO/1000?Gigabit?Ethernet?support<*>?Intel?(R)?PRO/1000?PCI-Express?Gigabit?Ethernet?support其他選項全部去掉[?]?Wireless?LAN?----Input?device?support?---><*>?Mouse?interface[*]?Keyboards?--->[*]?Mice?--->[*]?USB?support?---><*>?Support?for?Host-side?USB<*>?xHCI?HCD?(USB?3.0)?support<*>?EHCI?HCD?(USB?2.0)?support<*>?OHCI?HCD?(USB?1.1)?support<*>?UHCI?HCD?(most?Intel?and?VIA)?support File?systems?---><*>?The?Extended?4?(ext4)?filesystem保存退出,編譯內核
將數據同步到磁盤
#?sync #?sync #?sync掛起虛擬宿主機,啟動Cl7這臺虛擬主機看看效果
紅框里的意思為:執行/sbin/init失敗,究其原因在于沒有找到init。這就對了,因為我們根本就沒有提供這個文件,因此,接下來,我們就來提供此文件。
編輯/sbin/init并配置其執行環境
建立根文件系統文件
編輯init文件
[root@localhost ~]# vim /mnt/sysroot/sbin/init
#!/bin/bash echo?-e?"Welcome?to?\033[35mMini-Linux\033[0m" mount?-n?-t?proc?proc?/proc mount?-n?-t?sysfs?sysfs?/sys mount?-n?-t?devtmpfs?none?/dev mount?-n?-o?remount,rw?/dev/sda2/bin/bash設置init為可執行并檢查其是否有語法錯誤
在init文件中要使用需要bash來執行程序,因此我們得為其提供執行環境。本文使用bincp.sh腳本將命令及其所以來的庫復制到相應的目錄中,bincp.sh腳本如下:
#!/bin/bash # target=/mnt/sysroot/ [?-d?$target?]?||?mkdir?$target preCommand()?{if?which?$1?&>?/dev/null;?thencommandPath=`which?--skip-alias?$1`return?0elseecho?"No?such?command."return?1fi } 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:]]+"`;?dolibDir=`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'?];?doif?preCommand?$command?;?thencommandCopy?$commandPathlibCopy?$commandPathfiread?-p?"Plz?enter?a?command:?"?command done執行bincp.sh腳本,復制幾個命令
[root@localhost ~]# bash bincp.sh
Plz?enter?a?command:?bash Plz?enter?a?command:?ls Plz?enter?a?command:?mount Plz?enter?a?command:?umount Plz?enter?a?command:?quit將數據同步到磁盤
#?sync #?sync #?sync掛起虛擬宿主機,啟動Cl7這臺虛擬主機看看效果
在上圖中,將鼠標點擊進去并敲Enter鍵得到bash提示符
仔細查看上圖,其實bash提示符在敲Enter鍵之前已經出現了,只是被后來的信息淹沒在浩瀚的海洋中,不容易被察覺而已!!!
以上內容僅僅是提供了一個bash程序以及系init腳本,對于系統服務以及獲得bash提示符之前的很多內容并沒有添加進來。接下來的工作就是為這個Mini-Linux系統提供系統服務和bash執行環境等內容。
轉載于:https://blog.51cto.com/muluhe/1545584
總結
以上是生活随笔為你收集整理的自己动手制作(DIY)一个Mini-Linux系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ICallbackEventHandle
- 下一篇: sql常用函数总结