找回密码
 注册
查看: 411|回复: 4

virsh kvm虚机动态添加磁盘等设备

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-9-10 17:00:01 | 显示全部楼层 |阅读模式
动态挂载硬盘
! y7 \  p0 y  s  g" P; L! p; ^
" O- s; ]# Y; g9 W3 J" zvirsh list
9 C/ _" U. f5 Z6 x% Q% k. h3 S; J9 Q Id   Name              State
6 M! R/ G' I" _" ]7 u0 t8 z2 ^---------------------------------4 ?% G8 u% Y9 ^. u8 v; Y7 K. `  e
6 Z5 e- j* _5 c9 j# v; b  {
9    Windows2019_x86   running+ l, y3 _$ M0 d
% ?" r6 u' t& U7 G# f$ M% b
/ T9 L$ ]/ R. w

+ s. M7 ~; U& p3 |# E0 T编辑挂在硬盘xml( Z% v& n3 m$ O, R' z9 u$ s) ^5 m+ I
vim attach.xml 9 r8 P8 x8 g/ ]+ \2 h& X' v
<disk type='file' device='disk'>
# a; l* k% e/ N, g1 C6 `  <driver name='qemu' type='qcow2'/>
9 L2 c* P) |% [4 {& x1 J# W4 u  <source file='/usr/src/CloudbaseInitSetup_1_1_2_x86.msi.iso'/>
+ e1 y, P' T4 s. ?$ Y  <target dev='sdc' bus='virtio'/>' r  \0 ]0 b) q
</disk>
: w7 t" Q" b# m" Q& f! B) j1 _1 O$ v0 t' o$ x
$ L0 s/ n0 c  W0 o  _
使用命令挂载
( {$ Q, [; B8 E5 F5 @' \
8 l5 c! u- o8 v5 xsudo virsh attach-device Windows2019_x86 attach.xml
7 f& w$ @, q* s+ n/ B; X* y1 p9 T6 C# v  y' `. |& _

$ ]& V  @1 B& [5 T" J5 W' B- w) W5 p6 n' g4 m
virsh attach-device Windows2019_x86 attach.xml
+ e5 d% q6 m1 x) z3 L  r* Yerror: Failed to attach device from attach.xml
$ o* H: l) h6 S. \7 y# t5 q" eerror: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format, B7 g9 D: `+ ~

5 Z3 m9 J7 a5 ^3 L' m2 M$ G
- e8 a: _* x+ c& V3 t; [修改
# d! ]6 g1 m3 Tvim attach.xml 9 o# u$ A2 D8 O, e7 w9 R
<disk type='file' device='disk'>% s2 B' w% y% g/ ?1 c) [- [) x
  <driver name='qemu' type='raw'/>9 R1 B8 {9 F6 A! A9 j+ A: F
  <source file='/usr/src/CloudbaseInitSetup_1_1_2_x86.msi.iso'/>
/ d9 L& n9 @) R- K4 @! L  <target dev='sdc' bus='virtio'/>
" Z! P: o4 Y2 W! w' `</disk>6 ]8 [+ U2 N5 r4 `( i4 |0 `+ @3 u

) a% _$ v% b/ s! o* M
" R" F& p  W# ^8 \/ `* sroot@compute01:/usr/src# virsh attach-device Windows2019_x86 attach.xml 5 m5 _$ i7 o7 `0 p9 L5 ~
Device attached successfully/ o7 D( p) z  K6 V
( H; w4 g: K$ x& U  v5 k6 a& Z$ O
为了马上识别到文件信息:* U: m! |6 R$ z6 i; P0 c$ A' o
, M/ y9 Z9 ?4 S8 S$ F
<disk type='file' device='cdrom'>% G3 B  G0 h% u
   <driver name='qemu' type='raw'/>* c# \% s) k; I8 m8 O5 _# K/ x
   <source file='/usr/src/CloudbaseInitSetup_1_1_2_x86.msi.iso'/>0 U$ I! V( V! F
   <target dev='sdc' bus='sata'/>
' _( x/ V& t' _4 Q9 u   <readonly/>8 ~) D! g% p& {, X( p5 A  r2 \5 q
   <address type='drive' controller='0' bus='0' target='0' unit='1'/>
! r/ q+ K% W# c$ A$ k. I </disk># u2 A  [% U6 s

/ w! c0 `: a' K. n0 k% R' C) r- I6 {: o$ B1 r

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 17:00:02 | 显示全部楼层
virsh detach-device Windows2019_x86 attach.xml
9 q% ^. L. N% c( U! a" L  \0 O# xDevice detached successfully2 o0 e. U6 v2 ]/ [
) N4 ~2 k# z  O9 O" ]! S& F& N: F
<disk type='file' device='cdrom'>0 m' |, q5 {- D) U- b
   <driver name='qemu' type='raw'/>5 ~9 m& |) b% e8 o6 \% |
   <source file='/usr/src/CloudbaseInitSetup_1_1_2_x86.msi.iso'/>1 Q: ?7 }. T& ]3 R- E) r: m0 ]' b
   <target dev='sdc' bus='sata'/>
. L* U7 S: C+ ^* i) t) _0 U   <readonly/>5 r! X# t4 A' G# p. C7 z2 u
   <address type='drive' controller='0' bus='0' target='0' unit='1'/>7 |$ Q0 ~2 P6 \9 b
</disk>0 G( N' n8 r+ P5 D8 r! ^

2 n( {" k8 K9 x% t3 `! j

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 17:00:03 | 显示全部楼层
virsh attach-device Windows2019_x86 attach.xml ( Y, J1 B5 ~3 H- y- ?4 v- r( }
error: Failed to attach device from attach.xml* n1 A  l" f7 F8 Z: t
error: unsupported configuration: readonly sata disks are not supported/ |0 a- Z/ T6 Y- E

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 17:00:04 | 显示全部楼层
root@compute01:~# virsh change-media Windows2019_x86 sdb --eject
. j. c( ^4 m( U% C8 b8 S5 @9 Y2 GSuccessfully ejected media.
/ d' j, C8 K% D7 Vroot@compute01:~# virsh change-media Windows2019_x86 sdb --
0 H2 n3 g: y! Y% r- B: \. `5 E--block      --config     --current    --eject      --force      --insert     --live       --print-xml  --source     --update     
! @* U8 x1 l7 ?! l! Qroot@compute01:~# virsh change-media Windows2019_x86 sdb --' J, C/ N2 I0 |) m" l$ ?% c
--block      --config     --current    --eject      --force      --insert     --live       --print-xml  --source     --update     $ D: `5 g. W+ O" {8 z  ?
root@compute01:~# virsh change-media Windows2019_x86 sdb --insert /usr/src/CloudbaseInitSetup_1_1_2_x86.msi.iso
$ e9 B# }: s0 B- DSuccessfully inserted media.
. W, x# u/ t- c8 }; q% c1 Eroot@compute01:~# ) U( C, L& n7 U) V# O5 ^4 B
! [0 `4 e, P% R. \
终于用命令解决了。

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 17:00:05 | 显示全部楼层
root@compute01:/usr/src# virsh change-media Windows2019_x86 sdb --insert /usr/src/CloudbaseInitSetup.iso + i' F+ A* b1 `+ l/ q2 u( t$ D
error: The disk device 'sdb' already has media
/ W' A' c4 o! Z, `* W) S/ a. E+ I2 _5 i2 Q, n' r4 C
root@compute01:/usr/src# virsh change-media Windows2019_x86 sdb --eject 9 {% ~0 ?" r- S" s. }  [
Successfully ejected media.
' K+ b" E9 |! {3 `% {0 u, Aroot@compute01:/usr/src# virsh change-media Windows2019_x86 sdb --insert /usr/src/CloudbaseInitSetup.iso $ }$ c1 S) t. N
Successfully inserted media.
+ j3 ]. T& ?0 U6 c* F# C$ @* y
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 00:48 , Processed in 0.015537 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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