yishen 发表于 2013-7-11 08:58:01

Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十二)

      Libvirt除了在虚拟机的定义文件中可以初始挂载光驱之外,还有一种动态换盘挂载的方式,那就是采用 virsh命令中的attach-device命令,该命令的具体格式如下:
dev@devhost:/opt/vm/xpvm1$ sudo virsh attach-device<domain-name> filename
其中,filename是一个用XML格式定义的文件(我们把它命名为disk.xml):
<disk type=”file” device=”cdrom”><source file=”/opt/vm/drivers.iso”/><target dev=”hdc”/><readonly/></disk>
最初虚拟机挂的光盘是windows_xp_professional_sp3_x86.iso:
<domain type='kvm'>   <name>XP_VM</name>   <uuid>91f15b08-e115-4016-a522-b550ff593af9</uuid>   <memory>1024000</memory>   <currentMemory>1024000</currentMemory><vcpu>1</vcpu><os>    <type arch='x86_64'machine='pc'>hvm</type>    <boot dev='hd'/>    <boot dev='cdrom'/>    <bootmenuenable='yes'/></os><features>    <acpi/>    <apic/>    <pae/></features><cpu>    <topology sockets='1'cores='1' threads='1'/></cpu><clockoffset='localtime'/>   <on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot>   <on_crash>restart</on_crash><devices>   <emulator>/usr/bin/qemu-system-x86_64</emulator>    <disk type='file'device='disk'>      <driver name='qemu'type='qcow2'/>      <sourcefile='/opt/vm/xpvm1/xp_c.img' lock='exclusive'/>      <target dev='hda'bus='ide'/>    </disk>    <disk type='file'device='disk'>      <driver name='qemu'type='qcow2'/>      <sourcefile='/opt/vm/xpvm1/xp_d.img' lock='exclusive'/>      <target dev='hdb' bus='ide'/>    </disk>   <disktype='file' device='cdrom'>      <source file='/opt/vm/windows_xp_professional_sp3_x86.iso'/>      <target dev='hdc'/>      <readonly/>    </disk>    <channeltype='spicevmc'>         <targettype='virtio' name='com.redhat.spice.0'/>         <aliasname='virserial-channel1'/>    </channel>    <interfacetype='bridge'>      <macaddress='52:54:00:7b:a8:d8'/>      <sourcebridge='virbr0'/>      <targetdev='vnet1'/>      <modeltype='virtio'/>    </interface>    <input type='tablet'bus='usb'/>    <graphicstype='spice' port='4000' autoport='no' listen='0.0.0.0'>      <listentype='address' address='0.0.0.0'/>      <agent_mousemode='off'/>    </graphics>    <memballoonmodel='virtio'>             <alias name='balloon0'/>    </memballoon>    <soundmodel='ac97'>      <address type='pci'domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>    </sound>    <video>      <model type='qxl'vram='65536' heads='1'/>    </video></devices><qemu:commandline>   <qemu:argvalue="-cpu"/>   <qemu:argvalue="kvm64"/></qemu:commandline></domain>
装完系统后,在虚拟机里看到的是: 我们可以不重新关闭、定义新的光盘文件,而直接采用 virsh attach-device命令来更改光盘:

dev@devhost:/opt/vm/xpvm1$ sudo virsh attach-deviceXP_VM disk.xml

这样做的好处是在某些情况下我们可以做到类似热插拔光盘的效果而不用重启虚拟机。注:需要说明的是,经过试验,首先必须在定义的XML里有个初始的光盘设备,否则执行attach-device 另一个光盘文件会失败:报的错误是: internal error No device with bus 'ide' and target 'hdc'.也就是说这其实是一种换盘,而不是动态挂载,光驱设备本身必须在启动虚拟机之前必须在XML中定义。

奥尔覅几22 发表于 2013-7-13 03:24:21

没看完~~~~~~ 先顶,好同志

liyao20060101 发表于 2013-7-14 23:44:13

漏洞与补丁齐飞,蓝屏共死机一色!

艾辉 发表于 2013-7-17 21:53:42

看尽天下A片,心中自然无码~

yzqzs 发表于 2013-7-20 08:36:49

站的更高,尿的更远。

gznz12345 发表于 2013-7-22 08:48:03

天塌下来你顶着,我垫着!

chinaab 发表于 2013-7-24 05:26:11

修养的艺术,其实就是说谎的艺术。
页: [1]
查看完整版本: Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十二)