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

来源:互联网 发布:电视剧网络播放量查询 编辑:程序博客网 时间:2024/05/20 00:12

         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'/>

    <bootmenu  enable='yes'/>

  </os>

  <features>

    <acpi/>

    <apic/>

    <pae/>

  </features>

  <cpu>

    <topology sockets='1'  cores='1' threads='1'/>

  </cpu>

  <clock  offset='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'/>

      <source  file='/opt/vm/xpvm1/xp_c.img' lock='exclusive'/>

      <target dev='hda'  bus='ide'/>

    </disk>

    <disk type='file'  device='disk'>

      <driver name='qemu'  type='qcow2'/>

      <source  file='/opt/vm/xpvm1/xp_d.img' lock='exclusive'/>

      <target dev='hdb' bus='ide'/>

    </disk>

   <disk  type='file' device='cdrom'>

      <source file='/opt/vm/windows_xp_professional_sp3_x86.iso'/>

      <target dev='hdc'/>

      <readonly/>

    </disk>

    <channel  type='spicevmc'>

         <target  type='virtio' name='com.redhat.spice.0'/>

         <alias  name='virserial-channel1'/>

    </channel>

    <interface  type='bridge'>

      <mac  address='52:54:00:7b:a8:d8'/>

      <source  bridge='virbr0'/>

      <target  dev='vnet1'/>

      <model  type='virtio'/>

    </interface>

    <input type='tablet'  bus='usb'/>

    <graphics  type='spice' port='4000' autoport='no' listen='0.0.0.0'>

      <listen  type='address' address='0.0.0.0'/>

      <agent_mouse  mode='off'/>

    </graphics>

    <memballoon  model='virtio'>

             <alias name='balloon0'/>

    </memballoon>

    <sound  model='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:arg  value="-cpu"/>

     <qemu:arg  value="kvm64"/>

  </qemu:commandline>

</domain>

 

装完系统后,在虚拟机里看到的是:

 

我们可以不重新关闭、定义新的光盘文件,而直接采用 virsh attach-device命令来更改光盘:

 

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

 

  

这样做的好处是在某些情况下我们可以做到类似热插拔光盘的效果而不用重启虚拟机。

注:需要说明的是,经过试验,首先必须在定义的XML里有个初始的光盘设备,否则执行attach-device 另一个光盘文件会失败:报的错误是: internal error No device with bus 'ide' and target 'hdc'.

也就是说这其实是一种换盘,而不是动态挂载,光驱设备本身必须在启动虚拟机之前必须在XML中定义。

 

原创粉丝点击