MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > linux / 正文

centos6系统裁剪

2024-11-26 15:24 huorong linux 3 ℃ 0 评论

1、系统裁剪要求:有GRUB引导系统,并且使用init脚本实现网卡驱动的装载,以及IP地址的配置,添加tserver服务,并在系统启动时启动此服务并显示。

2、操作环境的目标

(1)在virtual box中操作,系统为Centos6.5

(2)基于宿主机(Host)制作一个简单可启动的Linux

3、流程概括

(1)为虚拟机添加一块新硬盘,并为其分区,安装GRUB

(2)复制内核文件(vmllinuz)和initrd文件至新硬盘中。

(3)移植bash,网络相关命令ifconfig、ip、ping命令,文件列表命令ls,服务管理命令chkconfig,模块装载卸载命令insmod、rmmod,挂载卸载命令mount、umount,文件查看命令cat至目标主机(Target)的/文件系统中。

(4)为grub提供配置文件。

(5)为目标主机配置/sbin/init文件启动网络服务和其他服务。 (6)将目标主机的硬盘装载在其他虚拟机上。

4、添加新硬盘并分区、格式化和挂载

(1)在虚拟机上添加一块1G的硬盘,名为TinyLinux。


裁剪过程

1.创建一个linux虚拟机,磁盘大小为1G,这里我们只建空的虚拟机,不安装操作系统。

2.把tinylinux.vmdk 磁盘添加上来,并打开centos6 虚拟机

3.给tinylinux.vmdk 硬盘分区, /boot 100M ,/ 900M

[root@centos6 ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-130, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130):
Using default value 130

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

检查分区是否成功
[root@centos6 ~]# lsblk
NAME                        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                           8:0    0    64G  0 disk
├─sda1                        8:1    0   500M  0 part /boot
└─sda2                        8:2    0  63.5G  0 part
  ├─VolGroup-lv_root (dm-0) 253:0    0    50G  0 lvm  /
  ├─VolGroup-lv_swap (dm-1) 253:1    0     2G  0 lvm  [SWAP]
  └─VolGroup-lv_home (dm-2) 253:2    0  11.6G  0 lvm  /home
sdb                           8:16   0     1G  0 disk
├─sdb1                        8:17   0 109.8M  0 part
└─sdb2     

4.分区格式化

# mke2fs -t ext4 /dev/sdb1
# mke2fs -t ext4 /dev/sdb2

5.在/mnt目录下创建boot目录以及sysroot目录,并将上面两个文件系统进行挂载。

# mkdir /mnt/{boot,sysroot}
# mount /dev/sdb1 /mnt/boot
# mount /dev/sdb2 /mnt/sysroot
[root@centos6 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  1.1G   46G   3% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   36M  417M   8% /boot
/dev/mapper/VolGroup-lv_home
                       12G   29M   11G   1% /home
vagrant               236G  219G   17G  94% /vagrant
/dev/sdb1             103M  1.6M   96M   2% /mnt/boot
/dev/sdb2             880M  1.2M  833M   1% /mnt/sysroot

6.安装GRUB

[root@centos6 ~]# 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

检查grub目录
[root@centos6 ~]# ls /mnt/boot
grub  lost+found
[root@centos6 ~]# ls /mnt/boot/grub
device.map     fat_stage1_5  iso9660_stage1_5  minix_stage1_5     stage1  ufs2_stage1_5    xfs_stage1_5
e2fs_stage1_5  ffs_stage1_5  jfs_stage1_5      reiserfs_stage1_5  stage2  vstafs_stage1_5
[root@centos6 ~]#

7.复制内核文件和initrd文件至目标主机磁盘

(1)内核文件为宿主机中的/boot/vmlinuz-VERSION

(2)initrd文件为宿主机的/boot/initramfs-VERSION.img

[root@centos6 ~]# cp /boot/vmlinuz-2.6.32-754.35.1.el6.x86_64 /mnt/boot/vmlinuz
[root@centos6 ~]# cp /boot/initramfs-2.6.32-754.35.1.el6.x86_64.img /mnt/boot/initramfs.img
[root@centos6 ~]# ls /mnt/boot
grub  initramfs.img  lost+found  vmlinuz

8.创建目标主机的根文件系统

根文件系统一般包含的目录: etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,root

# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,root}

9.移植bash等命令至目标主机根文件系统

(1)移植的时候需要把命令的可执行文件以及所依赖的库文件复制,命令才能被执行。 (2)可执行文件位于/bin目录下,库文件在/lib目录 或者/lib64目录下。

(3)用which COMMAND 命令可查看命令的执行文件在哪个位置 用 ldd which COMMAND命令可查看命令所依赖的库文件在哪个位置。

(4)用一个脚本实现将命令的可执行文件以及其所依赖的库文件复制到目标主机根文件系统中

# cat /root/cpcmd.sh

#!/bin/bash
#
target=/mnt/sysroot

clearCmd(){
if which $cmd &> /dev/null;then
cmdPath=`which --skip-alias $cmd`
        else
             echo "No such command"
            return 5
        fi

}

 cmdCopy(){
         cmdDir=`dirname $1`
        [ -d ${target}${cmdDir} ] || mkdir -p ${target}${cmdDir}
        [ -f ${target}${1} ] && echo -e "\033[31mThe ${1} exist!\033[0m" && return 5
        [ -f ${target}${1} ] || cp $1 ${target}${cmdDir}
        echo -e "\033[32mCopy ${1} success.\033[0m"
}

libCopy(){
        for lib in `ldd $1 | grep -o "/[^[:space:]]\{1,\}"`;do
                libDir=`dirname $lib`
                [ -d ${target}${libDir} ] || mkdir -p ${target}${libDir}
                [ -f ${target}${lib} ] && echo -e "\033[31mThe ${lib} exist!\033[0m" && continue
                [ -f ${target}${lib} ] || cp $lib ${target}${libDir}
                echo -e "\033[32mCopy ${lib} success.\033[0m"
        done

}

while true;do
        read -p "Enter a command: " cmd
        if [ "$cmd" == "quit" ];then
                echo "quit"
                exit 0
        fi
        clearCmd $cmd
        [ $? -eq 5 ]&& continue

        cmdCopy $cmdPath
        libCopy $cmdPath
done

执行脚本,复制命令:

[root@centos6 ~]# bash /root/cpcmd.sh
Enter a command: bash
Enter a command: ls
Enter a command: cat
Enter a command: insmod
Enter a command: rmmod
Enter a command: mount
Enter a command: umount
Enter a command: ifconfig
Enter a command: ip
Enter a command: ping
Enter a command: chkconfig
Enter a command: df
Enter a command: quit
quit
[root@centos6 ~]#

10.创建配置文件grub.conf

# vi /mnt/boot/grub/grub.conf

default=0
timeout=5
title Tiny Linux
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/sda2  quiet selinux=0 init=/bin/bash
        initrd /initramfs.img

字段解释:

<1>default =# :指定默认启动的内核或者OS(Operating System)

<2>timeout=# :等待用户选择要启动的内核或OS的时长,单位为秒。

<3>splashimage=/path/to/splashimage_file :指定使用的背景图片

<4>hiddenmenu:隐藏菜单

<5>tittle

    root (Device(磁盘设备),Part(磁盘分区) )
        ①Device表示方式:在grub中,统统以hd开头,并紧跟一个数字做各磁盘设备的标志,从0开始编号。
        ②Part表示方式:代表分区,从0开始编号

    kernel 
        ①指定内核文件及传递给内核的参数,常用参数:ro root=/path/to/device,
        表示把该设备文件所表示的设备当作内核去挂载时的根使用的真正根分区所在的设备。(只读挂载) 
        参数格式:ro root=/path/to/device quiet
        quiet为静默模式

    initrd
        ①文件通常为cpio归档,并使用gzip压缩;通常以.img作为文件后缀名(映像文件)

11.启动Tinylinux虚拟机

先umount 分区,在关闭虚拟机centos6

[root@centos6 ~]# umount /mnt/boot
[root@centos6 ~]# umount /mnt/sysroot
[root@centos6 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  1.1G   46G   3% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   36M  417M   8% /boot
/dev/mapper/VolGroup-lv_home
                       12G   29M   11G   1% /home
vagrant               236G  219G   17G  94% /vagrant

启动Tinylinux,若有grub引导并出现如下界面,并且命令正确,证明TinyLinux小系统能成功运行。

这个时候,我们TinyLinux系统只能执行简单的命令,也没有网络,文件系统也没有挂载。接下来,我们复制网卡,挂载文件系统。

先把TinyLinux系统关机,启动centos6

12.复制网卡模块至目标主机

网卡模块所在路径:/lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko

创建/mnt/sysroot/lib/modules目录

# mount /dev/sdb1 /mnt/boot
# mount /dev/sdb2 /mnt/sysroot/
# mkdir /mnt/sysroot/lib/modules

复制网卡模块至/mnt/sysroot/lib/modules目录下

# cp /lib/modules/2.6.32-754.35.1.el6.x86_64/kernel/drivers/net/e1000/e1000.ko  /mnt/sysroot/lib/modules/

13.为目标主机配置/sbin/init文件启动网络服务和其他服务

编辑/mnt/sysroot/sbin/init文件

# vi /mnt/sysroot/sbin/init

编辑内容:
#!/bin/bash
#
echo -e "\tWelcome to \033[34mKingOlie Tiny \033[0mLinux"
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && echo -e "Loaded e1000 module \033[60G[ \033[32mOK\033[0m ]"
ifconfig lo 127.0.0.0/8
ifconfig eth0 192.168.112.100/24
mount -n -o remount,rw /dev/sda2 /
/bin/bash

# chmod  +x /mnt/sysroot/sbin/init
谨记必须给init脚本加上执行权限,否则系统启动的时候并不会执行init,会出现kernel panic错误。
# sync
使用sync将内存写入的数据同步到磁盘中去。可以使用多次,以确保文件数据已经被同步到磁盘中。

编辑/mnt/boot/grub/grub.conf文件,将init指定为sbin目录下的init

# vi /mnt/boot/grub/grub.conf
default=0
timeout=5
title Tiny Linux
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/sda2  quiet selinux=0 init=/sbin/init
        initrd /initramfs.img

卸载硬盘FS,并关机

[root@centos6 /]# umount /mnt/boot
[root@centos6 /]# umount /mnt/sysroot
[root@centos6 /]# init 0

14.启动TinyLinux 虚拟机并验证

如果目标主机启动时候显示绿色OK字样,证明网卡模块已经被装载。

我们继续检查一下,ip , 文件系统

手工挂载一下

ip设置,如下

此时,ctrl+c按键并不能中断ping命令,因为该小系统并没有配置ctrl+c热键。 建议使用以下命令限定ping次数和时间检测:

# ping -w 5 -c 5 192.168.112.100

至此,centos6系统裁剪已经完成。

Tags:linux内核裁剪

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言