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+TFTP+NGINX+Kickstart自动安装脚本_PXE

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

PXE+TFTP+NGINX+Kickstart自动安装脚本_PXE_02

PXE+TFTP+NGINX+Kickstart自动安装脚本_NGINX_03

 

脚本代码:

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

Kickstart创建LVM命令

 

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