提交 7fb4c471 编写于 作者: P Pavel Hrdina

qemu: separate virQEMUCapsInitCached out of virQEMUCapsNewForBinaryInternal

Preparation for switching to virFileCache where there are two callbacks,
one to get a new data and second one to load a cached data.

This also removes virQEMUCapsReset which is no longer required.
Signed-off-by: NPavel Hrdina <phrdina@redhat.com>
Reviewed-by: NJiri Denemark <jdenemar@redhat.com>
上级 56a047a6
...@@ -4250,37 +4250,6 @@ virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, const char *cacheDir) ...@@ -4250,37 +4250,6 @@ virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, const char *cacheDir)
} }
static void
virQEMUCapsReset(virQEMUCapsPtr qemuCaps)
{
size_t i;
virBitmapClearAll(qemuCaps->flags);
qemuCaps->version = qemuCaps->kvmVersion = 0;
VIR_FREE(qemuCaps->package);
qemuCaps->arch = VIR_ARCH_NONE;
qemuCaps->usedQMP = false;
virObjectUnref(qemuCaps->kvmCPUModels);
qemuCaps->kvmCPUModels = NULL;
virObjectUnref(qemuCaps->tcgCPUModels);
qemuCaps->tcgCPUModels = NULL;
for (i = 0; i < qemuCaps->nmachineTypes; i++) {
VIR_FREE(qemuCaps->machineTypes[i].name);
VIR_FREE(qemuCaps->machineTypes[i].alias);
}
VIR_FREE(qemuCaps->machineTypes);
qemuCaps->nmachineTypes = 0;
VIR_FREE(qemuCaps->gicCapabilities);
qemuCaps->ngicCapabilities = 0;
virQEMUCapsHostCPUDataClear(&qemuCaps->kvmCPU);
virQEMUCapsHostCPUDataClear(&qemuCaps->tcgCPU);
}
static bool static bool
virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps,
uid_t runUid, uid_t runUid,
...@@ -4346,7 +4315,8 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps, ...@@ -4346,7 +4315,8 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps,
static int static int
virQEMUCapsInitCached(virCapsPtr caps, virQEMUCapsInitCached(virCapsPtr caps,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr *qemuCaps,
const char *binary,
const char *cacheDir, const char *cacheDir,
uid_t runUid, uid_t runUid,
gid_t runGid) gid_t runGid)
...@@ -4356,14 +4326,12 @@ virQEMUCapsInitCached(virCapsPtr caps, ...@@ -4356,14 +4326,12 @@ virQEMUCapsInitCached(virCapsPtr caps,
int ret = -1; int ret = -1;
char *binaryhash = NULL; char *binaryhash = NULL;
struct stat sb; struct stat sb;
time_t qemuctime = qemuCaps->ctime; virQEMUCapsPtr qemuCapsNew = NULL;
if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0) if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0)
goto cleanup; goto cleanup;
if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, binary, &binaryhash) < 0)
qemuCaps->binary,
&binaryhash) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0) if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0)
...@@ -4379,33 +4347,39 @@ virQEMUCapsInitCached(virCapsPtr caps, ...@@ -4379,33 +4347,39 @@ virQEMUCapsInitCached(virCapsPtr caps,
if (stat(capsfile, &sb) < 0) { if (stat(capsfile, &sb) < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
VIR_DEBUG("No cached capabilities '%s' for '%s'", VIR_DEBUG("No cached capabilities '%s' for '%s'",
capsfile, qemuCaps->binary); capsfile, binary);
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to access cache '%s' for '%s'"), _("Unable to access cache '%s' for '%s'"),
capsfile, qemuCaps->binary); capsfile, binary);
goto cleanup; goto cleanup;
} }
if (virQEMUCapsLoadCache(caps, qemuCaps, capsfile) < 0) { if (!(qemuCapsNew = virQEMUCapsNew()))
goto cleanup;
if (VIR_STRDUP(qemuCapsNew->binary, binary) < 0)
goto discard;
if (virQEMUCapsLoadCache(caps, qemuCapsNew, capsfile) < 0) {
VIR_WARN("Failed to load cached caps from '%s' for '%s': %s", VIR_WARN("Failed to load cached caps from '%s' for '%s': %s",
capsfile, qemuCaps->binary, virGetLastErrorMessage()); capsfile, qemuCapsNew->binary, virGetLastErrorMessage());
virResetLastError(); virResetLastError();
goto discard; goto discard;
} }
if (!virQEMUCapsIsValid(qemuCaps, runUid, runGid)) if (!virQEMUCapsIsValid(qemuCapsNew, runUid, runGid))
goto discard; goto discard;
VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=%d", VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=%d",
capsfile, qemuCaps->binary, capsfile, qemuCapsNew->binary,
(long long)qemuCaps->ctime, qemuCaps->usedQMP); (long long)qemuCapsNew->ctime, qemuCapsNew->usedQMP);
ret = 1; ret = 1;
*qemuCaps = qemuCapsNew;
cleanup: cleanup:
qemuCaps->ctime = qemuctime;
VIR_FREE(binaryhash); VIR_FREE(binaryhash);
VIR_FREE(capsfile); VIR_FREE(capsfile);
VIR_FREE(capsdir); VIR_FREE(capsdir);
...@@ -4413,9 +4387,9 @@ virQEMUCapsInitCached(virCapsPtr caps, ...@@ -4413,9 +4387,9 @@ virQEMUCapsInitCached(virCapsPtr caps,
discard: discard:
VIR_DEBUG("Dropping cached capabilities '%s' for '%s'", VIR_DEBUG("Dropping cached capabilities '%s' for '%s'",
capsfile, qemuCaps->binary); capsfile, qemuCapsNew->binary);
ignore_value(unlink(capsfile)); ignore_value(unlink(capsfile));
virQEMUCapsReset(qemuCaps); virObjectUnref(qemuCapsNew);
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }
...@@ -5230,14 +5204,12 @@ virQEMUCapsPtr ...@@ -5230,14 +5204,12 @@ virQEMUCapsPtr
virQEMUCapsNewForBinaryInternal(virCapsPtr caps, virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
const char *binary, const char *binary,
const char *libDir, const char *libDir,
const char *cacheDir,
uid_t runUid, uid_t runUid,
gid_t runGid, gid_t runGid,
bool qmpOnly) bool qmpOnly)
{ {
virQEMUCapsPtr qemuCaps; virQEMUCapsPtr qemuCaps;
struct stat sb; struct stat sb;
int rv;
char *qmperr = NULL; char *qmperr = NULL;
if (!(qemuCaps = virQEMUCapsNew())) if (!(qemuCaps = virQEMUCapsNew()))
...@@ -5265,42 +5237,30 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps, ...@@ -5265,42 +5237,30 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
goto error; goto error;
} }
if (!cacheDir) if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid, &qmperr) < 0) {
rv = 0; virQEMUCapsLogProbeFailure(binary);
else if ((rv = virQEMUCapsInitCached(caps, qemuCaps, cacheDir,
runUid, runGid)) < 0)
goto error; goto error;
}
if (rv == 0) { if (qmpOnly && !qemuCaps->usedQMP) {
if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid, &qmperr) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR,
virQEMUCapsLogProbeFailure(binary); _("Failed to probe QEMU binary with QMP: %s"),
goto error; qmperr ? qmperr : _("unknown error"));
} virQEMUCapsLogProbeFailure(binary);
goto error;
if (qmpOnly && !qemuCaps->usedQMP) { }
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to probe QEMU binary with QMP: %s"),
qmperr ? qmperr : _("unknown error"));
virQEMUCapsLogProbeFailure(binary);
goto error;
}
if (!qemuCaps->usedQMP &&
virQEMUCapsInitHelp(qemuCaps, runUid, runGid, qmperr) < 0) {
virQEMUCapsLogProbeFailure(binary);
goto error;
}
qemuCaps->libvirtCtime = virGetSelfLastChanged(); if (!qemuCaps->usedQMP &&
qemuCaps->libvirtVersion = LIBVIR_VERSION_NUMBER; virQEMUCapsInitHelp(qemuCaps, runUid, runGid, qmperr) < 0) {
virQEMUCapsLogProbeFailure(binary);
goto error;
}
if (cacheDir && qemuCaps->libvirtCtime = virGetSelfLastChanged();
virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0) qemuCaps->libvirtVersion = LIBVIR_VERSION_NUMBER;
goto error;
virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_KVM); virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_KVM);
virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_QEMU); virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_QEMU);
}
cleanup: cleanup:
VIR_FREE(qmperr); VIR_FREE(qmperr);
...@@ -5320,8 +5280,29 @@ virQEMUCapsNewForBinary(virCapsPtr caps, ...@@ -5320,8 +5280,29 @@ virQEMUCapsNewForBinary(virCapsPtr caps,
uid_t runUid, uid_t runUid,
gid_t runGid) gid_t runGid)
{ {
return virQEMUCapsNewForBinaryInternal(caps, binary, libDir, cacheDir, int rv;
runUid, runGid, false); virQEMUCapsPtr qemuCaps = NULL;
if ((rv = virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir,
runUid, runGid)) < 0)
goto error;
if (rv == 0) {
if (!(qemuCaps = virQEMUCapsNewForBinaryInternal(caps, binary,
libDir, runUid,
runGid, false))) {
goto error;
}
if (virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0)
goto error;
}
return qemuCaps;
error:
virObjectUnref(qemuCaps);
return NULL;
} }
......
...@@ -43,7 +43,6 @@ virQEMUCapsPtr ...@@ -43,7 +43,6 @@ virQEMUCapsPtr
virQEMUCapsNewForBinaryInternal(virCapsPtr caps, virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
const char *binary, const char *binary,
const char *libDir, const char *libDir,
const char *cacheDir,
uid_t runUid, uid_t runUid,
gid_t runGid, gid_t runGid,
bool qmpOnly); bool qmpOnly);
......
...@@ -70,7 +70,7 @@ main(int argc, char **argv) ...@@ -70,7 +70,7 @@ main(int argc, char **argv)
if (virThreadCreate(&thread, false, eventLoop, NULL) < 0) if (virThreadCreate(&thread, false, eventLoop, NULL) < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
if (!(caps = virQEMUCapsNewForBinaryInternal(NULL, argv[1], "/tmp", NULL, if (!(caps = virQEMUCapsNewForBinaryInternal(NULL, argv[1], "/tmp",
-1, -1, true))) -1, -1, true)))
return EXIT_FAILURE; return EXIT_FAILURE;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册