找回密码
 注册
查看: 430|回复: 2

virsh 在线添加设备cdrom或者iso镜像

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-9-10 15:00:04 | 显示全部楼层 |阅读模式
virsh 在线添加设备cdrom或者iso镜像8 _3 `4 E1 ?: N, R: j
virsh domblklist Windows2019_x86 " C& R6 K! d! ?
Target   Source
. Q# [, w- m! q6 e  T3 `----------------------------------------------------------------
0 J3 e( A' H8 z- |: M vda      /usr/src/Windows2019-x86.qcow2; L1 w) `) c" t$ d  S' R; ~/ Q
sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
$ t" `" S& h& [0 `" i3 M- c
8 F. o4 e, o- N, b+ c; o) t- R, R" b+ H  _: \1 e; U$ n
5 N5 h  b# K* C$ H9 k1 b8 B5 N
virsh attach-disk  Windows2019_x86 --target sdb   --type cdrom  /usr/src/virtio-win-0.1.225.iso  --mode readonly  
" }8 r* _2 c0 _5 ~error: Failed to attach disk8 A/ y& L8 R$ l& _& a
error: Operation not supported: cdrom/floppy device hotplug isn't supported7 p# @3 S$ r0 x. _! {. Y
+ Z$ G8 e& m1 d/ [! C% E# \

3 |6 V. ~- X1 }2 \7 E1 p; F* x/ G7 _
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent
8 }, e( w6 C! u) h" Herror: Failed to attach disk
, c/ e' \7 n* t& Serror: unsupported configuration: unknown driver format value 'cdrom'
# ]! R8 X5 z" ^! @8 H1 F1 O! `
8 H" d$ V6 {. x' N, \' [root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver qcow2 --config --persistent
# `3 L9 S( B% L# @% S% Ferror: Failed to attach disk
+ n8 U3 V2 s5 m0 ^error: internal error: No more available PCI slots
* `& p9 l" l/ E# U3 p* J0 V* y; g$ K
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver qcow2 --config --persistent % x+ V$ i9 R4 d
error: Failed to attach disk5 c- g, l5 v9 q
error: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format. \6 l' O: ?% m& v6 }0 O3 z

4 I! x& G% p. A1 ^; g: broot@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent
0 L' I- h0 L/ F! Q2 c0 JDisk attached successfully9 }6 ~0 P; C+ H! p& w. c7 U, R
, @* e8 s9 k. u9 M
6 y- I1 h2 a0 o' M9 e, N# G! l! L$ \

+ W" b9 ~' g6 Y' L$ aroot@compute01:/usr/src# virt-install --name=Windows2019_x86  --ram 8192 --vcpus=4 --disk path=/usr/src/Windows2019-x86.qcow2,format=qcow2,size=15,bus=virtio --accelerate --cdrom /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso --vnc --vncport=5906 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole --disk path=/usr/src/virtio-win-0.1.225.iso,device=cdrom
4 q9 J1 w5 W# e6 L
9 s$ l  P- C* z6 g8 S9 p: L2 RStarting install...
% G, X2 t5 Q- @8 l: ZERROR    Cannot get interface MTU on 'br0': No such device/ }% ?$ {1 s" i" F1 y: x( h& f" j
Domain installation does not appear to have been successful.
! y+ Y8 _/ h# v% p' e8 p$ C, k# RIf it was, you can restart your domain by running:5 n) Q5 i$ Q/ F; _
  virsh --connect qemu:///system start Windows2019_x86
0 h6 @; S5 p) c7 xotherwise, please restart your installation.
& Y7 p& c. W, w9 p0 g0 G7 vroot@compute01:/usr/src#
. @6 K. E1 ^+ B1 N9 A/ l+ c- V( jroot@compute01:/usr/src#
$ ]2 G4 p  I2 Y* m: \! Oroot@compute01:/usr/src# netplan apply
7 }# R: j# {* i) r: E0 O/ O. \) p, K+ X; i! C
** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead.2 g( z4 a) c. W& f( j" m
See the 'Default routes' section of the documentation for more details.$ r4 ?; b$ {; p/ F2 b  B
! T& D1 Y: W3 X8 h$ E" r8 Z
** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.
# B4 {/ `9 d6 t6 ~- R- sSee the 'Default routes' section of the documentation for more details.
9 f% ?! \3 W0 ~! n( v2 ~7 ]% H2 B, n# {
** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.* S1 \; L% V; \2 P
See the 'Default routes' section of the documentation for more details.
. r' _$ w8 r4 B2 x! b; o  O& M6 k4 `$ h0 j; S
: V  t$ X9 n9 Y9 t** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.4 F1 g$ g. r% U( Z
See the 'Default routes' section of the documentation for more details.
8 P  q6 k! G. L, _& C3 C$ ]2 H# G" j  W& x- c% d: r4 F
** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.
8 n8 J4 i( f) |4 ISee the 'Default routes' section of the documentation for more details.
' r% m$ j$ x, j- l/ Aroot@compute01:/usr/src#
; v. Y2 u- a5 [4 Z$ Q' {: vroot@compute01:/usr/src# / f9 A0 J7 d6 y0 @3 t
root@compute01:/usr/src# virt-install --name=Windows2019_x86  --ram 8192 --vcpus=4 --disk path=/usr/src/Windows2019-x86.qcow2,format=qcow2,size=15,bus=virtio --accelerate --cdrom /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso --vnc --vncport=5906 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole --disk path=/usr/src/virtio-win-0.1.225.iso,device=cdrom+ Q: C% {- @7 Q6 }. i
3 m9 X: g9 V1 P) \& W8 @6 U: U& S
Starting install...
6 L  M$ y3 k5 O% d) \& \Creating domain...                                          |    0 B  00:00:00     - l- j6 W; X! W( W/ V

$ L% E% C1 N% h3 y, C1 K8 a) a' gDomain is still running. Installation may be in progress.' M  y2 U1 J9 Y5 `! n/ Z9 J
You can reconnect to the console to complete the installation process.! G. @' c& Y! B# a% X' D3 g/ N
root@compute01:/usr/src# 4 ~/ S" m8 z  @/ s- h/ Z( _! m8 X

$ S6 b2 L; T- \" ]0 A: \$ }9 {) v/ Q1 K在线添加光盘& s% n5 W$ V  k# y: ^2 C! K
virsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd# v5 m& L" C  n# w& S; L% e2 I
( H7 D# z% h& \# G+ k8 ^
其他方法:
7 \, ?9 s/ b. W( v7 x1 h) @7 N
$ n9 z* g) z1 u: j2 ^, Q6 K. L3 R: d" ^0 T, T8 `3 ]
再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:6 U5 a. b1 m& Z: l

$ z1 a% h( q: q" l3 Q. F6 N<disk type='file' device='cdrom'>
! i9 O3 B* F+ O. i, A+ R$ E9 p  <driver name='qemu' type='raw'/>+ p: h, ^" O% `) K
  <source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>
. r+ ?; P4 g: _. i  <backingStore/>4 b0 d$ T3 v: B$ F
  <target dev='sda' bus='sata'/>
5 L0 `  C1 H- U5 W. |  <readonly/>
' J1 P, k* t; o+ r( H/ r  <alias name='sata0-0-0'/>
5 n) r: J  o' j% t! m6 c  f  <address type='drive' controller='0' bus='0' target='0' unit='0'/>1 k8 k' J' D+ A' ^$ M4 q/ e
</disk>
3 q! r6 |3 i1 t  {3 W; a9 ^2 ^如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:/ Q( P  c& |" ]

' I' F$ E+ Y4 B! M, `% A<disk type='file' device='cdrom'>
! i* \# r4 D1 M  <driver name='qemu' type='raw'/>
% O4 y3 }2 A: O) F- n# T' M! R  <backingStore/>4 k( M) g) ~4 l" r$ {
  <target dev='sda' bus='sata'/>& J. Y) x! J; O2 {* d) t
  <readonly/>! |" _" V# V9 _( q- b  S7 N
  <alias name='sata0-0-0'/>
7 R- x2 T6 J5 a$ Z( l# V: T$ B, V  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
7 P( z0 A# z! o  G: |. r# M' X</disk>
/ D# W7 m7 m. a* [  k3 _( P% T然后执行设备更新:
* d' r& G, _; M$ ?' J
7 @) C8 L4 y- t& t5 q7 d7 a* Svirsh update-device sles12-sp3 detach_iso.xml
/ l. O$ q0 C2 w, D9 Y" w6 h此时提示:
* f0 d, }# g4 G. H7 C, n
( R4 b! Q! U7 C) B( K( \Device updated successfully
# O" k0 x' L& E. d4 H) h再检查虚拟机配置,就看到iso文件已经卸载了。
3 Q3 B' [& ^1 o) o1 Z, k" u3 g; q4 L% o- W3 M2 v) C& c

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 15:00:05 | 显示全部楼层
virt-install --name=Windows2019_x86  --ram 8192 --vcpus=4 --disk path=/usr/src/Windows2019-x86.qcow2,format=qcow2,size=15,bus=virtio --accelerate --cdrom /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso --vnc --vncport=5906 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole --disk path=/usr/src/virtio-win-0.1.225.iso,device=cdrom  创建kvm时,添加对应的多个设备即可。9 b9 V9 V- L. r3 K3 ^, B
9 P- z9 L; U' _: \) m: ^
virsh domblklist Windows2019_x86 & T( q8 H. g- u4 b2 f3 v" E" [
Target   Source
, k7 b3 y. s$ W) y/ P----------------------------------------------------------------% e" Y. Z+ t+ E2 W
vda      /usr/src/Windows2019-x86.qcow2
3 i/ `2 N# u$ o sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso' w3 d1 _1 Z! W& {4 ]8 ?( C- a' r
sdb      /usr/src/virtio-win-0.1.225.iso, S6 i7 S( I! m

# u' z' B" r' P, L8 `

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 15:00:06 | 显示全部楼层
2.在线调整虚拟机内存(增大或减小)
9 i! O9 A' X6 R- i( g1 g1 y# V#调整为4G
/ o' f$ O$ E6 ovirsh setmem cos_v1 4G
; l/ W  @) w0 I/ r8 g: G8 x* o" p/ {9 e: h1 B+ o" h5 i8 J
#调整为2G
) S! b% W5 y; L4 z1 Y1 D; a; J2 |virsh setmem cos_v1 2G
  Y$ `- a& e0 l  r#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存
5 f. H, d7 C! e. w8 U* v; {+ @" g8 r- l4 G( Z

# L. F0 H2 |: L% `6 J3 D3.在线调整虚拟机CPU(只能增大,不能减小)
, k1 d( f3 y4 Z. |% k( ^8 O3 f1 Xvirsh setvcpus cos_v1 4
6 Y# Q# h. U& Z! n" e2 ~virsh setvcpus cos_v1 8
7 g; @+ \( x* X2 W6 m#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量8 j- v6 r) k) h& I% t  p. ~
) V  h  Y7 T" {$ `" x: Z  j

( V# h0 F2 @1 G  Y" U4.在线添加硬盘
4 o' m, o9 A& `& `9 b  O#添加qcow2格式硬盘
* L9 q( h7 Z6 Q5 t  j
  C8 V5 n# o6 v0 Q) ?" ~* s% |" J4 a#创建qcow2格式的新磁盘,大小为40G2 C0 @4 X8 O0 j! x, A; W
qemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G6 p$ s$ N" v: i4 H' Y& g! V
virsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent
0 B3 I1 E" m' g5 ~" n  S# K5 y#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘( a# E: @. |. O6 l) @
#--config 参数同时更新虚拟机xml文件,确保重启后依然生效
4 h9 \" P: e$ I& L#添加raw格式硬盘
9 n7 i0 }6 [9 _1 ~#创建raw格式的新磁盘,大小为40G
& Z3 [* v2 \- @: G+ \0 E$ zqemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G0 U$ [( b, Q+ |8 y5 w7 C9 q- T
virsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent
1 w" G0 V4 r1 Z0 m
& ]  y' m" u" S6 k6 e5.在线移除硬盘# I  t, k& M% a; @
#可以查看虚拟机所有磁盘
0 U, G; @  ^  F' k+ Rvirsh domblklist cos_v1. k/ x  ~' f9 o  B; s5 ~# X1 z
virsh detach-disk cos_v1 vdb: @6 j5 O# r$ p) X8 H
+ l+ I& }( p2 H. Z9 U# o1 Y1 g( e
6.在线添加网卡( [8 E2 p5 R" s
virsh attach-interface --domain cos_v1 --type network --source default --model virtio --config
' _- |) S; o$ u3 L& G#可以查看新添加的网卡. f* S3 L& c' y$ F6 k' ^
virsh domiflist cos_v1+ l1 }3 x/ _8 s6 z

. ~2 G8 N  C) q+ E  @% ?* p& s7.在线添加光盘
/ s8 l3 A/ l" P0 f' A: f* [virsh attach-disk centosbase /data_lij/iso/CentOS-6.4-x86_64-bin-DVD1.iso vdd
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 01:50 , Processed in 0.018029 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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