PXE 自动部署CentOS

  1 #!/bin/bash
  2 
  3 #  配置管理IP
  4 #vi /etc/sysconfig/network-scripts/ifcfg-ens192
  5 
  6 #  配置pxe IP    192.168.0.1/24    不能更换成其他IP
  7 #vi /etc/sysconfig/network-scripts/ifcfg-ens224
  8 
  9 #  配置主机名    pxe-server
 10 hostname pxe-server
 11 echo pxe-server > /etc/hostname
 12 
 13 #  创建存放iso的目录    /mnt/iso
 14 mkdir -p /mnt/iso
 15 
 16 #  下载最新版 CentOS6 和 CentOS7 iso文件(随着版本的更新,镜像网站将停止老版本的下载支持,需要自己搞定iso下载)
 17 #wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-6.10-x86_64-bin-DVD1.iso
 18 #wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-6.10-x86_64-bin-DVD2.iso
 19 #wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-7-x86_64-Everything-1908.iso
 20 
 21 ####    自动化配置基础环境
 22 
 23 #  配置阿里云YUM源
 24 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
 25 
 26 #  安装EPEL
 27 yum install -y epel-release
 28 
 29 #  配置阿里云EPEL源
 30 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
 31 
 32 #  安装常用软件
 33 yum install -y vim wget lftp net-tools bash-completion jq git sysstat lrzsz
 34 
 35 #  禁用防火墙
 36 systemctl  stop  firewalld && systemctl  disable  firewalld
 37 
 38 #  关闭SELinux
 39 setenforce  0  &&  getenforce
 40 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
 41 
 42 #  优化SSH访问速度
 43 echo 'UseDNS no' >> /etc/ssh/sshd_config
 44 systemctl restart sshd.service
 45 echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config
 46 
 47 ####    自动化部署 DHCP 服务
 48 
 49 #  安装 DHCP 服务
 50 yum install -y dhcp
 51 
 52 #  配置 DHCP 服务
 53 echo '
 54 subnet 192.168.0.0 netmask 255.255.255.0 {
 55     range 192.168.0.100 192.168.0.200 ;
 56     next-server 192.168.0.1 ;
 57     filename "pxelinux.0" ;
 58 }
 59 ' > /etc/dhcp/dhcpd.conf
 60 
 61 #  启动 DHCP 服务,并设置开机自启动
 62 systemctl restart dhcpd
 63 systemctl enable dhcpd
 64 
 65 ####    自动化部署 TFTP 服务
 66 
 67 #  安装 TFTP 服务
 68 yum install -y tftp-server
 69 
 70 #  配置 TFTP 服务
 71 sed -i '14s/yes/no/' /etc/xinetd.d/tftp
 72 
 73 #  启动 TFTP 服务,并设置开机自启动
 74 systemctl restart tftp
 75 systemctl enable tftp
 76 
 77 ####    自动化部署 FTP 服务
 78 
 79 #  安装 FTP 服务
 80 yum install -y vsftpd
 81 
 82 #  配置 FTP 默认目录位置到 /mnt/ftp
 83 mkdir -p /mnt/ftp
 84 echo 'anon_root=/mnt/ftp' >> /etc/vsftpd/vsftpd.conf
 85 
 86 #  启动 FTP 服务,并设置开机自启动
 87 systemctl restart vsftpd
 88 systemctl enable vsftpd
 89 
 90 ####    自动化配置 PXE 核心配置部分
 91 
 92 #  安装 syslinux
 93 yum install -y syslinux
 94 
 95 #  拷贝 pxelinux.0 到 TFTP 目录下
 96 cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
 97 
 98 #  挂载 CentOS6.10 iso到 /mnt/ftp/centos/centos610,并设置开机自动挂载
 99 mkdir -p /mnt/ftp/centos/centos610
100 mount /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso /mnt/ftp/centos/centos610
101 echo 'mount /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso /mnt/ftp/centos/centos610' >> /etc/rc.d/rc.local
102 
103 #  挂载 CentOS7.7 iso到 /mnt/ftp/centos/centos77,并设置开机自动挂载
104 mkdir -p /mnt/ftp/centos/centos77
105 mount /mnt/iso/CentOS-7-x86_64-Everything-1908.iso /mnt/ftp/centos/centos77
106 echo 'mount /mnt/iso/CentOS-7-x86_64-Everything-1908.iso /mnt/ftp/centos/centos77' >> /etc/rc.d/rc.local
107 
108 #  拷贝 CentOS7.7 光盘中 isolinux 目录下所有文件到 TFTP目录
109 cp /mnt/ftp/centos/centos77/isolinux/* /var/lib/tftpboot/
110 
111 #  拷贝 CentOS6.10 光盘中引导文件到 TFTP目录,因为与默认CentOS7的重名,需要改下名字
112 cp /mnt/ftp/centos/centos610/isolinux/initrd.img /var/lib/tftpboot/initrd6.img
113 cp /mnt/ftp/centos/centos610/isolinux/vmlinuz /var/lib/tftpboot/vmlinuz6
114 
115 #  创建PXE核心配置文件
116 mkdir -p  /var/lib/tftpboot/pxelinux.cfg
117 cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
118 
119 #  设置PXE工具标题:CentOS PXE Install Tools
120 sed -i 's/menu title CentOS.*/menu title CentOS PXE Install Tools/' /var/lib/tftpboot/pxelinux.cfg/default
121 
122 #  删掉默认光盘启动菜单内容
123 sed -i '61,200d' /var/lib/tftpboot/pxelinux.cfg/default
124 
125 #  全新启动菜单:
126 #    Boot From Local Disk        默认值,从本地磁盘启动,防止误装机
127 #    Install CentOS 7.7  Automatic    自动执行 CentOS7.7 最小化安装
128 #    Install CentOS 7.7  Manual    手动安装 CentOS7.7
129 #    Install CentOS 6.10 Automatic    自动执行 CentOS6.10 最小化安装
130 #    Install CentOS 6.10 Manual    手动安装 CentOS6.10
131 echo '
132 label boot from local
133   menu label Boot From Local Disk
134   menu default
135   localboot 0xffff
136 
137 label linux
138   menu label Install CentOS 7.7  Automatic
139   kernel vmlinuz
140   append initrd=initrd.img ks=ftp://192.168.0.1/ks/centos7.cfg
141 
142 label linux
143   menu label Install CentOS 7.7  Manual
144   kernel vmlinuz
145   append initrd=initrd.img method=ftp://192.168.0.1/centos/centos77
146 
147 label linux
148   menu label Install CentOS 6.10 Automatic
149   kernel vmlinuz6
150   append initrd=initrd6.img ks=ftp://192.168.0.1/ks/centos6.cfg
151 
152 label linux
153   menu label Install CentOS 6.10 Manual
154   kernel vmlinuz6
155   append initrd=initrd6.img method=ftp://192.168.0.1/centos/centos610
156 
157 menu separator # insert an empty line
158 
159 ' >> /var/lib/tftpboot/pxelinux.cfg/default
160 
161 #  创建 Kickstart.cfg 文件ftp目录
162 mkdir -p /mnt/ftp/ks
163 
164 #  生成 CentOS7.7 Kickstart.cfg 文件
165 echo '
166 #platform=x86, AMD64, or Intel EM64T
167 #version=DEVEL
168 # Install OS instead of upgrade
169 install
170 # System keyboard
171 keyboard us
172 # System language
173 lang en_US
174 # Root password
175 rootpw --plaintext cnblogs.C0M
176 # System authorization information
177 auth  --useshadow  --passalgo=sha512
178 # Use graphical install
179 graphical
180 firstboot --disable
181 # SELinux configuration
182 selinux --disabled
183 # Firewall configuration
184 firewall --disabled
185 # Reboot after installation
186 reboot
187 # System timezone
188 timezone Asia/Shanghai
189 # System bootloader configuration
190 bootloader --location=mbr
191 # Clear the Master Boot Record
192 zerombr
193 # Partition clearing information
194 clearpart --all
195 
196 # Use network installation
197 url --url="ftp://192.168.0.1/centos/centos77"
198 # Disk partitioning information
199 part /boot --fstype="xfs" --size=1024
200 part swap --fstype="swap" --size=2048
201 part / --fstype="xfs" --grow --size=1
202 
203 %packages
204 @base
205 %end
206 ' > /mnt/ftp/ks/centos7.cfg
207 
208 #  生成 CentOS6.10 Kickstart.cfg 文件
209 echo '
210 #platform=x86, AMD64, or Intel EM64T
211 #version=DEVEL
212 # Install OS instead of upgrade
213 install
214 # System keyboard
215 keyboard us
216 # System language
217 lang en_US
218 # Root password
219 rootpw --plaintext cnblogs.C0M
220 # System authorization information
221 auth  --useshadow  --passalgo=sha512
222 # Use graphical install
223 graphical
224 firstboot --disable
225 # SELinux configuration
226 selinux --disabled
227 # Firewall configuration
228 firewall --disabled
229 # Reboot after installation
230 reboot
231 # System timezone
232 timezone Asia/Shanghai
233 # System bootloader configuration
234 bootloader --location=mbr
235 # Clear the Master Boot Record
236 zerombr
237 # Partition clearing information
238 clearpart --all
239 
240 # Use network installation
241 url --url="ftp://192.168.0.1/centos/centos610"
242 # Disk partitioning information
243 part /boot --fstype="ext4" --size=1024
244 part swap --fstype="swap" --size=2048
245 part / --fstype="ext4" --grow --size=1
246 
247 %packages
248 @base
249 %end
250 ' > /mnt/ftp/ks/centos6.cfg

猜你喜欢

转载自www.cnblogs.com/www1707/p/12581871.html
PXE