天天看點

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

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

Disk /dev/sda: 

128.8

GB, 

128849018880

bytes 

255

heads, 

63

sectors/track, 

15665

cylinders 

Units = cylinders of 

16065

512

8225280

bytes 

Sector size (logical/physical): 

512

bytes / 

512

bytes 

I/O size (minimum/optimal): 

512

bytes / 

512

bytes 

Disk identifier: 

0x0001c38d

Device Boot      Start         End      Blocks   Id  System 

/dev/sda1   *           

1

26

204800

83

Linux 

Partition 

1

does not end on cylinder boundary. 

/dev/sda2              

26

7859

62914560

8e  Linux LVM 

Disk /dev/sdb: 

21.5

GB, 

21474836480

bytes 

255

heads, 

63

sectors/track, 

2610

cylinders 

Units = cylinders of 

16065

512

8225280

bytes 

Sector size (logical/physical): 

512

bytes / 

512

bytes 

I/O size (minimum/optimal): 

512

bytes / 

512

bytes 

Disk identifier: 

0x00000000

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

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

[root@localhost ~]# echo -e 

"n\np\n1\n\n+100M\nn\np\n2\n\n+512M\nw"

|fdisk /dev/sdb

   格式化建立的分區

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

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

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

[root@localhost mnt]# mount 

/dev/sdb1 on /mnt/boot type ext4 (rw) 

/dev/sdb2 on /mnt/sysroot type ext4 (rw)

4.安裝grub至指定的分區

[root@localhost mnt]# 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

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

[root@localhost grub]# cp /boot/vmlinuz-

2.6

.

32

-

358

.el6.x86_64 /mnt/boot/wangfengvmlinz 

[root@localhost grub]# cp /boot/initramfs-

2.6

.

32

-

358

.el6.x86_64.img /mnt/boot/wangfenginitramfs.img

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

[root@localhost grub]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,

var

,proc,sys,dev,lib,lib64,bin,sbin,boot,src,mnt,media,home,root}

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

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

 為了防止核心恐慌,需要為bash建立一個軟連結sh

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 子產品名稱來檢視子產品的詳細資訊

[root@localhost ~]# lsmod |grep e1000  -->檢視網卡的資訊 

e1000                 

170646

[root@localhost ~]# modinfo e1000 

filename:       /lib/modules/

2.6

.

32

-

358

.el6.x86_64/kernel/drivers/net/e1000/e1000.ko -->此處為網卡子產品的所在位置 

version:        

7.3

.

21

-k8-NAPI 

license:        GPL 

description:    Intel(R) PRO/

1000

Network Driver 

author:         Intel Corporation, <[email protected]

srcversion:     1D4F1E82BB99EA36D320B1B 

alias:          pci:v00008086d00002E6Esv*sd*bc*sc*i* 

alias:          pci:v00008086d000010B5sv*sd*bc*sc*i* 

alias:          pci:v00008086d00001099sv*sd*bc*sc*i* 

alias:          pci:v00008086d0000108Asv*sd*bc*sc*i* 

alias:          pci:v00008086d0000107Csv*sd*bc*sc*i* 

alias:          pci:v00008086d0000107Bsv*sd*bc*sc*i* 

alias:          pci:v00008086d0000107Asv*sd*bc*sc*i* 

alias:          pci:v00008086d00001079sv*sd*bc*sc*i* 

alias:          pci:v00008086d00001078sv*sd*bc*sc*i*

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

[root@localhost ~]# cp /lib/modules/

2.6

.

32

-

358

.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/modules/e1000.ko

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

開啟主控端,可以看到效果

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

也可以ping通外網

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

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

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" ]] && echo "Quit " && exit 0

 bash=`which --skip-alias $pidname &> /dev/null`

 if [[ -x $bash ]];then

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

   options $bash

 else

   echo "No such command!"

  fi

腳本簡要說明:

  大家都知道一個指令的運作需要依賴于二進制檔案和庫檔案,本執行個體以cat為例,列舉cat的二進制檔案所在的路徑和檔案所在的路徑

二進制檔案所在的位置 

[root@localhost ~]# which cat 

/bin/cat

依賴的庫檔案

[root@localhost ~]# ldd `which cat` |grep -o 

"/[^[:space:]]\{1,\}"

/lib64/libc.so.

6

/lib64/ld-linux-x86-

64

.so.

2

由于本人水準有限,請各位大神批評指正,同時為在昆明3.01事件中遇難的同胞默哀,願逝者安息,傷者平安!!

繼續閱讀