天天看點

linux裁剪—定制自己所需要的linux

    Linux以其開源思想和啟動速度快為廣大技術人員所喜愛,本文主要講述通過自己對核心的包裝以及對預設程式的設定,來實作自己定制一個自己需要的os系統,并能夠實作開機自動加載網卡,并為網卡配置ip位址,本文不涉及核心的編譯,核心編譯内容将在後續推出,敬請大家期待!

   本文是通過主控端——>目标機的形式來實作。

1、為虛拟機添加一個20G的硬碟,并将磁盤設定為單個檔案系統,并命名為smallcentos.vmdk

  檢視主控端現在的硬碟資訊

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<code>[root@localhost ~]# fdisk -l /dev/sd[a-z]</code>

<code>Disk /dev/sda: </code><code>128.8</code> <code>GB, </code><code>128849018880</code> <code>bytes</code>

<code>255</code> <code>heads, </code><code>63</code> <code>sectors/track, </code><code>15665</code> <code>cylinders</code>

<code>Units = cylinders of </code><code>16065</code> <code>* </code><code>512</code> <code>= </code><code>8225280</code> <code>bytes</code>

<code>Sector size (logical/physical): </code><code>512</code> <code>bytes / </code><code>512</code> <code>bytes</code>

<code>I/O size (minimum/optimal): </code><code>512</code> <code>bytes / </code><code>512</code> <code>bytes</code>

<code>Disk identifier: </code><code>0x0001c38d</code>

<code>   </code><code>Device Boot      Start         End      Blocks   Id  System</code>

<code>/dev/sda1   *           </code><code>1</code>          <code>26</code>      <code>204800</code>   <code>83</code>  <code>Linux</code>

<code>Partition </code><code>1</code> <code>does not end on cylinder boundary.</code>

<code>/dev/sda2              </code><code>26</code>        <code>7859</code>    <code>62914560</code>   <code>8e  Linux LVM</code>

<code>Disk /dev/sdb: </code><code>21.5</code> <code>GB, </code><code>21474836480</code> <code>bytes</code>

<code>255</code> <code>heads, </code><code>63</code> <code>sectors/track, </code><code>2610</code> <code>cylinders</code>

<code>Disk identifier: </code><code>0x00000000</code>

新添加的硬碟被識别為sdb,目标主機将通過加載sdb來啟動

2、在sdb建立兩個基本100M、512M分區,并将檔案系統格式化成ext4格式

<code>[root@localhost ~]# echo -e </code><code>"n\np\n1\n\n+100M\nn\np\n2\n\n+512M\nw"</code> <code>|fdisk /dev/sdb</code>

   格式化建立的分區

<code>[root@localhost ~]# mke2fs -t ext4 /dev/sdb1</code>

<code>[root@localhost ~]# mke2fs -t ext4 /dev/sdb2</code>

3.将新建立的分區分别挂載至/mnt/boot目錄和/mnt/sysroot

<code>[root@localhost mnt]# mount</code>

<code>/dev/sdb1 on /mnt/boot type ext4 (rw)</code>

<code>/dev/sdb2 on /mnt/sysroot type ext4 (rw)</code>

4.安裝grub至指定的分區

<code>[root@localhost mnt]# grub-install --root-directory=/mnt /dev/sdb</code>

<code>Probing devices to guess BIOS drives. This may take a long time.</code>

<code>Installation finished. No error reported.</code>

<code>This </code><code>is</code> <code>the contents of the device map /mnt/boot/grub/device.map.</code>

<code>Check </code><code>if</code> <code>this</code> <code>is</code> <code>correct or not. If any of the lines </code><code>is</code> <code>incorrect,</code>

<code>fix it and re-run the script `grub-install'.</code>

<code>(fd0)   /dev/fd0</code>

<code>(hd0)   /dev/sda</code>

<code>(hd1)   /dev/sdb</code>

5.複制/boot目錄中的grub和initrd檔案至/mnt/boot目錄中(将啟動檔案複制到定制系統中)

<code>[root@localhost grub]# cp /boot/vmlinuz-</code><code>2.6</code><code>.</code><code>32</code><code>-</code><code>358</code><code>.el6.x86_64 /mnt/boot/wangfengvmlinz</code>

<code>[root@localhost grub]# cp /boot/initramfs-</code><code>2.6</code><code>.</code><code>32</code><code>-</code><code>358</code><code>.el6.x86_64.img /mnt/boot/wangfenginitramfs.img</code>

6.建立Linux需要的一些基本檔案(在定制系統上需要的)

<code>[root@localhost grub]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,</code><code>var</code><code>,proc,sys,dev,lib,lib64,bin,sbin,boot,src,mnt,media,home,root}</code>

7.在主控端上移植一個可執行的二進制檔案和庫到目标機的硬碟上,如ls,cat,mkdir,mount,reboot,useradd,passwd,ifconfig,ip,ping等

     此處不再累贅,後面将會附上腳本實作方式

8.在目标機的/boot/grub目錄中建立grub.conf,已實作開機自檢,内容如下

default=0

timeout=10

hiddenmenu

title wangfengLinux

  root(hd0,0)

  kernel /wangfengvmlinuz ro root=/dev/sda2 selinux=0 init=/sbin/init

  initrd /wangfenginitramfs.img

9.為了能夠實作開機啟動網卡,需要将主控端上的網卡配置檔案複制到目标機上,可以通過lsmod檢視目前系統的所有子產品,可以通過modinfo 子產品名稱來檢視子產品的詳細資訊

<code>[root@localhost ~]# lsmod</code>

<code>[root@localhost ~]# modinfo e1000</code>

<code>[root@localhost ~]# mkdir -p /mnt/sysroot/lib/modules</code>

<code>[root@localhost ~]# cp /lib/modules/</code><code>2.6</code><code>.</code><code>32</code><code>-</code><code>358</code><code>.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/modules/e1000.ko</code>

10.為了使系統能夠開機自動挂載一些檔案系統和初始化一些服務,需要在目标機上的/sbin/目錄下建立init檔案已實作需求,内容如下

#!/bin/bash

echo -e "Welcome to \033[32m Wangfeng\033[0m Linux"

mount -n -t proc /proc proc

mount -n -t sysfs sysfs /sys

insmod /lib/modules/e1000.ko

ifconfig lo 127.0.0.1/8

ifconfig eth0 192.168.1.200/24

route add -net 0.0.0.0 gw 192.168.1.253

/bin/bash

也可以ping通外網

<a href="http://s3.51cto.com/wyfs02/M02/19/E1/wKioL1MUJw7ydDoxAAKayWsyuuE121.jpg" target="_blank"></a>

附:拷貝庫檔案和二進制檔案的腳本

options(){

for i in $*;do

  dirname=`dirname $i`

  [ -d /mnt/sysroot$dirname ] || mkdir -p /mnt/sysroot$dirname

  [ -f /mnt/sysroot$i ]||cp $i /mnt/sysroot$dirname/

done

}

while true;do

read -p "Enter a command : " pidname

 [[ "$pidname" == "quit" ]] &amp;&amp; echo "Quit " &amp;&amp; exit 0

 bash=`which --skip-alias $pidname &amp;&gt; /dev/null`

 if [[ -x $bash ]];then

   options `/usr/bin/ldd $bash |grep -o "/[^[:space:]]\{1,\}"`

   options $bash

 else

   echo "No such command!"

  fi

本文轉自wangfeng7399 51CTO部落格,原文連結:http://blog.51cto.com/wangfeng7399/1366670,如需轉載請自行聯系原作者

繼續閱讀