天天看点

Linux裁剪之定制系统

   听到这个标题也许会让人认为不可思议,怎么可以定制自己的系统呢?是的。这就是Linux为什么一直深受广大系统爱好者青睐之一,它可以根据自己的需求来定制自己想要和不想要的。本文就从最基础的开始来搭建属于自己的一个小型OS,并且能够实现开机自动加载网卡,配置IP与其他主机通信。本文暂不做内核编译的操作,不过会在之后陆续补上,尽请期待!

实验环境;  宿主机:Centos6.5-64位  目标主机:Centos6.5-64位

1、为宿主机添加一块新的20G硬盘,特命名为;testlinux6.5.vmdk(由于目标主机是通过宿主机的sdb硬盘来启动,故在此宿主机上添加一块新的硬盘)

  下图为硬盘信息;

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: 0xe0c1f6c9
Device Boot      Start         End      Blocks   Id  System      

2、为新添加的硬盘分区格式化以及创建文件系统ext4

fdisk /dev/sdb磁盘分区;

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: 0xe0c1f6c9
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          26      208813+  83  Linux
/dev/sdb2              27          91      522112+  83  Linux      

3、格式化新建的硬盘分区,并且为两个分区创建挂载目录

[root@station103 ~]# mke2fs -t ext4 /dev/sdb1
[root@station103 ~]# mke2fs -t ext4 /dev/sdb2
[root@station103 mnt]# mkdir -pv /mnt/{boot,sysroot}      

4、先把/dev/sdb1挂载到/mnt/boot/目录下

[root@station103 ~]# mount /dev/sdb1 /mnt/boot/
[root@station103 ~]# mount
/dev/sdb1 on /mnt/boot type ext4 (rw)      

5、为新的磁盘分区sdb1装入grub;

[root@station103 ~]# 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
[root@station103 ~]# ls /mnt/boot/
grub  lost+found
[root@station103 ~]#      

6、复制宿主机的内核至新磁盘分区上sdb1挂载的/boot目录下;

[root@station103 ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz
[root@station103 ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/initramfs.img
[root@station103 ~]# cd /mnt/boot/
[root@station103 boot]# ll
total 21074
drwxr-xr-x 2 root root     1024 Feb 22 23:32 grub
-rw------- 1 root root 17437215 Feb 22 23:36 initramfs.img
drwx------ 2 root root    12288 Feb 22 23:23 lost+found
-rwxr-xr-x 1 root root  4128368 Feb 22 23:36 vmlinuz      

7、挂载sdb2至/mnt/sysroot下,并且创建目标所需的根文件;

[root@station103 ~]# mount /dev/sdb2 /mnt/sysroot
[root@station103 ~]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}
[root@station103 ~]# cd /mnt/sysroot/
[root@station103 sysroot]# ls
bin   dev  home  lib64       media  proc  sbin  sys  var
boot  etc  lib   lost+found  mnt    root  srv   usr      

8、在宿主机上移植bash的二进制的可执行文件及库文件至目标主机的根文件系统;(此处用的脚本实现,不做多的介绍.)

[root@station103 etc]# ./bashcp.sh
Enter a command: ls
Enter a command: ifconfig
Enter a command: ping
Enter a command: mv
Enter a command: cp
Enter a command: cat
Enter a command: mkdir
Enter a command: bash
Enter a command: useradd
Enter a command: chmod
Enter a command: chown
Enter a command: reboot
Enter a command: insmod
Enter a command: exit
Enter a command: quit      

9、查看下刚刚复制的命令,并且为/mnt/sysroot/bin创建链接文件;

[root@station103 etc]# chroot /mnt/sysroot
bash-4.1# ls
bin   dev  home  lib64       media  proc  sbin  sys  var
boot  etc  lib   lost+found  mnt    root  srv   usr
bash-4.1# cd /bin
bash-4.1# ls
bash  cat  chmod  chown  cp  ls  mkdir  mv  ping
bash-4.1# cd /sbin
bash-4.1# ls
ifconfig  insmod  reboot
[root@station103 bin]# ln -sv hash sh    创建链接文件
`sh' -> `hash'      

10、为目标主机/mnt/boot/grub/下创建开机自检grub.conf配置文件;

default=0  指定默认启动的内核或OS
timeout=5  等待用户选择要启动的内核或OS的时间,默认单位为秒
title OuYang Linux System   标题名
        root (hd0,0)  在grub中,都以hd开头
        kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
                        指定内核文件及传递给内核的参数
        initrd /initramfs.img      

   到此目标机已经可以启动了,先测试一下,但在测试之前建议先把/sdb1,/sdb2设置为开机自动挂载。启动目标机时,宿主机得挂起或者关机。

   目标机测试OK。此时目标机还不能实现设置ip.还得得宿主机上把网卡模块复制到目标机上

Linux裁剪之定制系统

11、将宿主机上的网卡模块复制到目标机。

[root@station103 ~]# mkdir -p /mnt/sysroot/lib/modules
[root@station103 ~]# cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko/mnt/sysroot/lib/module/
[root@station103 ~]#      

12、为了能够实现让系统启动自动装载网卡,下面我特把grup.conf配置文件里的

init=

/bin/bash 改成init=/sbin/init

,让/mnt/sysroot/sbin/init脚本来自动实现

[root@station103 sbin]# pwd
/mnt/sysroot/sbin
[root@station103 sbin]# vim init
#!/bin/bash
echo -e "\tWelcome to \033[32m ouyang \033[0m Linux"
mount -n -t proc /proc /proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && echo -e "Load e1000 modules succeeded                        [ \033[32mOK\033[0m ]"
ifconfig lo 127.0.0.1/8
ifconfig eth0 172.16.251.200/16
mount -n -o remount,rw /dev/sda2 /
#route add -net 172.16.0.0 gw 172.16.0.1
/bin/bash      

13、停止宿主机,启动目标机;

Linux裁剪之定制系统

   ip地址已自动加载

Linux裁剪之定制系统
Linux裁剪之定制系统

继续阅读