通过安装及配置DHCP,tftp-server,xinetd,httpd,syslinux来实现批量安装Linux系统
#!/bin/bash #检查环境 se_state=`getenforce` if [ $se_state != "Disabled" ];then setenforce 0 sed -i 's/=enforcing/=disabled/g' /etc/selinux/config fi systemctl start firewalld systemctl enable firewalld &> /dev/null #检查yum源 mo_point=`ls / |grep local_dvd` if [ -z $mo_point ];then mkdir /local_dvd mount /dev/cdrom /local_dvd #echo "/dev/sr0 /local_dvd iso9660 defaults 0 0 " >> /etc/fstab mkdir /etc/yum.repos.d/old mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/old echo "[local] name=local baseurl=file:///local_dvd enabled=1 gpgcheck=0" >> /etc/yum.repos.d/local.repo yum clean all yum makecache fi #安装服务 yum -y install dhcp tftp-server xinetd httpd syslinux #配置DHCP IP=`ifconfig |grep -w inet |grep broad |awk '{print $2}'` NETM=`ifconfig |grep -w netmask |grep broad |awk '{print $4}'` NETW=`echo $IP |awk -F. '{print $1"."$2"."$3}'` echo "option domain-name "example.org"; option domain-name-servers ns1.example.org, ns2.example.org; default-lease-time 600; max-lease-time 7200; log-facility local7; subnet $NETW.0 netmask $NETM { range $NETW.10 $NETW.200; option routers $IP; filename "pxelinux.0"; next-server $IP; }" > /etc/dhcp/dhcpd.conf #配置tftp mkdir /tftpboot sed -i '13s//var/lib//g' /etc/xinetd.d/tftp sed -i '14s/yes/no/g' /etc/xinetd.d/tftp #配置httpd,挂载系统光盘 mkdir /var/www/html/http_iso mount /dev/cdrom /var/www/html/http_iso #echo "/dev/cdrom /var/www/html/http_iso iso9660 defaults 0 0 " >> /etc/fstab #拷贝引导程序到tftp目录下 cp /var/www/html/http_iso/isolinux/vmlinuz /tftpboot cp /var/www/html/http_iso/isolinux/initrd.img /tftpboot mkdir /tftpboot/pxelinux.cfg cp /var/www/html/http_iso/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default cp /usr/share/syslinux/pxelinux.0 /tftpboot #修改默认启动程序 sed -i '1s/vesamenu.c32/ks/g' /tftpboot/pxelinux.cfg/default sed -i 120d /tftpboot/pxelinux.cfg/default echo "label ks menu label ^Install CentOS 7 kernel vmlinuz append initrd=initrd.img method=http://$IP/http_iso ks=http://$IP/ks.cfg devfs=nomount menu end " >> /tftpboot/pxelinux.cfg/default #拷贝ks文件到httpd工作目录下 cp ~/anaconda-ks.cfg /var/www/html/ks.cfg #给ks文件增加可读权限 chmod a+r /var/www/html/ks.cfg #修改ks文件的系统安装方式 sed -i '1,10s/cdrom/install/g' /var/www/html/ks.cfg sed -i 5a"url --url="http://$IP/http_iso"" /var/www/html/ks.cfg #启动服务 systemctl restart dhcpd httpd xinetd #systemctl enable dhcpd httpd xinetd #添加防火墙 firewall-cmd --add-port=67/udp --permanent firewall-cmd --add-port=69/udp --permanent firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload