代码之家  ›  专栏  ›  技术社区  ›  nadermx

主机重新启动后,使用Libvirt生成的KVM未持久化

  •  1
  • nadermx  · 技术社区  · 8 年前

    我目前正在使用以下代码使用libvirt制作ubuntu KVM

    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)
    

    我遇到的问题是,在我重新启动主机(笔记本电脑)后,vm就会消失。

    这个img文件仍在 /var/lib/libvirt/pool/ 但当我执行 virsh list -all

    配置xml上是否缺少某些内容?我在引用 this question 考虑到需要先创建存储池,然后创建卷

    1 回复  |  直到 8 年前
        1
  •  1
  •   Michael Hampton    8 年前

    让我们从。。。 createLinux() 已弃用;您应该使用 createXML() 相反它采用相同的参数。

    但是 createXML() 仅创建和启动 转瞬即逝的 虚拟机。要创建持久化VM,需要调用 defineXML() 相反这会创建一个持久的VM,但不会启动它。准备好后,您可以自己开始 create() .