Move libxlSaveImageOpen from libxl_driver to libxl_domain for use by other libxl modules. For consistency, rename to libxlDomainSaveImageOpen. Signed-off-by: Jim Fehlig <jfehlig@xxxxxxxx> --- src/libxl/libxl_domain.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ src/libxl/libxl_domain.h | 8 +++++ src/libxl/libxl_driver.c | 80 +++--------------------------------------------- 3 files changed, 88 insertions(+), 76 deletions(-) diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c index b83d1fa..109a4e2 100644 --- a/src/libxl/libxl_domain.c +++ b/src/libxl/libxl_domain.c @@ -23,6 +23,8 @@ #include <config.h> +#include <fcntl.h> + #include "libxl_domain.h" #include "viralloc.h" @@ -555,3 +557,77 @@ libxlDomainManagedSavePath(libxlDriverPrivatePtr driver, virDomainObjPtr vm) { virObjectUnref(cfg); return ret; } + +/* + * Open a saved image file and initialize domain definition from the header. + * + * Returns the opened fd on success, -1 on failure. + */ +int +libxlDomainSaveImageOpen(libxlDriverPrivatePtr driver, + libxlDriverConfigPtr cfg, + const char *from, + virDomainDefPtr *ret_def, + libxlSavefileHeaderPtr ret_hdr) +{ + int fd; + virDomainDefPtr def = NULL; + libxlSavefileHeader hdr; + char *xml = NULL; + + if ((fd = virFileOpenAs(from, O_RDONLY, 0, -1, -1, 0)) < 0) { + virReportSystemError(-fd, + _("Failed to open domain image file '%s'"), from); + goto error; + } + + if (saferead(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { + virReportError(VIR_ERR_OPERATION_FAILED, + "%s", _("failed to read libxl header")); + goto error; + } + + if (memcmp(hdr.magic, LIBXL_SAVE_MAGIC, sizeof(hdr.magic))) { + virReportError(VIR_ERR_INVALID_ARG, "%s", _("image magic is incorrect")); + goto error; + } + + if (hdr.version > LIBXL_SAVE_VERSION) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("image version is not supported (%d > %d)"), + hdr.version, LIBXL_SAVE_VERSION); + goto error; + } + + if (hdr.xmlLen <= 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("invalid XML length: %d"), hdr.xmlLen); + goto error; + } + + if (VIR_ALLOC_N(xml, hdr.xmlLen) < 0) + goto error; + + if (saferead(fd, xml, hdr.xmlLen) != hdr.xmlLen) { + virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("failed to read XML")); + goto error; + } + + if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt, + 1 << VIR_DOMAIN_VIRT_XEN, + VIR_DOMAIN_XML_INACTIVE))) + goto error; + + VIR_FREE(xml); + + *ret_def = def; + *ret_hdr = hdr; + + return fd; + +error: + VIR_FREE(xml); + virDomainDefFree(def); + VIR_FORCE_CLOSE(fd); + return -1; +} diff --git a/src/libxl/libxl_domain.h b/src/libxl/libxl_domain.h index 5558009..3c1f5c0 100644 --- a/src/libxl/libxl_domain.h +++ b/src/libxl/libxl_domain.h @@ -99,4 +99,12 @@ char * libxlDomainManagedSavePath(libxlDriverPrivatePtr driver, virDomainObjPtr vm); +int +libxlDomainSaveImageOpen(libxlDriverPrivatePtr driver, + libxlDriverConfigPtr cfg, + const char *from, + virDomainDefPtr *ret_def, + libxlSavefileHeaderPtr ret_hdr) + ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5); + #endif /* LIBXL_DOMAIN_H */ diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index ef623d5..a2e982c 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -161,78 +161,6 @@ cleanup: return ret; } -/* - * This internal function expects the driver lock to already be held on - * entry. - */ -static int ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) -libxlSaveImageOpen(libxlDriverPrivatePtr driver, - libxlDriverConfigPtr cfg, - const char *from, - virDomainDefPtr *ret_def, - libxlSavefileHeaderPtr ret_hdr) -{ - int fd; - virDomainDefPtr def = NULL; - libxlSavefileHeader hdr; - char *xml = NULL; - - if ((fd = virFileOpenAs(from, O_RDONLY, 0, -1, -1, 0)) < 0) { - virReportSystemError(-fd, - _("Failed to open domain image file '%s'"), from); - goto error; - } - - if (saferead(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { - virReportError(VIR_ERR_OPERATION_FAILED, - "%s", _("failed to read libxl header")); - goto error; - } - - if (memcmp(hdr.magic, LIBXL_SAVE_MAGIC, sizeof(hdr.magic))) { - virReportError(VIR_ERR_INVALID_ARG, "%s", _("image magic is incorrect")); - goto error; - } - - if (hdr.version > LIBXL_SAVE_VERSION) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("image version is not supported (%d > %d)"), - hdr.version, LIBXL_SAVE_VERSION); - goto error; - } - - if (hdr.xmlLen <= 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("invalid XML length: %d"), hdr.xmlLen); - goto error; - } - - if (VIR_ALLOC_N(xml, hdr.xmlLen) < 0) - goto error; - - if (saferead(fd, xml, hdr.xmlLen) != hdr.xmlLen) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("failed to read XML")); - goto error; - } - - if (!(def = virDomainDefParseString(xml, cfg->caps, driver->xmlopt, - 1 << VIR_DOMAIN_VIRT_XEN, - VIR_DOMAIN_XML_INACTIVE))) - goto error; - - VIR_FREE(xml); - - *ret_def = def; - *ret_hdr = hdr; - - return fd; - -error: - VIR_FREE(xml); - virDomainDefFree(def); - VIR_FORCE_CLOSE(fd); - return -1; -} /* * Core dump domain to default dump path. @@ -700,9 +628,9 @@ libxlVmStart(libxlDriverPrivatePtr driver, virDomainObjPtr vm, if (virFileExists(managed_save_path)) { - managed_save_fd = libxlSaveImageOpen(driver, cfg, - managed_save_path, - &def, &hdr); + managed_save_fd = libxlDomainSaveImageOpen(driver, cfg, + managed_save_path, + &def, &hdr); if (managed_save_fd < 0) goto endjob; @@ -2100,7 +2028,7 @@ libxlDomainRestoreFlags(virConnectPtr conn, const char *from, return -1; } - fd = libxlSaveImageOpen(driver, cfg, from, &def, &hdr); + fd = libxlDomainSaveImageOpen(driver, cfg, from, &def, &hdr); if (fd < 0) goto cleanup_unlock; -- 1.8.1.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list