某公司内部需要对大批量服务器进行安装centos操作系统人工进行一台一台安装费时又费力而且效率很低因此搭建一台PXE服务器显得十分重要通过搭建PXE服务器可以给所有需要安装系统的服务器进行快速安装而无需人工干预方便又快捷。但是如何进行快速有效的搭建一台PXE服务器呢我们可以通过编写一个Shell脚本进行快速搭建PXE平台。
搭建此PXE服务器需要能实现centos6.0X86_64、centos5.5i386以及centos5.5X86_64三种系统的选择安装用户可以选择自动化安装或者手动安装。所以作为PXE平台的服务器要求有centos6.0的kicstart文件、centos5.5X86_64的kickstart和centos5.5i386的kickstart文件这三个文件可以先在相应的系统下生成以及相应的镜像文件都要预先传送到被用来搭建成一台PXE服务器的主机上。
3.1首先服务器上必须要有安装各种类型的系统的镜像文件可以将这些镜像文件放在/opt目录下
[root@sswans20opt]#llC*
-rw-r--r--.1rootroot4185118720Mar1616:48CentOS-5.5-i386-bin-DVD.iso
-rw-r--r--.1rootroot4185118720Mar1616:48CentOS-5.5-x86_64-bin-DVD.iso
-rw-r--r--.1rootroot4238800896Mar1616:29CentOS-6.0-x86_64-bin-DVD1.iso
3.2将相应的ks.cfg文件也放在/opt目录下面
[root@sswans20opt]#llks*
-rwxrwxrwx.1rootroot1252Mar1617:02ks5.5-32.cfg#Centos5.5i386生成的kickstart文件
-rwxrwxrwx.1rootroot1256Mar1616:50ks5.5-64.cfg#Centos5.5X86_64生成的kickstart文件
-rwxrwxrwx.1rootroot1068Mar1617:02ks6.0-64.cfg#Centos6.0X86_64生成的kickstart文件
3.3自动化搭建PXE服务器的shell脚本内容如下
[root@sswans20opt]#vimauto_install.sh
#!/bin/bash
######################################################################################
##############Thisshellfilecanautoinstallcentos6.0andinstallcentos5.5###################
##############################CloseiptablesandSELINUX############################
setenforce0
echo"
#ThisfilecontrolsthestateofSELinuxonthesystem.
#SELINUX=cantakeoneofthesethreevalues:
#enforcing-SELinuxsecuritypolicyisenforced.
#permissive-SELinuxprintswarningsinsteadofenforcing.
#disabled-NoSELinuxpolicyisloaded.
SELINUX=permissive
#SELINUXTYPE=cantakeoneofthesetwovalues:
#targeted-Targetedprocessesareprotected,
#mls-MultiLevelSecurityprotection.
SELINUXTYPE=targeted
">/etc/sysconfig/selinux
iptables-F
/etc/init.d/iptablessave>>/dev/null
##################################AutoMount######################################
mkdir-p/var/ftp/centos/5.5/i386/dvd
mkdir-p/var/ftp/centos/5.5/x86_64/dvd
mkdir-p/var/ftp/centos/6.0/x86_64/dvd
mkdir-p/var/ftp/centos/ks
echo"/opt/CentOS-5.5-i386-bin-DVD.iso/var/ftp/centos/5.5/i386/dvdiso9660defaults,loop,ro00">>/etc/fstab
echo"/opt/CentOS-5.5-x86_64-bin-DVD.iso/var/ftp/centos/5.5/x86_64/dvdiso9660defaults,loop,ro00">>/etc/fstab
echo"/opt/CentOS-6.0-x86_64-bin-DVD1.iso/var/ftp/centos/6.0/x86_64/dvdiso9660defaults,loop,ro00">>/etc/fstab
mount-a
################################Installsomepackages################################
yuminstalldhcpvsftpdsyslinuxtftp-server-y>>/dev/null
servicevsftpdstart>>/dev/null
##############################ConfigureDHCPService################################
echo'
#dhcpd.conf
#SampleconfigurationfileforISCdhcpd
#optiondefinitionscommontoallsupportednetworks...
default-lease-time600;
max-lease-time7200;
allowbooting;
allowbootp;
subnet172.25.0.0netmask255.255.0.0{#需分配的网络段以及子网掩码
range172.25.11.10172.25.11.30;#给主机分配的IP地址范围
optiondomain-name-servers172.25.11.20;#DNS服务器地址
optionrouters172.25.11.20;#路由器地址
optionbroadcast-address172.25.255.255;#广播地址
next-server172.25.11.20;#TFTP服务器的地址
filename"pxelinux.0";
}'>/etc/dhcp/dhcpd.conf
servicedhcpdstart>>/dev/null
############################ConfigureTFTPService################################
echo"
#default:off
#description:Thetftpserverservesfilesusingthetrivialfiletransfer\
#protocol.Thetftpprotocolisoftenusedtobootdiskless\
#workstations,downloadconfigurationfilestonetwork-awareprinters,\
#andtostarttheinstallationprocessforsomeoperatingsystems.
servicetftp
{
socket_type=dgram
protocol=udp
wait=yes
user=root
server=/usr/sbin/in.tftpd
server_args=-unobody-s/var/lib/tftpboot
disable=no
per_source=11
cps=1002
flags=IPv4
}">/etc/xinetd.d/tftp
servicexinetdstart>>/dev/null
DIR1=/var/lib/tftpboot/pxelinux.cfg
if[-d$DIR1];then
rm-rf$DIR
else
mkdir-p/var/lib/tftpboot/pxelinux.cfg
fi
########################cpsomefilesto/var/lib/tftpboot################################
cp/usr/share/syslinux/pxelinux.0/var/lib/tftpboot/
mkdir/var/lib/tftpboot/centos6.0_x86_64
cp/var/ftp/centos/6.0/x86_64/dvd/images/pxeboot/vmlinuz/var/lib/tftpboot/centos6.0_x86_64
cp/var/ftp/centos/6.0/x86_64/dvd/images/pxeboot/initrd.img/var/lib/tftpboot/centos6.0_x86_64
cp/var/ftp/centos/6.0/x86_64/dvd/isolinux/boot.msg/var/lib/tftpboot/
cp/var/ftp/centos/6.0/x86_64/dvd/isolinux/vesamenu.c32/var/lib/tftpboot/
cp/var/ftp/centos/6.0/x86_64/dvd/isolinux/splash.jpg/var/lib/tftpboot/
#######################Addanewcentos5i386system#################################
mkdir/var/lib/tftpboot/centos5.5_i386
cp/var/ftp/centos/5.5/i386/dvd/images/pxeboot/vmlinuz/var/lib/tftpboot/centos5.5_i386
cp/var/ftp/centos/5.5/i386/dvd/images/pxeboot/initrd.img/var/lib/tftpboot/centos5.5_i386
#######################Addanewcentos5x86_64system##############################
mkdir/var/lib/tftpboot/centos5.5_x86_64
cp/var/ftp/centos/5.5/x86_64/dvd/images/pxeboot/vmlinuz/var/lib/tftpboot/centos5.5_i386
cp/var/ftp/centos/5.5/x86_64/dvd/images/pxeboot/initrd.img/var/lib/tftpboot/centos5.5_i386
##########################Configurethedefaultfile####################################
echo"
defaultvesamenu.c32
timeout600
displayboot.msg
menubackgroundsplash.jpg
menutitleWelcometoKEDACOMInstallCentOS!
menucolorborder0#ffffffff#00000000
menucolorsel7#ffffffff#ff000000
menucolortitle0#ffffffff#00000000
menucolortabmsg0#ffffffff#00000000
menucolorunsel0#ffffffff#00000000
menucolorhotsel0#ff000000#ffffffff
menucolorhotkey7#ffffffff#ff000000
menucolorscrollbar0#ffffffff#00000000
labelCentOS6.0_x86_64
menulabelCentOS6.0X86_84installwithkickstart
menudefault
kernelcentos6.0_x86_64/vmlinuz
appendinitrd=centos6.0_x86_64/initrd.imgks=ftp://172.25.11.20/centos/ks/ks6.0-64.cfgtextksdevice=eth0
labelCentOS5.5_i386
menulabelCentOS5.5i386installwithkickstart
kernelcentos5.5_i386/vmlinuz
appendinitrd=centos5.5_i386/initrd.imgks=ftp://172.25.11.20/centos/ks/ks5.5-32.cfgtextksdevice=eth0
labelCentOS5.5_x86_64
menulabelCentOS5.5X86_64installwithkickstart
kernelcentos5.5_x86_64/vmlinuz
appendinitrd=centos5.5_x86_64/initrd.imgks=ftp://172.25.11.20/centos/ks/ks5.5-64.cfgtext
ksdevice=eth0
labelCentOS6.0-x86_64
menulabelShoudonginstallCentOS6.0X86_84
kernelcentos6.0_x86_64/vmlinuz
appendinitrd=centos6.0_x86_64/initrd.img
labelCentOS5.5-i386
menulabelShoudonginstallCentOS5.5i386
kernelcentos5.5_i386/vmlinuz
appendinitrd=centos5.5_i386/initrd.img
labelCentOS5.5-x86_64
menulabelShoudonginstallCentOS5.5X86_64
kernelcentos5.5_x86_64/vmlinuz
appendinitrd=centos5.5_x86_64/initrd.img
">/var/lib/tftpboot/pxelinux.cfg/default
############################ConfigureKickstart####################################
mv/opt/ks5.5-32.cfg/var/ftp/centos/ks
chmod777/var/ftp/centos/ks/ks5.5-32.cfg
mv/opt/ks5.5-64.cfg/var/ftp/centos/ks
chmod777/var/ftp/centos/ks/ks5.5-64.cfg
mv/opt/ks6.0-64.cfg/var/ftp/centos/ks
chmod777/var/ftp/centos/ks/ks6.0-64.cfg
############################ALLServiceRestart#####################################
forFWindhcpdxinetdvsftpd
do
service$FWrestart
chkconfig$FWon
done
3.4用户对编写好的自动化脚本进行赋予可执行的权限这样脚本才能执行
[root@sswans20~]#chmod777auto_install.sh
[root@sswans20~]#llauto_install.sh
-rwxrwxrwx.1rootroot5236Mar1616:52auto_install.sh
特别说明
由于我的DHCP服务器、FTP服务器、TFTP服务器、DNS服务器的地址都是172.25.11.20所以在本脚本执行前根据你的实际环境需要做如下修改
、分别打开每个ks文件查看url--url=ftp://172.25.11.20/是否跟你的FTP服务器地址一致不一致要修改
、确保此的脚本中红色标记的IP地址一定是你的FTP服务器、TFTP服务器地址
、脚本中蓝色标记的IP地址可改可不改根据你的实际情况需要自行决定
3.5以上无误后执行此脚本文件
[root@sswans20~]#./auto_install.sh#执行后需要等待一段时间
Shuttingdowndhcpd:[OK]
Startingdhcpd:[OK]
Stoppingxinetd:[OK]
Startingxinetd:[OK]
Shuttingdownvsftpd:[OK]
Startingvsftpdforvsftpd:[OK]
3.6验证PXE服务器是否搭建成功通过给另一台服务器安装系统来求证
由于服务器上我没有上传centos5.5x86_64位系统镜像文件所以只能看到四个安装选项。
我选择centos5.5i386自动安装的过程截图如下
通过此安装界面用户使用上下键选择安装用户需要使用的系统类型选择后按回车键系统开始自动化安装过程不选的话60s后默认选择第一项安装。
自动化搭建PXE服务器关键在于shell脚本的编写这要求编写者对shell语法的熟悉以及一定的逻辑思维能力。通过此脚本给那些对PXE搭建还不太清楚的朋友们提供了傻瓜式安装。感兴趣的朋友可以对脚本内容进行详细分析与阅读。有疑问的地方可以发微博咨询我谢谢
本文出自 “706737” 博客,谢绝转载!