|
|
示例拓扑如下,DHCP、TFTP、HTTP、PXE Server在1台主机上部署,KVM宿主机与NFS server分别在另2台主机上部署。9 ~2 O. v# Q% O# s0 j1 W
( ^# ~3 b8 p+ E9 q: G4 p. I+ x KVM虚机PXE方式自动安装示例-2629585-17 q/ c; x/ i/ l4 R8 U, D) c
. w( U u# [9 \* f1 x; Z/ ]( d
确认主机CPU支持并打开了虚拟化支持(Intel/AMD),如果是多核CPU会有多个核的信息:: k/ T& u* v1 u; S6 K
[root-KVM-1 ~]# egrep '(vmx|svm)' /proc/cpuinfo 1 c6 I- T1 [* }' _1 c0 }8 h
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts" p {: f, ]$ v* Y, h! }6 c- \
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts. ~/ V$ J9 [. U- }6 k) Y0 ?
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts
: x7 F* Y' V6 j: R$ V) R. V; x) l) Gflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts
# B5 L% e% j) ^7 y4 J' H7 M: hflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts1 M6 B* Q* l8 L. {; d
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts8 |+ M# q+ P$ d4 _& Z# \& G, `1 ]
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts( e6 U- x; z3 f$ k) Z' F
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust smep dtherm arat pln pts
; M: K- ^' C. k q, w- y( {9 g, \6 Q7 [1 i
确认主机加载了KVM内核模块并是在用状态:
$ V$ m; Q! e, N[root-KVM-1 ~]# lsmod | grep kvm
* w- x# d; S8 G9 E* tkvm_intel 170086 8 / T, `7 V! ]) x2 ?
kvm 566340 1 kvm_intel+ W5 V( W4 Y% Z* d
irqbypass 13503 1 kvm: }% ^* D5 ?6 [+ c* t
+ C) G/ |, q# |
在KVM上创建2个虚机,本例中创建1个,另一个直接clone即可,虚机的网卡与系统网桥br0桥接:
: G' y) o$ i& z5 w& ~
5 W I0 g4 e' `. T* Q+ F1 W K[root-KVM-1 ~]# virsh list
5 @% a( w2 I8 V( N Id Name State$ E, u2 y. r% H: c
----------------------------------------------------
! b4 X$ h; Y4 D* ]) c8 b 7 centos7 running
. ]7 K$ |% Z# X8 K 8 centos7-clone running; s) |# E1 a0 Y6 u
[root-KVM-1 ~]# virsh domiflist centos7
$ M' h; s( u! S/ d' G( D* z' i1 EInterface Type Source Model MAC
% J0 \: M; O$ \/ \2 g" |8 ^-------------------------------------------------------
, Z4 a: [; U$ d# {4 d- M. vvnet0 bridge br0 e1000 52:54:00:1c:c8:92) n8 n; m \. j2 l/ w1 O
[root-KVM-1 ~]# virsh domiflist centos7-clone
3 g) Z( @9 {( w, w2 W x4 }Interface Type Source Model MAC9 P' U6 c# v3 C" P2 F2 E: A
-------------------------------------------------------$ k' M5 d3 ~- B7 G0 K" [4 x
vnet1 bridge br0 e1000 52:54:00:ee:71:759 v4 j6 Q" a5 r3 h
5 }: |6 C" t7 F+ u在dhcp server 的配置文件中主要包含下列内容:5 c% J) U! c5 a& p& d
[root-PXE-server ~]# cat /etc/dhcp/dhcpd.conf ; T" S7 p4 f, B6 {# d7 ]
#
0 R# R1 E1 Q. c% o% Y# DHCP Server Configuration file., G0 b- ~; O) } J( K
# see /usr/share/doc/dhcp*/dhcpd.conf.example2 `, I0 U% |" L
# see dhcpd.conf(5) man page& J+ Z* \( X5 r
#* g O; A% d$ h8 @
#ddns-uptate-style interim;
- m( O% O) l7 F#ignore client-updates;6 W! J$ n' K- Z' @- D) n W% i
filename "pxelinux.0"; //pxe的引导镜像文件
5 h) G- Z# F5 I2 \next-server 192.168.56.2;
7 C' U2 O8 s8 W" V" ^# o+ r6 \subnet 192.168.56.0 netmask 255.255.255.0 {
% A$ K2 r% i# d0 R/ aoption routers 192.168.56.2;
. C4 ~/ J K- F3 ?) Voption subnet-mask 255.255.255.0;4 Y* R5 B0 y. a; u
range dynamic-bootp 192.168.56.150 192.168.56.200;
* X+ o; j8 ~8 C8 Gdefault-lease-time 21600;: ]. c" d# P" j; b
}0 f! j7 ?; K9 ]; m7 g+ d
需要将pxelinux.0镜像文件放到tftp server的目录中,该文件可以从系统光盘镜像或者系统中获得,同时还有内核文件、引导文件:
) d; M/ J& j' T# @! J: q[root-PXE-server ~]# ls /var/lib/tftpboot/: e. ^: Q. {/ f1 t1 o
boot.msg initrd.img pxelinux.0 pxelinux.cfg vesamenu.c32 vmlinuz
L! w' Q+ d( u1 F 将CentOS7光盘复制到/var/www/html的mnt目录中:
5 T& e p& X; m$ O[root-PXE-server html]# ls mnt; m% v4 f, \ [% D9 I8 x7 }$ e
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL
, V' N: `4 \2 l# |1 A 需要配置pxelinux.cfg目录下的default文件,已指示系统通过pxe加载镜像后进一步的动作,这里我们关注的是CentOS7标签,通过http方式结合kickstart自应答文件进行网络自动安装:
$ J5 u6 Q, q) K; u M# g8 `9 H' H( \- h" [8 W6 B
[root-PXE-server pxelinux.cfg]# cat default 4 w8 W, Z/ `! X* G" P
default CentOS7 $ S; T8 F: {9 W5 M L2 [- o6 ~% I1 ~
#default vesamenu.c32
- p+ g/ A8 P2 e2 `% f& @prompt 0 V# R) i4 I6 E+ V- [% j5 P7 G
timeout 300/ O% v! ?, A4 C% n, [
ontimeout local7 m" o' N9 H8 _$ k) R
display boot.msg
% }/ X7 R7 Z" O4 JF1 boot.msg5 O" \/ r. e1 T: i0 p7 ]+ C
F2 options.msg& k; Q- c% l1 b
F3 general.msg" i: S$ T! \ t& I, @, q% G( N
F4 param.msg- X ?5 [8 g# O# I- q3 V6 c
F5 rescue.msg4 B' `& k" a, V; j
# Clear the screen when exiting the menu, instead of leaving the menu displayed.% W8 t1 L! n3 q4 ~$ c
# For vesamenu, this means the graphical background is still displayed without \3 b# M4 H$ B6 Y9 P+ c) d
# the menu itself for as long as the screen remains in graphics mode.
5 x' ^. L( @) z: L- _' Omenu clear, y' M8 k/ {2 r2 B* T+ i! D/ H9 M0 H
menu background splash.png* D: C/ g* b7 C7 i+ F
menu title CentOS 7
0 g, X& M- e& xmenu vshift 83 @+ ~) d0 y" k- N) T/ C& O! |
menu rows 18; H3 @ F) I# A- ^
menu margin 8
: A W# C+ X: C" N" `% l6 U! x+ G" bmenu hidden
9 M4 C3 ^/ H6 {3 v& F" K+ Vmenu helpmsgrow 15
9 s# m: l0 |1 H& c: g" U8 {menu tabmsgrow 13( l2 s( F; N* l6 s" z2 w
# Border Area$ S4 Y* |! |8 R9 h/ V8 G2 A5 j% P' P
menu color border * #00000000 #00000000 none
! \. N2 \! E, O$ w# Selected item- ^. S2 U) P: S i g) z
menu color sel 0 #ffffffff #00000000 none
$ L3 u' i- y- _) W/ \4 y% Z# Title bar1 D2 p4 s9 y% U7 e' I
menu color title 0 #ff7ba3d0 #00000000 none
. m6 R6 a! P$ d3 l( i# Press [Tab] message
* O5 O5 |; p+ q) j4 q6 r$ z) ]. Emenu color tabmsg 0 #ff3a6496 #00000000 none6 ]1 \/ T" F. ]7 w
# Unselected menu item
" o! w1 C' Z- Gmenu color unsel 0 #84b8ffff #00000000 none7 r4 K! ?3 J4 \- C+ X7 G6 e2 r
# Selected hotkey5 Y: V, g. g* H* q' n
menu color hotsel 0 #84b8ffff #00000000 none7 V1 C! D8 P! ]# f L
# Unselected hotkey
' L0 k0 P$ j& h5 _9 V" omenu color hotkey 0 #ffffffff #00000000 none+ e8 E0 _/ L! c% h
# Help text
0 V g; w' w+ M( H3 L1 Z0 Lmenu color help 0 #ffffffff #00000000 none
3 C! B3 h& `* R7 N* e# A scrollbar of some type? Not sure./ @* } t4 H" e$ C x, e+ |) F/ I
menu color scrollbar 0 #ffffffff #ff355594 none: I7 x* R# ~) @2 E, ^2 R
# Timeout msg
7 b) ] ^( c Z% W0 T) hmenu color timeout 0 #ffffffff #00000000 none
# A* T2 C, y1 i( Bmenu color timeout_msg 0 #ffffffff #00000000 none# V. ?" E3 {$ t& p8 h: r# l
# Command prompt text* k% {, x' W Z2 U: A! M
menu color cmdmark 0 #84b8ffff #00000000 none
; Q. P' z6 ]* {" p9 Hmenu color cmdline 0 #ffffffff #00000000 none
5 Z5 o9 k; e) P# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.. I. a4 G$ |/ w. t' j0 a
menu tabmsg Press Tab for full configuration options on menu items.# N' L2 h/ [- Z4 g* v
menu separator # insert an empty line0 j- j8 P1 w1 S9 x/ H# _) l/ D
menu separator # insert an empty line; z, k- W$ N+ n+ {
label CentOS7: A) v& `- _2 b, P2 `0 G
kernel vmlinuz
0 \. x w; V7 {) u0 x+ X append initrd=initrd.img ks=http://192.168.56.2/ks.cfg vga=normal devfs=nomount ramdisk_size=9216 nofb . U' e( x5 G l. `& n3 i( F1 d0 q% R
KS自应答文件可以通过system-config-kickstart工具生成,也可以复制系统中的anaconda目录中的样例,编辑生成:
?9 I" f2 k. z4 T KVM虚机PXE方式自动安装示例-2629585-2
" B w! c( Z5 Y2 K, q: Q+ w: n# ]! a% |6 |. y% j2 F
本例中的ks应答文件主要配置了http网络安装的路径,以及root密码、硬盘分区、以及安装的基本软件包。
6 L% _- m5 j+ V3 }; r& p7 f o[root-PXE-server pxelinux.cfg]# cat /var/www/html/ks.cfg
, [$ c$ R2 {4 J#platform=x86, AMD64, or Intel EM64T
- ~7 ~. u) ^$ j4 E9 Y. ~4 T( [ l: d#version=DEVEL4 g# r1 ~' Y, q: {' s
# Install OS instead of upgrade
" G: D( u, O! F+ Oinstall
% `0 m: k3 V) e1 \# Keyboard layouts
1 n+ c E+ W0 X. _keyboard 'us'9 k! t- T5 a+ \! x' I1 y
# Root password
v* ]) c( d, z6 i1 @) Vrootpw --iscrypted $1$2g8RzvvY$YqlnUGED1Js5RAjnchHKK1
4 Q/ j* T- }: h( |" e o# Use network installation
6 G7 w( K ^& `; ?- Kurl --url="http://192.168.56.2/mnt/"
6 C8 V0 G$ p. u# System language
7 I6 ^4 O A z' J& flang en_US
: f; [$ P \9 p4 y) d, M6 ^1 Y d& @% l# Firewall configuration h( ?! n" T) R
firewall --disabled
8 V5 w, o2 S$ i# System authorization information
' Z' r8 {, `& n0 E) J, hauth --useshadow --passalgo=sha512
1 V/ |( c2 ~* M3 ]) r5 r# Use graphical install# b3 S5 }% o9 t# {! L$ g
graphical# G* p4 s2 L+ Z0 {& L [: N
firstboot --disable+ a( e& N. y( N" r9 Q
# SELinux configuration
1 m7 `* @& s8 P2 |; \selinux --disabled
' e7 r8 ]3 t4 R, N7 s9 x/ z# Network information
& f0 c. L Z2 K7 _3 }+ T, R5 Bnetwork --bootproto=dhcp --device=eth0) {) \+ c; L2 C9 ]8 X
# Reboot after installation. ^' V, f# V8 y+ m" u! U( X& H
reboot9 m- M) {' \6 ~8 E6 X9 r# y
# System timezone
2 U$ q# [& T" [timezone Africa/Abidjan
! Z) I9 D4 b) T# H# System bootloader configuration' Q. H/ B$ W2 ~$ B! o
bootloader --location=mbr 9 ?! y! c1 a4 G# n2 F1 E
# Clear the Master Boot Record$ Z) r- U$ M+ ~5 k) |
zerombr
1 A; l- K! \7 O5 [# Partition clearing information
( N, D# k4 S1 qclearpart --all --initlabel9 O s2 ~$ d$ k; k$ ~
# Disk partitioning information
3 m) J9 n: D0 ^4 A( Rpart /boot --asprimary --fstype="ext3" --ondisk=sda --size=200
+ q+ ~" r% U1 T# a, p5 Q' ]part swap --fstype="swap" --ondisk=sda --size=1024
2 l- d5 _3 d; h7 K9 \part / --fstype="ext3" --ondisk=sda --size=16384
) H1 w, c7 c+ A% T1 H+ s%packages; O6 C3 C- y' g" Y- Q: ^1 R; N
@base1 f |; _ E6 Y9 v
%end3 Z/ c5 y0 | ~! n, h- G
1 ~1 h- ]# v% b% n 在KVM中启动2个VM,虚机在启动的时候通过DHCP获得IP,同时活得PXE Server地址及启动镜像:
4 R0 Y4 x$ I. n$ M, _* k! fKVM虚机PXE方式自动安装示例-2629585-3
* I$ t4 m$ [$ d ]" c9 c1 L 经过pxe引导过程后,开始加载配置文件中的linux内核及引导镜像:
% u& g8 x5 U* N' Z9 ], EKVM虚机PXE方式自动安装示例-2629585-4
% z6 @! l: O( m' o3 \ 内核与引导镜像加载完毕后,会进一步根据ks应道文件的指示通过http方式进行网络安装,可以看到root密码、硬盘分区、软件包安装都是根据应答文件中相应项目自动进行的,整个安装过程无需人工干预。5 O& W. p3 o3 X7 }
KVM虚机PXE方式自动安装示例-2629585-5
: {! J6 M8 I$ H9 @, R9 ]# y3 d 安装过程结束后系统会根据自应答文件指示自动重启,系统就安装完毕了,最后附两个截图,图1为DHCP应答中关于next-server和file的;图2为PXE-kickstart 无人值守安装流程图。- e/ f* i6 l+ G$ p% f, x* c. ?
KVM虚机PXE方式自动安装示例-2629585-6
/ N' @' R# ?) R' s0 B9 N5 P3 M5 z! J3 ~/ J- L# s0 q
KVM虚机PXE方式自动安装示例-2629585-7& w, \3 I& u3 Y
谢谢。
! v& G) F8 L/ Q/ V% H; i9 A2 V" b |
|