易陆发现互联网技术论坛

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

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

[复制链接]
发表于 2023-9-10 15:00:04 | 显示全部楼层 |阅读模式

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

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

x
virsh 在线添加设备cdrom或者iso镜像+ F9 M/ D& D& U) }7 f8 D5 Q, G
virsh domblklist Windows2019_x86 / P9 t5 f5 \' L6 P) V
Target   Source
# `% d# r0 A9 V$ s& C----------------------------------------------------------------
) @! S$ p, s. q# ]1 R" M  V/ ~+ S& W vda      /usr/src/Windows2019-x86.qcow2
: Y' B/ R/ V1 I; w+ B8 ^( Y sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
3 s% u1 {! [' w1 j5 Z& P8 y) U9 L! k$ W: c3 o3 u7 n

3 O0 V# q% z& I" I. ]% {) {# f/ w7 r9 L9 v
virsh attach-disk  Windows2019_x86 --target sdb   --type cdrom  /usr/src/virtio-win-0.1.225.iso  --mode readonly  
: k2 `6 O6 i2 u! o- F- a: a' ?error: Failed to attach disk
/ ~1 _1 @% o6 s* Cerror: Operation not supported: cdrom/floppy device hotplug isn't supported7 x# L8 g: A; L7 |0 d' @

; W8 {/ f; M5 D5 P' B9 J5 M, I8 v  n$ w

9 r/ Y& E: B4 w# b8 _# k3 f) groot@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent # x# C& |0 C* d0 @5 X
error: Failed to attach disk
! @5 [& Q: @& B& \0 herror: unsupported configuration: unknown driver format value 'cdrom') o/ u- \  A1 L% |; x4 j
- k% R/ @. E$ p6 }1 h9 n9 |' 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 5 \( L( L" u- X$ x  W
error: Failed to attach disk6 _7 E7 F( k6 P' h+ L$ g
error: internal error: No more available PCI slots5 K$ g+ t# _- f+ D" p: E
. G' ?; l- J! w3 s. g
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver qcow2 --config --persistent
0 I; Z* i5 V6 ?1 B; |9 Zerror: Failed to attach disk4 [8 f: M$ n9 [- k0 v
error: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format: Q6 A; p5 i# y  C, \$ X0 h

. t- ^, D$ R  z/ l( ~- h# \root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent 4 \. s/ w, E2 |# M5 @# j6 D# L
Disk attached successfully3 l1 [% J& q$ K2 u5 M5 Y
: H$ m# o9 \' P* k; A( D  L

" d; _2 K6 B* U* p4 t
& \$ F+ }. b. u6 _! s- p/ z; oroot@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=cdrom5 Z' L3 x' Q& R" h8 r' p: T) G

) h* l, P/ [% K+ H8 T9 xStarting install...
  ?; H4 k& Q; @3 k9 L* {; |ERROR    Cannot get interface MTU on 'br0': No such device
0 X/ O$ x' x1 a7 e% oDomain installation does not appear to have been successful.- x. J4 q0 t$ L; f3 b8 G
If it was, you can restart your domain by running:
% `' a0 b& B; a5 c! K9 p) d; @& r  virsh --connect qemu:///system start Windows2019_x86
' H" p" S0 }4 g* S$ `- h) j+ motherwise, please restart your installation.
" j$ _5 v) ~" f% y& J- x$ `root@compute01:/usr/src# 3 r3 A2 q0 P2 a+ q
root@compute01:/usr/src#
/ B+ u  G& j1 @root@compute01:/usr/src# netplan apply . \, Y  a8 D% j* i

# z0 O  C, s: O( m: {6 g" x  B1 t5 Q- `** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead.
' |' p5 G2 O& e  n: A4 o+ \See the 'Default routes' section of the documentation for more details.
. T, c/ N* R; J# u- v% k
; R, p1 Y: o  {( T1 i- C' t# x** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.! {: m  c# i2 Y) B4 y6 s: F
See the 'Default routes' section of the documentation for more details.2 j" _9 I: r  O; Y! O" f6 H

) n' D9 A$ V5 R9 x** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.
7 ~# F4 f! }! ?* S9 p$ iSee the 'Default routes' section of the documentation for more details.0 a; v) x/ }  U# ?
& f0 d$ J# ?+ p0 D- L
** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead." y# e- h8 P% g9 W+ J
See the 'Default routes' section of the documentation for more details.
2 ^4 Y& ~8 F" s1 D' h
8 l/ ^' _/ @6 O0 N) {5 N** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.% N7 S+ S  a' b9 G- r, `
See the 'Default routes' section of the documentation for more details.
6 D2 |& T* w  l) C; v* Lroot@compute01:/usr/src# $ ]: T8 r7 n8 o- v
root@compute01:/usr/src#
% U1 @  W' w/ ?1 o" b- u* w8 y: vroot@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=cdrom2 y  S7 ?' z4 x* i6 A  ]% k
# k8 S9 L9 U- _. _. b
Starting install...# L/ }' Z* j  Z
Creating domain...                                          |    0 B  00:00:00     
  x+ j$ |! s7 B. A
! N) Z& D  R" J8 p5 iDomain is still running. Installation may be in progress.
. L9 o; \; d1 KYou can reconnect to the console to complete the installation process., Q9 D7 _2 }9 |5 |1 t- B4 B) {
root@compute01:/usr/src# - ~, @  Y# Z3 N1 o- B! p
, K/ W3 }: h' c7 N
在线添加光盘7 ?! P- l! L: q3 g
virsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd
5 l# e1 D9 L2 R
: W* W8 M$ c, Y( D2 q其他方法:/ f) M* _4 @2 n3 C

4 I. `8 d7 i/ [* Q, J. N+ k5 O7 _
6 `3 u' |. @8 B, {: r/ d) c再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:
: b$ [* {8 r8 }7 p0 g6 g/ {" c8 \; Y3 K
<disk type='file' device='cdrom'>
8 R9 P" C* n9 H8 T1 I. P  <driver name='qemu' type='raw'/>- A4 d; t+ `: v6 n
  <source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>- E3 l$ ^3 e+ R  ?$ b* `
  <backingStore/>
3 f( [6 @4 n& D# O  <target dev='sda' bus='sata'/>& t+ [0 d+ I( w; \
  <readonly/># d5 V( L2 J/ f# D  q
  <alias name='sata0-0-0'/>% L+ ~- t" m: c4 r
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>4 \8 h% K/ Y% g& f+ s9 G
</disk>
9 J6 a, |# O$ J' K, P如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:
8 J6 s, I% C, p9 _% ]0 J( f7 j6 u0 U$ s( o1 m3 [
<disk type='file' device='cdrom'>
8 P+ v, @' h4 @0 R, N6 \  <driver name='qemu' type='raw'/>1 i) P  I2 E+ n
  <backingStore/>
% F; M4 Z& D, t- C3 R% ]5 i  <target dev='sda' bus='sata'/>4 L% X- K# a2 S4 q5 P$ K
  <readonly/>( D4 X0 G' G; A
  <alias name='sata0-0-0'/>
! f2 {1 J* U, C( o3 x  <address type='drive' controller='0' bus='0' target='0' unit='0'/>0 N) Y# O! S7 _6 p4 s+ h
</disk>7 O! L- D( d" z6 n7 t' @( \
然后执行设备更新:9 ^" {/ m1 p( R) n* g
$ L5 \4 d* q8 v3 k) o
virsh update-device sles12-sp3 detach_iso.xml* b# Y5 n% I$ x$ h( L% ?2 C9 I# V
此时提示:5 Y; F6 R- ]' q" k

( h" r1 Y* U2 Q/ O' k5 ^; IDevice updated successfully/ v5 }+ }( i3 j7 `0 x4 v
再检查虚拟机配置,就看到iso文件已经卸载了。
' H+ Q6 H1 G$ z/ M4 \% Y7 o$ q2 E
/ |9 U9 d9 Q1 ?: b/ Y% l* V
 楼主| 发表于 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时,添加对应的多个设备即可。: K% l/ C2 S0 S3 t+ N6 n
8 q# w" A; ]0 p7 V0 h* n* [
virsh domblklist Windows2019_x86
( p0 y" _! n* b Target   Source
5 g( c6 W3 B6 H* Z* _----------------------------------------------------------------
0 `% `2 F' g7 D/ Z! }3 F3 y vda      /usr/src/Windows2019-x86.qcow2
: z+ F% ~8 y1 j! e sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
' ]# s1 V, D& Q/ p: y) Z: G sdb      /usr/src/virtio-win-0.1.225.iso* a# h1 P2 ~* A7 U9 R

% O( Z5 @6 q4 l) Z7 y0 E3 w' ~
 楼主| 发表于 2023-9-10 15:00:06 | 显示全部楼层
2.在线调整虚拟机内存(增大或减小)
, \" u/ r6 E* r  Q/ ?#调整为4G5 [4 O4 q9 C/ x: s) N! g
virsh setmem cos_v1 4G6 ~" Q, s( s2 u& O: F/ @. X: ?* Y
* B8 l! e7 o, j" D4 Q6 k) S
#调整为2G0 O# J+ K3 R. u$ j6 K! H5 V7 G
virsh setmem cos_v1 2G
* q7 H( _: p) h. Q7 |+ j4 Q#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存/ A, x8 {' E/ i$ h! R* \7 ?

9 g9 b( D# L/ O% e
9 g: l, Y( x; H, }3.在线调整虚拟机CPU(只能增大,不能减小); c% R  U) ?& E
virsh setvcpus cos_v1 4
7 t& F5 [. }) w5 [$ Fvirsh setvcpus cos_v1 8
' I* m# F3 k/ F+ Y! I#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量- a( X1 e% x* ~' d# ^( N! V8 b
9 u5 ^9 U7 e& x2 s4 d6 ~0 \5 n
! W: O; ]3 {$ @) @
4.在线添加硬盘
" T9 l5 A8 C% b, h* s2 \. S0 \4 w0 s#添加qcow2格式硬盘
5 B- L4 S% Y1 A+ C+ J
( e1 U6 D9 T6 ?' B5 U# K, }#创建qcow2格式的新磁盘,大小为40G, ]+ X8 a; u, @$ k$ n8 y
qemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G
2 a# S& G2 @2 Y. l8 fvirsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent) [! n5 Y% I1 K* G. c
#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘3 k. F* p! H" B' `% u2 ?
#--config 参数同时更新虚拟机xml文件,确保重启后依然生效
& ?' D4 D7 ]4 r#添加raw格式硬盘
% \% I5 t% [  [( d  j' }# K  [#创建raw格式的新磁盘,大小为40G: P! Y4 M' O: c  t' P( S
qemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G1 l) \3 X+ r2 \9 h8 s
virsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent" ?7 h" g  x) t* V& T

# R# U; |; f) D, v% K5.在线移除硬盘/ \- G: S. x) x* V# R( L- n! |/ f
#可以查看虚拟机所有磁盘
( L; Q  F; A. Uvirsh domblklist cos_v1& c+ v, e. w8 b  o8 n$ [" g. o! J6 ?
virsh detach-disk cos_v1 vdb
; T. b( M- O  s" D
1 R7 ^: n. r6 }6.在线添加网卡0 l3 U  w) i6 v& Q
virsh attach-interface --domain cos_v1 --type network --source default --model virtio --config# r  o  k; ]4 c
#可以查看新添加的网卡* ?  v6 ]9 m! y+ h( F
virsh domiflist cos_v11 K. \% M8 f3 Z$ N& l
7 [" q- W5 C, _$ ~& V" P! X
7.在线添加光盘
' s0 K, Q5 n+ R! N1 ]$ _1 H& Q2 e5 ivirsh attach-disk centosbase /data_lij/iso/CentOS-6.4-x86_64-bin-DVD1.iso vdd
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

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

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

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

GMT+8, 2026-4-9 00:02 , Processed in 0.046127 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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