From: ShaoHe Feng <shaohef(a)linux.vnet.ibm.com>
Libvirt support two API to set and get domain xml metadata.
These two function wrap them.
Put them in model/utils.py, so vm_storage and vm_iface can make use of
them.
For set domain xml metadata, we set live and config xml by default.
For get domain xml metadata, we get the current xml by default.
Signed-off-by: ShaoHe Feng <shaohef(a)linux.vnet.ibm.com>
Signed-off-by: Royce Lv <lvroyce(a)linux.vnet.ibm.com>
---
src/kimchi/i18n.py | 1 +
src/kimchi/model/utils.py | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index a1af1f7..e28f689 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -87,6 +87,7 @@ messages = {
"KCHVM0027E": _("User(s) '%(users)s' do not exist"),
"KCHVM0028E": _("Group(s) '%(groups)s' do not exist"),
"KCHVM0029E": _("Unable to shutdown virtual machine %(name)s. Details:
%(err)s"),
+ "KCHVM0030E": _("Unable to get access metadata of virtual machine
%(name)s. Details: %(err)s"),
"KCHVMIF0001E": _("Interface %(iface)s does not exist in virtual
machine %(name)s"),
"KCHVMIF0002E": _("Network %(network)s specified for virtual machine
%(name)s does not exist"),
diff --git a/src/kimchi/model/utils.py b/src/kimchi/model/utils.py
index 80e1801..9b94a06 100644
--- a/src/kimchi/model/utils.py
+++ b/src/kimchi/model/utils.py
@@ -18,6 +18,12 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from kimchi.exception import OperationFailed
+import libvirt
+from lxml import etree
+
+
+KIMCHI_META_URL = "https://github.com/kimchi-project/kimchi/metadata/"
+KIMCHI_NAMESPACE = "kimchi"
def get_vm_name(vm_name, t_name, name_list):
@@ -28,3 +34,35 @@ def get_vm_name(vm_name, t_name, name_list):
if vm_name not in name_list:
return vm_name
raise OperationFailed("KCHUTILS0003E")
+
+
+def get_vm_config_flag(dom, mode="persistent"):
+ # libvirt.VIR_DOMAIN_AFFECT_CURRENT is 0
+ # VIR_DOMAIN_AFFECT_LIVE is 1, VIR_DOMAIN_AFFECT_CONFIG is 2
+ flag = {"live": libvirt.VIR_DOMAIN_AFFECT_LIVE,
+ "persistent": libvirt.VIR_DOMAIN_AFFECT_CONFIG,
+ "current": libvirt.VIR_DOMAIN_AFFECT_CURRENT,
+ "all": libvirt.VIR_DOMAIN_AFFECT_CONFIG +
+ libvirt.VIR_DOMAIN_AFFECT_LIVE if dom.isActive() and
+ dom.isPersistent() else libvirt.VIR_DOMAIN_AFFECT_CURRENT}
+
+ return flag[mode]
+
+
+def set_metadata_node(dom, meta_xml, mode="all"):
+ element = etree.fromstring(meta_xml)
+ # From libvirt doc, Passing None for @metadata says to remove that element
+ # from the domain XML (passing the empty string leaves the element present)
+ # Do not support remove the old metadata
+ dom.setMetadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT, meta_xml,
+ KIMCHI_NAMESPACE, KIMCHI_META_URL + element.tag,
+ flags=get_vm_config_flag(dom, mode))
+
+
+def get_metadata_node(dom, element, mode="current"):
+ try:
+ return dom.metadata(libvirt.VIR_DOMAIN_METADATA_ELEMENT,
+ KIMCHI_META_URL + element,
+ flags=get_vm_config_flag(dom, mode))
+ except libvirt.libvirtError:
+ return ""
--
1.9.0