打造自己的專屬Linux(一):快速建立一個小型Linux

來源:互聯網
上載者:User

##請腳下留情。。。踩我的同學請告訴我原因,(二)剛發上去一上午被踩了10次。。。

##打造自己的專屬Linux(二)的連結http://blog.csdn.net/deansrk/article/details/6664558

      跟著馬哥學Linux已經快一個月了,在前幾天建立一個小型Linux,大小不足10M,萌生了一個想法:DIY一個自己的Linux,所以準備寫一個系列篇,通過宿主機的方式,一步一步建立一個小型Linux,並慢慢增加它的功能模組:網路,服務,ssh,圖形介面等等~如果你有興趣,那麼我們一起來學習吧!

如果你對linux的啟動流程不熟悉的話可以讀我的文章:
深入淺出:linux啟動流程刨析    http://blog.csdn.net/deansrk/article/details/6659421

我的linux版本:redhat 5.4
虛擬機器用戶端:Vmware Workstation 6.5
我們分步驟來實現這個小型linux

第一步:在虛擬機器上添加一塊新硬碟到linux,大小20G (可以自定)

第二步:1)開啟終端,使用fdisk格式化分區剛才掛載的新硬碟/dev/sdb,劃分出一個50M大小的主要磁碟分割和一個512M大小的主要磁碟分割。

[root@server69 ~]# fdisk /dev/sdbThe number of cylinders for this disk is set to 2610.There is nothing wrong with that, but this is larger than 1024,and could in certain setups cause problems with:1) software that runs at boot time (e.g., old versions of LILO)2) booting and partitioning software from other OSs   (e.g., DOS FDISK, OS/2 FDISK)Command (m for help): pDisk /dev/sdb: 21.4 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes   Device Boot      Start         End      Blocks   Id  System/dev/sdb1               1           7       56196   83  Linux/dev/sdb2               8          70      506047+  83  Linux/dev/sdb3              71         102      257040   82  Linux swap / SolarisCommand (m for help): nCommand action   e   extended   p   primary partition (1-4)pSelected partition 4First cylinder (103-2610, default 103): Using default value 103Last cylinder or +size or +sizeM or +sizeK (103-2610, default 2610): +50M  Command (m for help): w  

2)重讀下分區表

[root@server69 ~]# partprobe /dev/sdb

格式化剛建立的分區為ext3檔案系統

[root@server69 ~]# mke2fs -j /dev/sdb1[root@server69 ~]# mke2fs -j /dev/sdb2

第三步:將剛格式化好的分區/dev/sdb1掛載到/mnt/boot  /dev/sdb2掛載到/mnt/sysroot,如果你沒有這兩個檔案夾,請手動建立,如果你掛載了其他裝置,可以換其他路徑。

[root@server69 LX]# mount /dev/sdb1 /mnt/boot[root@server69 LX]# mount /dev/sdb2 /mnt/sysroot

第四步:1)安裝grub到/dev/sdb

[root@server69 mnt]# grub-install --root-directory=/mnt/ /dev/sdbInstallation 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/sdb2)修改/mnt/boot/grub/device.map[root@server69 mnt]# vim /mnt/boot/grub/device.map (fd0)   /dev/fd0                               //去掉這一行  fd指的是磁碟片(hd0)   /dev/sda(hd1)   /dev/sdb                            //去掉這一行

第五步:拷貝kernel和initrd到小linux裡,如果對kernel和initrd沒有概念的話也請看我的文章,裡面有詳細的說明
深入淺出:linux啟動流程刨析

1)拷貝kernel檔案,並且重新命名方便記憶。

[root@server69 LX]# cp /boot/vmlinuz-2.6.18-164.el5 /mnt/boot/vmlinuz   

2)拷貝initrd檔案,並且重新命名方便記憶。

[root@server69 LX]# cp /boot/initrd-2.6.18-164.el5.img /mnt/boot/initrd

第六步:解壓縮initrd, 編輯裡面的init檔案,然後重新壓縮封裝。

[root@server69 mnt]# mv initrd initrd.gz                                                  //剛其改名,否則gzip不識別[root@server69 mnt]# gzip -d initrd.gz                                                     //解壓[root@server69 mnt]# cpio -id < initrd.gz                                                 //釋放歸檔14731 blocks[root@server69 mnt]# lsbin  boot  dev  etc  init   lib  proc    sbin  sys  sysroot//看到了嗎?initrd幾乎具有所有的linux檔案目錄,簡直就是一個小linux[root@server69 mnt]# vim init 99 mkrootdev -t ext3 -o defaults,ro /dev/vol0/root                               // 修改為  mkrootdev -t ext3 -o defaults,ro /dev/sda2 [root@server69 mnt]#find. | cpio -H newc -o quiet   | gzip -9              //重新歸檔並壓縮

第七步:建立小型linux的檔案目錄結構

[root@server69 mnt]# cd /mnt/sysroot/[root@server69 sysroot]# mkdir -pv root bin sbin usr/{bin,sbin} lib dev tmp proc sys home mnt media boot 

第八步:拷貝命令到小型linux裡去
1)怎麼樣拷貝命令?

[root@server69 sysroot]# which bash                                             //使用which命令查看命令所在路徑/bin/bash[root@server69 sysroot]# ldd /bin/bash                                           //ldd 加命令路徑可以顯示出這個命令所依賴的庫檔案linux-gate.so.1 =>  (0x008e6000)                                                      //這個檔案不用拷貝libtermcap.so.2 => /lib/libtermcap.so.2 (0x0041e000)libdl.so.2 => /lib/libdl.so.2 (0x003ef000)libc.so.6 => /lib/libc.so.6 (0x002a7000)/lib/ld-linux.so.2 (0x00284000)[root@server69 sysroot]# cp /lib/lidkl.so.2 /mnt/sysroot/lib/               //將上面的庫檔案都複製到小linux對應的目錄

2)拷貝init 和bash ,mount , ls 這幾個個命令去linux   注意,init和bash必須拷貝。

第九步:建立 /mnt/etc/fstab檔案和/mnt/etc/inittab 檔案,(這個檔案的說明可以在我的啟動流程文章裡找到)

[root@server69 ~]# vim /mnt/sysroot/etc/fstab/dev/sda2            /                          ext3               defaults                                  0   0/dev/sda1            /boot                      ext3               defaults                                  0   0sysfs                /sys                       sysfs              defaults                                  0   0                                             proc                 /proc                      proc               defaults                                  0   0[root@server69 ~]# vim /mnt/sysroot/etc/inittabid:3:initdefault:si::sysinit:/etc/rc.d/rc.sysinit

第十步:建立/mnt/boot/grub/grub.conf 和 /etc/rc.d/rc.sysinit,並給rc,sysyinit 賦執行許可權

[root@server69 ~]# vim /mnt/boot/grub/grub.conf default=0timeout=10title Little Linux                                                                                           //grub的標題,可以自己寫                     kernel /vmlinuz  ro  root=/dev/sda2 quiet                       //載入核心,唯讀方式掛載根檔案系統                     initrd /initrd.gz                                                                       //載入記憶體檔案系統[root@server69 ~]# vim /mnt/sysroot/etc/rc.d/rc.sysinit#!/bin/bashecho -e "\t\tWelcome to \033[31mLittle\033[0m Linux"                     //這裡可以寫自己想寫的歡迎介面 ^^mount -n -o remount.rw /                                                                           //以讀寫方式重啟掛載根檔案系統mount -n -a                                                                                                    //掛載所有裝置,並不記錄/bin/bash                                                                                                        //載入bash shell
 
[root@server69 ~]# chmod +x /etc/rc.d/rc.sysinit                //給rc.sysinit賦執行許可權   

好了,大功告成,建立一個虛擬機器,選擇linux2.6xx ,掛載上這塊硬碟看下效果吧~

這是我的grub介面很樸素吧~你可以自己更改背景哈

啟動完成的效果,很簡陋吧,注意Little

##更新:第十步最後要給rc.sysinit賦執行許可權,否則可能系統載入時無法執行!

下篇預告:全自動指令碼實現自動建立小linux和命令移植,讓你的小linux健步如飛~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.