mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-28 08:35:22 +00:00
qemu: Refactor qemuDomainSaveHeader
The function is now called virQEMUSaveDataWrite and it is now doing everything it needs to save both the save image header and domain XML to a file. Be it a new file or an existing file in which a user wants to change the domain XML. Signed-off-by: Jiri Denemark <jdenemar@redhat.com> Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
parent
ec986bc572
commit
33ae270bee
@ -2832,7 +2832,7 @@ virQEMUSaveDataFree(virQEMUSaveHeaderPtr header)
|
|||||||
|
|
||||||
|
|
||||||
static virQEMUSaveHeaderPtr
|
static virQEMUSaveHeaderPtr
|
||||||
virQEMUSaveDataNew(char *domXML,
|
virQEMUSaveDataNew(char *domXML ATTRIBUTE_UNUSED,
|
||||||
bool running,
|
bool running,
|
||||||
int compressed)
|
int compressed)
|
||||||
{
|
{
|
||||||
@ -2845,34 +2845,66 @@ virQEMUSaveDataNew(char *domXML,
|
|||||||
header->version = QEMU_SAVE_VERSION;
|
header->version = QEMU_SAVE_VERSION;
|
||||||
header->was_running = running ? 1 : 0;
|
header->was_running = running ? 1 : 0;
|
||||||
header->compressed = compressed;
|
header->compressed = compressed;
|
||||||
header->data_len = strlen(domXML) + 1;
|
|
||||||
|
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* return -errno on failure, or 0 on success */
|
/* virQEMUSaveDataWrite:
|
||||||
|
*
|
||||||
|
* Writes libvirt's header (including domain XML) into a saved image of a
|
||||||
|
* running domain. If @header has data_len filled in (because it was previously
|
||||||
|
* read from the file), the function will make sure the new data will fit
|
||||||
|
* within data_len.
|
||||||
|
*
|
||||||
|
* Returns -1 on failure, or 0 on success.
|
||||||
|
*/
|
||||||
static int
|
static int
|
||||||
qemuDomainSaveHeader(int fd, const char *path, const char *xml,
|
virQEMUSaveDataWrite(virQEMUSaveHeaderPtr header,
|
||||||
virQEMUSaveHeaderPtr header)
|
const char *xml,
|
||||||
|
int fd,
|
||||||
|
const char *path)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
size_t len;
|
||||||
|
int ret = -1;
|
||||||
|
size_t zerosLen = 0;
|
||||||
|
char *zeros = NULL;
|
||||||
|
|
||||||
|
len = strlen(xml) + 1;
|
||||||
|
|
||||||
|
if (header->data_len > 0) {
|
||||||
|
if (len > header->data_len) {
|
||||||
|
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
||||||
|
_("new xml too large to fit in file"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
zerosLen = header->data_len - len;
|
||||||
|
if (VIR_ALLOC_N(zeros, zerosLen) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
} else {
|
||||||
|
header->data_len = len;
|
||||||
|
}
|
||||||
|
|
||||||
if (safewrite(fd, header, sizeof(*header)) != sizeof(*header)) {
|
if (safewrite(fd, header, sizeof(*header)) != sizeof(*header)) {
|
||||||
ret = -errno;
|
virReportSystemError(errno,
|
||||||
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
||||||
_("failed to write header to domain save file '%s'"),
|
_("failed to write header to domain save file '%s'"),
|
||||||
path);
|
path);
|
||||||
goto endjob;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (safewrite(fd, xml, header->data_len) != header->data_len) {
|
if (safewrite(fd, xml, header->data_len) != header->data_len ||
|
||||||
ret = -errno;
|
safewrite(fd, zeros, zerosLen) != zerosLen) {
|
||||||
virReportError(VIR_ERR_OPERATION_FAILED,
|
virReportSystemError(errno,
|
||||||
_("failed to write xml to '%s'"), path);
|
_("failed to write domain xml to '%s'"),
|
||||||
goto endjob;
|
path);
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
endjob:
|
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(zeros);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3146,8 +3178,7 @@ qemuDomainSaveMemory(virQEMUDriverPtr driver,
|
|||||||
if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags)))
|
if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
/* Write header to file, followed by XML */
|
if (virQEMUSaveDataWrite(header, domXML, fd, path) < 0)
|
||||||
if (qemuDomainSaveHeader(fd, path, domXML, header) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
/* Perform the migration */
|
/* Perform the migration */
|
||||||
@ -6626,7 +6657,6 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path,
|
|||||||
virDomainDefPtr newdef = NULL;
|
virDomainDefPtr newdef = NULL;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
char *xml = NULL;
|
char *xml = NULL;
|
||||||
size_t len;
|
|
||||||
virQEMUSaveHeaderPtr header = NULL;
|
virQEMUSaveHeaderPtr header = NULL;
|
||||||
int state = -1;
|
int state = -1;
|
||||||
|
|
||||||
@ -6662,30 +6692,22 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path,
|
|||||||
|
|
||||||
VIR_FREE(xml);
|
VIR_FREE(xml);
|
||||||
|
|
||||||
xml = qemuDomainDefFormatXML(driver, newdef,
|
if (!(xml = qemuDomainDefFormatXML(driver, newdef,
|
||||||
VIR_DOMAIN_XML_INACTIVE |
|
VIR_DOMAIN_XML_INACTIVE |
|
||||||
VIR_DOMAIN_XML_SECURE |
|
VIR_DOMAIN_XML_SECURE |
|
||||||
VIR_DOMAIN_XML_MIGRATABLE);
|
VIR_DOMAIN_XML_MIGRATABLE)))
|
||||||
if (!xml)
|
|
||||||
goto cleanup;
|
|
||||||
len = strlen(xml) + 1;
|
|
||||||
|
|
||||||
if (len > header->data_len) {
|
|
||||||
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
|
|
||||||
_("new xml too large to fit in file"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
if (VIR_EXPAND_N(xml, len, header->data_len - len) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (lseek(fd, 0, SEEK_SET) != 0) {
|
if (lseek(fd, 0, SEEK_SET) != 0) {
|
||||||
virReportSystemError(errno, _("cannot seek in '%s'"), path);
|
virReportSystemError(errno, _("cannot seek in '%s'"), path);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if (safewrite(fd, &header, sizeof(header)) != sizeof(header) ||
|
|
||||||
safewrite(fd, xml, len) != len ||
|
if (virQEMUSaveDataWrite(header, xml, fd, path) < 0)
|
||||||
VIR_CLOSE(fd) < 0) {
|
goto cleanup;
|
||||||
virReportSystemError(errno, _("failed to write xml to '%s'"), path);
|
|
||||||
|
if (VIR_CLOSE(fd) < 0) {
|
||||||
|
virReportSystemError(errno, _("failed to write header data to '%s'"), path);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user