Base64 encode input file id in qemuDomainSaveFlags(), qemuDomainRestoreFlags(), qemuDomainSaveImageGetXMLDesc(), qemuDomainSaveImageDefineXML(), qemudDomainCoreDump(), and put generated files in controlled directories: qemu_driver->savedImageDir, and qemu_driver->coreDumpDir. * src/qemu/qemu_conf.h: add 2 fields to struct qemud_driver * src/qemu/qemu_driver.c: base64 encoding file id and id->path translation Signed-off-by: Hong Xiang <hxiang@xxxxxxxxxxxxxxxxxx> --- src/qemu/qemu_conf.h | 3 + src/qemu/qemu_driver.c | 98 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index ff5cf23..f7f12da 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -138,6 +138,9 @@ struct qemud_driver { * of guests which will be automatically killed * when the virConnectPtr is closed*/ virHashTablePtr autodestroy; + + char *savedImageDir; + char *coreDumpDir; }; typedef struct _qemuDomainCmdlineDef qemuDomainCmdlineDef; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 6b65716..36d6284 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -472,6 +472,12 @@ qemudStartup(int privileged) { if (virAsprintf(&qemu_driver->autoDumpPath, "%s/lib/libvirt/qemu/dump", LOCALSTATEDIR) == -1) goto out_of_memory; + if (virAsprintf(&qemu_driver->savedImageDir, + "%s/lib/libvirt/qemu/saved-image", LOCALSTATEDIR) == -1) + goto out_of_memory; + if (virAsprintf(&qemu_driver->coreDumpDir, + "%s/lib/libvirt/qemu/core-dump", LOCALSTATEDIR) == -1) + goto out_of_memory; } else { uid_t uid = geteuid(); char *userdir = virGetUserDirectory(uid); @@ -502,6 +508,10 @@ qemudStartup(int privileged) { goto out_of_memory; if (virAsprintf(&qemu_driver->autoDumpPath, "%s/qemu/dump", base) == -1) goto out_of_memory; + if (virAsprintf(&qemu_driver->savedImageDir, "%s/qemu/saved-image", base) == -1) + goto out_of_memory; + if (virAsprintf(&qemu_driver->coreDumpDir, "%s/qemu/core-dump", base) == -1) + goto out_of_memory; } if (virFileMakePath(qemu_driver->stateDir) < 0) { @@ -540,6 +550,18 @@ qemudStartup(int privileged) { qemu_driver->autoDumpPath, virStrerror(errno, ebuf, sizeof ebuf)); goto error; } + if (virFileMakePath(qemu_driver->savedImageDir) < 0) { + char ebuf[1024]; + VIR_ERROR(_("Failed to create saved-image dir '%s': %s"), + qemu_driver->savedImageDir, virStrerror(errno, ebuf, sizeof ebuf)); + goto error; + } + if (virFileMakePath(qemu_driver->coreDumpDir) < 0) { + char ebuf[1024]; + VIR_ERROR(_("Failed to create core-dump dir '%s': %s"), + qemu_driver->coreDumpDir, virStrerror(errno, ebuf, sizeof ebuf)); + goto error; + } /* Configuration paths are either ~/.libvirt/qemu/... (session) or * /etc/libvirt/qemu/... (system). @@ -790,6 +812,8 @@ qemudShutdown(void) { VIR_FREE(qemu_driver->snapshotDir); VIR_FREE(qemu_driver->qemuImgBinary); VIR_FREE(qemu_driver->autoDumpPath); + VIR_FREE(qemu_driver->savedImageDir); + VIR_FREE(qemu_driver->coreDumpDir); VIR_FREE(qemu_driver->vncTLSx509certdir); VIR_FREE(qemu_driver->vncListen); VIR_FREE(qemu_driver->vncPassword); @@ -2648,14 +2672,34 @@ static bool qemudCompressProgramAvailable(enum qemud_save_formats compress) return true; } +static char * +qemuSavedImagePath(struct qemud_driver *driver, const char *file) +{ + char *enc_file, *ret; + + if(0 > virBase64EncodePathname(file, &enc_file)) { + virReportOOMError(); + return NULL; + } + if (virAsprintf(&ret, "%s/%s", driver->savedImageDir, enc_file) < 0) { + virReportOOMError(); + VIR_FREE(enc_file); + return NULL; + } + VIR_FREE(enc_file); + + return(ret); +} + static int -qemuDomainSaveFlags(virDomainPtr dom, const char *path, const char *dxml, +qemuDomainSaveFlags(virDomainPtr dom, const char *file, const char *dxml, unsigned int flags) { struct qemud_driver *driver = dom->conn->privateData; int compressed; int ret = -1; virDomainObjPtr vm = NULL; + char *path = NULL; virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE | VIR_DOMAIN_SAVE_RUNNING | @@ -2696,11 +2740,15 @@ qemuDomainSaveFlags(virDomainPtr dom, const char *path, const char *dxml, goto cleanup; } + path = qemuSavedImagePath(driver, file); + if(!path) + goto cleanup; ret = qemuDomainSaveInternal(driver, dom, vm, path, compressed, dxml, flags); vm = NULL; cleanup: + VIR_FREE(path); if (vm) virDomainObjUnlock(vm); qemuDriverUnlock(driver); @@ -2933,8 +2981,27 @@ getCompressionType(struct qemud_driver *driver) return compress; } +static char * +qemuCoreDumpPath(struct qemud_driver *driver, const char *file) +{ + char *enc_file, *ret; + + if(0 > virBase64EncodePathname(file, &enc_file)) { + virReportOOMError(); + return NULL; + } + if (virAsprintf(&ret, "%s/%s", driver->coreDumpDir, enc_file) < 0) { + virReportOOMError(); + VIR_FREE(enc_file); + return NULL; + } + VIR_FREE(enc_file); + + return(ret); +} + static int qemudDomainCoreDump(virDomainPtr dom, - const char *path, + const char *file, unsigned int flags) { struct qemud_driver *driver = dom->conn->privateData; @@ -2943,6 +3010,7 @@ static int qemudDomainCoreDump(virDomainPtr dom, int resume = 0, paused = 0; int ret = -1; virDomainEventPtr event = NULL; + char *path = NULL; virCheckFlags(VIR_DUMP_LIVE | VIR_DUMP_CRASH | VIR_DUMP_BYPASS_CACHE | VIR_DUMP_RESET, -1); @@ -2958,6 +3026,10 @@ static int qemudDomainCoreDump(virDomainPtr dom, goto cleanup; } + path = qemuCoreDumpPath(driver, file); + if(!path) + goto cleanup; + if (qemuDomainObjBeginAsyncJobWithDriver(driver, vm, QEMU_ASYNC_JOB_DUMP) < 0) goto cleanup; @@ -3032,6 +3104,7 @@ endjob: } cleanup: + VIR_FREE(path); if (vm) virDomainObjUnlock(vm); if (event) @@ -4178,7 +4251,7 @@ out: static int qemuDomainRestoreFlags(virConnectPtr conn, - const char *path, + const char *file, const char *dxml, unsigned int flags) { @@ -4190,6 +4263,7 @@ qemuDomainRestoreFlags(virConnectPtr conn, struct qemud_save_header header; virFileDirectFdPtr directFd = NULL; int state = -1; + char *path = NULL; virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE | VIR_DOMAIN_SAVE_RUNNING | @@ -4202,6 +4276,9 @@ qemuDomainRestoreFlags(virConnectPtr conn, else if (flags & VIR_DOMAIN_SAVE_PAUSED) state = 0; + path = qemuSavedImagePath(driver, file); + if(!path) + goto cleanup; fd = qemuDomainSaveImageOpen(driver, path, &def, &header, (flags & VIR_DOMAIN_SAVE_BYPASS_CACHE) != 0, &directFd, dxml, state, false, false); @@ -4240,6 +4317,7 @@ cleanup: virFileDirectFdFree(directFd); if (vm) virDomainObjUnlock(vm); + VIR_FREE(path); qemuDriverUnlock(driver); return ret; } @@ -4252,7 +4330,7 @@ qemuDomainRestore(virConnectPtr conn, } static char * -qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, const char *path, +qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, const char *file, unsigned int flags) { struct qemud_driver *driver = conn->privateData; @@ -4260,12 +4338,16 @@ qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, const char *path, virDomainDefPtr def = NULL; int fd = -1; struct qemud_save_header header; + char *path = NULL; /* We only take subset of virDomainDefFormat flags. */ virCheckFlags(VIR_DOMAIN_XML_SECURE, NULL); qemuDriverLock(driver); + path = qemuSavedImagePath(driver, file); + if(!path) + goto cleanup; fd = qemuDomainSaveImageOpen(driver, path, &def, &header, false, NULL, NULL, -1, false, false); @@ -4277,12 +4359,13 @@ qemuDomainSaveImageGetXMLDesc(virConnectPtr conn, const char *path, cleanup: virDomainDefFree(def); VIR_FORCE_CLOSE(fd); + VIR_FREE(path); qemuDriverUnlock(driver); return ret; } static int -qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path, +qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *file, const char *dxml, unsigned int flags) { struct qemud_driver *driver = conn->privateData; @@ -4293,6 +4376,7 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path, char *xml = NULL; size_t len; int state = -1; + char *path = NULL; virCheckFlags(VIR_DOMAIN_SAVE_RUNNING | VIR_DOMAIN_SAVE_PAUSED, -1); @@ -4304,6 +4388,9 @@ qemuDomainSaveImageDefineXML(virConnectPtr conn, const char *path, else if (flags & VIR_DOMAIN_SAVE_PAUSED) state = 0; + path = qemuSavedImagePath(driver, file); + if(!path) + goto cleanup; fd = qemuDomainSaveImageOpen(driver, path, &def, &header, false, NULL, dxml, state, true, false); @@ -4347,6 +4434,7 @@ cleanup: virDomainDefFree(def); VIR_FORCE_CLOSE(fd); VIR_FREE(xml); + VIR_FREE(path); qemuDriverUnlock(driver); return ret; } -- 1.7.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list