Python 使用Libvirt创建的KVM在主机重新启动后未持久化

Python 使用Libvirt创建的KVM在主机重新启动后未持久化,python,ubuntu,kvm,libvirt,Python,Ubuntu,Kvm,Libvirt,我目前正在使用以下代码制作一个带有libvirt的ubuntukvm import libvirt conn = libvirt.open('qemu:///system') pool_name = 'VMPOOL' name = 'ubuntu0' pools = conn.listAllStoragePools(0) for pool in pools: # check if pool is active print(pool.isActive()) if po

我目前正在使用以下代码制作一个带有libvirt的ubuntukvm

import libvirt

conn = libvirt.open('qemu:///system')
pool_name = 'VMPOOL'
name = 'ubuntu0'

pools = conn.listAllStoragePools(0)

for pool in pools:
    # check if pool is active
    print(pool.isActive())
    if pool.isActive() == 0:
        #activate pool
        pool.create()

    stgvols = pool.listVolumes()
    print('Storage pool: '+pool.name())
    for stgvol in stgvols :
        print('  Storage vol: '+stgvol)


def createStoragePool(conn, pool_name):
    xmlDesc = """
    <pool type='dir'>
      <name>""" + pool_name + """</name>
      <capacity unit="G">10</capacity>
      <allocation unit='bytes'>237457858</allocation>
      <available unit='bytes'>4069322956</available>
      <source>
      </source>
      <target>
        <path>/var/lib/libvirt/pool</path>
        <format type='qcow2'/>
        <permissions>
          <mode>0755</mode>
          <owner>-1</owner>
          <group>-1</group>
        </permissions>
      </target>
    </pool>"""
    pool = conn.storagePoolDefineXML(xmlDesc, 0)

    # set storage pool autostart
    pool.setAutostart(1)
    print(pool.name(), 'pool name in create')
    return pool


for pool in pools:
    # check if pool is active
    print(pool.isActive())
    if pool.isActive() == 0:
        #activate pool
        pool.create()

    stgvols = pool.listVolumes()
    print('Storage pool: '+pool.name())
    for stgvol in stgvols :
        print('  Storage vol: '+stgvol)

def createStoragePoolVolume(pool, name):
    stpVolXml = """
    <volume>
      <name>""" + name + """.img</name>
      <allocation>0</allocation>
      <capacity unit="G">10</capacity>
      <target>
        <path>/var/lib/libvirt/pool/""" + name + """.img</path>
        <permissions>
          <owner>107</owner>
          <group>107</group>
          <mode>0744</mode>
          <label>virt_image_t</label>
        </permissions>
      </target>
    </volume>"""
    stpVol = pool.createXML(stpVolXml, 0)
    return stpVol


def deleteVolStoragePool(conn, name):
    volume = conn.storageVolLookupByPath('/var/lib/libvirt/pool/%s.img' % name)
    volume.wipe()
    volume.delete()
    return True

##make kvm via xml
def makeKvm(name, conn):
    xmldesc = """
    <domain type="kvm">
    <name>""" + name + """</name>
    <memory unit='GB'>1</memory>
    <vcpu>1</vcpu>
    <os>
        <type arch='x86_64' machine='pc'>hvm</type>
        <boot dev='cdrom'/>
    </os>
    <iothreads>1</iothreads>
    <on_poweroff>destroy</on_poweroff>
    <on_reboot>restart</on_reboot>
    <on_crash>preserve</on_crash>
    <devices>
        <emulator>/usr/bin/qemu-system-x86_64</emulator>
        <disk type='file' device='disk'>
          <driver name='qemu' type='raw'/>
          <source file='/var/lib/libvirt/pool/""" + name + """.img'/>
          <target dev='vda' bus='virtio'/>
        </disk>
        <disk type='file' device='cdrom'>
          <driver name='qemu' type='raw'/>
          <source file='/var/lib/libvirt/iso/ubuntu-16.04.3-desktop-amd64.iso'/>
          <target dev='hdb' bus='ide'/>
        <readonly/>
        </disk>
        <interface type='bridge'>
          <source bridge='br0'/>
          <model type='virtio'/>
        </interface>
        <input type='mouse' bus='ps2'/>
        <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'/>
      </devices>
    </domain>
    """
    dom = conn.createLinux(xmldesc, 0)
    return dom


try:
    pool = conn.storagePoolLookupByName(pool_name)
except:
    pool = createStoragePool(conn, pool_name)

createStoragePoolVolume(pool, name)
makeKvm(name, conn)
导入libvirt conn=libvirt.open('qemu:///system') 池名称='VMPOOL' 名称='ubuntu0' pools=conn.listAllStoragePools(0) 对于池中的池: #检查池是否处于活动状态 打印(pool.isActive()) 如果pool.isActive()==0: #激活池 pool.create() stgvols=pool.listVolumes() 打印('存储池:'+pool.name()) 对于stgvol中的stgvol: 打印(‘存储卷:’+stgvol) def createStoragePool(连接,池名称): xmlDesc=“” “+pool\u name+” 10
关于首先创建存储池的需要,那么卷

让我们从…
createLinux()
开始,它已被弃用;您应该改用
createXML()
。它采用相同的参数


但是,
createXML()
只创建和启动临时VM。要创建持久VM,您需要调用
defineXML()
。这会创建一个持久VM,但不会启动它。您可以在准备好
create()时自己启动它

谢谢!我想我应该再问一个问题,但是有什么我必须做的吗?当我安装ubuntu时,它从未安装过,它只要求
删除安装介质
,然后按enter键`@nadermx我不知道。它似乎与这个问题也没有关系。