易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 797|回复: 0
收起左侧

KVM虚机PXE方式自动安装

[复制链接]
发表于 2022-4-11 16:19:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
  示例拓扑如下,DHCP、TFTP、HTTP、PXE Server在1台主机上部署,KVM宿主机与NFS server分别在另2台主机上部署。
  Q6 C) |/ v* E5 g" ?          ! v# O. L% ]6 T" W- }; r1 q& }4 Y
      KVM虚机PXE方式自动安装示例-2629585-1
- V, Q" W+ G8 |. w$ q& N9 \$ l
0 C3 g, @6 `& e( w确认主机CPU支持并打开了虚拟化支持(Intel/AMD),如果是多核CPU会有多个核的信息:
- M, n* j; s. v# A; A" D[root-KVM-1 ~]# egrep '(vmx|svm)' /proc/cpuinfo
, b2 X4 s8 ]3 }+ H( a6 ?/ g4 Bflags : 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. U9 d! H' D. R! v0 R$ W! 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 pts1 D* |( m, \8 i1 h! Y
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
3 G0 a: a) a& 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 pts
& |. Q& ~2 Z! |. ~* uflags : 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 pts7 R/ y" F+ i- J" E# a0 F2 E
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 pts4 _' r, h1 }9 J1 z; x5 A/ R
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 y+ o9 {2 S; P5 ~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 k$ r8 J8 k# s' V

0 a# {# L" ^$ ^) ~' J% l! @确认主机加载了KVM内核模块并是在用状态:
' h. u/ e$ ^1 S" {9 Z[root-KVM-1 ~]# lsmod | grep kvm
% O. K, a: B- z  o, k7 i) ykvm_intel             170086  8 . v% r6 r% B2 O
kvm                     566340  1 kvm_intel
- I2 L  R- P) g8 dirqbypass              13503  1 kvm2 N* T0 K# [% K# H! ~
  3 p$ L: N5 B+ s# U0 N5 a
在KVM上创建2个虚机,本例中创建1个,另一个直接clone即可,虚机的网卡与系统网桥br0桥接:
* Z. `4 |3 @2 m5 [# {3 A" C
; ?$ V/ A; K$ s# ?[root-KVM-1 ~]# virsh list( @: b* I  ~+ a. H
Id    Name                           State0 q: K; c1 c# X( K$ w
----------------------------------------------------; j) Q( f( Z" Q* F! K4 ~
7     centos7                            running
! ]* N" l. z/ u: y 8     centos7-clone                  running/ ]" S* m. c' g/ G9 l, T4 {, E  K
[root-KVM-1 ~]# virsh domiflist centos7
* M' @$ s- N; _: b9 AInterface  Type       Source     Model       MAC+ A! u1 l# a" n/ k/ H- k0 c
-------------------------------------------------------+ K2 x% ?5 x" `
vnet0      bridge     br0        e1000       52:54:00:1c:c8:92
$ j6 a. A+ V  c2 H3 b[root-KVM-1 ~]# virsh domiflist centos7-clone
) U2 V0 q# b/ Z! N, N5 l4 LInterface  Type       Source     Model       MAC5 X# h- N' x1 Y3 l1 s* X
-------------------------------------------------------
+ _! y" s8 g0 @0 Z! kvnet1      bridge     br0        e1000       52:54:00:ee:71:75
% U, S3 N' j% ~2 J% G* E( }; s0 {# y* m( ?& x! Y6 R
在dhcp server 的配置文件中主要包含下列内容:
% L' W8 N- y* N3 F[root-PXE-server ~]# cat /etc/dhcp/dhcpd.conf $ I; O% e1 k  J* D' P0 e$ `
#
7 Y# F* U; x! P3 b. D# DHCP Server Configuration file.
* w2 [% m% a- x6 y#   see /usr/share/doc/dhcp*/dhcpd.conf.example  z* g# [4 t, i1 x/ S( I& X- t
#   see dhcpd.conf(5) man page. R" d- f- ~1 l* ?$ d+ v* K. i
#  s9 X( k' ^' x7 r) y3 t
#ddns-uptate-style interim;- k8 D9 k: i, i- c; t. B0 E
#ignore client-updates;
8 h4 x6 C$ U" K  c5 @+ Afilename "pxelinux.0";       //pxe的引导镜像文件5 N! `- R* @% ^9 I/ C  q
next-server 192.168.56.2;
/ e& _: t6 F1 o7 Usubnet 192.168.56.0 netmask 255.255.255.0 {
' ]2 h2 ]; Y" x- C, {  g7 soption routers 192.168.56.2;- _2 c% g: c, ~5 |* c% v; q
option subnet-mask 255.255.255.0;4 u6 y3 D" z) V/ J7 [
range dynamic-bootp 192.168.56.150 192.168.56.200;6 h+ z* h& q" z, \7 A! @3 s
default-lease-time 21600;
$ Q; a, p1 b+ |5 V( v}$ E1 s1 P( r$ O" p
    需要将pxelinux.0镜像文件放到tftp server的目录中,该文件可以从系统光盘镜像或者系统中获得,同时还有内核文件、引导文件:
8 W" [8 i0 z8 N6 j5 V7 r0 i2 }, j: X[root-PXE-server ~]# ls /var/lib/tftpboot/! l1 ~/ A5 q  A+ b8 E
boot.msg  initrd.img  pxelinux.0  pxelinux.cfg  vesamenu.c32  vmlinuz4 E% k, e* j+ ~& g+ K" E5 r
    将CentOS7光盘复制到/var/www/html的mnt目录中:
' j- ?8 b+ b3 M[root-PXE-server html]# ls mnt) W5 @$ [- r/ b8 U% u. O
CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL' ^! l6 H7 m! L$ }* W0 }
     需要配置pxelinux.cfg目录下的default文件,已指示系统通过pxe加载镜像后进一步的动作,这里我们关注的是CentOS7标签,通过http方式结合kickstart自应答文件进行网络自动安装:
  R+ I# [8 |9 {. y$ ?6 W7 _7 c; v
) C, E, M0 Z& e, K* A[root-PXE-server pxelinux.cfg]# cat default 1 ^+ o, ^: @8 d  H( u/ {' k7 y9 F
default  CentOS7
/ [' E# k8 L3 w: c) T! j2 H#default vesamenu.c32 5 ?. C$ x& J3 S
prompt 0' K1 s$ q3 u/ D2 p- ]$ e  Q
timeout 300
' a. c& V5 l! X/ ]3 x. R8 Q, Vontimeout local
/ _) I5 Z- Z+ b3 B0 j2 Ldisplay boot.msg
% J0 `7 j6 A) r: R# QF1 boot.msg& F( S, b& P' e
F2 options.msg$ F% R4 j+ ^/ w* w& D0 q
F3 general.msg" z: w2 ^8 E2 `  j
F4 param.msg
1 Y+ \8 [# e. i1 a5 N4 c- NF5 rescue.msg
% U  d, B7 y1 g6 P0 z# Clear the screen when exiting the menu, instead of leaving the menu displayed.1 R  N) N1 ^. R' S, ?9 W5 p
# For vesamenu, this means the graphical background is still displayed without& X; \- A) u, b; Q$ V: ~2 d! f
# the menu itself for as long as the screen remains in graphics mode.
& A% [5 x3 e. imenu clear/ D' y. h, F1 ?3 r1 U  x
menu background splash.png
- G' ~/ A# H6 Y' v2 Imenu title CentOS 79 `2 f& f) R1 |8 _& m1 d
menu vshift 8
, \" m3 S: [- n) {" o; l! bmenu rows 18, @2 s9 z' x- p( V
menu margin 87 [5 x# h6 z8 f: N: m4 |- M$ R
menu hidden+ w3 R8 E/ g7 K& A
menu helpmsgrow 15
2 Y* E. J. H; ]- ]4 M* Q, T: wmenu tabmsgrow 131 N( x5 g3 [; h. G
# Border Area! \- N- h) H# W. ^5 P9 F! |
menu color border * #00000000 #00000000 none' \8 {) i4 y4 k8 D; g* c
# Selected item: c3 ~3 _$ |# `, M$ |+ _
menu color sel 0 #ffffffff #00000000 none( B& p; F: [' H
# Title bar6 W7 Y& ]. Z1 X+ h
menu color title 0 #ff7ba3d0 #00000000 none; P6 R1 |  ^1 k; b1 R7 n
# Press [Tab] message) i( x5 Y* D) F
menu color tabmsg 0 #ff3a6496 #00000000 none
3 _# G/ O4 v+ X9 A# Unselected menu item
: s) y2 H- k4 f& s$ rmenu color unsel 0 #84b8ffff #00000000 none8 a4 W, D+ H: E. O) w5 f
# Selected hotkey, ?" R/ z, p9 h
menu color hotsel 0 #84b8ffff #00000000 none
. l2 w" f/ o. Q/ ^5 ]# Unselected hotkey
& M0 Y0 S  w2 m4 R# k) {" Vmenu color hotkey 0 #ffffffff #00000000 none* r4 `/ ^6 x5 V- O; j9 F
# Help text& w& Y5 }( r! Z- A2 `5 S( q
menu color help 0 #ffffffff #00000000 none4 m3 E$ n: h9 C# X
# A scrollbar of some type? Not sure.8 o7 R& m, X' _5 X
menu color scrollbar 0 #ffffffff #ff355594 none
7 O$ N; v% y0 X$ @3 |) v# Timeout msg
) C# }: P" d+ B9 C' b: X7 d( Rmenu color timeout 0 #ffffffff #00000000 none
- r5 J& V8 t3 Zmenu color timeout_msg 0 #ffffffff #00000000 none* c; E/ w, ]/ @! E& K
# Command prompt text1 l9 M- T# P7 u
menu color cmdmark 0 #84b8ffff #00000000 none
% m, B1 Q6 G$ l+ t' I& cmenu color cmdline 0 #ffffffff #00000000 none  m" I5 B: b, C3 R; L
# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.
  |1 s' L, I7 }. i- u: g1 R2 p1 [menu tabmsg Press Tab for full configuration options on menu items.
# W) B6 [2 S9 [% gmenu separator # insert an empty line5 U% f1 Y8 }' M- P( l
menu separator # insert an empty line; A) D" ^7 q9 y1 q
label  CentOS73 f4 z9 q/ X1 r. B; F
  kernel vmlinuz
; V+ q9 k3 Q! R  append initrd=initrd.img ks=http://192.168.56.2/ks.cfg  vga=normal devfs=nomount ramdisk_size=9216 nofb   : X9 B9 M7 ~' B3 t' L' {
    KS自应答文件可以通过system-config-kickstart工具生成,也可以复制系统中的anaconda目录中的样例,编辑生成:3 Z$ k2 _5 A; I2 m) \
  KVM虚机PXE方式自动安装示例-2629585-2$ A9 i  ?& f% s1 _

1 O% _4 k( q0 f2 V本例中的ks应答文件主要配置了http网络安装的路径,以及root密码、硬盘分区、以及安装的基本软件包。( n$ w& {" Q5 V( W5 t# d6 c
[root-PXE-server pxelinux.cfg]# cat /var/www/html/ks.cfg
0 w1 w/ C; Y  ^* j#platform=x86, AMD64, or Intel EM64T
* k" a) v; y# R) N1 n8 b6 I#version=DEVEL% Y) I+ M( P! p+ {5 E- m
# Install OS instead of upgrade
# S2 t4 B+ c* l. qinstall$ N4 A7 Y0 \( j$ H6 Y2 v
# Keyboard layouts
& L  W4 o: {% n8 N8 h2 c6 Pkeyboard 'us'
3 g- @" g9 B9 @: z/ s! R# Root password; g1 p1 G7 {7 l7 L* b+ H: [
rootpw --iscrypted $1$2g8RzvvY$YqlnUGED1Js5RAjnchHKK1. b3 n" j# g& R$ M. @' j
# Use network installation& {4 O: S% I3 K6 L% s, I. K* `
url --url="http://192.168.56.2/mnt/"
+ h2 E% S# j: [/ h# System language) T* W# S6 K' B, C. C8 T6 s3 M# ?
lang en_US8 s( @' x7 n# u0 J+ G2 U
# Firewall configuration
+ P0 g0 z# X, S5 a: }firewall --disabled( a) g  {- k, P  X" W
# System authorization information9 v% O& U" E, t
auth  --useshadow  --passalgo=sha512
/ a, J2 M0 x& l; r% `# Use graphical install
# P' i1 X' z9 Y3 h5 Qgraphical
6 B$ q0 o, M3 p5 w) _, V6 hfirstboot --disable
  z  D, J8 u0 B  I9 \5 }5 J1 [# SELinux configuration, z1 |; K6 U/ e9 s" q6 i/ w
selinux --disabled
4 J& \6 Z  w4 c; @7 D) _# Network information
; h& r  n8 n6 c  j& c5 s$ cnetwork  --bootproto=dhcp --device=eth0
& O9 q" Y. E4 ~( L# Reboot after installation
5 x1 X$ J5 C( K. h! f. ]reboot( V! t) e& K& X( a) u
# System timezone
+ A9 e9 w# @; Ttimezone Africa/Abidjan
0 f& y- N# ?  E* [# System bootloader configuration
) K' W' D. Q0 z" A: F7 vbootloader  --location=mbr 0 O9 e  n# c$ N2 J, t; A$ K
# Clear the Master Boot Record
+ R7 v/ T$ e0 k; ?5 Tzerombr
; n+ {( |. l5 e/ B  {" Z; X2 U# Partition clearing information
+ R6 A" R- O& J$ A8 ^7 Cclearpart --all --initlabel
! C5 ?: t; H* k3 i# Disk partitioning information" u$ z. w% L: O# ]9 Z; k1 i
part /boot --asprimary --fstype="ext3" --ondisk=sda  --size=2009 R: c( R6 W+ Z% x) P
part swap --fstype="swap" --ondisk=sda   --size=1024
: C  L  i: y8 d, Bpart / --fstype="ext3" --ondisk=sda   --size=16384
9 n& q, M" i: G0 o# q%packages
/ R: q! R& c$ `) D; `4 C@base
, v% {# w# q9 V' J) c( E7 g%end
: A' i8 B1 m+ o) B' `% N9 d. G+ L; ?0 G, p$ S1 X+ _
  在KVM中启动2个VM,虚机在启动的时候通过DHCP获得IP,同时活得PXE Server地址及启动镜像:& g/ @' R2 T. t9 r- [8 h& I
KVM虚机PXE方式自动安装示例-2629585-3
/ L) C7 H7 w; J$ @& J* ?/ F   经过pxe引导过程后,开始加载配置文件中的linux内核及引导镜像:& m/ D/ ^" s9 R$ H& ]- ^
KVM虚机PXE方式自动安装示例-2629585-4
- ^, b7 D# v6 E* L- T   内核与引导镜像加载完毕后,会进一步根据ks应道文件的指示通过http方式进行网络安装,可以看到root密码、硬盘分区、软件包安装都是根据应答文件中相应项目自动进行的,整个安装过程无需人工干预。
' W/ |7 K' o+ v- P! q: n  KVM虚机PXE方式自动安装示例-2629585-5. F& r8 `: y; D' L9 b
     安装过程结束后系统会根据自应答文件指示自动重启,系统就安装完毕了,最后附两个截图,图1为DHCP应答中关于next-server和file的;图2为PXE-kickstart 无人值守安装流程图。7 w: V% A/ p+ Z- l3 k% d4 x
KVM虚机PXE方式自动安装示例-2629585-6
% \1 j& h1 @! L' o7 s7 C; D
0 n: [6 `# G9 v) [2 t# ^KVM虚机PXE方式自动安装示例-2629585-70 S" X8 h0 Q0 J: h# Z! v
谢谢。
1 T) W: }" k) E; B6 }; `. M
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-9 07:17 , Processed in 0.051433 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

快速回复 返回顶部 返回列表