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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-9-10 15:00:04 | 显示全部楼层 |阅读模式
virsh 在线添加设备cdrom或者iso镜像
( ^3 a1 }4 v' Gvirsh domblklist Windows2019_x86 3 F: V8 `& T" ]
Target   Source9 N& K9 |7 w9 E2 y; T; k
----------------------------------------------------------------9 w: E/ a$ i; o) x
vda      /usr/src/Windows2019-x86.qcow2
' M7 E6 c0 d5 u' Z sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso3 S' g% Z) a  x( k3 B
  q# f% h5 a2 |7 @% a2 B) S
" A. y+ T, F) m0 ^
% {' F+ T1 ~( _7 `
virsh attach-disk  Windows2019_x86 --target sdb   --type cdrom  /usr/src/virtio-win-0.1.225.iso  --mode readonly  
3 I# I7 K8 W; a2 [  X: p  Terror: Failed to attach disk
) J& k3 h* N6 f4 x% X1 }, Oerror: Operation not supported: cdrom/floppy device hotplug isn't supported& t0 d/ T! p' M8 o: r, i& ^7 c
1 w$ O2 p7 {! ~4 W, X1 f, l
$ g. Y* n. x0 s- S/ Y) F( [
! `2 c3 x& q7 T* M8 L" z- ]
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent . b; ~  h) \- X
error: Failed to attach disk( x; N! u2 A7 `
error: unsupported configuration: unknown driver format value 'cdrom'3 S3 Z: z9 R7 v# j6 ~
& u0 t8 q' J% f$ G9 V% {7 H
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver qcow2 --config --persistent % \9 n2 L3 [: @1 w" B
error: Failed to attach disk# a+ R( B' b8 X: q3 W/ ?/ h
error: internal error: No more available PCI slots
$ u  F  j. b2 q% o  P9 [8 J+ f% a7 f) h9 \. i# A1 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
# F9 H4 i4 u3 k+ r3 @( N7 U( Q& gerror: Failed to attach disk
# ?* s& h" Z9 o; Merror: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format
- C1 W! `/ Y- H) k5 P! Q  ]; Q" f4 ?& i' r! }. K" r2 j  n
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent ; z) i. g5 ~2 l$ `$ }+ `" @
Disk attached successfully
4 T4 T5 d0 e: V, h+ m
5 K1 A  s9 p9 y  f1 [3 {1 e. {! a
! Q, G5 {7 P1 E7 }4 e# D
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
9 ?- h/ k, |0 k1 l  e+ i7 w+ x' |2 Q5 X! q/ d( `
Starting install...
7 c: s6 ^1 b( l1 R1 _ERROR    Cannot get interface MTU on 'br0': No such device
3 N: I/ X4 \# ~+ h3 C) }) }3 yDomain installation does not appear to have been successful.6 K1 s: `- t$ ^7 F- r5 B
If it was, you can restart your domain by running:
0 m1 ^% l' G2 Q. C5 Z8 H& m1 ~6 q6 i  virsh --connect qemu:///system start Windows2019_x86( R' x/ L1 c) |4 ]5 ^% s: j& l' |+ k
otherwise, please restart your installation.
5 R( s, o7 o/ H7 Q" o' @6 t+ {, wroot@compute01:/usr/src#
3 v2 H) ~; h) [( z7 a$ B, ^root@compute01:/usr/src#   \6 T0 C- K& k; e0 Z" l* r' `
root@compute01:/usr/src# netplan apply 2 k$ l! V7 N0 m5 s

! h: P9 E& n$ U. [** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead.
0 ^# V  Z8 ~+ i+ f. ]5 }See the 'Default routes' section of the documentation for more details.
- M1 N( m0 b; q" n0 |% N$ d6 z1 v- M# Z1 G
** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.$ U. R  x' d. @2 D3 V( m; K  J
See the 'Default routes' section of the documentation for more details.
9 _& G6 ~/ \, P" h9 Q# j5 O# T2 S" d5 h' w2 u5 m
** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.% ^7 ]$ g+ T" J  u( A3 O6 e, X5 i: u
See the 'Default routes' section of the documentation for more details.) ^- j( v* o0 S' c

* C& d# z( z# u9 I) P8 m9 l7 m. I% C** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.
% l3 \/ Z& ~; ?8 u; D1 }See the 'Default routes' section of the documentation for more details.$ X0 d- m4 ~: ^7 z/ b" L

0 R: ~- W1 H4 g7 ]** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.
! i: N( b8 [1 e, }; LSee the 'Default routes' section of the documentation for more details.+ I/ f1 v) M" J2 `- W
root@compute01:/usr/src# ( u, o/ H) ]( |
root@compute01:/usr/src#
8 c/ X7 x% B9 ~$ {/ @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
" K3 }0 _5 z2 d/ x% o" k
8 M0 a; Z' [7 t* NStarting install...4 M' J6 [9 q3 S# x4 g/ m4 n& ~+ U
Creating domain...                                          |    0 B  00:00:00     
8 v: r- C& t& G( `( B. H* l
8 b0 [& v$ t, a8 KDomain is still running. Installation may be in progress.
; b+ j" b1 c( P" ^, YYou can reconnect to the console to complete the installation process.+ n. U! G, ~" Z7 `( w* z) d
root@compute01:/usr/src# - T# O8 V6 L# k1 }, p
' q9 J( T, A6 F! H
在线添加光盘- b+ r% G. T7 `; ]4 H9 `7 j* K6 }
virsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd
; t  ?1 h; U4 k& t9 a8 J; e  X; Y8 A" Y) r9 Y8 R) d3 r5 o
其他方法:; c1 V4 `: M! [0 B
  s$ ~2 X, i, ]3 i! B% V; C
: y9 `- t; k7 ]
再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:
% P# S, x4 O0 q: s- _* o! b. [, N/ t1 b0 T7 z9 D. W
<disk type='file' device='cdrom'>
, Q' Q2 M7 w1 [# ]  <driver name='qemu' type='raw'/>1 j. s& N4 y1 r9 H
  <source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>
& }; N: F% Y5 U6 f: c  <backingStore/>
: _& l8 m) t, h, G9 d$ T# ^( b  <target dev='sda' bus='sata'/>3 K6 l7 G# x; }. N& }" z
  <readonly/>
0 p: y5 K8 h$ H/ c  `( ~  <alias name='sata0-0-0'/>( `6 _9 f9 F% z3 ^! }
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
. d& a- y  S3 G" ^$ E* ^</disk>3 I. O# d8 L" C- o9 t- ?5 s5 ^9 z& X* d
如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:6 v& X3 E; Y* h! j. b* D

" d* R+ o) Y6 ~" ]<disk type='file' device='cdrom'>
# K. r- ~7 z3 J1 L9 h/ p  <driver name='qemu' type='raw'/>
5 `* ~, I1 h* q  q; Z* P, v) i  <backingStore/>
% u+ g) f/ v1 ?3 u  <target dev='sda' bus='sata'/>% c9 c9 a% M- y$ Q
  <readonly/>
4 U% m. u7 {, g, K+ n0 E: ]  <alias name='sata0-0-0'/>1 W( C  l( O8 `3 X4 X8 W3 z2 i  i
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
1 S( o) k: C4 I, M+ @) ]</disk>9 Q0 d/ r/ E. ~7 Z
然后执行设备更新:
/ u# ?1 m- z; J; v- c: ~
, Y1 i% _% z- t/ g" rvirsh update-device sles12-sp3 detach_iso.xml% P9 _6 v2 q; u5 x
此时提示:
! E$ u5 X, b$ o$ C' {
5 z; u9 J. H* Z  j: XDevice updated successfully
8 H  o* D! ]" B2 t" d2 w( m0 x5 o再检查虚拟机配置,就看到iso文件已经卸载了。
2 w" i3 p$ G( f' N
1 \: U+ A0 S9 }0 f, Y1 r( W/ v

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 X: w; a8 x! t' d3 |- O
6 p8 b2 ]" O" Q1 P
virsh domblklist Windows2019_x86
6 n5 x( y" U3 u1 s$ _ Target   Source
; o( z# m- w. y: ?) t( Y! r----------------------------------------------------------------
% o1 A" g1 Z& k) }3 K$ \+ I2 E vda      /usr/src/Windows2019-x86.qcow2
/ I: d/ h+ @2 l4 ? sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
. J, _  N( A1 \& E, e1 E5 ]; A sdb      /usr/src/virtio-win-0.1.225.iso; Z" {) W2 P! y4 T
: j* e. a, ?. H) _% U) ~

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 15:00:06 | 显示全部楼层
2.在线调整虚拟机内存(增大或减小)
; c. a: f8 ]$ W#调整为4G
" F6 [: Y/ \, W- ^: ?0 Z4 `virsh setmem cos_v1 4G
- x% e/ G0 g  C1 F& z4 W6 x% y& Q. f1 z# }
#调整为2G7 c; E- S6 e& c' c
virsh setmem cos_v1 2G) Z6 y# L4 e  h3 X
#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存- d% X* v5 Y% v, U6 R

' _% f3 o( i, e6 F
# W% o8 A" ~% A9 ~3.在线调整虚拟机CPU(只能增大,不能减小); Z( t( M" o9 {7 K6 Q" h5 k# |- C
virsh setvcpus cos_v1 4
7 z( B+ j" n: J* s2 \" g4 xvirsh setvcpus cos_v1 84 d8 u& A$ f  `7 V7 F2 K# X5 n+ [
#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量0 q! S1 c! f  O! I) [+ E
6 T! J1 M2 q6 B* T% \

" S9 Y0 n. y$ M8 b9 {/ I5 N! k* F9 d4.在线添加硬盘( r3 l7 M5 Y& V. e  ]
#添加qcow2格式硬盘
. O$ \5 O* s$ h" |; n
( }' T7 c1 s) u5 h* u#创建qcow2格式的新磁盘,大小为40G: k  X* O. |; }( I, g# W
qemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G
/ ^8 ^9 }2 e/ {2 S  _virsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent
% m& ?7 j3 L) J) e& {#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘, Q  R2 O5 X+ K5 ^4 ^$ I2 ~
#--config 参数同时更新虚拟机xml文件,确保重启后依然生效4 {* U1 X2 {3 `
#添加raw格式硬盘* i& j1 k! {5 u2 C9 J
#创建raw格式的新磁盘,大小为40G
+ b1 u  N7 u% `qemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G
3 l" \  B/ W$ E4 avirsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent3 w: t! F. l  ?& [  N

9 Q$ C9 E/ p& {. _: _! N0 A% y5.在线移除硬盘
; b% [. O; ?1 w$ |#可以查看虚拟机所有磁盘
( q+ L" l; n/ W1 Ivirsh domblklist cos_v1& B. z- L$ N! f' r
virsh detach-disk cos_v1 vdb
9 z& k. I8 ^1 ` - \  \' \, Y3 Z8 ^; J
6.在线添加网卡
' \- n$ O8 _' [+ R2 A! N0 bvirsh attach-interface --domain cos_v1 --type network --source default --model virtio --config0 Q3 z8 c5 S4 Z$ E! |  _+ `
#可以查看新添加的网卡2 N) r  A/ i7 z  |/ `7 y1 l
virsh domiflist cos_v1
; s" Q7 ^. g  Z  c  @
9 q8 L; Z$ r: b: l  f7.在线添加光盘
& m/ U/ K' A/ q8 n( m& q% Z$ J, ~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 04:14 , Processed in 0.014793 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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