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

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

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-9-10 15:00:04 | 显示全部楼层 |阅读模式
virsh 在线添加设备cdrom或者iso镜像
# }( X( f5 \: `* Kvirsh domblklist Windows2019_x86 * Q( @7 d  k: V& ~/ D$ n. x9 N! F$ {" z
Target   Source
% U$ z/ Y' s  q, ?----------------------------------------------------------------2 o+ S2 {. c# }  o
vda      /usr/src/Windows2019-x86.qcow2
( k# I( v- _8 E6 M7 Z- L* z sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso
$ B3 B& `8 b; F9 R% T7 t  Q
- P0 g( \6 y* g. N5 N
1 q8 K  v5 P; U3 t7 q0 l4 L( ~0 r
. z9 A% ^6 x! p; ~/ Svirsh attach-disk  Windows2019_x86 --target sdb   --type cdrom  /usr/src/virtio-win-0.1.225.iso  --mode readonly  % p. _  v  w+ D; E# G- U
error: Failed to attach disk5 U/ I. Y4 r5 r
error: Operation not supported: cdrom/floppy device hotplug isn't supported. ?0 I. O( S) l5 X0 a
$ A% X  |1 c, [
$ H6 x( E/ c2 z: Y9 U1 @' V" a) |

( d) S* i' w4 P! p. N% J8 H, }2 Vroot@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso sdb --cache none --subdriver cdrom --config --persistent : @, `- `- u: z9 N& z, j( L
error: Failed to attach disk! E0 v+ a$ a$ V7 R! M) P
error: unsupported configuration: unknown driver format value 'cdrom'" B+ H' D3 c7 l; n7 v
& X2 v# ~: N. L# b
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 `1 d- s, i& Q8 q+ L' Y
error: Failed to attach disk/ _( }& ~6 B# H3 m: t. K" x, ~
error: internal error: No more available PCI slots
2 B+ O' w/ Z4 O, L2 x9 R" q7 @: f1 k* e8 s: a
root@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver qcow2 --config --persistent - X7 g/ U3 M/ V* ?3 l3 m( u
error: Failed to attach disk6 R$ W( x' _- o# n" x5 C2 V& p! _: y" M
error: internal error: unable to execute QEMU command 'blockdev-add': Image is not in qcow2 format8 X5 Y8 _6 |' a1 q1 ^( C

8 Z: M% e+ J* w- j0 K3 zroot@compute01:/usr/src# virsh attach-disk  Windows2019_x86 /usr/src/virtio-win-0.1.225.iso vdc --cache none --subdriver raw --config --persistent 6 ]% K+ v9 H- I8 Y5 V+ I
Disk attached successfully
$ a. |! G- A& G3 o  S& x7 i; C+ I
2 D1 F$ K  t* L! T* E
5 T$ o+ j) f3 s' s$ \, a
4 H6 n) [) t& A2 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=cdrom$ Q8 V8 y; p! j; H7 Z1 G* V" O4 ~

8 Q; m( a" n3 \+ t1 KStarting install...% \+ F; K4 P: o  \. ~
ERROR    Cannot get interface MTU on 'br0': No such device7 x+ B/ s. V' B5 l* \
Domain installation does not appear to have been successful., @0 s9 e: \; }; T( }4 w% X
If it was, you can restart your domain by running:/ T$ E6 w( J8 Q/ }: p) Y. Z
  virsh --connect qemu:///system start Windows2019_x86+ U2 D8 F& [( M( A
otherwise, please restart your installation.
* u1 O) v; y0 d, J# Zroot@compute01:/usr/src# 0 d5 C6 A9 Z; {+ ^8 W! e3 H* c
root@compute01:/usr/src# % U8 v+ y9 ?; _2 u+ Z8 @0 G
root@compute01:/usr/src# netplan apply ( O6 R' j: R+ B( y5 e6 W

+ F+ j& J  d+ H4 d% T0 Q0 @& D** (generate:8799): WARNING **: 13:35:59.651: `gateway4` has been deprecated, use default routes instead./ M( y+ `6 I9 A) ^0 k4 r
See the 'Default routes' section of the documentation for more details./ ]1 i" P* U7 M$ D
8 n: H: a  R9 f; H3 p2 R) v
** (process:8797): WARNING **: 13:36:01.040: `gateway4` has been deprecated, use default routes instead.& o5 t( m) y9 C" O
See the 'Default routes' section of the documentation for more details.
, o4 W; d; J6 V' V0 ^# n6 J! q$ T$ R9 c  k0 j
** (process:8797): WARNING **: 13:36:01.052: `gateway4` has been deprecated, use default routes instead.1 P) G& n1 K6 s9 |( }! P  M$ M
See the 'Default routes' section of the documentation for more details.' h3 b2 r# v: ?/ Z
2 \7 A$ H7 {0 P9 T+ c) Z5 P  Y
** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.
4 M- x% Z& m; v" x3 bSee the 'Default routes' section of the documentation for more details.2 X! f! w6 @/ j* U1 ^. h+ R
. P5 ~# A6 u* }& B* U3 D
** (process:8797): WARNING **: 13:36:01.589: `gateway4` has been deprecated, use default routes instead.6 Z7 [1 z9 D7 e# ^
See the 'Default routes' section of the documentation for more details.
# c/ i& S) I$ d1 ^& Kroot@compute01:/usr/src# ; z; f' l- X" |
root@compute01:/usr/src#
9 h# x. {* f( _: {1 Hroot@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 X# W  |& ?/ x2 B8 x# {

/ d. A% r* }/ b$ jStarting install...
8 p9 m- v) [4 f/ o- ~+ P% SCreating domain...                                          |    0 B  00:00:00     
8 k3 r/ k3 s) {# p" {$ p
$ |4 X  k2 M0 i2 ~5 GDomain is still running. Installation may be in progress.5 u" c- n9 q& E, E5 R
You can reconnect to the console to complete the installation process.# @1 ~1 Z! _) F4 M, P# S
root@compute01:/usr/src#
. G- S1 z2 w* G( N7 {" n/ K
7 J: W. H8 ?  t8 ~* k0 h在线添加光盘" u' J  b, e" |3 y( u4 I6 X
virsh attach-disk Windows2019_x86 /usr/src/virtio-win-0.1.225.iso/ vdd
# m, O$ s& G9 U- h
; J( y. B* e7 w& ]" l: K其他方法:
! @8 C- i! T8 e: {9 C, F. b/ \- U5 Q1 w' z
) _# U! }, n" c/ f: }
再次使用 virsh dumpxml sles12-sp3 可以看到iso文件加载:  y5 y3 J* l$ W' P" X% y

+ V9 g! e# z; I4 R0 c* N7 \<disk type='file' device='cdrom'>
2 U9 o; m6 V2 u/ u, F  <driver name='qemu' type='raw'/>0 w- g' W7 k+ P& ?
  <source file='/var/lib/libvirt/images/SLE-12-SP3-Server-DVD-x86_64-GM-DVD1.iso' index='3'/>
! V% l! b& g. ^- u# m# Z/ l# w  <backingStore/>
. a2 Y$ `. J% T, c  <target dev='sda' bus='sata'/>
1 c& G- Q" @# f8 m4 b  <readonly/>
7 n6 x. w: d0 Y  }  <alias name='sata0-0-0'/>) r; k, i& G% e3 A* t
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>: e9 v2 O$ V- [0 H  F7 k1 v6 [; z
</disk>
' m' \: J. \; x( Y如果要卸载这个iso文件,则创建一个相同结构的xml文件 detach_iso.xml ,但是保持 <source/> 行删除:- K: t: w* B4 Z# `# ^* t! _$ A
7 C" F' v8 U- ^* f- e8 B! a+ [
<disk type='file' device='cdrom'>7 ?$ o" w- \# y- |5 K  M- g+ v9 x
  <driver name='qemu' type='raw'/>& S2 |, \4 B2 q2 N* _
  <backingStore/>) P5 b' R/ r6 w- Y  o
  <target dev='sda' bus='sata'/>/ @/ b2 Z- S3 [
  <readonly/>
  Q9 r0 ^$ t& w7 y  <alias name='sata0-0-0'/>: ]3 H8 c% w0 K2 v9 \& Q* Q
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
$ S7 o. y* K) }  d& s6 L7 Z, W: o</disk>; k+ O+ q7 I7 ^9 D/ D* Q
然后执行设备更新:0 Z% |& C0 C" c' _0 W

. y# B4 Z+ x. a0 S5 S' fvirsh update-device sles12-sp3 detach_iso.xml
$ H& D" t- L1 y. c此时提示:
$ z' F& _7 h, D! p$ n9 g  [7 Q" v! M  ^7 j* e
Device updated successfully, Y3 f+ |! P9 C0 w. F) b, I% N
再检查虚拟机配置,就看到iso文件已经卸载了。( P# E- U& X/ E5 R
; r! T# `' w& z, S* \) u/ n1 k- s

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时,添加对应的多个设备即可。* \8 c) A# e0 d, \' e

# y/ e; S# C6 p/ a6 {. bvirsh domblklist Windows2019_x86 # K4 G$ Y( y. {! b0 X
Target   Source
) F/ G5 r' l) h9 B1 E+ @/ D1 k; s! K----------------------------------------------------------------+ F! L6 l- }8 w( _7 e
vda      /usr/src/Windows2019-x86.qcow2
/ T7 G% }8 z& V- Z sda      /usr/src/cn_windows_server_2019_x64_dvd_2d80e042.iso" O! ]3 ~& k$ H- G( \
sdb      /usr/src/virtio-win-0.1.225.iso
- i; K; F+ i  r8 Q9 Y* @& y" b, b: t- Z5 v9 f

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-9-10 15:00:06 | 显示全部楼层
2.在线调整虚拟机内存(增大或减小)! f) g! `' Y; p
#调整为4G! A5 B5 ]# E- _6 P( J9 \* x1 H
virsh setmem cos_v1 4G
9 T5 n0 E$ V6 @0 F! H4 ?5 n& y7 n8 Y
#调整为2G
/ U: A5 W4 `* U  i% U7 y- ^virsh setmem cos_v1 2G/ ?8 P; F: M" T4 X
#能够在线调整的最大内存不能超过为虚拟机分配的最大内存(上面xml文件中设置的最大为4G),否则需要关闭虚拟机上调最大内存9 q$ ~7 Q5 K. P, m" \( v* n7 a
; T" K* B' q( p- Q9 p6 C0 G
5 q& Z5 M1 C4 U9 ~, c) W
3.在线调整虚拟机CPU(只能增大,不能减小)5 ^4 F) e4 H4 m
virsh setvcpus cos_v1 4
9 M+ ?' k# h3 q" h) Cvirsh setvcpus cos_v1 8" w) v5 p  ~. b; H
#同样,能够动态调整的最大VCPU个数也不能超过为虚拟机设置的最大VCPU数量4 |7 n& D' O$ A, j/ h. `! u5 i
) ~1 |2 W; Y7 b- S, d2 ?; Q% U  I, q

* ~6 u0 |3 |, _4.在线添加硬盘6 ~! U& s$ ^# h( X6 m# ]
#添加qcow2格式硬盘, @0 M" Q, C4 B7 \+ n
( y' |" i6 x# c! \0 w, r5 i- U# x
#创建qcow2格式的新磁盘,大小为40G
/ M+ }3 x6 m. I& H, gqemu-img create -f qcow2 /data/vhosts/test/cos_v1-add1.disk 40G
* R& T0 i# w$ L* P: gvirsh attach-disk cos_v1 /data/vhosts/test/cos_v1-add1.disk vdb --cache none --subdriver qcow2 --config --persistent/ ?0 p' C1 ], e/ V0 K3 {
#虚拟机根磁盘为vda,因此这里使用vdb表示新添加磁盘
% u/ |# z& L& b2 l  k$ k#--config 参数同时更新虚拟机xml文件,确保重启后依然生效  z0 M- D3 _1 B9 g: e) h0 w
#添加raw格式硬盘
7 d7 n4 I5 O, O( t- A  b5 S2 p#创建raw格式的新磁盘,大小为40G4 |2 `  t4 O3 O0 x
qemu-img create -f raw /data/vhosts/test/cos_v1-add2.disk 40G  T' t3 y6 P% H* I2 W
virsh attach-disk lnmptest-107 /data/vhosts/test/cos_v1-add2.disk vdc --cache none --subdriver raw --config --persistent" ?( |2 x2 T- `/ L6 K+ i+ |- I
. e; p2 J) H- L& F% U
5.在线移除硬盘
) f! e; F4 E* _; f#可以查看虚拟机所有磁盘3 t. X2 X; ?( `2 X
virsh domblklist cos_v1" `6 C3 p5 m; q8 q* s
virsh detach-disk cos_v1 vdb/ B' h( C/ k. J, \1 h$ u5 N
# M6 m& ]3 N1 ]3 R& _8 i) p
6.在线添加网卡
; P1 m0 l+ G$ cvirsh attach-interface --domain cos_v1 --type network --source default --model virtio --config
% h2 I' E( w' C* z1 v! x#可以查看新添加的网卡
& I% a+ ]3 o2 d' d0 O( yvirsh domiflist cos_v17 C# q4 U% M5 e3 h! `/ I7 s( i6 Y. v5 v

  F) C7 N, Y1 l! `0 S$ c1 W7.在线添加光盘
) P' ^$ y9 j& e6 Y, g5 _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:05 , Processed in 0.024634 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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