生活随笔
收集整理的這篇文章主要介紹了
手把手教你从零构建属于自己的小linux
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??????????????????????????????????????? 從零構建屬于自己的小linux
本文講述的是利用一個宿主機一步步根據自己喜好構建屬于自己的一個小型linux系統。
直接切入正題開始構建屬于自己的linux系統。
準備工作:
本次制是在VMware 9 上實現的
宿主機:redhat5.8?; 內核2.6.18-308.el5
一塊10G大小IDE硬盤,10G 已經完全足夠
業務不是很熟練在, 在做裁剪linux? 有寫錯或者使用不當的地方希望各位技術達人和博友 批評指正,也希望此博文能給大家帶來幫助,更希望達人們給予指導。
???????????????????????????? ?!!!!在此先謝過!!!!
制作過程:
制作過程: ?1,添加兩塊磁盤格式化并掛載 ?????#mkdir?-pv?/mnt/{boot,sysroot} ?????#mke2fs?-j?/dev/hda1 ?????#mke2fs?-j?/dev/hda2 ?????#mount?/dev/hda1?/mnt/boot/ ?????#mount?/dev/hda2?/mnt/sysroot/ ?#################?添加內核和grub階段################## ??2,#cp?/boot/vmlinuz-2.6.18-308.el5??/mnt/boot/vmlinuz? ?3,?#mkdir?test ?4,?#cd?test/ ?5,?#zcat?/boot/initrd-2.6.18-308.el5.img?|?cpio?-id???????????????????#展開initrd?文件 ?6,?#vim?init ????找到:?mkrootdev?-t?ext3?-o?defaults,ro?/dev/vol0/root ??????改為?mkrootdev?-t?ext3?-o?defaults,ro?/dev/hda2??????????????????# ?7,?#cd?test/lib/?? ?????rm?-f?dm-* ?8,?#cd?.. ?9,?#find?.?|?cpio?-H?newc??10,?#grub-instasll?-root-directory=/mnt?/dev/hda???????????????????????#生成grub目錄 ?11,?#ls?/mnt/boot ?12,?#vim?/mnt/boot/grub/grub.conf??????????????????????????????????????#手動配置grub引導內核和initrd ?????default=0 ?????timeout=3 ?????title?mageedu?linux(2.6.18) ??????????root(hd0,0) ??????????kernel?/vmlinuz ??????????initrd?/initrd.gz ?14,?#cd?/mnt/sysroot ?15,?#mkdir?etc/{rc.d/init.d}?bin?sbin?proc?sys?dev?lib?root?mnt?media??var/{log,run,lock/subsys,tmp}?usr/{bin,sbin,local}?tmp?home?boot?opt?-pv ?16,?#vim?/etc/inittab?????????????????????????????????????????????????#創建系統啟動等級?和初始化環境執行腳本 ????????id:3:initdefault: ????????si::sysinit:/etc/rc.d/rc.sysinit ?17,?#vim?/etc/rc.d/rc.sysinit?????????????????????????????????????????#配置系統初始化內容?和腳本 ?????????????#!/bin/bash ?????????????echo?-e?"\tWelcome?to?\033[34mMageEdu\033[0m?linux"?????????????/bin/bash ?18,?#chmod?+x?/etc/rc.d/rc.sysinit ?19,?#cd ???#############################?此腳本作用是拷貝小linux上所用到的各種系統命令和依賴包################## ?20,?新建?bincp.sh?腳本??復制命令 ??#!/bin/bash ??# ??DEST=/mnt/sysroot ??libcp()?{ ??LIBPATH=${1%/*} ??[?!?-d?$DEST$LIBPATH?]?&&?mkdir?-p?$DEST$LIBPATH ??[?!?-e?$DEST${1}?]?&&?cp?$1?$DEST$LIBPATH?&&?echo?"copy?lib?$1?finished."??} ???bincp()?{ ??CMDPATH=${1%/*} ??[?!?-d?$DEST$CMDPATH?]?&&?mkdir?-p?$DEST$CMDPATH ??[?!?-e?$DEST${1}?]?&&?cp?$1?$DEST$CMDPATH ???for?LIB?in??`ldd?$1?|?grep?-o?"/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`;?do ??libcp?$LIB ??done ??} ???read?-p?"Your?command:?"?CMD ??until?[?$CMD?==?'q'?];?do ??!?which?$CMD?&&?echo?"Wrong?command"?&&?read?-p?"Input?againe"?CMD?&&?continue??COMMAND=`?which?$CMD?|?grep?-v?"^alias"?|?grep?-o?"[^[:space:]]\{1,\}"` ??bincp?$COMMAND ??echo?"copy?$COMMAND?finished."??read?-p?"Continue:?"?CMD ??done ?21,?運行?./bincp.sh?復制?init??bash?ls? ?22,?#sync ?21,?#?chroot?/mnt/sysroot?????????????????????#切換到小linux上測試一下利用腳本復制的命令 ?22,創建一個新的虛擬機?測試一下 ?23,運行?bincp.sh?拷貝命令 ?24,#mkdir?rm?mv?cp?cat?mount?umount?vi?vim?chmod?chown?ping?ifconfig?insmod?modprobe?rmmod?route?halt?reboot?shutdown?hostname? ???????????????????????????????????????????????#以上就是添加小linux所用的到的命令 ?25,?#sync? ?26,?重新切換到小系統?測試?cp過來的命令?這里的切換時把宿主機掛起,添加一個新的虛擬機,在把硬盤掛載上去 ?27,#mount?-n?-o?remount,rw?/???????????????#重新以讀寫方式掛載?根文件系統 ?28,切換到宿主機 ?######################################?添加關機功能################################ ?29,#cd?/mnt/sysroot/ ?30,?#vim?etc/rc.d/rc.sysdone?????????????????#添加關機功能腳本 ??????#!/bin/bash ??????sync ??????sleep?2 ??????exec?/sbin/halt?-p ?31,?#chmod?+?etc/rc.d/rc.sysdone ?31,?#?./bincp.sh??啟動腳本拷貝?sync?命令 ?32,#cd?/mnt/sysroot/ ?33,?#vim?etc/inittab?????????????????????????#指定切換到0級別下的關機動作 ????添加:?l0:0:wait:/etc/rc.d/rc.sysdone?? ?34,?#sync????????????????????????????????????#同步一下數據 ?35,?切換到小系統上測試?是否可以正常關機?(切換的時候先把宿主機掛起) ?36,?#init?0??或者?halt? ?37,?關閉小linux??切換到宿主機?添加重啟功能 ?##################################添加重啟功能##################### ?38,?#cd?/mnt/sysroot ?39,?#vim?etc/inittab?????????????????????????#添加6級別下重啟腳本? ??????l6:6:wait:/etc/rc.d/rc.reboot?? ?40,?#vim?etc/rc.d/rc.reboot??定義重啟腳本 ??????#!/bin/bash ???????sync ???????sleep?1 ???????sync ???????exec?/sbin/reboot ?41,?#chmod?+x?etc/rc.d/rc.reboot ?42,?#sync ?43,?#cd ?44,?掛起宿主機?切換到小linux ?45,?#init?6???測試重啟 ?46,?關閉小linux??切換到?宿主機? ?47,?規范小linux的關機和重啟功能,把init?0?和init?6?整合到一起 ??#################,?規范小linux的關機和重啟功能,把init?0?和init?6?整合到一起############ ?? ?48,?#cd?/mnt/sysroot ?49,?#vim?/etc/rc.d/init.d/halt??整合關機和重啟腳本 ??????#!/bin/bsha ???????case?$0?in????????*reboot)? ??????????COMMAND='/sbin/reboot'?;; ????????*halt) ??????????COMMAND='/sbin/halt?-p'?;; ????????esac ?????????case?$1?in??????????start) ???????????;; ??????????stop) ???????????;; ???????????*) ????????????echo?"Usage:?`basename?$0`?{start|stop}"?????????esac ??????????exec?$COMMAND?? ?50,?#chmod?+x?etc/rc.d/init.d/halt ?51,??創建符號鏈接?方便使用腳本里的?*reboot??*halt??調用 ?52,?#cd?etc/rc.d/ ?53,?#mkdir?rc0.d?rc6.d ?54,?#?cd?rc0.d/ ?55,?#?ln?-sv?../init.d/halt??S99halt ?56,?#?ll?查看一下鏈接是否創建 ?57,?#?cd?.. ?58,?#?cd?rc6.d/ ?59,?#?ln?-sv?../init.d/halt?S99reboot ?60,?#?cd?.. ?61,?#?rm?-rf?rc.reboot?rc.sysdone???????????????????????#刪除這兩個腳本現在已經不需要了 ?62,??創建rc?腳本?功能是把所有?S開頭的開啟??把所有K?開頭的都關閉 ?63,?#?vim?rc ????????#!/bin/bash ????????RUNLEVEL=$1 ????????for?I?in?/etc/rc.d/rc$RUNLEVEL.d/K*;do ?????????$I?stop ????????done ??????? ????????for?I?in?/etc/rc.d/rc.$RUNLEVEL.d/S*;do ?????????$I?start ????????done ?64,?#?chmod?+x?rc ?65,?#?cd?.. ?66,?#?vim?inittab???????????????????????????????????????#修改啟動參數 ???????l0:0:wait:/etc/rc.d/rc?0 ???????l6:6:wait:/etc/rc.d/rc?6 ?67,?#?sync ?68,??掛起宿主機?切換到小linux??測試 ?69,?#?init?0?? ?70,?#?init?6 ?80,??切換回宿主機 ?90,??添加?init?3?級別 ?91,?#?cd?/mnt/sysroot ?92,?#?vim?etc/inittab???添加: ????????l3::wait:/etc/rc.d/rc?3 ?93,?#?cd??etc/rc.d/ ?94,?#?mkdir?rc3.d ?95,?#?vim?init.d/tserver ???????#!/bin/bash ???????#?chkconfig:?35?66?33 ???????#?description:?test?service?script ???????prog=`basename?$0` ???????lockfile=/var/lock/subsys/$prog ?????? ???????start(){ ????????echo?"starting?$prog..."????????touch?$lockfile ???????} ?????? ???????stop()?{ ????????echo?"stopping?$prog..."????????rm?-f?$loclfile ???????} ?????? ???????status(){ ????????if?[?-f?$lockfile?];?then?????????echo?"Running..."????????else?????????echo"Stopped..."???????fi ???????} ?????? ???????usage()?{ ?????????echo?"Usage:$prog?{start|stop|status|restart}"???????}?????????? ???????case?$1?in???????start) ???????start?;; ???????stop) ???????stop?;; ???????restart) ????????stop ????????start ?????????;; ??????status) ???????status ???????;; ???????*) ???????usage ???????exit?1 ???????;; ???????esac ?96,?#?chmod?+x?init.d/tserver ?97,?#?init.d/tserver?start???????????????????????測試腳本 ?98,?#?ls?/var/lock/subsys????????????????????????查看此目錄下是否有?tserver?文件 ?99,?#?init.d/tserver?stop????????????????????????查看是否刪除?tserver?文件 ?100,#?cd?rc3.d/ ?101,#?ln?-sv?../init.d/tserver?S66tserver ?102,#?cd?.. ?103,#?cd?rc0.d/ ?104,#?ln?-sv?../init.d/tserver?K33tserver ?105,#?sync ?????此處加一步如下: ???????1,?cd? ???????2,??stty?-F?/dev/console?speed??查看屋里終端速率 ??##################實現tty終端########?? ?106,#?cd?/mnt/sysroot/etc/ ?107,#?vim?inittab ???????1:2345:respawn:/sbin/agetty?-n?-l?/bin/bash?38400?tty1 ???????1:2345:respawn:/sbin/agetty?-n?-l?/bin/bash?38400?tty2 ?108,#?vim?rc.d/rc.sysinit ???????#!/bin/bash ????????echo?-e?"\tWelcome?to?\033[34mMageEdu\033[0m?linux"????????/bin/bash???##?去掉此行 ?109,#?cd ?110,#?./bincp.sh???移植?agetty??basename??seq??stty ?111,#?syuc ?112,#?cd?/mnt/sysroot??????? ?113,#?ln?-sv?bash?sh ?114,#?sync ?115,?掛起?宿主機?開啟小linux???測試 ???如果文件系統錯亂?修復方法如下: ???切換到宿主機上? ???1,#?cd?/mnt/sysroot/ ???2,#?find?.?|?cpio?-H?newc????3,#?cd? ???4,#?umount?/dec/hda2 ???5,#?fuser?-km?/dev/hda2 ???6,#?umount?/dev/hda2 ???7,#?mke2fs?-j?/dev/hda2??或者用?e2fsck?-f?/dev/hda2 ???8,#?mount?/dev/hda2?/mnt/sysroot ???9,#?cd?/mnt/sysroot ???10,#?zcat?/root/sysroot.gz?|cpio?-id? ???11,#?sync ??116,??###################實現讓根文件系統能自動掛載############# ?117,#?cd?/mnt/sysroot ?118,#?vim?etc/fstab??創建fstab?文件 ???????/dev/hda2???/?????????ext3?????defaults??0?0 ???????/dev/hda1???/boot?????ext3?????defaults??0?0 ???????proc????????/proc?????ext3?????defaults??0?0 ???????sysfs???????/sys??????sysfs????defaults??0?0? ?119,#?mkdir?etc/sysconfig ??#######################?添加主機名############### ?120,#?vim?etc/sysconfig/network?????設置主機名 ???????HOSTNAME=minilinux.magedu.com ?121,#?vim?etc/rc.d/rc.sysinit???讓開機以讀寫方式掛載根文件系統,并設置主機名 ???????echo?"Remount?rootfs"???????mount?-n?-o??remount,rw??/ ?????? ???????echo?"set?the?hostname..."???????[?-f?/etc/sysconfig/network?]?&&?/etc/sysconfig/network ???????[?-z?$HOSTNAME?-o??"$HOSTNAME"?==?'(none)'?]?&&?HOSTNAME=localhost ???????/bin/hostname??$HOSTNAME ?122,#?sync? ?123,#?切換到?小linux?測試?? ?124,#?切換到宿主機??添加系統啟動時?顯示的?綠色OK?和?紅色報錯 ??###################添加系統啟動時?顯示的?綠色OK?和?紅色報錯############################# ?125,#?cd?/mnt/sysroot ?126,#?vim??etc/rc.d/init.d/functions??????????????#定義啟動時候的綠色?[?OK?]??和紅色?[FAILED] ?????? ????????SCREEN=`stty?-F?/dev/console?size?2>?/dev/null` ????????COLUMNS=${SCREEN#*} ????????[?-z?$COLUMNS?]?&&?COLUMNS=80 ????????SPA_COL=$[COLUMNS-14] ?????? ????????RED='\033[31m'????????GREEN='\033[32m'????????YELLOW='\033[33m'????????BLUE='\033\34m'????????NORMAL='\033[0m'?????? ????????success()?{ ?????????string=$1 ?????????RT_SPA=$[$SPA_COL-${#string}] ?????????echo?-n?"$string"???????for?I?in?`seq?1?$RT_SPA`;do ?????????echo?-n?"?"???????done ?????????echo?-e?"[???${GREEN}OK${NORMAL}???]"????????} ??????? ???????failure()?{ ?????????string=$1 ?????????RT_SPA=$[$SPA_COL-${#string}] ?????????echo?-n?"$string"????????for?I?in?`seq?1?$RT_SPA`;do ?????????echo?-n?"?"????????done ?????????echo?-e?"[?$[RED]FAILED${NORMAL}]"????????} ???????? ?127,#?vim?etc/rc.d/init.d/tserver??修改成如下內容: ?????? ???????#!/bin/bash ???????#?chkconfig:?35?66?33 ???????#?description:?test?service?script ???????.?/etc/rc.d/init.d/functions ?????? ???????prog=tserver ???????lockfile=/var/lock/subsys/$prog ?????? ???????start(){ ????????touch?$lockfile ????????[?$??-eq?0?]?&&?success?"Starting?$prog"?||?failure?"Staring?$prog"???????} ?????? ??????stop()?{ ????????rm?-f?$loclfile ????????[?$??-eq?0?]?&&?success?"Stoppting?$prog"?||?failure?"Stopping?$prog"???????} ?????? ???????status(){ ????????if?[?-f?$lockfile?];?then????????echo?"Running..."???????else????????echo"Stopped..."???????fi ???????} ?????? ???????usage()?{ ?????????echo?"Usage:$prog?{start|stop|status|restart}"???????}?????????? ???????case?$1?in????????start) ?????????start?;; ????????stop) ?????????stop?;; ???????restart) ????????stop ????????start ?????????;; ???????status) ????????status ????????;; ???????*) ????????usage ????????exit?1 ????????;; ???????esac ?????? ?128,#?sync ?129,?切換到下linux?下測試?啟動信息是否?顯示?綠色?[?OK?] ?130,?切換到?宿主機??添加?ip地址功能 ??#########################實現網卡功能##################### ?131,#?cd?/mnt/sysroot ?132,#?mkdir?lib/modules ?133,#?modinfo?pcnet32???查看網卡模塊都有那些依賴塊 ?134,#?cp?/lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko?/mnt/sysroot/lib/modules ?135,#?modinfo?mii ?136,#?cp?/lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko?/mnt/sysroot/lib/modules ?137,#??vim?etc/rc.d/rc.sysinit??讓系統啟動自動裝載網卡模塊 ?????????echo?"Initializing?network?device..."????????/sbin/insmod??/lib/modules/mii.ko ????????/sbin/insmod??/lib/modeles/pcnet32.ko ?138,#?mkdir?etc/sysconfig/network-scripts??創建網卡目錄 ?139,#?vim?etc/sysconfig/network-scripts/ifcfg-eth0 ???????DEVICE=eth0 ???????BOOTPROTO=static???????IPADDR=172.16.100.5 ???????NETMASK=255.255.0.0 ???????GATEWAY=172.16.0.1 ???????ONBOOT=yes ?140,#?vim?etc/rc.d/init.d/network???讓ip能在服務啟動腳本自動啟動 ????????#!/bin/bash ????????#?chkconfig:?35?09?90 ?????????prog=network ?????????.?/etc/rc.d/init.d/functions ?????????CONF=/etc/sysconfig/network-scripts/ifcfg-eth0?? ?????????.?$CONF ????????? ?????????NETMASK=16 ??????????start()?{ ???????????ifconfig?eth0?$IPADDR/$NETMASK?up ???????????[?-z?$GATEWAY?]?&&?route?add?default?gw?$GATEWAY ??????????}?? ?????????? ??????????stop()?{ ???????????ifconfig?eth0?down ??????????} ?????????? ??????????status()?{ ???????????ifconfig?eth0 ??????????} ?????????? ??????????usage()?{ ???????????echo?"$prog:?{start|stop|restart|status}"??????????} ?????????? ??????????case?$1?in? ??????????start) ???????????start? ???????????success?"Config?network?eth0"???????????;; ??????????stop) ???????????stop ???????????success?"Config?network?card?eth0"???????????;; ??????????restart) ???????????stop ???????????start ???????????success?"Restart?network?card?eth0"???????????;; ??????????status) ???????????status ???????????return?0 ???????????;; ??????????*) ???????????usage ???????????exit?1 ???????????;; ??????????esac ?141,#?chmod?+x?etc/rc.d/init.d/network?? ?142,?讓網卡能在級別3?開啟??級別6?和?0?關閉???????? ?143,#?cd?et/rc.d/rc0.d/ ?144,#?ln?-sv?../init.d/network?K90network ?145,#?cd?../rc6.d ?146,#?ln?-sv?../init.d/network?K90network ?147,#?cd?../rc3.d ?148,#?ln?-sv?../init.d/network?S09network ?149,#?sync ?150,?測試一下網卡是否可以啟動 ?151,#?chroot?/mnt/sysroot???在宿主機上切換到小linux ?152,#?/etc/rc.d/init.d/network?start ??到此為止?以個簡小的linux?制作完成。?由于初次做裁剪linux??有寫錯或者使用不當的地方希望各位技術達人和博友?批評指正。 ??????!!!!在此先謝過!!!! ??? ?
轉載于:https://blog.51cto.com/linuxnx/1167675
總結
以上是生活随笔為你收集整理的手把手教你从零构建属于自己的小linux的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。