1案例描述
某公司內部需要對大批量伺服器進行安裝centos作業系統人工進行一台一台安裝費時又費力而且效率很低因此搭建一台PXE伺服器顯得十分重要通過搭建PXE伺服器可以給所有需要安裝系統的伺服器進行快速安裝而無需人工幹預方便又快捷。但是如何進行快速有效搭建一台PXE伺服器呢我們可以通過編寫一個Shell指令碼進行快速搭建PXE平台。
2案例分析
搭建此PXE伺服器需要能實現centos6.0X86_64、centos5.5i386以及centos5.5X86_64三種系統的選擇安裝使用者可以選擇自動化安裝或者手動安裝。所以作為PXE平台的伺服器要求有centos6.0的kicstart檔案、centos5.5X86_64的kickstart和centos5.5i386的kickstart檔案這三個檔案可以先在相應的系統下產生以及相應的鏡像檔案都要預先傳送到被用來搭建成一台PXE伺服器的主機上。
3解決過程
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伺服器是否搭建成功通過給另一台伺服器安裝系統來求證
650) this.width=650;" width="578" height="260" style="border:1px solid #DDDDDD;" src="/e/u/themes/default/images/spacer.gif" alt="spacer.gif" />
由於伺服器上我沒有上傳centos5.5x86_64位系統鏡像檔案所以只能看到四個安裝選項。
我選擇centos5.5i386自動安裝的過程如下
650) this.width=650;" width="604" height="336" style="border:1px solid #DDDDDD;" src="/e/u/themes/default/images/spacer.gif" alt="spacer.gif" />
4解決結果
通過此安裝介面使用者使用上下鍵選擇安裝使用者需要使用的系統類別型選擇後按斷行符號鍵系統開始自動化安裝過程不選的話60s後預設選擇第一項安裝。
5總結
自動化搭建PXE伺服器關鍵在於shell指令碼的編寫這要求編寫者對shell文法的熟悉以及一定的邏輯思維能力。通過此指令碼給那些對PXE搭建還不太清楚的朋友們提供了傻瓜式安裝。感興趣的朋友可以對指令碼內容進行詳細分析與閱讀。有疑問的地方可以發微博諮詢我謝謝
本文出自 “706737” 部落格,謝絕轉載!