PXE+TFTP+NGINX+Kickstart安装并测试完成,并用脚本实现自动安装PXE环境,脚本介绍如下:
脚本功能: 自动安装配置PXE+TFTP+NGINX+Kickstart环境
运行前提: 将CentOS DVD文件准备好,默认是从光驱挂载并复制
脚本变量: 根据实际情况修改如下变量
HTML_DIR: nginx默认的html目录
NAME_SERVER: dhcpd.conf的domain-name-servers的值
SUBNET: dhcpd.conf的subnet的值
NETMASK: dhcpd.conf的netmask的值
RANG_START: dhcpd.conf的IP地址池开始地址
RANG_END: dhcpd.conf的IP地址池结束地址
NEXT_SERVER: dhcpd.conf的next_server的值
TFTPBOOT_DIR: tftp默认目录
PXELINUX_DIR: pxelinux.cfg目录
BOOT_SIZE: /boot分区大小
SWAP_SIZE: swap大小
ROOT_SIZE: /分区大小, 其余空间为/opt分区
使用注意: PXE环境和生产环境要隔离,并且限制主机的访问,防止有服务器默认从网卡启动,重启后就直接重新安装系统
脚本功能演示:
演示时为了节省时间,没有将iso挂载到光驱,所以找不到iso下面的文件
运行完脚本后,环境就搭建完成,将服务器从PXE启动:
脚本代码:
- #! /bin/bash
- #
- # FILE: pxe_kickstart_install.sh
- #
- # USAGE: ./pxe_kickstart_install.sh
- #
- # DESCRIPTION: quickly install pxe+tftp+nginx+kickstart, auto install centos 5
- #
- # AUTHOR: http://waydee.blog.51cto.com/#
- # CREATED: 2012-4-24
- # VERSION: 1.0
- #################### VARIABLES ####################
- ISO_MNT="/mnt/iso" #ISO_MNT is CentOS DVD mount dir
- G_LEFT="\e[0;32m\033[1m" #G_LEFT and G_RIGHT to echo green strings
- G_RIGHT="\e[m" #G_LEFT and G_RIGHT to echo green strings
- R_LEFT="\e[0;31m\033[1m" #R_LEFT and R_RIGHT to echo red strings
- R_RIGHT="\e[m" #R_LEFT and R_RIGHT to echo red strings
- HTML_DIR="/usr/share/nginx/html" #HTML_DIR is nginx / dir,and copy centos dvd to this dir
- NAME_SERVER="192.168.2.101" #NAME_SERVER is for dhcpd.conf domain-name-servers
- SUBNET="192.168.2.0" #SUBNET is for dhcpd.conf subnet
- NETMASK="255.255.255.0" #NETMASK is for dhcpd.conf netmask
- RANG_START="192.168.2.220" #RANG_START is for dhcpd.conf range start
- RANG_END="192.168.2.225" #RANG_END is for dhcpd.conf rang end
- NEXT_SERVER="192.168.2.101" #NEXT_SERVER is for dhcpd.conf next_server
- TFTPBOOT_DIR="/var/lib/tftpboot" #tftp boot dir ,default is /var/lib/tftpboot
- PXELINUX_DIR="${TFTPBOOT_DIR}/pxelinux.cfg" #pxelinux.cfg dir
- BOOT_SIZE="100" #set /boot size
- SWAP_SIZE="4096" #set swap size
- ROOT_SIZE="40960" #set / size , the rest set to /opt
- #################### FUNCTIONS ####################
- #check_mnt_dir use to check if ISO_MNT dir exits
- check_mnt_dir()
- {
- if [ -e ${ISO_MNT} ]
- then
- echo -e "INFO: ${ISO_MNT} dir has ${G_LEFT}exits${G_RIGHT}."
- else
- mkdir -p ${ISO_MNT}
- echo -e "INFO: Create dir ${ISO_MNT} ${G_LEFT}done${G_RIGHT}."
- fi
- }
- #################### MAIN ####################
- #install EPEL
- if [ ! -e /etc/yum.repos.d/epel.repo ]
- then
- rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm 1>/dev/null
- echo -e "Install EPEL source ${G_LEFT}done${G_RIGHT}."
- fi
- #install tftp-server system-config-kickstart dhcp syslinux nginx
- yum -y install tftp-server system-config-kickstart dhcp syslinux nginx 1>/dev/null
- echo -e "INFO: Install RPMS ${G_LEFT}done${G_RIGHT}."
- #create mnt dir
- check_mnt_dir
- #mount DVD image to ISO_MNT
- mount -t iso9660 /dev/cdrom ${ISO_MNT} 1>/dev/null
- echo -e "INFO: Mount cdrom to ${ISO_MNT} ${G_LEFT}done${G_RIGHT}."
- #copy file to html dir
- echo -e "INFO: Start to copy CentOS image, this may take severl minitus ..."
- cp -a ${ISO_MNT}/* ${HTML_DIR} 1>/dev/null
- echo -e "INFO: Copy Centos DVD ${G_LEFT}done${G_RIGHT}."
- #config dhcp server
- cat>/etc/dhcp/dhcpd.conf<<EOF
- option domain-name-servers ${NAME_SERVER};
- max-lease-time 7200;
- authoritative;
- subnet ${SUBNET} netmask ${NETMASK} {
- range ${RANG_START} ${RANG_END};
- next-server ${NEXT_SERVER};
- filename "pxelinux.0";
- }
- EOF
- echo -e "INFO: Config DHCP ${G_LEFT}done${G_RIGHT}."
- #start dhcp server
- service dhcpd restart 1>/dev/null
- sleep 2
- #check if dhcpd is started
- ps aux|grep dhcpd|grep -v grep 1>/dev/null
- DHCP_STATUS=$?
- if [[ ${DHCP_STATUS} == 1 ]]
- then
- echo -e "ERROR: DHCPD start ${R_LEFT}Failed${R_RIGHT}."
- exit 1
- else
- echo -e "INFO: DHCPD start ${G_LEFT}OK${G_RIGHT}."
- fi
- #turn on tftp
- sed -i 's/disable.*$/disable =no/' /etc/xinetd.d/tftp
- echo -e "INFO: TFTP turn on ${G_LEFT}OK${G_RIGHT}."
- #restart xinetd service
- service xinetd restart 1>/dev/null
- echo -e "INFO: Xinetd restart ${G_LEFT}OK${G_RIGHT}.."
- #create pxelinux.cfg dir
- if [ -e ${PXELINUX_DIR} ]
- then
- echo -e "INFO: ${PXELINUX_DIR} dir has ${G_LEFT}exits${G_RIGHT}."
- else
- mkdir -p ${PXELINUX_DIR}
- echo -e "INFO: Create dir ${PXELINUX_DIR} ${G_LEFT}done${G_RIGHT}."
- fi
- #copy initrd.img vmlinuz pxelinux.0
- cp -a ${ISO_MNT}/images/pxeboot/initrd.img ${TFTPBOOT_DIR}
- echo -e "INFO: Copy initrd.img to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
- cp -a ${ISO_MNT}/images/pxeboot/vmlinuz ${TFTPBOOT_DIR}
- echo -e "INFO: Copy vmlinuz to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
- cp -a /usr/share/syslinux/pxelinux.0 ${TFTPBOOT_DIR}
- echo -e "INFO: Copy pxelinux.0 to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
- #config pxelinux.cfg file default
- cat>${PXELINUX_DIR}/default<<EOF
- default ks
- prompt 1
- timeout 600
- display boot.msg
- F1 boot.msg
- F2 options.msg
- F3 general.msg
- F4 param.msg
- F5 rescue.msg
- label linux
- kernel vmlinuz
- append initrd=initrd.img
- label text
- kernel vmlinuz
- append initrd=initrd.img text
- label ks
- menu default
- kernel vmlinuz
- append initrd=initrd.img ksdevice=eth0 ks=http://${NAME_SERVER}/ks.cfg
- label local
- localboot 1
- label memtest86
- kernel memtest
- append -
- EOF
- echo -e "INFO: pxelinux.cfg/default ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
- #config ks.cf file
- cat>${HTML_DIR}/ks.cfg<<EOF
- #platform=x86, AMD64, or Intel EM64T
- #version=DEVEL
- # Firewall configuration
- firewall --enabled --http --ssh
- # Install OS instead of upgrade
- install
- # Use hard drive installation media
- #harddrive --dir=/ --partition=/dev/sdb1
- url --url=http://${NAME_SERVER}/
- # Root password
- rootpw --iscrypted $1$kBe14BGG$GEWFllYBwDpMn055nK7Jk0
- # System authorization information
- auth --useshadow --passalgo=sha512
- # Use text mode install
- text
- # System keyboard
- keyboard us
- # System language
- lang en_US
- # SELinux configuration
- selinux --disabled
- # Do not configure the X Window System
- skipx
- # Installation logging level
- logging --level=info
- # Reboot after installation
- reboot
- # System timezone
- timezone Asia/Shanghai
- # Network information
- network --bootproto=dhcp --device=eth0 --onboot=on
- # System bootloader configuration
- bootloader --location=mbr
- # Partition clearing information
- clearpart --all
- # Disk partitioning information
- part /boot --asprimary --fstype="ext3" --size=${BOOT_SIZE}
- part swap --fstype="swap" --size=${SWAP_SIZE}
- part / --fstype="ext3" --size=${ROOT_SIZE}
- part /opt --fstype="ext3" --grow --size=1
- %packages
- @base
- @core
- imake
- keyutils
- trousers
- fipscheck
- device-mapper-multipath
- @development-libs
- @admin-tools
- @system-tools
- @chinese-support
- @text-internet
- EOF
- echo -e "INFO: ks.cfg config ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
- #restart nginx
- service nginx restart 2>&1 >/dev/null
- sleep 2
- ps aux|grep nginx|grep -v grep 1>/dev/null
- NGINX_STATUS=$?
- if [[ ${NGINX_STATUS} == 1 ]]
- then
- echo -e "ERROR: nginx start ${R_LEFT}Failed${R_RIGHT}."
- exit 1
- else
- echo -e "INFO: nginx start ${G_LEFT}OK${G_RIGHT}."
- fi
- #pxe_kickstart_install done
- echo -e "INFO: PXE+TFTP+NGINX+Kickstart has been installed ${G_LEFT}SUCCESS${G_RIGHT}."
Kickstart创建LVM命令
- # Disk partitioning information
- part /boot --fstype="ext3" --size=100
- part pv.01 --size=1 --grow
- volgroup vg_root pv.01
- logvol / --vgname=vg_root --size=30720 --name=lv_root
- logvol swap --vgname=vg_root --size=32768 --name=lv_swap
- logvol /opt --vgname=vg_root --size=1 --grow --name=lv_opt