主机重启后,用Libvirt制作的KVM无法持久保存 [英] KVM made with Libvirt not persisting after host restart
问题描述
我目前正在使用以下代码使用libvirt制作ubuntu KVM
I currently am making a ubuntu KVM with libvirt using the following code
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)
我遇到的问题是重新启动虚拟机后主机消失(我的笔记本电脑).
The problem I'm having is after I reboot the host machine (my laptop) the vm disapears.
.img文件仍位于/var/lib/libvirt/pool/
中,但是当我执行virsh list -all
The .img file is still in /var/lib/libvirt/pool/
but the vm doesn't show when I do a virsh list -all
配置xml是否缺少某些内容?关于需要先创建存储池,然后创建卷
Is there something missing on the config xml? I'm referencing this question in regards of the need to make the storage pool first, then the volume
推荐答案
不推荐使用... createLinux()
;您应该改用createXML()
.它采用相同的参数.
Let's start with... createLinux()
is deprecated; you should be using createXML()
instead. It takes identical arguments.
但是,createXML()
仅创建并启动瞬时虚拟机.要制作持久性VM,您需要调用defineXML()
.这将创建一个持久性VM,但不会启动它.准备好create()
即可自己启动它.
But, createXML()
only creates and starts transient VMs. To make a persistent VM, you need to call defineXML()
instead. This creates a persistent VM, but does not start it. You can start it yourself when ready with create()
.
这篇关于主机重启后,用Libvirt制作的KVM无法持久保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!