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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-9-10 15:00:04 | 显示全部楼层 |阅读模式
virsh 在线添加设备cdrom或者iso镜像/ P+ F) a+ Y) c8 r! }
virsh domblklist Windows2019_x86 ; i# u- ]; l+ C6 G2 c+ M
Target   Source
  M/ _) |* f( X: ~- b* }. f! g----------------------------------------------------------------+ [+ G! [$ O$ a: r' G
vda      /usr/src/Windows2019-x86.qcow22 i/ m. U& }& D' t9 }. B' V. `
sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso2 y& b0 j) ]0 J" E, x& e
3 i0 s2 ~) [& _, y, B3 t
9 f3 ?- b! K, h3 ?( u9 O2 E2 K

( H: w! c. @" {* z5 X& f2 mvirsh attach-disk  Windows2019_x86 --target sdb   --type cdrom  /usr/src/virtio-win-0.1.225.iso  --mode readonly  " x, r9 f: w  @# w2 B
error: Failed to attach disk$ P7 a. m. {) k' |
error: Operation not supported: cdrom/floppy device hotplug isn't supported
7 m! {6 k7 `! v3 s, e2 o' j+ }3 l: Y% c3 |1 n
" d) h- i$ u; V: B8 {' f

: ?; j. X- H8 r$ E5 Droot@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent
" y4 f% L, n$ o, g4 terror: Failed to attach disk
% [5 Q  `; ?" ]0 Y* o, kerror: unsupported configuration: unknown driver format value 'cdrom'5 U$ F( e0 P- q/ N5 Z) N
! j% \! g! U) k7 F/ l( \
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver qcow2 --config --persistent
1 I5 s; P  x/ q8 O' G; K" j0 v* m4 Jerror: Failed to attach disk
$ ~" Z' U! z- X5 p* w; z3 w4 }$ f+ Eerror: internal error: No more available PCI slots
4 o7 @# t- S0 W. Y3 j' p, t% f/ ?% I% i) Z! o( c* m/ i! z4 p
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver qcow2 --config --persistent
' m7 v% R- d4 j# K9 _% D) V% }1 o: jerror: Failed to attach disk8 P3 S9 `6 e  ~. F
error: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format
6 Y  e* _! y! l+ z$ h* V! w1 d  v" ]# P) z: R; j
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent
: X; f* [5 Y# U# A+ l0 pDisk attached successfully" O9 |( X9 R) R( `5 P! r: q9 C
% c" R+ D+ ?5 H) q) c

% Z' U0 r) L1 _* ^/ T0 d
% J2 X. v, w/ M( ?% o. Rroot@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
) W/ B  T- _3 p( r5 M
& f5 V0 Z+ P+ CStarting install.../ n& a  l; B; F6 n1 L" P  |$ z
ERROR    Cannot get interface MTU on 'br0': No such device
" b9 ^0 T# I  m7 bDomain installation does not appear to have been successful.
/ W# q: U& X7 F- UIf it was, you can restart your domain by running:& g4 c: H) J! m7 ?
  virsh --connect qemu:///system start Windows2019_x86
" x5 z. ~' o9 p" |# [/ ?) F+ ]otherwise, please restart your installation.
: n% }8 Z  n+ m3 [* M6 C: F3 oroot@compute01:/usr/src#
# G# q* @( `$ S* `root@compute01:/usr/src#
- t7 B3 x" [; @! broot@compute01:/usr/src# netplan apply
' ]" k" C& h, a9 r4 D8 Y
( ~: I- j& e: Y4 l# L** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead.+ Y6 }2 l1 X; B  s
See the 'Default routes' section of the documentation for more details.
% C% @* \2 L# k! b" ^
" x" K: B3 h  C. B( _** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.
6 N3 i0 O+ T9 f* ?( Z$ mSee the 'Default routes' section of the documentation for more details.
; C6 E) r9 k( {' J5 w0 w
, @# ]7 V9 ?# N& |4 b3 a+ y** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.$ H6 j; e* c5 x2 w1 {1 p* S& }
See the 'Default routes' section of the documentation for more details.
% ^* _$ \# q! v) f4 X
- r, }' [. ~# f: p% ?& w* @** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.0 J, U4 n' `5 s9 a
See the 'Default routes' section of the documentation for more details.8 t# x; v2 L4 K9 c2 p% H+ o

8 Q6 k6 l* C  B" c- w% d9 w** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.9 Z" R: `9 O0 U( x
See the 'Default routes' section of the documentation for more details.4 i& W! \) C$ t4 P7 B
root@compute01:/usr/src# 4 o! V* c( y- m
root@compute01:/usr/src#
7 R7 a  k4 S8 n7 a* I1 ^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
* k2 i( F# }& [' J! ^" g% r' g  U% P
Starting install...' k0 f! m1 d9 Z$ U+ ~
Creating domain...                                          |    0 B  00:00:00     % u' U4 I* Y: P/ W; l1 J
/ C1 W7 V9 ]6 c. F! Q+ D% K
Domain is still running. Installation may be in progress.4 L, H0 o* O/ B; h9 L- e$ s7 w
You can reconnect to the console to complete the installation process.
$ ~# |9 ?/ F0 O2 I, K* F7 y9 m) Proot@compute01:/usr/src#
6 w* D& r9 x* F2 v+ o. k5 I
' m3 n) G5 o) C# W% G$ @; D在线添加光盘
) x9 z* t* H+ _6 Z9 qvirsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd# K5 m3 [+ S( `4 L

3 X2 ?+ L: D2 B! k/ G4 x+ I: U7 A& L2 ^其他方法:
) X7 A  J1 Y% |$ P& J7 T1 u, s0 b6 K
- N) O9 W3 `( u" m
再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:5 D9 ~  N. R8 Z; G1 A5 p% X
& O3 c) s* D+ d+ l' D
<disk type='file' device='cdrom'>
; Q9 }3 i4 v1 W  <driver name='qemu' type='raw'/>4 i. Y1 k# P* \+ n2 b
  <source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>& p. \8 E3 n$ w! x: @
  <backingStore/>. K" P% F8 G  P) B  {7 e
  <target dev='sda' bus='sata'/>
* B* C# r3 ]4 a  <readonly/>5 n5 P! W, W+ X: U- e2 e0 j
  <alias name='sata0-0-0'/>
* I$ S6 Y& t' v8 r  v  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
2 k: N" [* v# x) b</disk>6 K' R& J/ t" o& f. @
如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:
5 L  b5 S2 J# o8 Z5 M3 ~- K1 k" u4 G3 O) p9 V0 g7 g& {: f2 H
<disk type='file' device='cdrom'>4 j) `* h* B: W7 l5 c* Q
  <driver name='qemu' type='raw'/>, J1 F# {- C7 t1 [' J$ P6 _8 M' Z, `
  <backingStore/>( _$ {( M: N6 N
  <target dev='sda' bus='sata'/>
: V" L4 ?0 b1 G" n% E- Y  <readonly/>
2 X4 k5 i: @6 C; c4 x  <alias name='sata0-0-0'/>% t8 O' H7 H. @+ ^
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
- k: \5 P/ |4 E8 l8 i</disk>& M5 h: L, Q; N. p- ~
然后执行设备更新:
: e0 L) P5 S3 G! }1 q+ t+ a4 X9 X+ U7 B: z" R
virsh update-device sles12-sp3 detach_iso.xml' g4 m4 X; D$ [; R. O
此时提示:
5 a7 [4 E- R$ O  c4 H/ G9 y. @3 l; l
; q& H- {) ~0 w# Q5 q3 H2 [$ dDevice updated successfully
$ H& N+ J. X  \4 Q# d1 B) c再检查虚拟机配置,就看到iso文件已经卸载了。
+ z! W+ j+ Q* m0 B( ~- q7 X# L) J8 b0 U) F

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时,添加对应的多个设备即可。
7 g1 k4 V, Q/ J) v
% _$ Z4 M, a0 O. j' ?% k9 n8 Z/ Evirsh domblklist Windows2019_x86 " D1 ^9 E+ S: O5 ?! K' T6 z" b9 `
Target   Source
9 Q; e! U( E/ T8 m5 E----------------------------------------------------------------( i% p2 c+ P% p2 h
vda      /usr/src/Windows2019-x86.qcow2
0 K, N# i3 k' ~- j0 |5 L5 z sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
4 [7 p! `0 I- K5 I sdb      /usr/src/virtio-win-0.1.225.iso% q# k9 P; [+ `. r+ E# m9 z* H
  K  i1 S2 _% m  K2 d8 U9 [

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 15:00:06 | 显示全部楼层
2.在线调整虚拟机内存(增大或减小)
$ H- m5 h: [# G! k#调整为4G
1 i- X* V* N( T5 u1 wvirsh setmem cos_v1 4G# o8 B! c0 T6 I
: O2 k  _) h9 S
#调整为2G# x2 c5 t* n( c! v* q
virsh setmem cos_v1 2G: a; S' c  M! _/ ]  {2 }. @2 j
#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存0 G* H5 A' z: j8 e. S, ^- h0 A* @
; N# g) f* @/ ?2 S: b( u

" U- I8 q" \# R8 i- m3.在线调整虚拟机CPU(只能增大,不能减小)4 [5 G4 A; C; ~7 y6 c
virsh setvcpus cos_v1 4* S3 Y9 {1 y- n5 J* C3 z
virsh setvcpus cos_v1 8* B- a' U6 E' {
#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量
- ?: O0 d+ L3 d# d5 Z1 E0 b8 \; _! k* g

0 o* M3 l! u) K! R, B5 {' m2 N4.在线添加硬盘
1 k( B! [( E$ g( \0 Q- H#添加qcow2格式硬盘
2 t7 D# y3 g$ d! S; V$ v5 V- U+ x8 U% ?" }" l5 {+ i+ N+ _
#创建qcow2格式的新磁盘,大小为40G
3 o! _- h9 b7 j) E( U  o. X9 W- `qemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G
" R/ I- f2 u% ^1 v2 j7 ~% ovirsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent
3 n! U6 u) ^5 Q: t. U4 H  e  ^#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘
: J' d8 n2 G& e( Y; u1 A#--config 参数同时更新虚拟机xml文件,确保重启后依然生效
8 N* w7 Q% U; T9 o# L/ R' a#添加raw格式硬盘$ l. S5 E( |# p( b" c: Z
#创建raw格式的新磁盘,大小为40G3 |/ x: g/ m4 |( c
qemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G  m- b! [% z6 X- c5 t/ K. P1 H
virsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent
! M5 i6 Q# W; h- J; Q 3 N8 O7 N. \1 R3 `- V- s* b
5.在线移除硬盘
5 x; o( E8 n7 U#可以查看虚拟机所有磁盘6 x" @' o/ {. N0 j# s
virsh domblklist cos_v1
( h' Z, |9 V9 E2 A, d: Avirsh detach-disk cos_v1 vdb' f1 @  a5 Y# q% g& G
- c; n+ {2 }& q) U# c
6.在线添加网卡
- U4 Y1 n6 w' X) v9 |' yvirsh attach-interface --domain cos_v1 --type network --source default --model virtio --config
- v# ~& w" J4 F2 C! G  M2 _7 c! O7 a#可以查看新添加的网卡" d! j2 t5 a$ f* q" ~
virsh domiflist cos_v1
; h2 L) ?5 x. g+ p
' L2 C5 V* i2 z# E8 t. q' S, F$ g7.在线添加光盘+ r" T1 Y1 L# M: T8 p
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 03:08 , Processed in 0.017302 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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