在大型企业或者是IDC做运维的朋友经常会有这样子的需求,有些人可能还不是很了解Linux的二次封装有什么好处,这里简单说一下,就是当你要反复做一个操作的时候,比如说要部署300台WEB服务器,这时候你如果一台一台装,然后装完系统后再去配置这个WEB应用,那要花多少时间啊,是吧!但是如果你装好一台服务器,然后将这个系统进行二次封装,这样子你下次再要部署相同的WEB服务器的时候就只需要拿这个封装的系统去部署就OK,这是不是简单多了,这时候有人可能会怀疑会不会存在驱动问题,当然系统本身OK的,他是没有问题的,除非你是特殊的硬件。其实简单的理解就是一个windows的ghost封装差不多。如果你还是不是很明白,可能你需要多读几次这段文字。前期有测试过网上的一些教程,发现在CentOS6.0以上都不太好用了,由于时间的原因,我这里只是贴出封装的shell,其实整个还需要有一个比较复杂的过程,你可能需要用心去了解一下下面这个shell的意思,有需要的朋友可以联系我。当然我经常会用它来封装像 openvpn、vsftpd、web等服务器,让它能够实现全自动化快速部署,以提高我的工作效率,后期有机会将上次发布的vsftpd服务器的封装包发现来大家分享一下,大侠不要拍砖!头已经在流血。
#!/bin/bash
#web:
SYSTEM_DIR=/home/linuxidc
SYSTEM_NAME=linuxidc
mount_ISO (){
mkdir -p /home/linuxidc/Packages
mkdir -p /home/source
mkdir -p /mnt/cdrom1
mkdir -p /mnt/cdrom2
echo "请确保光驱里面有ISO文件,且可以被挂载"
echo "请输入Y/y继续,任意键则退出运行"
read ret
[ ${ret} != "Y" -a ${ret} != "y" ] && exit 1
echo "echo 默认情况,挂载的是/dev/cdrom到/mnt/cdrom1"
mount /dev/cdrom /mnt/cdrom1
mount /dev/cdrom1 /mnt/cdrom2
ls /mnt/cdrom2 |grep Packages
if [ "$?" != 0 ]
then
echo "光盘挂载不成功,请手动重新挂载,或者尝试本地ISO挂载"
echo "请输入本地ISO的路径:"
read ret
ls ${ret}
[ "$?" != 0 ] && echo "本地ISO不存在或者路径错误,退出运行" && exit 1
[ "$?" == 0 ] && mount -o loop ${ret} /mnt/cdcom1
fi
}
copy_ISO_file (){
#awk '/安装/{print $2}' install.log | sed -e '/^$/d' -e 's/^ //g' | grep -v FINISHED | grep -v ":" > /home/source/packges.list
mkdir -p /home/source
cp -p /root/packges.list /home/source/packges.list
mkdir -p /home/linuxidc/Packages/conf/
cp -p /root/PS/* /home/linuxidc/Packages/conf/
for packges in $(cat /home/source/packges.list)
do
cp /mnt/cdrom1/Packages/$packges* /home/linuxidc/Packages
[ $? != 0 ] && echo "copy $packges is faied!"&& cp /mnt/cdrom2/Packages/$packges* /home/linuxidc/Packages
done
rsync -a --exclude=Packages --exclude=repodata /mnt/cdrom2/ /home/linuxidc/
cp -p /root/initrd.img /home/linuxidc/isolinux/initrd.img
mv /home/linuxidc/CentOS_BuildTag /home/linuxidc/linuxidc_BuildTag
}
rebuild_repo_xml (){
cd ${SYSTEM_DIR}
rm -fv ${SYSTEM_DIR}/repodata/*
mkdir -p ${SYSTEM_DIR}/repodata/
cp -fv /mnt/cdrom2/repodata/*-c6-x86_64-comps.xml ${SYSTEM_DIR}/repodata/comps.xml
createrepo -u "media://$(head -1 .discinfo)" -g ${SYSTEM_DIR}/repodata/comps.xml ${SYSTEM_DIR}/
find ${SYSTEM_DIR}/repodata/ -type f -name TRANS.TBL | xargs /bin/rm -fv
}
isolinux_cfg (){
cat > ${SYSTEM_DIR}/isolinux/isolinux.cfg <<EOF
default auto
timeout 600
#default vesamenu.c32
#prompt 1
display boot.msg
menu background splash.jpg
menu title Welcome to linuxidc 6.4!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000
label auto
menu label ^Auto Install system
menu default
kernel vmlinuz
append ks=cdrom:/isolinux/ks.cfg initrd=initrd.img
label linux
menu label ^Install or upgrade an existing system
kernel vmlinuz
append initrd=initrd.img
label vesa
menu label Install system with ^basic video driver
kernel vmlinuz
append initrd=initrd.img xdriver=vesa nomodeset
label rescue
menu label ^Rescue installed system
kernel vmlinuz
append initrd=initrd.img rescue
label local
menu label Boot from ^local drive
localboot 0xffff
label memtest86
menu label ^Memory test
kernel memtest
append -
EOF
}
ks_file(){
cat > ${SYSTEM_DIR}/isolinux/ks.cfg <<EOF
# Kickstart file automatically generated by anaconda.
#linuxidc
#date 2013-07-20
#version=V6.4
install
text
cdrom
lang zh_CN.UTF-8
keyboard us
skipx
network --device eth0 --bootproto static --ip 192.168.50.1 --netmask 255.255.255.0 --gateway 192.168.50.254 --hostname linuxidc-FTP
rootpw --iscrypted /gvPu5dZM6eis
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append=" rhgb crashkernel=auto quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
#clearpart --none
clearpart --all --initlabel
zerombr yes
part /boot --fstype=ext4 --size=100
part swap --size=4000
part / --fstype=ext4 --size=10000
part /opt --fstype=ext4 --grow --size=1000
reboot
#repo --name="linuxidc" --baseurl=cdrom:sr0 --cost=100
%packages --nobase
@base
%packages
@chinese-support
@core
glibc-2.12-1.107.el6.i686
lrzsz
%post --nochroot
cp /mnt/source/Packages/conf/settings.sh /mnt/sysimage/tmp/settings.sh
mv /mnt/sysimage/etc/vsftpd/vsftpd.conf /mnt/sysimage/etc/vsftpd/vsftpd.conf.back
mv /mnt/sysimage/etc/pam.d/vsftpd /mnt/sysimage/etc/pam.d/vsftpd.back
cp /mnt/source/Packages/conf/vsftpd.conf /mnt/sysimage/etc/vsftpd/vsftpd.conf
cp /mnt/source/Packages/conf/ftpmgt.exe /mnt/sysimage/etc/vsftpd/ftpmgt.exe
cp /mnt/source/Packages/conf/vsftpd /mnt/sysimage/etc/pam.d/vsftpd
cp /mnt/source/Packages/conf/moban /mnt/sysimage/etc/vsftpd/moban
cp /mnt/source/Packages/conf/xianzhi /mnt/sysimage/etc/vsftpd/xianzhi
chmod 750 /mnt/sysimage/tmp/settings.sh
%post
/tmp/settings.sh
EOF
}
build_new_ISO (){
cd ${SYSTEM_DIR}
mkisofs -o ${SYSTEM_NAME}.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -V linuxidc -T ${SYSTEM_DIR}
}
main (){
mount_ISO
copy_ISO_file
isolinux_cfg
ks_file
rebuild_repo_xml
build_new_ISO
}
main