From nobody Wed May 14 14:10:42 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1524151579632840.0136429050323; Thu, 19 Apr 2018 08:26:19 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2567626AC4B; Thu, 19 Apr 2018 15:26:18 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D206A82AC5; Thu, 19 Apr 2018 15:26:17 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 736F8180BAE8; Thu, 19 Apr 2018 15:26:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w3JFPrDt021987 for ; Thu, 19 Apr 2018 11:25:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF05984425; Thu, 19 Apr 2018 15:25:53 +0000 (UTC) Received: from andariel.redhat.com (ovpn-204-221.brq.redhat.com [10.40.204.221]) by smtp.corp.redhat.com (Postfix) with ESMTP id B53846F9E8; Thu, 19 Apr 2018 15:25:52 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 19 Apr 2018 17:25:12 +0200 Message-Id: <7c863d8f71cc73cdef4f934397b2ef2ea5eb352b.1524151023.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: kwolf@redhat.com, Peter Krempa Subject: [libvirt] [RFC PATCH 15/30] qemu: block: Add support for creating 'format' layer for blockdev-add X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 19 Apr 2018 15:26:18 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" When using blockdev-add and friends, libvirt will need to create also properties for the qcow2/raw/... format handler in qemu. This patch adds the infrastructure and implements all formats known to libvirt including all properties which are expressed at the format level in qemu. Signed-off-by: Peter Krempa --- src/qemu/qemu_block.c | 296 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ src/qemu/qemu_block.h | 3 + 2 files changed, 299 insertions(+) diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index 0357d93384..87f3cf9cbe 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -1168,3 +1168,299 @@ qemuBlockStorageSourceGetBackendProps(virStorageSou= rcePtr src, virJSONValueFree(fileprops); return ret; } + + +static int +qemuBlockStorageSourceGetFormatRawProps(virStorageSourcePtr src, + virJSONValuePtr props) +{ + qemuDomainStorageSourcePrivatePtr srcPriv =3D QEMU_DOMAIN_STORAGE_SOUR= CE_PRIVATE(src); + const char *driver =3D "raw"; + const char *secretalias =3D NULL; + + if (src->encryption && + src->encryption->format =3D=3D VIR_STORAGE_ENCRYPTION_FORMAT_LUKS = && + srcPriv && + srcPriv->encinfo) { + driver =3D "luks"; + secretalias =3D srcPriv->encinfo->s.aes.alias; + } + + /* currently unhandled properties for the 'raw' driver: + * 'offset' + * 'size' + */ + + if (virJSONValueObjectAdd(props, + "s:driver", driver, + "S:key-secret", secretalias, NULL) < 0) + return -1; + + return 0; +} + + +static int +qemuBlockStorageSourceGetCryptoProps(virStorageSourcePtr src, + virJSONValuePtr *encprops) +{ + qemuDomainStorageSourcePrivatePtr srcpriv =3D QEMU_DOMAIN_STORAGE_SOUR= CE_PRIVATE(src); + const char *encformat =3D NULL; + + *encprops =3D NULL; + + /* qemu requires encrypted secrets regardless of encryption method use= d when + * passed using the blockdev infrastructure, thus only + * VIR_DOMAIN_SECRET_INFO_TYPE_AES works here. The correct type needs = to be + * instantiated elsewhere. */ + if (!src->encryption || + !srcpriv || + !srcpriv->encinfo || + srcpriv->encinfo->type !=3D VIR_DOMAIN_SECRET_INFO_TYPE_AES) + return 0; + + switch ((virStorageEncryptionFormatType) src->encryption->format) { + case VIR_STORAGE_ENCRYPTION_FORMAT_QCOW: + encformat =3D "aes"; + break; + + case VIR_STORAGE_ENCRYPTION_FORMAT_LUKS: + encformat =3D "luks"; + break; + + case VIR_STORAGE_ENCRYPTION_FORMAT_DEFAULT: + case VIR_STORAGE_ENCRYPTION_FORMAT_LAST: + default: + virReportEnumRangeError(virStorageEncryptionFormatType, + src->encryption->format); + return -1; + } + + return virJSONValueObjectCreate(encprops, + "s:format", encformat, + "s:key-secret", srcpriv->encinfo->s.ae= s.alias, + NULL); +} + + +static int +qemuBlockStorageSourceGetFormatQcowGenericProps(virStorageSourcePtr src, + const char *format, + virJSONValuePtr props) +{ + virJSONValuePtr encprops =3D NULL; + int ret =3D -1; + + if (qemuBlockStorageSourceGetCryptoProps(src, &encprops) < 0) + return -1; + + if (virJSONValueObjectAdd(props, + "s:driver", format, + "A:encrypt", &encprops, NULL) < 0) + goto cleanup; + + ret =3D 0; + + cleanup: + virJSONValueFree(encprops); + return ret; +} + + +static int +qemuBlockStorageSourceGetFormatQcow2Props(virStorageSourcePtr src, + virJSONValuePtr props) +{ + /* currently unhandled qcow2 props: + * + * 'lazy-refcounts' + * 'pass-discard-request' + * 'pass-discard-snapshot' + * 'pass-discard-other' + * 'overlap-check' + * 'l2-cache-size' + * 'l2-cache-entry-size' + * 'refcount-cache-size' + * 'cache-clean-interval' + */ + + if (qemuBlockStorageSourceGetFormatQcowGenericProps(src, "qcow2", prop= s) < 0) + return -1; + + return 0; +} + + +static virJSONValuePtr +qemuBlockStorageSourceGetBlockdevFormatCommonProps(virStorageSourcePtr src) +{ + const char *detectZeroes =3D NULL; + const char *discard =3D NULL; + int detectZeroesMode =3D virDomainDiskGetDetectZeroesMode(src->discard, + src->detect_ze= roes); + virJSONValuePtr props =3D NULL; + virJSONValuePtr ret =3D NULL; + + if (qemuBlockNodeNameValidate(src->nodeformat) < 0) + return NULL; + + if (src->discard) + discard =3D virDomainDiskDiscardTypeToString(src->discard); + + if (detectZeroesMode) + detectZeroes =3D virDomainDiskDetectZeroesTypeToString(detectZeroe= sMode); + + /* currently unhandled global properties: + * '*force-share': 'bool' + */ + + if (virJSONValueObjectCreate(&props, + "s:node-name", src->nodeformat, + "b:read-only", src->readonly, + "S:discard", discard, + "S:detect-zeroes", detectZeroes, + NULL) < 0) + return NULL; + + if (qemuBlockStorageSourceGetBlockdevGetCacheProps(src, props) < 0) + goto cleanup; + + VIR_STEAL_PTR(ret, props); + + cleanup: + virJSONValueFree(props); + return ret; +} + + +static virJSONValuePtr +qemuBlockStorageSourceGetBlockdevFormatProps(virStorageSourcePtr src) +{ + const char *driver =3D NULL; + virJSONValuePtr props =3D NULL; + virJSONValuePtr ret =3D NULL; + + if (!(props =3D qemuBlockStorageSourceGetBlockdevFormatCommonProps(src= ))) + goto cleanup; + + switch ((virStorageFileFormat) src->format) { + case VIR_STORAGE_FILE_FAT: + /* The fat layer is emulated by the storage access layer, so we ne= ed to + * put a raw layer on top */ + case VIR_STORAGE_FILE_RAW: + if (qemuBlockStorageSourceGetFormatRawProps(src, props) < 0) + goto cleanup; + + break; + + case VIR_STORAGE_FILE_QCOW2: + if (qemuBlockStorageSourceGetFormatQcow2Props(src, props) < 0) + goto cleanup; + break; + + case VIR_STORAGE_FILE_QCOW: + if (qemuBlockStorageSourceGetFormatQcowGenericProps(src, "qcow", p= rops) < 0) + goto cleanup; + break; + + /* formats without any special parameters */ + case VIR_STORAGE_FILE_PLOOP: + driver =3D "parallels"; + break; + case VIR_STORAGE_FILE_VHD: + driver =3D "vhdx"; + break; + case VIR_STORAGE_FILE_BOCHS: + case VIR_STORAGE_FILE_CLOOP: + case VIR_STORAGE_FILE_DMG: + case VIR_STORAGE_FILE_VDI: + case VIR_STORAGE_FILE_VPC: + case VIR_STORAGE_FILE_QED: + case VIR_STORAGE_FILE_VMDK: + driver =3D virStorageFileFormatTypeToString(src->format); + break; + + case VIR_STORAGE_FILE_AUTO_SAFE: + case VIR_STORAGE_FILE_AUTO: + case VIR_STORAGE_FILE_NONE: + case VIR_STORAGE_FILE_COW: + case VIR_STORAGE_FILE_ISO: + case VIR_STORAGE_FILE_DIR: + virReportError(VIR_ERR_INTERNAL_ERROR, + _("mishandled storage format '%s'"), + virStorageFileFormatTypeToString(src->format)); + goto cleanup; + + case VIR_STORAGE_FILE_LAST: + default: + virReportEnumRangeError(virStorageFileFormat, src->format); + goto cleanup; + } + + if (driver && + virJSONValueObjectAdd(props, "s:driver", driver, NULL) < 0) + goto cleanup; + + VIR_STEAL_PTR(ret, props); + + cleanup: + virJSONValueFree(props); + + return ret; +} + + +/** + * qemuBlockStorageSourceGetBlockdevProps: + * + * @src: storage source to format + * + * Formats @src into a JSON object which can be used with blockdev-add or + * -blockdev. The formatted object contains both the storage and format la= yer + * in nested form including link to the backing chain layer if necessary. + */ +virJSONValuePtr +qemuBlockStorageSourceGetBlockdevProps(virStorageSourcePtr src) +{ + bool backingSupported =3D src->format >=3D VIR_STORAGE_FILE_BACKING; + virJSONValuePtr storage =3D NULL; + virJSONValuePtr props =3D NULL; + virJSONValuePtr ret =3D NULL; + + if (virStorageSourceHasBacking(src) && !backingSupported) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("storage format '%s' does not support backing sto= re"), + virStorageFileFormatTypeToString(src->format)); + goto cleanup; + } + + if (!(props =3D qemuBlockStorageSourceGetBlockdevFormatProps(src))) + goto cleanup; + + if (!(storage =3D qemuBlockStorageSourceGetBackendProps(src, false))) + goto cleanup; + + if (virJSONValueObjectAppend(props, "file", storage) < 0) + goto cleanup; + storage =3D NULL; + + if (src->backingStore && backingSupported) { + if (virStorageSourceHasBacking(src)) { + if (virJSONValueObjectAppendString(props, "backing", + src->backingStore->nodeform= at) < 0) + goto cleanup; + } else { + /* chain is terminated, indicate that no detection should happ= en + * in qemu */ + if (virJSONValueObjectAppendNull(props, "backing") < 0) + goto cleanup; + } + } + + VIR_STEAL_PTR(ret, props); + + cleanup: + virJSONValueFree(storage); + virJSONValueFree(props); + return ret; +} diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h index 0e674437f4..f819c6f907 100644 --- a/src/qemu/qemu_block.h +++ b/src/qemu/qemu_block.h @@ -64,4 +64,7 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr= src, virURIPtr qemuBlockStorageSourceGetURI(virStorageSourcePtr src); +virJSONValuePtr +qemuBlockStorageSourceGetBlockdevProps(virStorageSourcePtr src); + #endif /* __QEMU_BLOCK_H__ */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list