1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
vim install_PXE.sh
#!/bin/bash
#
#********************************************************************
#Author: Mr.yang
#QQ: 1419946323
#Date: 2021-07-12
#FileName: auto_install_centos678_PXE.sh
#URL: www.yhtzjy.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************

ReleaseNo=`cat /etc/redhat-release 'sed -rn 's/.* ([[:digit:]]+)\..*/\1/p'`
PXEinstall(){
setenforce 0
systemctl stop firewalld
rpm -q net-tools ''yum -y install net-tools &> /dev/null
ifconfig 'grep eth0
if [ "$?" -ne "0" ];then
echo "请将网卡更名为eth0;才能继续运行本脚本;更改过程中会强制重启;您是否还要继续执行?"
read -p " 请按y继续运行: " ipsure
[ "${ipsure}" == "y" ] && sed -ir '/GRUB_CMDLINE_LINUX=/s/"$/ net.ifnames=0"/p' /etc/default/grub && grub2-mkconfig -o /etc/grub2.cfg &>/dev/null
read -p "请输入eth0的IP: " ip
read -p "请输入要设置的子网掩码(prefix)如 24: " prefix
read -p "请输入要设置的网关: " gateway
cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE="eth0"
BOOTPROTO="static"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR=$ip
PREFIX=$prefix
GATEWAY=$gateway
DNS1=114.114.114.114
EOF
echo "请重启系统;然后重新执行本脚本"
echo "重启后如果无法联网 请执行service network restart 或者 nmcli con reload; nmcli con up eth0"
reboot
fi
ipeth0=`ifconfig eth0'sed -rn '/inet\>/s/[^0-9]+([0-9.]+).*/\1/p'`
neteth0=`ifconfig eth0'sed -rn '/inet\>/s/[^0-9]+([0-9.]+).*/\1/p''cut -d. -f1-3`
route=`ip route'grep default'grep eth0'sed -rn 's/[^0-9]+([0-9.]+).*/\1/p'`
route6=`ip route'grep default'sed -rn 's/[^0-9]+([0-9.]+).*/\1/p'`
srinfo6=`lsblk'awk '/sr/{print $1}'`
srinfo=`lsblk'egrep -o 'sr[^ ]*'`
for scan in /sys/class/scsi_host/host*/scan;do echo "- - -" >$scan;done
if [ "$ReleaseNo" -eq "8" ];then
rpm -q dhcp-server tftp-server httpd syslinux-nonlinux'' dnf -y install dhcp-server tftp-server httpd syslinux-nonlinux
cat >/etc/dhcp/dhcpd.conf <<-EOF
option domain-name "example.org";
option domain-name-servers 180.76.76.76,114.114.114.114;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet ${neteth0}.0 netmask 255.255.255.0 {
range ${neteth0}.100 ${neteth0}.200;
option routers ${route};
next-server ${ipeth0};
filename "pxelinux.0";
}
EOF
systemctl enable --now httpd tftp dhcpd
fi
if [ "$ReleaseNo" -eq "7" ];then
rpm -q httpd tftp-server dhcp syslinux''yum -y install httpd tftp-server dhcp syslinux
cat >/etc/dhcp/dhcpd.conf <<-EOF
option domain-name "example.org";
option domain-name-servers 180.76.76.76,114.114.114.114;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet ${neteth0}.0 netmask 255.255.255.0 {
range ${neteth0}.100 ${neteth0}.200;
option routers ${route};
next-server ${ipeth0};
filename "pxelinux.0";
}
EOF
systemctl enable --now httpd tftp dhcpd
fi
if [ "$ReleaseNo" -eq "6" ];then
rpm -q httpd tftp-server dhcp syslinux''yum -y install httpd tftp-server dhcp syslinux
service httpd start
sed -i '/disable/s/yes/no/' /etc/xinetd.d/tftp
service xinetd restart
cat >/etc/dhcp/dhcpd.conf <<EOF
option domain-name "example.org";
option domain-name-servers 180.76.76.76,114.114.114.114;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet ${neteth0}.0 netmask 255.255.255.0 {
range ${neteth0}.100 ${neteth0}.200;
option routers ${route6};
next-server ${ipeth0};
filename "pxelinux.0";
}
EOF
service dhcpd start
fi
mkdir /var/www/html/centos/{6,7,8}/isos/x86_64/ -pv &>/dev/null
if [ "$ReleaseNo" -eq "7" -o "$ReleaseNo" -eq "8" ];then
srinfo6=${srinfo}
fi
for sr in ${srinfo6};do
mkdir /mnt/${sr} -p &>/dev/null
mount /dev/${sr} /mnt/${sr}
if [ -d /mnt/${sr}/AppStream ];then
mount /dev/${sr} /var/www/html/centos/8/isos/x86_64/ &>/dev/null
mkdir /var/www/html/ksdir -p &>/dev/null
mkdir /var/lib/tftpboot/8 -p &> /dev/null
\cp -a /mnt/${sr}/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/8/
\cp /mnt/${sr}/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/
cat >/var/www/html/ksdir/ks8.cfg <<-EOF
#version=RHEL8
ignoredisk --only-use=sda
zerombr
text
reboot
# Partition clearing information
clearpart --all --initlabel
selinux --disabled
firewall --disabled

# Use graphical install
url --url=http://${ipeth0}/centos/8/isos/x86_64/
#repo --name="AppStream" --baseurl=file:///run/install/repo/AppStream
# Use CDROM installation media
#cdrom
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network --bootproto=dhcp --device=ens160 --onboot=yes --ipv6=auto --no-activate
network --hostname=localhost.localdomain
# Root password
rootpw --iscrypted \$1\$jb2QI2x8\$wWAOeOxXerdfYXyVwV0890
# Run the Setup Agent on first boot
firstboot --enable
# Do not configure the X Window System
skipx
# System services
#services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# Disk partitioning information
part / --fstype="xfs" --ondisk=sda --size=102400
part /data --fstype="xfs" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="ext4" --ondisk=sda --size=1024
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end

%packages
@^minimal-environment
kexec-tools
%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
EOF
fi
if [ -e /mnt/${sr}/RPM-GPG-KEY-CentOS-6 ];then
mount /dev/${sr} /var/www/html/centos/6/isos/x86_64/ &>/dev/null
mkdir /var/www/html/ksdir -p &>/dev/null
mkdir /var/lib/tftpboot/6 -p &>/dev/null
\cp -a /mnt/${sr}/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/6/ &>/dev/null
cat >/var/www/html/ksdir/ks6.cfg <<EOF
#ckstart file automatically generated by anaconda.
#version=DEVEL
install
url --url=http://${ipeth0}/centos/6/isos/x86_64/
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw --iscrypted \$1\$jb2QI2x8\$wWAOeOxXerdfYXyVwV0890
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clea:r all partitions first, this is
# not guaranteed to work
clearpart --all
zerombr
reboot
text
part /boot --fstype=ext4 --size=1024
part / --fstype=ext4 --size=100000
part /data --fstype=ext4 --size=50000
part swap --size=2048

%packages
@core
autofs
%end
EOF
fi
if [ -e /mnt/${sr}/RPM-GPG-KEY-CentOS-7 ];then
mount /dev/${sr} /var/www/html/centos/7/isos/x86_64/ &>/dev/null
mkdir /var/www/html/ksdir -p &>/dev/null
mkdir /var/lib/tftpboot/7 -p &>/dev/null
\cp -a /mnt/${sr}/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/7/
cat >/var/www/html/ksdir/ks7.cfg <<EOF
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# X Window System configuration information
xconfig --startxonboot
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts='us'
# Root password
rootpw --iscrypted \$1\$jb2QI2x8\$wWAOeOxXerdfYXyVwV0890
# Use network installation
url --url="http://${ipeth0}/centos/7/isos/x86_64"
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use text mode install
text
# Run the Setup Agent on first boot
firstboot --enable
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# System services
#services --disabled="chronyd"
ignoredisk --only-use=sda
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --nontp
# System bootloader configuration
bootloader --append="crashkernel=auto" --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=2048
part / --fstype="xfs" --ondisk=sda --size=100000
part /boot --fstype="xfs" --ondisk=sda --size=1024
part /data --fstype="xfs" --ondisk=sda --size=50000

%packages
@core
%end

EOF
fi
done
\cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg/ -p
cat >/var/lib/tftpboot/pxelinux.cfg/default <<EOF
default menu.c32
timeout 600

menu title CentOS Linux

label linux8
menu label Auto Install CentOS Linux ^8.0 Mini
kernel 8/vmlinuz
append initrd=8/initrd.img ks=http://${ipeth0}/ksdir/ks8.cfg

label linux7
menu label Auto Install CentOS Linux ^7 Mini
kernel 7/vmlinuz
append initrd=7/initrd.img ks=http://${ipeth0}/ksdir/ks7.cfg

label linux6
menu label Auto Install CentOS Linux ^6 Mini
kernel 6/vmlinuz
append initrd=6/initrd.img ks=http://${ipeth0}/ksdir/ks6.cfg

label local
menu default
menu label Boot from ^local drive
localboot 0xffff
EOF
}

echo "**********************************************************************************************"
echo "***********************************请确保在nat网卡模式下运行***********************************"
echo "**********************************************************************************************"
echo "1..这是一个pxe 自动化安装的脚本;兼容centos6、7、8系统,需要将网卡同一设置为eth0;会自动更改网卡eth0"
echo "2..需要你至少挂载一个光盘;支持cenos6、7、8的光盘"
echo "3..新建虚拟机建议20G 2G内存以上 不然容易报错;"
echo "4..如果运行中有错误请与我联系!谢谢"
echo "5..如果网卡更改后重启了,务必确认是否能ping百度或查看是否有默认网关"
echo "6..如果执行本脚本后发现没有挂载光盘,请执行 yum remove dhcp'dhcp-server httpd tftp-server 清理环境"
echo "7..运行脚本需要在当前shell运行,不要开启子shell,否则无法使用自动更改网卡名功能"
echo "**************************************************************************************"
echo "**************************************************************************************"
echo "虚拟机默认密码为123"
j=5
for i in `seq 5`;do
echo "${j}s后自动开始运行"
let j--
sleep 1
done
PXEinstall