|
|
示例拓扑如下,DHCP、TFTP、HTTP、PXE Server在1台主机上部署,KVM宿主机与NFS server分别在另2台主机上部署。' v! ?$ f) ]$ }5 ]0 d; b
4 t8 N3 o8 {9 o: b
KVM虚机PXE方式自动安装示例-2629585-1) i y6 @7 [" P
, U' s& H/ \' k确认主机CPU支持并打开了虚拟化支持(Intel/AMD),如果是多核CPU会有多个核的信息:
& `3 L3 b+ q! }0 L% q5 S[root-KVM-1 ~]# egrep '(vmx|svm)' /proc/cpuinfo K# H1 ?1 I; T! m& 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 pts
8 t5 S0 B' M6 h8 T$ B; p. iflags : 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 e, j' K+ W1 w" 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
/ W& `1 |5 N- ~ e3 K& s- S. Tflags : 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
" `" Z0 U! B, iflags : 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" ]5 d$ f; i& a! c8 W
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
6 D4 v2 S6 U3 H( T$ g5 lflags : 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 pts0 H# A% c5 r6 a7 |
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
! b0 J" K: j5 |4 b! A
) Z0 T% n, \" b. O1 ~ V确认主机加载了KVM内核模块并是在用状态:$ ~: k/ D+ N E. m, I% p* J( j/ P& ^# I
[root-KVM-1 ~]# lsmod | grep kvm
1 H8 g1 P' e2 U. L, jkvm_intel 170086 8 ! _0 e) W9 [0 Q$ B) k8 _7 t
kvm 566340 1 kvm_intel5 i. C: ~9 E5 U6 M5 U- h/ q5 u$ ]
irqbypass 13503 1 kvm3 `8 B" s; N+ X% K
6 @+ G5 z: \" {0 ?在KVM上创建2个虚机,本例中创建1个,另一个直接clone即可,虚机的网卡与系统网桥br0桥接:' i% z2 u- ]9 h7 b
: Q* H9 H2 P2 @. p! p
[root-KVM-1 ~]# virsh list
4 ^3 X0 U" d6 D9 t X/ p Id Name State
( P; a8 e! N5 r* Y9 g- z----------------------------------------------------- W+ Z; i; z& h7 C
7 centos7 running
( \% c5 p) k: b( j) U) R( R 8 centos7-clone running
+ a, z" H' y7 ^ A- o2 M6 O[root-KVM-1 ~]# virsh domiflist centos70 T4 ?& b! L v8 }
Interface Type Source Model MAC( v! r7 u. w- ^6 D! j' H% ^9 Q
-------------------------------------------------------6 ]( ]7 ^1 {3 P
vnet0 bridge br0 e1000 52:54:00:1c:c8:92
% Y/ ~8 A( S2 d' F; e[root-KVM-1 ~]# virsh domiflist centos7-clone
4 k4 ?; ^, S) _+ p7 U/ J0 uInterface Type Source Model MAC" i5 J( w0 x1 w8 [, t" J
-------------------------------------------------------
?) f' f' X4 m6 M/ t6 ?: lvnet1 bridge br0 e1000 52:54:00:ee:71:75
[/ [; z5 X6 `: _. [: X Q
) r7 m" }9 M0 m3 _7 V) @在dhcp server 的配置文件中主要包含下列内容:
1 l9 C5 t( s# _8 i# ?7 d8 C[root-PXE-server ~]# cat /etc/dhcp/dhcpd.conf
- X& T p7 }5 M$ V6 m6 z# u" `% N- M n8 P- R/ h* R. x2 `
# DHCP Server Configuration file.
. P2 _9 g0 v. s# m1 r# see /usr/share/doc/dhcp*/dhcpd.conf.example6 I8 ?2 I3 D- b w8 m' M
# see dhcpd.conf(5) man page% x- u$ l0 H8 u0 {7 J0 M4 q: i6 |) A
#! i3 |" B7 l: j R, i8 x
#ddns-uptate-style interim;5 ?/ U3 \& G8 t6 r
#ignore client-updates;0 _, i! `9 \- \$ \4 j" x2 O
filename "pxelinux.0"; //pxe的引导镜像文件
9 ?# [6 q, T9 n; s/ }* a# v" U0 lnext-server 192.168.56.2;
0 d8 Q0 \5 I) Rsubnet 192.168.56.0 netmask 255.255.255.0 {+ [/ i5 n* y+ f: o& T! i
option routers 192.168.56.2;
+ l0 `2 n( u- {3 Woption subnet-mask 255.255.255.0;
9 ]. n7 G& P) A( u" [! R& @range dynamic-bootp 192.168.56.150 192.168.56.200;
. ], a& U# x$ k$ pdefault-lease-time 21600;6 y u7 ^' V( B
}
$ c/ b& g4 {, f/ S! c 需要将pxelinux.0镜像文件放到tftp server的目录中,该文件可以从系统光盘镜像或者系统中获得,同时还有内核文件、引导文件:+ t( q* ?/ o; U0 Y- u, Y" T
[root-PXE-server ~]# ls /var/lib/tftpboot/0 Q* P( ^+ E8 h' f
boot.msg initrd.img pxelinux.0 pxelinux.cfg vesamenu.c32 vmlinuz2 S2 o2 h5 l H! M" [$ `0 C
将CentOS7光盘复制到/var/www/html的mnt目录中:
& A4 e. e8 E& m, p1 ^! @% n0 x7 B9 u[root-PXE-server html]# ls mnt
. v2 P6 c+ ]3 Y: c+ ~CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL
' ]% p7 t+ j8 z2 n4 [' |6 j1 v 需要配置pxelinux.cfg目录下的default文件,已指示系统通过pxe加载镜像后进一步的动作,这里我们关注的是CentOS7标签,通过http方式结合kickstart自应答文件进行网络自动安装:. b5 v; ?3 @, a' o" A2 O! l) W0 \
5 ~9 W2 g6 R0 \$ _ v, Y8 c
[root-PXE-server pxelinux.cfg]# cat default
6 Y! h' y- k5 m. ^default CentOS7 ( P }; a. m2 N/ R0 M
#default vesamenu.c32
7 m& M) h4 J: K) k% Tprompt 0
, ?! T8 G& O" U: v, qtimeout 300
9 @5 s/ }8 ~! B" P9 nontimeout local. L6 I" z% Z' y; d# O
display boot.msg
' t0 Z3 `( @1 ]% R+ \F1 boot.msg+ m! R5 ~# M" y s
F2 options.msg
9 n" f6 V! Y5 R! A8 G7 O: Z0 U2 ]F3 general.msg& p+ S$ T! Y; ?: W: S
F4 param.msg+ S9 z2 n* n1 I
F5 rescue.msg3 @6 S7 V5 |6 x1 Q, w/ S
# Clear the screen when exiting the menu, instead of leaving the menu displayed.
, p9 s& y$ J" W/ v/ x0 N5 x# For vesamenu, this means the graphical background is still displayed without
9 V- k* h0 T9 A# the menu itself for as long as the screen remains in graphics mode.5 q! l r3 {1 u/ r
menu clear
" g2 a. Y6 m9 K+ K: A |menu background splash.png
1 V8 h% f8 W6 T* Nmenu title CentOS 7
+ C) H9 n9 C. m0 \menu vshift 8
0 e* ?5 _+ {; Z+ zmenu rows 18
8 N* g! u! F0 i8 p+ ?6 z; Z Nmenu margin 8! R: h0 T( f+ J; z! h0 y: J
menu hidden
" N( d w+ y$ hmenu helpmsgrow 15
" A4 T. I& D3 U1 V) {% w: k3 xmenu tabmsgrow 13+ i6 v4 c9 U: D& E& }
# Border Area
# Q( X7 ]2 c0 Fmenu color border * #00000000 #00000000 none
8 L" _7 W0 [2 S( X% {& y# Selected item! a Q+ v5 q+ J! k- J
menu color sel 0 #ffffffff #00000000 none
2 H; c R% E; k6 ^2 M% s# Title bar
* }; o& f' N3 j. f# m% N, O- N ^menu color title 0 #ff7ba3d0 #00000000 none
; Q& T3 W. r0 d$ ^0 D: F7 w# Press [Tab] message. S6 O: C8 `. n8 b) q3 r3 ^
menu color tabmsg 0 #ff3a6496 #00000000 none
- f( G) a0 x. T# Unselected menu item1 `; R) z' G+ B" j
menu color unsel 0 #84b8ffff #00000000 none
) e! v9 X) ^# a2 ^+ t8 [6 z/ f3 E3 F# Selected hotkey9 j4 {* x1 G3 O4 `8 j
menu color hotsel 0 #84b8ffff #00000000 none
7 I2 Q0 ? F8 R- F# A- ^# Unselected hotkey
" T: C3 O0 A1 ~. Smenu color hotkey 0 #ffffffff #00000000 none1 Z6 s+ R; @; b( v6 J
# Help text
! v9 I w1 e4 D e! @menu color help 0 #ffffffff #00000000 none c7 i8 j& P: t! y3 [
# A scrollbar of some type? Not sure.
4 A! v! S' ^1 m0 y3 f/ k @menu color scrollbar 0 #ffffffff #ff355594 none4 G& F) G' T+ C3 F) k) B& B1 B
# Timeout msg/ ?; |- [) P& V
menu color timeout 0 #ffffffff #00000000 none) W/ P; H; }3 Y# D7 C0 A2 b4 H: k; |
menu color timeout_msg 0 #ffffffff #00000000 none9 C. ]5 x' i0 f0 g
# Command prompt text
1 W$ D2 {' b+ `menu color cmdmark 0 #84b8ffff #00000000 none
% o& S1 y1 y; P4 i! n- i, ~menu color cmdline 0 #ffffffff #00000000 none
1 R% K9 `9 q- ?" {% Y# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
! d& t9 d V+ f$ ]9 q8 mmenu tabmsg Press Tab for full configuration options on menu items.% B; M4 p- {, C4 P' H1 v5 U
menu separator # insert an empty line8 B# q7 [- `6 R+ V R* s
menu separator # insert an empty line
( X4 v3 T1 H6 G6 ~, H) N$ slabel CentOS7
r, A- \8 Q$ }; F0 m3 y kernel vmlinuz2 Q2 [' {. h5 \6 J: I5 t
append initrd=initrd.img ks=http://192.168.56.2/ks.cfg vga=normal devfs=nomount ramdisk_size=9216 nofb . c2 e. _9 g. c& F
KS自应答文件可以通过system-config-kickstart工具生成,也可以复制系统中的anaconda目录中的样例,编辑生成:
6 J# M6 E3 o5 F% C" j KVM虚机PXE方式自动安装示例-2629585-2; x$ e% e4 _+ a* n" z- t* e
5 J8 y# T$ u* H5 `( w3 B
本例中的ks应答文件主要配置了http网络安装的路径,以及root密码、硬盘分区、以及安装的基本软件包。
7 i O8 l! ^# s8 L* H, ?* a( q# b[root-PXE-server pxelinux.cfg]# cat /var/www/html/ks.cfg c; K2 |+ I1 v$ S
#platform=x86, AMD64, or Intel EM64T
+ `! t+ m: L2 A$ l# k) b#version=DEVEL4 _5 E2 f" c# G+ j3 A6 R1 L
# Install OS instead of upgrade) S w& @' q4 |3 u# {, E
install
4 H! g( G- [: v/ e) L# m& P! A6 g# Keyboard layouts
) ]% u: z" ]6 a* rkeyboard 'us': `0 D9 V5 O H
# Root password
, K, Q6 K# x; J' s$ prootpw --iscrypted $1$2g8RzvvY$YqlnUGED1Js5RAjnchHKK14 M* O% W/ B/ l" v1 S1 V" u$ A4 M
# Use network installation* g8 @6 ^# p3 O* w3 r
url --url="http://192.168.56.2/mnt/"5 u' d. G3 \) y$ |$ w6 J
# System language
) j6 L3 r4 p* q2 H9 t N slang en_US
/ r. q7 A8 i" x4 y h& [# Firewall configuration
! k- N _0 e) Y+ x, rfirewall --disabled1 p) W% |. T3 U
# System authorization information
& |- ^2 U: F2 z0 L5 S0 nauth --useshadow --passalgo=sha512) `0 D/ ^2 ?! |
# Use graphical install
. F& b7 w3 A3 c6 G# r, ^graphical3 a! G* ^* \$ K) o' D
firstboot --disable8 q; |. Z+ l% M0 b
# SELinux configuration
3 C2 h8 M$ u: u, h4 {) }( Iselinux --disabled
1 f& s$ q& n) f& N# Network information( d- D& B' Q4 H% u
network --bootproto=dhcp --device=eth0, W* {1 q, P" P! J' d, a/ J1 I
# Reboot after installation( {7 v$ V2 z2 t+ D7 {) W' b( S) b
reboot2 w$ k- v9 S7 k, ?6 x
# System timezone; j0 n# [2 U8 f/ \
timezone Africa/Abidjan
7 z {5 ]+ |$ W+ s# System bootloader configuration
+ |0 q; j3 \% {; J% R- }bootloader --location=mbr
2 L: U; e9 |* X, { _3 a# Clear the Master Boot Record5 N2 M0 ?8 s1 Q
zerombr
' |( j5 q: G2 a' e1 i# Partition clearing information
2 u1 f, b2 j+ E3 N$ ]4 X) rclearpart --all --initlabel- z. M& d6 t1 w$ N6 a
# Disk partitioning information5 R: b H, q$ n J3 D) n* c$ }
part /boot --asprimary --fstype="ext3" --ondisk=sda --size=200
" n5 |4 E: P3 W0 m( ^part swap --fstype="swap" --ondisk=sda --size=1024$ t/ k2 X9 Y+ S& F* X0 S& a0 }
part / --fstype="ext3" --ondisk=sda --size=16384
/ ~; R# Q9 b- k0 V* B: |%packages$ k8 q. n* j3 @- I
@base
5 w3 g9 R& y* J$ Y1 ?# A0 V3 h%end
! U2 S4 d8 p9 F% d7 [- c; k6 X# E* z F! T- h" P9 i+ M/ t
在KVM中启动2个VM,虚机在启动的时候通过DHCP获得IP,同时活得PXE Server地址及启动镜像:
o5 c7 N: k2 I: W! d5 mKVM虚机PXE方式自动安装示例-2629585-3
' P( P1 u* ?( D- m0 D 经过pxe引导过程后,开始加载配置文件中的linux内核及引导镜像:
I9 e! }: i1 j6 ~KVM虚机PXE方式自动安装示例-2629585-4
1 q! l; g, w. G6 g3 E 内核与引导镜像加载完毕后,会进一步根据ks应道文件的指示通过http方式进行网络安装,可以看到root密码、硬盘分区、软件包安装都是根据应答文件中相应项目自动进行的,整个安装过程无需人工干预。
3 e; f) W" ?9 G) [9 g% r9 g h4 S9 u* u KVM虚机PXE方式自动安装示例-2629585-58 i* _+ j: {' a3 ^; i% i
安装过程结束后系统会根据自应答文件指示自动重启,系统就安装完毕了,最后附两个截图,图1为DHCP应答中关于next-server和file的;图2为PXE-kickstart 无人值守安装流程图。
3 {0 y1 A9 ]) Z/ _7 {) gKVM虚机PXE方式自动安装示例-2629585-6
; o# w6 M0 `6 ^$ p" o0 a
( [! Z! {$ k/ J- h: ?: pKVM虚机PXE方式自动安装示例-2629585-70 \" i) E" x+ @& v V
谢谢。8 r% `& Q/ R' q& b
|
|