通过安装及配置DHCP,tftp-server,xinetd,httpd,syslinux来实现批量安装Linux系统
1 #!/bin/bash 2 #检查环境 3 se_state=`getenforce` 4 if [ $se_state != "Disabled" ];then 5 setenforce 0 6 sed -i's/=enforcing/=disabled/g' /etc/selinux/config 7 fi 8 systemctl start firewalld 9 systemctl enable firewalld 10 #检查yum源 11 mo_point=`ls / |grep local_dvd` 12 if [ -z $mo_point ];then 13 mkdir /local_dvd 14 mount /dev/cdrom /local_dvd 15 echo "/dev/sr0 /local_dvd iso9660 default 0 0 " >> /etc/fstab 16 rm -rf /etc/yum.repos.d/*.repo 17 echo "[local] 18 name=local 19 baseurl=file:///local_dvd 20 enabled=1 21 gpgcheck=0" >> /etc/yum.repos.d/local.repo 22 yum clean all 23 yum repolist 24 fi 25 #安装服务 26 yum -y install dhcp tftp-server xinetd httpd syslinux 27 #配置DHCP 28 IP=`ifconfig |grep -w inet |grep broad |awk '{print $2}'` 29 NETM=`ifconfig |grep -w netmask |grep broad |awk '{print $4}'` 30 NETW=`echo $IP |awk -F. '{print $1"."$2"."$3}'` 31 echo "option domain-name \"example.org\"; 32 option domain-name-servers ns1.example.org, ns2.example.org; 33 default-lease-time 600; 34 max-lease-time 7200; 35 log-facility local7; 36 subnet $NETW.0 netmask $NETM { 37 range $NETW.10 $NETW.200; 38 option routers $IP; 39 filename \"pxelinux.0\"; 40 next-server $IP; 41 }" > /etc/dhcp/dhcpd.conf 42 #配置tftp 43 mkdir /tftpboot 44 sed -i '13s/\/var\/lib//g' /etc/xinetd.d/tftp 45 sed -i '14s/yes/no/g' /etc/xinetd.d/tftp 46 #配置httpd,挂载系统光盘 47 mkdir /var/www/html/http_iso 48 mount /dev/cdrom /var/www/html/http_iso 49 echo "/dev/cdrom /var/www/html/http_iso iso9660 default 0 0 " >> /etc/fstab 50 #拷贝引导程序到tftp目录下 51 cp /var/www/html/http_iso/isolinux/vmlinuz /tftpboot 52 cp /var/www/html/http_iso/isolinux/initrd.img /tftpboot 53 mkdir /tftpboot/pxelinux.cfg 54 cp /var/www/html/http_iso/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default 55 cp /usr/share/syslinux/pxelinux.0 /tftpboot 56 #修改默认启动程序 57 sed -i '1s/vesamenu.c32/ks/g' /tftpboot/pxelinux.cfg/default 58 sed -i 120d /tftpboot/pxelinux.cfg/default 59 echo "label ks 60 menu label ^Install CentOS 7 61 kernel vmlinuz 62 append initrd=initrd.img method=http://$IP/http_iso ks=http://$IP/ks.cfg devfs=nomount 63 menu end " >> /tftpboot/pxelinux.cfg/default 64 #拷贝ks文件到httpd工作目录下 65 cp ~/anaconda-ks.cfg /var/www/html/ks.cfg 66 #给ks文件增加可读权限 67 chmod a+r /var/www/html/ks.cfg 68 #修改ks文件的系统安装方式 69 sed -i '1,10s/cdrom/install/g' /var/www/html/ks.cfg 70 sed -i 5a"url --url=\"http://$IP/http_iso\"" /var/www/html/ks.cfg 71 #启动服务 72 systemctl restart dhcpd httpd xinetd 73 systemctl enable dhcpd httpd xinetd 74 #添加防火墙 75 firewall-cmd --add-port=67/udp --permanent 76 firewall-cmd --add-port=69/udp --permanent 77 firewall-cmd --add-port=80/tcp --permanent 78 firewall-cmd --reload