研究libvirt存储部分,简单例子进行测试验证:
import libvirt conn=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()
输出结果:
virttest 1 <libvirt.virStorageVol instance at 0x7faa5fa55518> ['qcow2.img'] 1
删除存储卷、存储池
import libvirt conn=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_v print s_v.delete(0) print storage_pool.destroy() #停用 存储 print storage_pool.delete(0) # 删除 存储,删除前应保证存储池内没有vol了 print storage_pool.undefine() # 删除 存储定义