From nobody Tue Mar 3 04:54:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1771536914; cv=none; d=zohomail.com; s=zohoarc; b=caIkU2yM3mnBw53xAM5pbiEj1VyrnNZZEMSlRVdWdn8BRlS4EMe7sKiSYYXVlGLW4WCBBoN3zTGqV/NYCZ3qTe4708ObVUyjLfnp/j8/EfllQHTzHRgTasP29koDI5ufukuKC00pQ4yaFuyBr4Vwh/9oLvmLHZwmJTu7WASs+G0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1771536914; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id; bh=RMzv7XuThnGF6vLm4yNzPyI5YUEj8abvDHscK0alPa8=; b=SDHoQ4WOFbqesmNN1ULEHJ5mMDTAuu0JAR4Ov2NtV8LjV/BbkLrhWtmiyrlSS81bYzN+m1rlDqUR7XiaA2Kf5wjagnlVofd388WsU4juR2Vf+2K4T9RYkC//6sIj5vU9bc2OIRg+ZcmVhhs05i7T0Qg51KJYxQ84GFP7ljFNZrQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1771536914160150.39114515505707; Thu, 19 Feb 2026 13:35:14 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 993) id 22A8A3FAE9; Thu, 19 Feb 2026 16:35:13 -0500 (EST) Received: from [172.19.199.9] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id 983EB41B26; Thu, 19 Feb 2026 16:31:45 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 993) id 4502D3F359; Thu, 19 Feb 2026 16:31:36 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 7D7173FA5C for ; Thu, 19 Feb 2026 16:31:35 -0500 (EST) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-wWaCAduKM8Ca2CjkRWca_g-1; Thu, 19 Feb 2026 16:31:32 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 887221800576 for ; Thu, 19 Feb 2026 21:31:31 +0000 (UTC) Received: from himantopus.redhat.com (unknown [10.22.90.32]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BF8A23003D91; Thu, 19 Feb 2026 21:31:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HELO_MISC_IP,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1771536695; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RMzv7XuThnGF6vLm4yNzPyI5YUEj8abvDHscK0alPa8=; b=D7cGMHgWhv3MopPYJzXmosYTbvcp0JmIhiMo/ySBucdQPEp9Yt6ZFNvS8bu6jCRuNEr/H8 kmfE6ZVn7zwUWxCiJZK6vNy78UD/1e2lB1RpIq5oQ79p3Bq+qIwHungC2b66sr91VLle42 o8aKn0y/U7quROQ4hfcUnerUc7z9XVI= X-MC-Unique: wWaCAduKM8Ca2CjkRWca_g-1 X-Mimecast-MFC-AGG-ID: wWaCAduKM8Ca2CjkRWca_g_1771536691 To: devel@lists.libvirt.org Subject: [PATCH 3/3] hyperv: Add basic domainGetBlockInfo() API implementation Date: Thu, 19 Feb 2026 15:31:26 -0600 Message-ID: <20260219213126.3387319-4-jjongsma@redhat.com> In-Reply-To: <20260219213126.3387319-1-jjongsma@redhat.com> References: <20260219213126.3387319-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: kocDLGZPXu-7g3MbT45b6GBKgcsHuagZabE4V2aH33Q_1771536691 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: J27D6ECPUBYQUEZHWL3UCKIDBFCSGDMB X-Message-ID-Hash: J27D6ECPUBYQUEZHWL3UCKIDBFCSGDMB X-MailFrom: jjongsma@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: ehazan@redhat.com X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Jonathon Jongsma via Devel Reply-To: Jonathon Jongsma X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1771536915117158500 Content-Type: text/plain; charset="utf-8"; x-default="true" This adds initial support for virDomainGetBlockInfo() for the hyperv driver. It currently supports: - physical disk drives that are assigned to a vm - virtual disk drives backed by a .VHD file that are local to the host - other drives backed by local files (e.g. cdrom with a .iso) It will fail to get allocation and physical values for any drives backed by files that are not local to the host (e.g. on network shares) Signed-off-by: Jonathon Jongsma --- src/hyperv/hyperv_driver.c | 205 ++++++++++++++++++++++++++ src/hyperv/hyperv_wmi.c | 41 ++++++ src/hyperv/hyperv_wmi.h | 7 + src/hyperv/hyperv_wmi_generator.input | 109 ++++++++++++++ 4 files changed, 362 insertions(+) diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c index 3cebab305b..e332b8a860 100644 --- a/src/hyperv/hyperv_driver.c +++ b/src/hyperv/hyperv_driver.c @@ -26,6 +26,9 @@ =20 #include "internal.h" #include "datatypes.h" +#include "libvirt/libvirt.h" +#include "libvirt/virterror.h" +#include "virbuffer.h" #include "virdomainobjlist.h" #include "virauth.h" #include "viralloc.h" @@ -3822,6 +3825,207 @@ hypervDomainInterfaceAddresses(virDomainPtr dom, } =20 =20 +static int +hypervGetFileSize(hypervPrivate *priv, + const char *filePath, + unsigned long long *fileSize) +{ + g_autoptr(CIM_DataFile) dataFile =3D NULL; + g_auto(virBuffer) query =3D VIR_BUFFER_INITIALIZER; + g_autofree char *escapedPath =3D NULL; + + virBufferAddLit(&query, CIM_DATAFILE_WQL_SELECT); + virBufferEscapeSQL(&query, "WHERE Name=3D'%s'", filePath); + + if (hypervGetWmiClass(CIM_DataFile, &dataFile) < 0 || !dataFile) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not query file size for '%1$s'"), filePath= ); + return -1; + } + + *fileSize =3D dataFile->data->FileSize; + return 0; +} + + +static int +hypervGetPhysicalDiskBlockInfo(hypervPrivate *priv, + unsigned int driveNumber, + virDomainBlockInfoPtr info) +{ + g_autoptr(Win32_DiskDrive) diskDrive =3D NULL; + g_auto(virBuffer) query =3D VIR_BUFFER_INITIALIZER; + + virBufferAsprintf(&query, WIN32_DISKDRIVE_WQL_SELECT "WHERE Index=3D%u= ", driveNumber); + + if (hypervGetWmiClass(Win32_DiskDrive, &diskDrive) < 0 || !diskDrive) { + virReportError(VIR_ERR_INVALID_ARG, + _("Could not find physical disk with drive number %= 1$u"), driveNumber); + return -1; + } + + info->capacity =3D info->allocation =3D info->physical =3D diskDrive->= data->Size; + return 0; +} + + +static int +hypervGetVHDCapacity(hypervPrivate *priv, + const char *path, + unsigned long long *capacity) +{ + g_auto(WsXmlDocH) settingDataDoc =3D NULL; + g_autofree char *maxInternalSizeStr =3D NULL; + + if (hypervImageManagementServiceGetVHDSD(priv, path, &settingDataDoc) = < 0) + return -1; + + maxInternalSizeStr =3D ws_xml_get_xpath_value(settingDataDoc, + (char *)"//PROPERTY[@NAME=3D'MaxInternalSize']/VALUE"); + + if (!maxInternalSizeStr) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not find MaxInternalSize in VHD SettingDat= a for '%1$s'"), path); + return -1; + } + + if (virStrToLong_ull(maxInternalSizeStr, NULL, 10, capacity) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to parse MaxInternalSize '%1$s' for '%2$s= '"), + maxInternalSizeStr, path); + return -1; + } + + return 0; +} + + +static int +hypervGetVirtualDiskBlockInfo(hypervPrivate *priv, + const char *diskpath, + virDomainBlockInfoPtr info) +{ + unsigned long long capacity =3D 0; + unsigned long long allocation =3D 0; + /* This might fail if diskpath is not a vhd file, but continue anyway = as it + * might be e.g. an ISO that is not supported by the ImageManagementSe= rvice */ + int rcapacity =3D hypervGetVHDCapacity(priv, diskpath, &capacity); + + /* querying actual file allocation only works for local files, so may = fail + * for files on network shares */ + int rallocation =3D hypervGetFileSize(priv, diskpath, &allocation); + + /* if both queries were unsuccessful, just return an error */ + if (rcapacity < 0 && rallocation < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to get info for d= isk '%s'"), diskpath); + return -1; + } + + /* if we failed to get the capacity from the ImageManagementService (i= .e. + * the disk path wasn't a vhd file), just use the file size */ + if (capacity =3D=3D 0) + capacity =3D allocation; + + info->capacity =3D capacity; + info->physical =3D info->allocation =3D allocation; + return 0; +} + +static int +hypervDomainGetBlockInfo(virDomainPtr domain, + const char *path, + virDomainBlockInfoPtr info, + unsigned int flags) +{ + hypervPrivate *priv =3D domain->conn->privateData; + char uuid_string[VIR_UUID_STRING_BUFLEN]; + g_autoptr(Msvm_ResourceAllocationSettingData) resource_settings =3D NU= LL; + g_autoptr(Msvm_StorageAllocationSettingData) storage_settings =3D NULL; + g_autoptr(Msvm_VirtualSystemSettingData) system_settings =3D NULL; + g_autoptr(virDomainDef) def =3D NULL; + virDomainDiskDef *disk =3D NULL; + const char *diskpath =3D NULL; + + virCheckFlags(0, -1); + + virUUIDFormat(domain->uuid, uuid_string); + + if (hypervGetMsvmVirtualSystemSettingDataFromUUID(priv, uuid_string, &= system_settings) < 0) { + virReportError(VIR_ERR_NO_DOMAIN, _("No domain with UUID %1$s"), u= uid_string); + return -1; + } + + if (hypervGetResourceAllocationSD(priv, + system_settings->data->InstanceID, + &resource_settings) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get resource allocation settings data"= )); + return -1; + } + + if (hypervGetStorageAllocationSD(priv, + system_settings->data->InstanceID, + &storage_settings) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Unable to get storage allocation settings data")= ); + return -1; + } + + if (!(def =3D virDomainDefNew(priv->xmlopt))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to create a new virDomainDef")); + return -1; + } + + /* Process storage and resources to get disk names */ + if (hypervDomainDefParseStorage(priv, def, resource_settings, storage_= settings) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to parse storage")); + return -1; + } + + disk =3D virDomainDiskByName(def, path, false); + if (!disk) { + virReportError(VIR_ERR_INVALID_ARG, + _("invalid path %1$s not assigned to domain"), path= ); + return -1; + } + + diskpath =3D virDomainDiskGetSource(disk); + if (!diskpath) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("disk '%1$s' has no source path"), path); + return -1; + } + + if (virDomainDiskGetType(disk) =3D=3D VIR_STORAGE_TYPE_BLOCK) { + unsigned int driveNumber =3D 0; + g_autoptr(Msvm_DiskDrive) diskdrive =3D NULL; + + /* BLOCK type disks have their source path set to the windows driv= e number */ + if (virStrToLong_ui(diskpath, NULL, 10, &driveNumber) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid drive number '%1$s' for physical dis= k"), diskpath); + return -1; + } + + if (hypervGetPhysicalDiskBlockInfo(priv, driveNumber, info) < 0) + return -1; + } else if (virDomainDiskGetType(disk) =3D=3D VIR_STORAGE_TYPE_FILE) { + /* first try querying the disk via the image management service wh= ich supports .vhd(x) files */ + if (hypervGetVirtualDiskBlockInfo(priv, diskpath, info) < 0) + return -1; + } else { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("Unsupported disk type %1$d for disk '%2$s'"), + virDomainDiskGetType(disk), path); + return -1; + } + + return 0; +} + + static virHypervisorDriver hypervHypervisorDriver =3D { .name =3D "Hyper-V", .connectOpen =3D hypervConnectOpen, /* 0.9.5 */ @@ -3886,6 +4090,7 @@ static virHypervisorDriver hypervHypervisorDriver =3D= { .domainSendKey =3D hypervDomainSendKey, /* 3.6.0 */ .connectIsAlive =3D hypervConnectIsAlive, /* 0.9.8 */ .domainInterfaceAddresses =3D hypervDomainInterfaceAddresses, /* 12.1.= 0 */ + .domainGetBlockInfo =3D hypervDomainGetBlockInfo, /* 12.1.0 */ }; =20 =20 diff --git a/src/hyperv/hyperv_wmi.c b/src/hyperv/hyperv_wmi.c index eee42b5c70..90bbc42b6d 100644 --- a/src/hyperv/hyperv_wmi.c +++ b/src/hyperv/hyperv_wmi.c @@ -31,6 +31,7 @@ #include =20 #include "internal.h" +#include "libvirt/virterror.h" #include "virerror.h" #include "datatypes.h" #include "viralloc.h" @@ -1507,6 +1508,46 @@ hypervGetEthernetPortAllocationSD(hypervPrivate *pri= v, } =20 =20 +int +hypervImageManagementServiceGetVHDSD(hypervPrivate *priv, + const char *vhdPath, + WsXmlDocH *settingDataDoc) +{ + hypervInvokeParamsList *params =3D NULL; + g_auto(WsXmlDocH) response =3D NULL; + g_autofree char *settingDataXmlStr =3D NULL; + + params =3D hypervCreateInvokeParamsList("GetVirtualHardDiskSettingData= ", + MSVM_IMAGEMANAGEMENTSERVICE_SELE= CTOR, + Msvm_ImageManagementService_WmiI= nfo); + if (hypervAddSimpleParam(params, "Path", vhdPath) < 0) + return -1; + + if (hypervInvokeMethod(priv, ¶ms, &response) < 0) + return -1; + + settingDataXmlStr =3D ws_xml_get_xpath_value(response, + (char *)"/s:Envelope/s:Body/p:GetVirtualHardDiskSettingData_OUTPUT= /p:SettingData"); + + if (!settingDataXmlStr) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not extract SettingData from response for = '%1$s'"), vhdPath); + return -1; + } + + /* the method returns an embedded CIM-XML document as a string, so we = need + * to parse it as xml */ + *settingDataDoc =3D ws_xml_read_memory(settingDataXmlStr, strlen(setti= ngDataXmlStr), "UTF-8", 0); + if (!*settingDataDoc) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not parse VHD SettingData XML for '%1$s'")= , vhdPath); + return -1; + } + + return 0; +} + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *= * * * Msvm_VirtualSystemManagementService */ diff --git a/src/hyperv/hyperv_wmi.h b/src/hyperv/hyperv_wmi.h index 9093aec455..11d61edfc5 100644 --- a/src/hyperv/hyperv_wmi.h +++ b/src/hyperv/hyperv_wmi.h @@ -36,6 +36,9 @@ #define MSVM_VIRTUALSYSTEMMANAGEMENTSERVICE_SELECTOR \ "CreationClassName=3DMsvm_VirtualSystemManagementService" =20 +#define MSVM_IMAGEMANAGEMENTSERVICE_SELECTOR \ + "CreationClassName=3DMsvm_ImageManagementService" + int hypervVerifyResponse(WsManClient *client, WsXmlDocH response, const char *detail); =20 @@ -263,6 +266,10 @@ int hypervGetEthernetPortAllocationSD(hypervPrivate *p= riv, const char *id, Msvm_EthernetPortAllocationSettingDa= ta **data); =20 +int hypervImageManagementServiceGetVHDSD(hypervPrivate *priv, + const char *vhdPath, + WsXmlDocH *settingDataDoc); + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *= * * * Msvm_VirtualSystemManagementService */ diff --git a/src/hyperv/hyperv_wmi_generator.input b/src/hyperv/hyperv_wmi_= generator.input index e6053267f8..017b7a0fa5 100644 --- a/src/hyperv/hyperv_wmi_generator.input +++ b/src/hyperv/hyperv_wmi_generator.input @@ -1143,3 +1143,112 @@ class Msvm_VideoHead uint32 CurrentNumberOfColumns uint64 CurrentNumberOfColors end + + +class Msvm_ImageManagementService + string InstanceID + string Caption + string Description + string ElementName + datetime InstallDate + string Name + uint16 OperationalStatus[] + string StatusDescriptions[] + string Status + uint16 HealthState + uint16 CommunicationStatus + uint16 DetailedStatus + uint16 OperatingStatus + uint16 PrimaryStatus + uint16 EnabledState + string OtherEnabledState + uint16 RequestedState + uint16 EnabledDefault + datetime TimeOfLastStateChange + uint16 AvailableRequestedStates[] + uint16 TransitioningToState + string SystemCreationClassName + string SystemName + string CreationClassName + string PrimaryOwnerName + string PrimaryOwnerContact + string StartMode + boolean Started +end + + +class Msvm_VirtualHardDiskSettingData + string InstanceID + string Caption + string Description + string ElementName + uint16 Type + uint16 Format + string Path + string ParentPath + datetime ParentTimestamp + string ParentIdentifier + uint64 MaxInternalSize + uint32 BlockSize + uint32 LogicalSectorSize + uint32 PhysicalSectorSize + string VirtualDiskId + uint64 DataAlignment + uint16 PmemAddressAbstractionType + boolean IsPmemCompatible +end + + +class Win32_DiskDrive + uint16 Availability + uint32 BytesPerSector + uint16 Capabilities[] + string CapabilityDescriptions[] + string Caption + string CompressionMethod + uint32 ConfigManagerErrorCode + boolean ConfigManagerUserConfig + string CreationClassName + uint64 DefaultBlockSize + string Description + string DeviceID + boolean ErrorCleared + string ErrorDescription + string ErrorMethodology + string FirmwareRevision + uint32 Index + datetime InstallDate + string InterfaceType + uint32 LastErrorCode + string Manufacturer + uint64 MaxBlockSize + uint64 MaxMediaSize + boolean MediaLoaded + string MediaType + uint64 MinBlockSize + string Model + string Name + boolean NeedsCleaning + uint32 NumberOfMediaSupported + uint32 Partitions + string PNPDeviceID + uint16 PowerManagementCapabilities[] + boolean PowerManagementSupported + uint32 SCSIBus + uint16 SCSILogicalUnit + uint16 SCSIPort + uint16 SCSITargetId + uint32 SectorsPerTrack + string SerialNumber + uint32 Signature + uint64 Size + string Status + uint16 StatusInfo + string SystemCreationClassName + string SystemName + uint64 TotalCylinders + uint32 TotalHeads + uint64 TotalSectors + uint64 TotalTracks + uint32 TracksPerCylinder +end --=20 2.53.0