Bug 1309271

Summary: VM creation fails by defineXML(), if not using domain.undefine().
Product: [Community] Virtualization Tools Reporter: Robert Ozga <robert.ozga>
Component: libvirt-pythonAssignee: Libvirt Maintainers <libvirt-maint>
Status: CLOSED NOTABUG QA Contact: Virtualization Bugs <virt-bugs>
Severity: medium Docs Contact:
Priority: unspecified    
Version: unspecifiedCC: jdenemar, jtomko, robert.ozga
Target Milestone: ---   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-02-17 11:20:12 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description Robert Ozga 2016-02-17 10:58:03 UTC
Description of problem:


By redefine of Domain using the function "defineXML()" from libvirt-module in python, causes that Domain creation fails. 
Domain has current state "shutdown()". In documentation of defineXML() I read:

"...A previous definition for this domain would be overridden if it already exists. ..."

But this works only than, before using the function undefine().


Version-Release number of selected component (if applicable):
libvirt 1.2.2
QEMU 1.2.2
Hypervisor: QEMU 2.0.0


Steps to Reproduce:

1. Define Domain with libvirt.defineXML(xml)
2. Create VM using domain.create()
3. Using domain.shutdown() function
4. Redefine Domain with libvirt.defineXML(xml)
5. New creation using domain.create()

Actual results:
File "/usr/lib/python2.7/dist-packages/libvirt.py", line 3240, in defineXML
    if ret is None:raise libvirtError('virDomainDefineXML() failed', conn=self)

libvirtd.log
------------
2016-02-17 08:41:16.552+0000: 1658: error : virDomainObjListAddLocked:2335 : operation failed: domain 'ecmprt11' already exists with uuid 491cf4b7-7573-4f36-acb8-52dbd9be8e48

Expected results:

Creation of Domain with new definition.

Additional info:

It works for me perfectly, if using between step 3 and 4 additionaly domain.undefine().

Question:
Is it only documentation bug, or I  missunderstood the description of defineXML()?

Many thanks and best regards

Comment 2 Ján Tomko 2016-02-17 11:16:31 UTC
libvirt 1.2.2 was never released in RHEL, moving to the upstream tracker.

Comment 3 Jiri Denemark 2016-02-17 11:20:12 UTC
It all depends on the XMLs you passed to defineXML (and you didn't attach them).
But in general, redefining a domain is only supported if the name and UUID in the new XML matches the original definition. Beware, if you don't specify any UUID libvirt will automatically assign it to a domain. Thus trying to redefine a domain with an XML which lacks UUID will never work. You should either generate the UUID in your script and use it in both XMLs or fetch it from libvirt after step 1 and use this UUID in step 4.