PXE+TFTP+NGINX+Kickstart
安装并测试完成,并用脚本实现自动安装
PXE
环境,脚本介绍如下:
脚本功能:
自动安装配置
PXE+TFTP+NGINX+Kickstart
环境
运行前提:
将
CentOS DVD
文件准备好,默认是从光驱挂载并复制
脚本变量:
根据实际情况修改如下变量
HTML_DIR
:
nginx
默认的
html
目录
NAME_SERVER
:
dhcpd.conf
的
domain-name-servers
的值
SUBNET
:
dhcpd.conf
的
subnet
的值
NETMASK
:
dhcpd.conf
的
netmask
的值
RANG_START
:
dhcpd.conf
的
IP
地址池开始地址
RANG_END
:
dhcpd.conf
的
IP
地址池结束地址
NEXT_SERVER
:
dhcpd.conf
的
next_server
的值
TFTPBOOT_DIR
:
tftp
默认目录
PXELINUX_DIR
:
pxelinux.cfg
目录
BOOT_SIZE
:
/boot
分区大小
SWAP_SIZE
:
swap
大小
ROOT_SIZE
:
/
分区大小,
其余空间为
/opt
分区
使用注意:
PXE
环境和生产环境要隔离,并且限制主机的访问,防止有服务器默认从网卡启动,重启后就直接重新安装系统
脚本功能演示:
演示时为了节省时间,没有将
iso
挂载到光驱,所以找不到
iso
下面的文件
运行完脚本后,环境就搭建完成,将服务器从
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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 1
2
3
4
5 #! /bin/bash
6
7 #
8
9 # FILE: pxe_kickstart_install.sh
10
11 #
12
13 # USAGE: ./pxe_kickstart_install.sh
14
15 #
16
17 # DESCRIPTION: quickly install pxe+tftp+nginx+kickstart, auto install centos 5
18
19 #
20
21 # AUTHOR: http://waydee.blog.51cto.com/#
22
23 # CREATED: 2012-4-24
24
25 # VERSION: 1.0
26
27
28
29
30
31
32
33 #################### VARIABLES ####################
34
35
36
37 ISO_MNT="/mnt/iso" #ISO_MNT is CentOS DVD mount dir
38
39 G_LEFT="\e[0;32m\033[1m" #G_LEFT and G_RIGHT to echo green strings
40
41 G_RIGHT="\e[m" #G_LEFT and G_RIGHT to echo green strings
42
43 R_LEFT="\e[0;31m\033[1m" #R_LEFT and R_RIGHT to echo red strings
44
45 R_RIGHT="\e[m" #R_LEFT and R_RIGHT to echo red strings
46
47 HTML_DIR="/usr/share/nginx/html" #HTML_DIR is nginx / dir,and copy centos dvd to this dir
48
49 NAME_SERVER="192.168.2.101" #NAME_SERVER is for dhcpd.conf domain-name-servers
50
51 SUBNET="192.168.2.0" #SUBNET is for dhcpd.conf subnet
52
53 NETMASK="255.255.255.0" #NETMASK is for dhcpd.conf netmask
54
55 RANG_START="192.168.2.220" #RANG_START is for dhcpd.conf range start
56
57 RANG_END="192.168.2.225" #RANG_END is for dhcpd.conf rang end
58
59 NEXT_SERVER="192.168.2.101" #NEXT_SERVER is for dhcpd.conf next_server
60
61 TFTPBOOT_DIR="/var/lib/tftpboot" #tftp boot dir ,default is /var/lib/tftpboot
62
63 PXELINUX_DIR="${TFTPBOOT_DIR}/pxelinux.cfg" #pxelinux.cfg dir
64
65 BOOT_SIZE="100" #set /boot size
66
67 SWAP_SIZE="4096" #set swap size
68
69 ROOT_SIZE="40960" #set / size , the rest set to /opt
70
71
72
73
74
75 #################### FUNCTIONS ####################
76
77
78
79 #check_mnt_dir use to check if ISO_MNT dir exits
80
81 check_mnt_dir()
82
83 {
84
85 if [ -e ${ISO_MNT} ]
86
87 then
88
89 echo -e "INFO: ${ISO_MNT} dir has ${G_LEFT}exits${G_RIGHT}."
90
91 else
92
93 mkdir -p ${ISO_MNT}
94
95 echo -e "INFO: Create dir ${ISO_MNT} ${G_LEFT}done${G_RIGHT}."
96
97 fi
98
99 }
100
101
102
103
104
105
106
107
108
109 #################### MAIN ####################
110
111
112
113 #install EPEL
114
115 if [ ! -e /etc/yum.repos.d/epel.repo ]
116
117 then
118
119 rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm 1>/dev/null
120
121 echo -e "Install EPEL source ${G_LEFT}done${G_RIGHT}."
122
123 fi
124
125
126
127 #install tftp-server system-config-kickstart dhcp syslinux nginx
128
129 yum -y install tftp-server system-config-kickstart dhcp syslinux nginx 1>/dev/null
130
131 echo -e "INFO: Install RPMS ${G_LEFT}done${G_RIGHT}."
132
133
134
135 #create mnt dir
136
137 check_mnt_dir
138
139
140
141 #mount DVD image to ISO_MNT
142
143 mount -t iso9660 /dev/cdrom ${ISO_MNT} 1>/dev/null
144
145 echo -e "INFO: Mount cdrom to ${ISO_MNT} ${G_LEFT}done${G_RIGHT}."
146
147
148
149 #copy file to html dir
150
151 echo -e "INFO: Start to copy CentOS image, this may take severl minitus ..."
152
153 cp -a ${ISO_MNT}/* ${HTML_DIR} 1>/dev/null
154
155 echo -e "INFO: Copy Centos DVD ${G_LEFT}done${G_RIGHT}."
156
157
158
159 #config dhcp server
160
161 cat>/etc/dhcp/dhcpd.conf<<EOF
162
163 option domain-name-servers ${NAME_SERVER};
164
165 max-lease-time 7200;
166
167 authoritative;
168
169
170
171 subnet ${SUBNET} netmask ${NETMASK} {
172
173 range ${RANG_START} ${RANG_END};
174
175 next-server ${NEXT_SERVER};
176
177 filename "pxelinux.0";
178
179 }
180
181 EOF
182
183
184
185 echo -e "INFO: Config DHCP ${G_LEFT}done${G_RIGHT}."
186
187
188
189 #start dhcp server
190
191 service dhcpd restart 1>/dev/null
192
193 sleep 2
194
195
196
197 #check if dhcpd is started
198
199 ps aux|grep dhcpd|grep -v grep 1>/dev/null
200
201 DHCP_STATUS=$?
202
203 if [[ ${DHCP_STATUS} == 1 ]]
204
205 then
206
207 echo -e "ERROR: DHCPD start ${R_LEFT}Failed${R_RIGHT}."
208
209 exit 1
210
211 else
212
213 echo -e "INFO: DHCPD start ${G_LEFT}OK${G_RIGHT}."
214
215 fi
216
217
218
219 #turn on tftp
220
221 sed -i 's/disable.*$/disable =no/' /etc/xinetd.d/tftp
222
223 echo -e "INFO: TFTP turn on ${G_LEFT}OK${G_RIGHT}."
224
225
226
227 #restart xinetd service
228
229 service xinetd restart 1>/dev/null
230
231 echo -e "INFO: Xinetd restart ${G_LEFT}OK${G_RIGHT}.."
232
233
234
235 #create pxelinux.cfg dir
236
237 if [ -e ${PXELINUX_DIR} ]
238
239 then
240
241 echo -e "INFO: ${PXELINUX_DIR} dir has ${G_LEFT}exits${G_RIGHT}."
242
243 else
244
245 mkdir -p ${PXELINUX_DIR}
246
247 echo -e "INFO: Create dir ${PXELINUX_DIR} ${G_LEFT}done${G_RIGHT}."
248
249 fi
250
251
252
253 #copy initrd.img vmlinuz pxelinux.0
254
255 cp -a ${ISO_MNT}/images/pxeboot/initrd.img ${TFTPBOOT_DIR}
256
257 echo -e "INFO: Copy initrd.img to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
258
259
260
261 cp -a ${ISO_MNT}/images/pxeboot/vmlinuz ${TFTPBOOT_DIR}
262
263 echo -e "INFO: Copy vmlinuz to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
264
265
266
267 cp -a /usr/share/syslinux/pxelinux.0 ${TFTPBOOT_DIR}
268
269 echo -e "INFO: Copy pxelinux.0 to ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
270
271
272
273
274
275 #config pxelinux.cfg file default
276
277 cat>${PXELINUX_DIR}/default<<EOF
278
279 default ks
280
281 prompt 1
282
283 timeout 600
284
285 display boot.msg
286
287 F1 boot.msg
288
289 F2 options.msg
290
291 F3 general.msg
292
293 F4 param.msg
294
295 F5 rescue.msg
296
297 label linux
298
299 kernel vmlinuz
300
301 append initrd=initrd.img
302
303 label text
304
305 kernel vmlinuz
306
307 append initrd=initrd.img text
308
309 label ks
310
311 menu default
312
313 kernel vmlinuz
314
315 append initrd=initrd.img ksdevice=eth0 ks=http://${NAME_SERVER}/ks.cfg
316
317 label local
318
319 localboot 1
320
321 label memtest86
322
323 kernel memtest
324
325 append -
326
327 EOF
328
329 echo -e "INFO: pxelinux.cfg/default ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
330
331
332
333 #config ks.cf file
334
335 cat>${HTML_DIR}/ks.cfg<<EOF
336
337 #platform=x86, AMD64, or Intel EM64T
338
339 #version=DEVEL
340
341 # Firewall configuration
342
343 firewall --enabled --http --ssh
344
345 # Install OS instead of upgrade
346
347 install
348
349 # Use hard drive installation media
350
351 #harddrive --dir=/ --partition=/dev/sdb1
352
353 url --url=http://${NAME_SERVER}/
354
355 # Root password
356
357 rootpw --iscrypted $1$kBe14BGG$GEWFllYBwDpMn055nK7Jk0
358
359 # System authorization information
360
361 auth --useshadow --passalgo=sha512
362
363 # Use text mode install
364
365 text
366
367 # System keyboard
368
369 keyboard us
370
371 # System language
372
373 lang en_US
374
375 # SELinux configuration
376
377 selinux --disabled
378
379 # Do not configure the X Window System
380
381 skipx
382
383 # Installation logging level
384
385 logging --level=info
386
387 # Reboot after installation
388
389 reboot
390
391 # System timezone
392
393 timezone Asia/Shanghai
394
395 # Network information
396
397 network --bootproto=dhcp --device=eth0 --onboot=on
398
399 # System bootloader configuration
400
401 bootloader --location=mbr
402
403 # Partition clearing information
404
405 clearpart --all
406
407 # Disk partitioning information
408
409 part /boot --asprimary --fstype="ext3" --size=${BOOT_SIZE}
410
411 part swap --fstype="swap" --size=${SWAP_SIZE}
412
413 part / --fstype="ext3" --size=${ROOT_SIZE}
414
415 part /opt --fstype="ext3" --grow --size=1
416
417
418
419 %packages
420
421 @base
422
423 @core
424
425 imake
426
427 keyutils
428
429 trousers
430
431 fipscheck
432
433 device-mapper-multipath
434
435 @development-libs
436
437 @admin-tools
438
439 @system-tools
440
441 @chinese-support
442
443 @text-internet
444
445 EOF
446
447 echo -e "INFO: ks.cfg config ${TFTPBOOT_DIR} ${G_LEFT}done${G_RIGHT}."
448
449
450
451 #restart nginx
452
453 service nginx restart 2>&1 >/dev/null
454
455 sleep 2
456
457
458
459 ps aux|grep nginx|grep -v grep 1>/dev/null
460
461 NGINX_STATUS=$?
462
463 if [[ ${NGINX_STATUS} == 1 ]]
464
465 then
466
467 echo -e "ERROR: nginx start ${R_LEFT}Failed${R_RIGHT}."
468
469 exit 1
470
471 else
472
473 echo -e "INFO: nginx start ${G_LEFT}OK${G_RIGHT}."
474
475 fi
476
477
478
479 #pxe_kickstart_install done
480
481 echo -e "INFO: PXE+TFTP+NGINX+Kickstart has been installed ${G_LEFT}SUCCESS${G_RIGHT}."
482
483
484
Kickstart创建LVM命令
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 1
2
3
4
5 # Disk partitioning information
6
7
8
9 part /boot --fstype="ext3" --size=100
10
11
12
13 part pv.01 --size=1 --grow
14
15
16
17 volgroup vg_root pv.01
18
19
20
21 logvol / --vgname=vg_root --size=30720 --name=lv_root
22
23
24
25 logvol swap --vgname=vg_root --size=32768 --name=lv_swap
26
27
28
29 logvol /opt --vgname=vg_root --size=1 --grow --name=lv_opt
30
31
32