PXE+TFTP+NGINX+Kickstart安装并测试完成,并用脚本实现自动安装PXE环境,脚本介绍如下:

     脚本功能: 自动安装配置PXE+TFTP+NGINX+Kickstart环境

     运行前提: CentOS DVD文件准备好,默认是从光驱挂载并复制

  脚本变量: 根据实际情况修改如下变量

         HTML_DIR nginx默认的html目录

         NAME_SERVER dhcpd.confdomain-name-servers的值

         SUBNET   dhcpd.confsubnet的值

         NETMASK  dhcpd.confnetmask的值

         RANG_START dhcpd.confIP地址池开始地址

         RANG_END  dhcpd.confIP地址池结束地址

         NEXT_SERVER dhcpd.confnext_server的值

         TFTPBOOT_DIR tftp默认目录

         PXELINUX_DIR pxelinux.cfg目录

         BOOT_SIZE    /boot分区大小

         SWAP_SIZE     swap大小

         ROOT_SIZE  /分区大小, 其余空间为/opt分区  

使用注意: PXE环境和生产环境要隔离,并且限制主机的访问,防止有服务器默认从网卡启动,重启后就直接重新安装系统

脚本功能演示:

演示时为了节省时间,没有将iso挂载到光驱,所以找不到iso下面的文件

运行完脚本后,环境就搭建完成,将服务器从PXE启动:

 

脚本代码:

 
  
  1. #! /bin/bash  
  2. # 
  3. # FILE: pxe_kickstart_install.sh  
  4.  
  5. # USAGE: ./pxe_kickstart_install.sh  
  6.  
  7. # DESCRIPTION: quickly install pxe+tftp+nginx+kickstart, auto install centos 5   
  8.  
  9. # AUTHOR: http://waydee.blog.51cto.com/# 
  10. # CREATED: 2012-4-24 
  11. # VERSION: 1.0  
  12.  
  13.  
  14.  
  15. #################### VARIABLES #################### 
  16.  
  17. ISO_MNT="/mnt/iso"                  #ISO_MNT is CentOS DVD mount dir 
  18. G_LEFT="\e[0;32m\033[1m"            #G_LEFT and G_RIGHT to echo green strings 
  19. G_RIGHT="\e[m"                      #G_LEFT and G_RIGHT to echo green strings 
  20. R_LEFT="\e[0;31m\033[1m"            #R_LEFT  and R_RIGHT  to echo red  strings 
  21. R_RIGHT="\e[m"                      #R_LEFT  and R_RIGHT  to echo red  strings 
  22. HTML_DIR="/usr/share/nginx/html"    #HTML_DIR is nginx / dir,and copy centos dvd to this dir  
  23. NAME_SERVER="192.168.2.101"         #NAME_SERVER is for  dhcpd.conf domain-name-servers   
  24. SUBNET="192.168.2.0"                #SUBNET is for  dhcpd.conf subnet 
  25. NETMASK="255.255.255.0"             #NETMASK is for dhcpd.conf  netmask  
  26. RANG_START="192.168.2.220"          #RANG_START is for dhcpd.conf  range start  
  27. RANG_END="192.168.2.225"            #RANG_END is  for dhcpd.conf  rang end 
  28. NEXT_SERVER="192.168.2.101"         #NEXT_SERVER is for dhcpd.conf  next_server  
  29. TFTPBOOT_DIR="/var/lib/tftpboot"    #tftp boot dir ,default is /var/lib/tftpboot 
  30. PXELINUX_DIR="${TFTPBOOT_DIR}/pxelinux.cfg"     #pxelinux.cfg dir  
  31. BOOT_SIZE="100"                     #set /boot size   
  32. SWAP_SIZE="4096"                    #set swap size  
  33. ROOT_SIZE="40960"                   #set / size , the rest set to /opt  
  34.  
  35.  
  36. #################### FUNCTIONS #################### 
  37.  
  38. #check_mnt_dir use to check if ISO_MNT dir exits 
  39. check_mnt_dir() 
  40.         if [ -e ${ISO_MNT} ] 
  41.         then 
  42.             echo -e "INFO: ${ISO_MNT} dir has ${G_LEFT}exits${G_RIGHT}." 
  43.         else 
  44.             mkdir -p ${ISO_MNT} 
  45.             echo -e "INFO: Create dir ${ISO_MNT} ${G_LEFT}done${G_RIGHT}." 
  46.         fi   
  47.  
  48.  
  49.  
  50.  
  51. ####################  MAIN  #################### 
  52.  
  53. #install EPEL  
  54. if [ ! -e /etc/yum.repos.d/epel.repo ] 
  55. then 
  56.      rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm 1>/dev/null 
  57.      echo -e "Install EPEL source ${G_LEFT}done${G_RIGHT}." 
  58. fi 
  59.  
  60. #install tftp-server system-config-kickstart dhcp syslinux nginx  
  61. yum -y install tftp-server system-config-kickstart dhcp syslinux nginx   1>/dev/null 
  62. echo -e "INFO: Install RPMS ${G_LEFT}done${G_RIGHT}." 
  63.  
  64. #create mnt dir 
  65. check_mnt_dir  
  66.  
  67. #mount DVD p_w_picpath to ISO_MNT 
  68. mount -t iso9660 /dev/cdrom  ${ISO_MNT} 1>/dev/null 
  69. echo -e "INFO: Mount cdrom to ${ISO_MNT} ${G_LEFT}done${G_RIGHT}." 
  70.  
  71. #copy file to html dir 
  72. echo -e "INFO: Start to copy CentOS p_w_picpath, this may take severl minitus ..." 
  73. cp -a ${ISO_MNT}/*     ${HTML_DIR} 1>/dev/null  
  74. echo -e "INFO: Copy Centos DVD ${G_LEFT}done${G_RIGHT}." 
  75.  
  76. #config dhcp server 
  77. cat>/etc/dhcp/dhcpd.conf<<EOF 
  78. option domain-name-servers ${NAME_SERVER}; 
  79. max-lease-time 7200;               
  80. authoritative;                
  81.  
  82. subnet ${SUBNET} netmask ${NETMASK} { 
  83. range ${RANG_START}  ${RANG_END}; 
  84. next-server ${NEXT_SERVER}; 
  85. filename "pxelinux.0"
  86. EOF 
  87.  
  88. echo -e "INFO: Config DHCP ${G_LEFT}done${G_RIGHT}." 
  89.  
  90. #start dhcp server  
  91. service dhcpd restart 1>/dev/null   
  92. sleep 2 
  93.  
  94. #check if dhcpd is started 
  95. ps aux|grep dhcpd|grep -v grep 1>/dev/null 
  96. DHCP_STATUS=$? 
  97. if [[ ${DHCP_STATUS} == 1  ]] 
  98. then 
  99.     echo -e "ERROR: DHCPD start ${R_LEFT}Failed${R_RIGHT}." 
  100.     exit 1 
  101. else 
  102.     echo -e "INFO: DHCPD start ${G_LEFT}OK${G_RIGHT}." 
  103. fi 
  104.  
  105. #turn on tftp 
  106. sed   -i  's/disable.*$/disable         =no/'  /etc/xinetd.d/tftp   
  107. echo -e "INFO: TFTP turn on ${G_LEFT}OK${G_RIGHT}." 
  108.  
  109. #restart xinetd service  
  110. service  xinetd restart 1>/dev/null 
  111. echo -e "INFO: Xinetd restart ${G_LEFT}OK${G_RIGHT}.." 
  112.  
  113. #create pxelinux.cfg dir  
  114. if [ -e ${PXELINUX_DIR} ] 
  115. then 
  116.     echo -e "INFO: ${PXELINUX_DIR} dir has ${G_LEFT}exits${G_RIGHT}." 
  117. else 
  118.     mkdir -p ${PXELINUX_DIR} 
  119.     echo -e "INFO: Create dir ${PXELINUX_DIR} ${G_LEFT}done${G_RIGHT}." 
  120. fi 
  121.  
  122. #copy initrd.img vmlinuz pxelinux.0   
  123. cp -a ${ISO_MNT}/p_w_picpaths/pxeboot/initrd.img  ${TFTPBOOT_DIR} 
  124. echo -e "INFO: Copy initrd.img to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}." 
  125.  
  126. cp -a ${ISO_MNT}/p_w_picpaths/pxeboot/vmlinuz  ${TFTPBOOT_DIR} 
  127. echo -e "INFO: Copy vmlinuz  to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}." 
  128.  
  129. cp -a /usr/share/syslinux/pxelinux.0  ${TFTPBOOT_DIR} 
  130. echo -e "INFO: Copy pxelinux.0 to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}." 
  131.  
  132.  
  133. #config pxelinux.cfg file  default  
  134. cat>${PXELINUX_DIR}/default<<EOF 
  135. default ks  
  136. prompt 1 
  137. timeout 600 
  138. display boot.msg 
  139. F1 boot.msg 
  140. F2 options.msg 
  141. F3 general.msg 
  142. F4 param.msg 
  143. F5 rescue.msg 
  144. label linux 
  145.   kernel vmlinuz 
  146.     append initrd=initrd.img 
  147. label text 
  148.   kernel vmlinuz 
  149.    append initrd=initrd.img text 
  150. label ks 
  151.   menu default  
  152.   kernel vmlinuz 
  153.   append initrd=initrd.img ksdevice=eth0 ks=http://${NAME_SERVER}/ks.cfg 
  154. label local 
  155.   localboot 1 
  156.   label memtest86 
  157.   kernel memtest 
  158. append - 
  159. EOF 
  160. echo -e "INFO: pxelinux.cfg/default ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}." 
  161.  
  162. #config ks.cf file  
  163. cat>${HTML_DIR}/ks.cfg<<EOF 
  164. #platform=x86, AMD64, or Intel EM64T 
  165. #version=DEVEL 
  166. # Firewall configuration 
  167. firewall --enabled --http --ssh 
  168. # Install OS instead of upgrade 
  169. install 
  170. # Use hard drive installation media 
  171. #harddrive --dir=/ --partition=/dev/sdb1 
  172. url --url=http://${NAME_SERVER}/ 
  173. # Root password 
  174. rootpw --iscrypted $1$kBe14BGG$GEWFllYBwDpMn055nK7Jk0 
  175. # System authorization information 
  176. auth  --useshadow  --passalgo=sha512 
  177. # Use text mode install 
  178. text 
  179. # System keyboard 
  180. keyboard us 
  181. # System language 
  182. lang en_US 
  183. # SELinux configuration 
  184. selinux --disabled 
  185. # Do not configure the X Window System 
  186. skipx 
  187. # Installation logging level 
  188. logging --level=info 
  189. # Reboot after installation 
  190. reboot 
  191. # System timezone 
  192. timezone  Asia/Shanghai 
  193. # Network information 
  194. network  --bootproto=dhcp --device=eth0 --onboot=on 
  195. # System bootloader configuration 
  196. bootloader --location=mbr 
  197. # Partition clearing information 
  198. clearpart --all   
  199. # Disk partitioning information 
  200. part /boot --asprimary --fstype="ext3" --size=${BOOT_SIZE} 
  201. part swap --fstype="swap" --size=${SWAP_SIZE} 
  202. part / --fstype="ext3" --size=${ROOT_SIZE} 
  203. part /opt --fstype="ext3" --grow --size=1 
  204.  
  205. %packages 
  206. @base 
  207. @core 
  208. imake 
  209. keyutils 
  210. trousers 
  211. fipscheck 
  212. device-mapper-multipath 
  213. @development-libs 
  214. @admin-tools 
  215. @system-tools 
  216. @chinese-support 
  217. @text-internet 
  218. EOF 
  219. echo -e "INFO: ks.cfg config ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}." 
  220.  
  221. #restart nginx  
  222. service nginx restart 2>&1 >/dev/null  
  223. sleep 2 
  224.  
  225. ps aux|grep nginx|grep -v grep 1>/dev/null  
  226. NGINX_STATUS=$? 
  227. if [[ ${NGINX_STATUS} == 1  ]] 
  228. then 
  229.     echo -e "ERROR: nginx start ${R_LEFT}Failed${R_RIGHT}." 
  230.     exit 1 
  231. else 
  232.     echo -e "INFO: nginx start ${G_LEFT}OK${G_RIGHT}." 
  233. fi 
  234.  
  235. #pxe_kickstart_install done 
  236. echo -e "INFO: PXE+TFTP+NGINX+Kickstart has been installed  ${G_LEFT}SUCCESS${G_RIGHT}." 

Kickstart创建LVM命令

 
  
  1. # Disk partitioning information 
  2.  
  3.  part /boot  --fstype="ext3" --size=100 
  4.  
  5.  part pv.01 --size=1 --grow 
  6.  
  7.  volgroup vg_root pv.01 
  8.  
  9.  logvol / --vgname=vg_root --size=30720 --name=lv_root 
  10.  
  11.  logvol swap --vgname=vg_root --size=32768 --name=lv_swap 
  12.  
  13.  logvol /opt --vgname=vg_root --size=1 --grow --name=lv_opt