From: John Ferlan Date: Wed, 25 May 2016 19:08:49 +0000 (-0400) Subject: storage: Split out a helper for encryption checks X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=4c6038a35e9205591f7662cbb4d0b36fc23c1282;p=libvirt.git storage: Split out a helper for encryption checks Split out a helper from virStorageBackendCreateQemuImgCmdFromVol to check the encryption - soon a new encryption sheriff will be patroling and that'll mean all sorts of new checks. Signed-off-by: John Ferlan --- diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index 3a23cd7e60..207615517f 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -1010,6 +1010,53 @@ virStorageBackendCreateQemuImgOpts(char **opts, return -1; } + +/* virStorageBackendCreateQemuImgCheckEncryption: + * @format: format of file found + * @conn: pointer to connection + * @vol: pointer to volume def + * + * Ensure the proper setup for encryption. + * + * Returns 0 on success, -1 on failure w/ error set + */ +static int +virStorageBackendCreateQemuImgCheckEncryption(int format, + const char *type, + virConnectPtr conn, + virStorageVolDefPtr vol) +{ + virStorageEncryptionPtr enc = vol->target.encryption; + + if (format == VIR_STORAGE_FILE_QCOW || format == VIR_STORAGE_FILE_QCOW2) { + if (enc->format != VIR_STORAGE_ENCRYPTION_FORMAT_QCOW && + enc->format != VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unsupported volume encryption format %d"), + vol->target.encryption->format); + return -1; + } + if (enc->nsecrets > 1) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("too many secrets for qcow encryption")); + return -1; + } + if (enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT || + enc->nsecrets == 0) { + if (virStorageGenerateQcowEncryption(conn, vol) < 0) + return -1; + } + } else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("qcow volume encryption unsupported with " + "volume format %s"), type); + return -1; + } + + return 0; +} + + /* Create a qemu-img virCommand from the supplied binary path, * volume definitions and imgformat */ @@ -1133,35 +1180,11 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn, } } - if (info.encryption) { - virStorageEncryptionPtr enc; + if (info.encryption && + virStorageBackendCreateQemuImgCheckEncryption(info.format, type, + conn, vol) < 0) + return NULL; - if (info.format != VIR_STORAGE_FILE_QCOW && - info.format != VIR_STORAGE_FILE_QCOW2) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("qcow volume encryption unsupported with " - "volume format %s"), type); - return NULL; - } - enc = vol->target.encryption; - if (enc->format != VIR_STORAGE_ENCRYPTION_FORMAT_QCOW && - enc->format != VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("unsupported volume encryption format %d"), - vol->target.encryption->format); - return NULL; - } - if (enc->nsecrets > 1) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("too many secrets for qcow encryption")); - return NULL; - } - if (enc->format == VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT || - enc->nsecrets == 0) { - if (virStorageGenerateQcowEncryption(conn, vol) < 0) - return NULL; - } - } /* Size in KB */ info.size_arg = VIR_DIV_UP(vol->target.capacity, 1024);