[TOC]

PXE批量装机环境搭建

PXE结构介绍

nginx服务 /etc/nginx/conf.d/default.conf
dhcp服务 /etc/dhcp/dhcpd.conf
tftp服务 /etc/xinetd.d/tftp
xinetd服务
grub启动文件 /var/lib/tftpboot/grub.cfg
iso镜像 /data/kylin/
ks文件 /data/ks/
内核文件:vmlinuz /data/kylin/images/pxeboot/vmlinuz
内核文件:initrd.img /data/kylin/images/pxeboot/initrd.img

UEFI与BIOS的区别:

/etc/dhcp/dhcpd.conf

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
option space PXE;
option PXE.mtftp-ip code 1 = ip-address;
option PXE.mtftp-cport code 2 = unsigned integer 16;
option PXE.mtftp-sport code 3 = unsigned integer 16;
option PXE.mtftp-tmout code 4 = unsigned integer 8;
option PXE.mtftp-delay code 5 = unsigned integer 8;
option arch code 93 = unsigned integer 16;
subnet 172.16.0.0 netmask 255.255.0.0 {
range 172.16.1.10 172.16.8.240; #分配的地址池范围
option routers 172.16.8.1; #网关地址
default-lease-time 600; #默认租约时间,单位为秒
max-lease-time 7200; #设置最大租约时间,单位为秒
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
next-server 172.16.8.193; #tftp地址
# match 只针对 centos
# 07 08 09为x86_64 UEFI;0b为ARM UEFI
if option arch = 00:07 {
filename "bootx64.efi"; #网卡引导文件,二进制文件 UEFI
} else if option arch = 00:08 {
filename "bootx64.efi";
} else if option arch = 00:09 {
filename "bootx64.efi";
} else if option arch = 00:0b {
filename "bootx64.efi";
} else {
filename "pxelinux.0";#网卡引导文件,二进制文件 legacy
}
}
}

/etc/nginx/conf.d/default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name localhost;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
charset utf-8;
location /ctyun{
alias /data/kylin;
}
location /ks {
alias /data/ks;
}
}

/var/lib/tftpboot/grub.cfg

1
2
3
4
5
6
set timeout=5
search --no-floppy --set=root -l 'Kylin-Desktop-V10-SP1-2303-update2-Release-20231023-X86_64'
menuentry 'Install kylin-v10-sp1' --class fedora --class gnu-linux --class gnu --class os {
linuxefi (tftp)/vmlinuz inst.repo=http://172.16.8.193/kylin ks=http://172.16.8.193/ks/ks.cfg ip=dhcp
initrdefi (tftp)/initrd.img
}

/etc/xinetd.d/tftp

1
sed  -i  's/.*disable.*/disable \ =\ no/'   /etc/xinetd.d/tftp

PXE手动部署教程

PXE自动部署脚本

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
#!/bin/bash

#------------------------------------------
# 2023/5/25
# pex安装ctyun系统环境脚本,使用阿里yum源
# 本机地址为172.16.8.193
# dhcp网络分配的地址为:172.16.1.10-172.16.8.240
#
#优化ip检测
#优化新窗口显示状态
#
#------------------------------------------

add_motd(){
cat > /etc/motd << EOF
------------------------------------------------------------------
$(tput setaf 3)
nginx config is /etc/nginx/conf.d/default
tftp config is /etc/xinetd.d/tftp
dhcp config is /etc/dhcp/dhcpd.conf
core config is /var/lib/tftpboot/grub.cfg (uefi)
iso mount of /data/ctyun/
ks.cfg is /data/ks/
$(tput sgr0)
------------------------------------------------------------------

EOF
}


ip_config(){

interfaces=( $(ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo$') )

# 判断当前服务器是否在 172.16.0.0/12 网段
if ! ip addr show | grep -q '172\.16'; then
# 找到第一个处于活动状态的网口
for iface in "${interfaces[@]}"; do
if ip link show "$iface" | grep -q 'state UP'; then
cat >/etc/sysconfig/network-scripts/ifcfg-"$iface"<<EOF
DEVICE=$iface
BOOTPROTO=static
IPADDR=172.16.8.193
PREFIX=16
IPV6INIT=no
ONBOOT=yes
VLAN=yes
MTU=1500
EOF
break
fi
done
else
echo "当前ip处于172.16.0.0/16 网段内"
fi



}



check_root() {
if [ $(id -u) -ne "0" ]; then
echo "\e[0;31mPlease use root!!!!\e[0m"
exit 1
fi
}

selinux_firewall() {
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
sed -n "/^SELINUX=/p" /etc/selinux/config
systemctl stop firewalld && systemctl disable firewalld
}

yum_install() {
yum install syslinux.x86_64 dhcp* xinetd tftp-server nginx tar -y >/dev/null
yum install epel* -y >/dev/null
if [ $? != 0 ]; then
yum clean all >/dev/null
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo > /dev/null
sleep 1s
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo >/dev/null
sleep 1s
yum install epel* -y >/dev/null
yum install syslinux.x86_64 dhcp* xinetd tftp-server nginx tar -y >/dev/null
if [ $? -nq 0 ]; then
echo yum配置失败
exit 1
fi
fi
}


pxe_dhcpd_cofig() {
cat > /etc/dhcp/dhcpd.conf <<EOF
option space PXE;
option PXE.mtftp-ip code 1 = ip-address;
option PXE.mtftp-cport code 2 = unsigned integer 16;
option PXE.mtftp-sport code 3 = unsigned integer 16;
option PXE.mtftp-tmout code 4 = unsigned integer 8;
option PXE.mtftp-delay code 5 = unsigned integer 8;
option arch code 93 = unsigned integer 16;
subnet 172.16.0.0 netmask 255.255.0.0 {
range 172.16.1.10 172.16.8.240; #分配的地址池范围
option routers 172.16.8.1; #网关地址
default-lease-time 600; #默认租约时间,单位为秒
max-lease-time 7200; #设置最大租约时间,单位为秒
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
next-server 172.16.8.193; #tftp地址
# match 只针对 centos
# 07 08 09为x86_64 UEFI;0b为ARM UEFI
if option arch = 00:07 {
filename "bootx64.efi"; #网卡引导文件,二进制文件 UEFI
} else if option arch = 00:08 {
filename "bootx64.efi";
} else if option arch = 00:09 {
filename "bootx64.efi";
} else if option arch = 00:0b {
filename "bootx64.efi";
} else {
filename "pxelinux.0";#网卡引导文件,二进制文件 legacy
}
}
}
EOF
}

tftp_config() {
sed -i 's/.*disable.*/disable \ =\ no/' /etc/xinetd.d/tftp
}

nginx_config() {
mkdir /data/ctyun /data/ks -p
cat > /etc/nginx/conf.d/default.conf <<EOF
server {
listen 80;
server_name localhost;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
charset utf-8;
location /ctyun{
alias /data/ctyun;
}
location /ks {
alias /data/ks;
}
}
EOF
}

mount_ctyun() {
findiso=`find / -name ctyun*.iso`
mount $findiso /data/ctyun/

}

grub_config() {
tftptar=`find / -name ctyun-ch.tar`
tar xf $tftptar
cd ctyun-ch
cp ipip.txt /data/ks/
cp ks0062.cfg /data/ks/
cp bootx64.efi /var/lib/tftpboot/bootx64.efi
cp /data/ctyun/isolinux/initrd.img /var/lib/tftpboot/
cp /data/ctyun/isolinux/vmlinuz /var/lib/tftpboot/
cat > /var/lib/tftpboot/grub.cfg <<EOF
set timeout=5
search --no-floppy --set=root -l 'ctyunos-2.0.1-220311-x86_64'
menuentry 'Install ctyunos kernel-0062' --class fedora --class gnu-linux --class gnu --class os {
linuxefi (tftp)/vmlinuz inst.repo=http://172.16.8.193/ctyun ks=http://172.16.8.193/ks/ks0062.cfg ip=dhcp
initrdefi (tftp)/initrd.img
}
menuentry 'Install ctyunos kernel-0064' --class fedora --class gnu-linux --class gnu --class os {
linuxefi (tftp)/vmlinuz inst.repo=http://172.16.8.193/ctyun ks=http://172.16.8.193/ks/ks0064.cfg ip=dhcp
initrdefi (tftp)/initrd.img
}
EOF
}

sys_ser(){
systemctl enable dhcpd xinetd tftp nginx --now
}

check_ser() {

cat >/etc/profile.d/system-info.sh << EOF
#/bin/bash

check_service() {
service=\$1

if systemctl is-active --quiet \$service; then
replacement="$(tput setaf 2)OK$(tput sgr0)"
else
replacement="$(tput setaf 1)Failed$(tput sgr0)"
fi

# sed -i "s/^\$service:.*/\$service:\$replacement/" /etc/motd
echo -e "\$service:\$replacement"
}

check_service xinetd
check_service dhcpd
check_service tftp
check_service nginx
if ! df -h | grep -q /data ;then
mount_stat="$(tput setaf 1)Failed$(tput sgr0)"
else
mount_stat="$(tput setaf 2)OK$(tput sgr0)"
fi


if [ ! -s /var/lib/tftpboot/ ] ;then
tftpconf="$(tput setaf 1)Failed$(tput sgr0)"
else
tftpconf="$(tput setaf 2)OK$(tput sgr0)"
fi

#sed -i "s/^mount:.*/mount:\$mount_stat/" /etc/motd
#sed -i "s/^tftpboot:.*/tftpboot:\$tftpconf/" /etc/motd
echo -e "mount:\$mount_stat"
echo -e "tftpboot:\$tftpconf"

EOF
}

add_motd
check_root
selinux_firewall
yum_install
pxe_dhcpd_cofig
tftp_config
nginx_config
mount_ctyun
grub_config #启动项
sys_ser #服务启动
check_ser #服务检测
ip_config

# ipip.txt
# ks0062.cfg

# sh -x

PXE错误总结

更新于

请我喝[茶]~( ̄▽ ̄)~*

Chen 微信支付

微信支付

Chen 支付宝

支付宝

Chen 贝宝

贝宝