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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-9-10 15:00:04 | 显示全部楼层 |阅读模式
virsh 在线添加设备cdrom或者iso镜像
) a5 D9 q+ X- u) Pvirsh domblklist Windows2019_x86
1 r, l& h! H4 g# q) A6 P/ G Target   Source
1 w8 P/ R" v- }----------------------------------------------------------------
$ ]( g1 n& F' L/ ^5 O, M vda      /usr/src/Windows2019-x86.qcow2. [# X  a! T+ v, b
sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso& s$ r& ~8 Q' Y5 J+ {

6 S% c' r* l  `8 m# E
; v, h9 d: K) w: m8 t' {
1 W1 S  O& p+ `& L- h% Qvirsh attach-disk  Windows2019_x86 --target sdb   --type cdrom  /usr/src/virtio-win-0.1.225.iso  --mode readonly  : ~2 I* P+ H. l
error: Failed to attach disk
/ H  t% {6 _" Uerror: Operation not supported: cdrom/floppy device hotplug isn't supported
3 d$ V/ i4 a: _# Q+ p3 N* i" I, ?  b6 R$ q

1 \/ J3 O9 J3 V% G! v/ I! o4 K, w! `3 r! c6 t+ c7 X# n
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent 6 t$ H$ b. s/ g& z; F
error: Failed to attach disk
4 U& L" r8 Y! y) eerror: unsupported configuration: unknown driver format value 'cdrom'
8 \0 E; u; e+ `% `! w* n. R
$ E6 M, s- w3 o# Uroot@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver qcow2 --config --persistent ' ~0 p" \; l' q1 n
error: Failed to attach disk
' i* U% z2 V8 s6 F$ Derror: internal error: No more available PCI slots6 p5 \& f( |. f' j# H, b: Z3 C
5 J3 ?: G3 R, X; i) r8 {; S) i
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver qcow2 --config --persistent % {' f/ m1 S) ?  i
error: Failed to attach disk0 x* R; f: r  p# p: U5 L5 D1 I
error: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format
9 o/ N1 l4 q1 a7 Y" Y: K! e+ J9 X3 ?; l2 b# S7 v- ~( L' W* F$ R$ n/ e, g
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent
, L7 I- _6 G1 DDisk attached successfully
: y. b/ M# M! s. `$ b: d9 r5 L6 s/ V
4 @3 E& z+ @6 f* j1 x+ O
  E, Q- G8 o( o9 p! E, J& P
+ w3 V( f+ J8 j8 v/ d# I+ iroot@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
3 _3 f; O& J- V+ K6 f% f  f8 t+ u
) u* D/ Q+ G9 k% C  J) KStarting install...
8 v2 c/ P/ C8 P# X) cERROR    Cannot get interface MTU on 'br0': No such device
+ I' Q% r  q2 z; b( a% k: j7 NDomain installation does not appear to have been successful.6 C3 W! a+ H7 G
If it was, you can restart your domain by running:
& R; N; K- i3 ]& f  virsh --connect qemu:///system start Windows2019_x86# n- [0 G# J9 b$ O6 g7 L
otherwise, please restart your installation.' v( {$ ]9 w# [- O% Y; z
root@compute01:/usr/src#
, N: M& E* Q) ~# X/ lroot@compute01:/usr/src#
& P- I! u9 {( n- Nroot@compute01:/usr/src# netplan apply
+ N% K3 {( `4 u4 O) K# k' q2 y' `' \8 S# {6 \0 v, \
** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead." l, C( _% c- u0 K1 U' r* M
See the 'Default routes' section of the documentation for more details.
9 _, d7 E) `4 |7 ~
) I) G6 ^9 D1 ?) d8 k" C** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.- u4 \  S# O" t+ P  `
See the 'Default routes' section of the documentation for more details.6 M* e. P; y3 N! X

5 x" l" d4 ~1 M! K- O** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.
$ l/ o1 L. m6 I3 HSee the 'Default routes' section of the documentation for more details.% f) {- Q0 o, q+ k* B8 u& u6 b

1 ^  Q  ^8 ^/ Q0 C8 ^4 W- U** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.7 K: p2 P  D$ N/ `& y
See the 'Default routes' section of the documentation for more details.( j7 |+ t" T$ Q! n
$ L% J* |" V1 S% Z0 p6 L2 X& X
** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.1 o% q: j+ W0 Q
See the 'Default routes' section of the documentation for more details.
8 U3 F# U, G! s( \root@compute01:/usr/src#
% c& w7 W3 }" W( P1 ^  Droot@compute01:/usr/src#
) R! Y& {5 Y4 w% l/ h+ g/ sroot@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=cdrom0 {; W! c3 k0 `5 W+ B: ?4 R

2 E5 S. P; k  V+ ZStarting install...- `+ r8 K+ r" y; T; L
Creating domain...                                          |    0 B  00:00:00     % _* Q1 F/ K/ D; [$ I

# j- s' O* ~5 ^+ D% s6 V- XDomain is still running. Installation may be in progress.' b6 f; l" e0 U  Q# C; _/ h- T
You can reconnect to the console to complete the installation process.
% a3 C9 I( v& w  j& qroot@compute01:/usr/src#
9 [$ o! W$ |8 c$ }& Z. U; B/ t' T
在线添加光盘+ C7 s8 o7 D) L2 s
virsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd
4 K! Q) I, E4 g8 M! R
& C8 \; i/ d1 L9 v% {. j其他方法:# N) p+ w2 U, }" g8 c1 O- v! \
* u/ j' n( z  o
$ L( f6 J' K& A: m
再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:, X$ f1 i1 H/ X% H, n
' H; y- q  r) A
<disk type='file' device='cdrom'>
" I0 y' P5 j: N) `6 h  <driver name='qemu' type='raw'/>
3 y: X7 o6 _- k5 N; W  <source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>- O- x$ Q7 }) @3 I( U' y
  <backingStore/>! I. v6 Y9 T0 m" [9 z" A% I
  <target dev='sda' bus='sata'/>0 n5 X$ l* g2 N3 \$ j% o
  <readonly/>
8 Z; {; R6 Q+ l8 c% Q  <alias name='sata0-0-0'/>- p* `& f4 ^3 j
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
+ C  D- R5 U- E6 U</disk>
. j5 `/ v7 M9 W+ t! D' W# K如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:2 S7 ]& @' q3 \) L

" P( P1 C+ F" C& M3 f1 u7 C* `- G<disk type='file' device='cdrom'>) q/ }2 V! }, r% ?  b. A2 j& q
  <driver name='qemu' type='raw'/>/ [& X' `0 H) j( g" z. l6 O+ w. E! ]8 C, a
  <backingStore/>
1 @2 e" g! _  g+ c0 J) T  <target dev='sda' bus='sata'/>: V3 t; G' u# i$ a" d( d
  <readonly/>
! @% T6 y' [7 t( I  <alias name='sata0-0-0'/>( i& J  U$ _/ ]
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>- F" A, V2 j% Y/ p8 \
</disk>
; O6 q7 z1 G+ K5 O/ \9 T然后执行设备更新:) l6 I9 D; j5 d* S9 [
. v# ]- z+ l9 V5 d7 j
virsh update-device sles12-sp3 detach_iso.xml
* p, }# M4 v6 ~3 n' b+ Y此时提示:' G* w  Z( b" T7 X( Q* z. o
) O6 x7 L/ \# H  w5 P" s0 }8 v
Device updated successfully
, L/ q1 f) O! Y" ^& J& V% y: f  ?$ U: N0 ^再检查虚拟机配置,就看到iso文件已经卸载了。! w4 v0 E7 \7 v3 b, R' R

+ T7 P- S0 \6 Q% E! g6 ]

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时,添加对应的多个设备即可。& ~& @6 C6 l9 w) {
# r7 Q9 q0 a* o; `$ x9 m0 w" V3 _1 {8 {
virsh domblklist Windows2019_x86
7 Z& w) e% U& _ Target   Source
! E+ b9 ~0 f( s( v. {" b& a----------------------------------------------------------------
9 v1 m0 L3 n8 U+ t6 _1 D vda      /usr/src/Windows2019-x86.qcow2
9 n" \2 z; w6 \! `1 z sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
- Q5 t' ?) W0 e sdb      /usr/src/virtio-win-0.1.225.iso* J2 o2 {* G" ^5 o( ~

+ T& L* J! m- H& s9 w! \

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 15:00:06 | 显示全部楼层
2.在线调整虚拟机内存(增大或减小)- L' v/ D. j$ a, y, `8 P
#调整为4G3 ]6 ^/ A  g+ S2 b8 k
virsh setmem cos_v1 4G* I1 P: b7 w4 Z2 Z
4 h7 Y' X. O/ m& J" C" D
#调整为2G
3 L* l6 `! R+ `# S: f" v% dvirsh setmem cos_v1 2G
- B- b2 O+ C9 i6 f* Y/ q* \#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存
1 K9 P& j: a7 u: Z! ~- X5 O: ?. j! i: t, R( a* r6 f% C3 {
' L1 z8 e+ Q2 `; n# B) E1 n9 O
3.在线调整虚拟机CPU(只能增大,不能减小)
5 ?: l) @0 L) T2 e/ Fvirsh setvcpus cos_v1 4
: W3 D7 y* k% t9 D3 Xvirsh setvcpus cos_v1 82 d" ]: K) G. V8 e
#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量
% I3 c$ Z+ B/ e( \2 B1 C+ m  a
/ x- D6 g. @1 B  F! Y4 Q
! U2 R* [3 A: Y9 b" j; F7 j4.在线添加硬盘$ V6 R/ X% n# a, h
#添加qcow2格式硬盘
! L: f0 t* R& H3 }! u! F6 `1 M
+ R3 H0 z, [0 h* h* j0 l# m#创建qcow2格式的新磁盘,大小为40G
3 m3 }/ X/ R3 P4 O! pqemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G1 m3 S2 L, S: c' Y' o
virsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent" Y3 y& N, t4 P9 v' T6 C
#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘6 p# t5 v& Y4 n! b/ P# x, c
#--config 参数同时更新虚拟机xml文件,确保重启后依然生效
2 w  ~+ j* K$ W. d2 W#添加raw格式硬盘
# G  ~/ }+ M1 I& \4 U/ T. `#创建raw格式的新磁盘,大小为40G
6 \: G1 A" z2 {8 Y2 l0 y# Y+ Oqemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G
) \& U. t* p6 h  N' g& `1 Uvirsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent* C3 v( h2 L, Q0 g3 `6 O$ f

) j& \# t" H: ~5.在线移除硬盘) c0 I: Z. G. Z( `, e1 j+ H0 k' e
#可以查看虚拟机所有磁盘! v2 y- x! F6 R7 W  @4 K9 Y
virsh domblklist cos_v1$ t1 f$ i7 P# F( V/ D+ ]! c
virsh detach-disk cos_v1 vdb! o4 e* l' ~$ C3 [

. k9 c$ k: r: U4 q7 I% S$ d6.在线添加网卡
. N7 k2 ~- q  t% g* W# y8 `virsh attach-interface --domain cos_v1 --type network --source default --model virtio --config
  U* L! W) m4 v$ i, C9 n6 R#可以查看新添加的网卡5 `3 k7 @! I+ w, w  ?( j
virsh domiflist cos_v1
" q; R4 X5 X. \0 S; Z2 y6 Q 8 N, d* t2 M" g8 H1 t4 V3 q. V& Y
7.在线添加光盘
* z8 {  q7 f5 \& T  fvirsh 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:53 , Processed in 0.019060 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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