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格式

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

   格式化新建的分区

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

3.将新创建的分区分别挂载至/mnt/boot目录和/mnt/sysroot

1
2
3
[root@localhost mnt]# mount
/dev/sdb1 on /mnt/boot type ext4 (rw)
/dev/sdb2 on /mnt/sysroot type ext4 (rw)

4.安装grub至指定的分区

1
2
3
4
5
6
7
8
9
[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目录中(将启动文件复制到定制系统中)

1
2
[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需要的一些基本文件(在定制系统上需要的)

1
[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等

     此处不再累赘,后面将会附上脚本实现方式

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 模块名称来查看模块的详细信息

1
2
3
4
[root@localhost ~]# lsmod
[root@localhost ~]# modinfo e1000
[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

开启宿主机,可以看到效果wKiom1MUHJDhbJgrAAKO-lZPNoA269.jpg

也可以ping通外网

wKioL1MUJw7ydDoxAAKayWsyuuE121.jpg

附:拷贝库文件和二进制文件的脚本

#!/bin/bash

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

done


本文转自wangfeng7399 51CTO博客,原文链接:http://blog.51cto.com/wangfeng7399/1366670,如需转载请自行联系原作者

相关文章
|
IDE 网络协议 Unix
Linux内核裁剪机制优化(make menuconfig)
Linux内核裁剪机制优化(make menuconfig)