使用python libvirt 接口函数创建 dir 类型存储池、存储卷及删除 存储卷、存储池

来源:互联网 发布:巅峰霍华德身体数据 编辑:程序博客网 时间:2024/05/17 16:12

 研究libvirt存储部分,简单例子进行测试验证:

import libvirtconn=libvirt.open('qemu:///system')#conn=libvirt.open('qemu:///system')xmldesc='''<pool type="dir">  <name>virttest</name>  <target>    <path>/var/images_test</path>  </target></pool>'''#storage_pool.connect()storage_pool= conn.storagePoolDefineXML(xmldesc, 0)  # virStoragePool 类对象print storage_pool.name()print conn.isAlive()storage_pool.build(0) # 创建storage_pool.create(0) # 开启存储storage_pool.isActive()xmldesd_vol='''<volume type="file">  <name>qcow2.img</name>    <allocation unit="M">10</allocation>    <capacity unit="M">1000</capacity>  <target>    <format type="qcow2"/>  </target></volume>'''print storage_pool.createXML(xmldesd_vol,0) #virStorageVol 类对象print storage_pool.listVolumes()print storage_pool.isActive()


输出结果:


virttest1<libvirt.virStorageVol instance at 0x7faa5fa55518>['qcow2.img']1


删除存储卷、存储池

import libvirtconn=libvirt.open('qemu:///system')#conn=libvirt.open('qemu:///system')'''  删除 pool  vol'''storage_pool=conn.storagePoolLookupByName("virttest")#storage_pool.create(0) # 开启存储池s_v=storage_pool.storageVolLookupByName('qcow2.img')print s_vprint s_v.delete(0) print storage_pool.destroy() #停用 存储print storage_pool.delete(0) # 删除 存储,删除前应保证存储池内没有vol了print storage_pool.undefine() # 删除 存储定义


原创粉丝点击