From bd84c8a35c7aedb2638bba087bc1756cfb9ccf9d Mon Sep 17 00:00:00 2001 From: Oliver Kurth Date: Thu, 28 Mar 2019 12:43:00 -0700 Subject: [PATCH] Query disks for UUID As part of identifying a guest disk so it can be associated with a vmdk, query its serial number, which is the same as the vmdk's ddb.uuid --- open-vm-tools/lib/include/conf.h | 9 ++++- .../services/plugins/guestInfo/guestInfoInt.h | 3 ++ .../plugins/guestInfo/guestInfoServer.c | 40 +++++++++++++++++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/open-vm-tools/lib/include/conf.h b/open-vm-tools/lib/include/conf.h index 6d33f4087..2c30978f6 100644 --- a/open-vm-tools/lib/include/conf.h +++ b/open-vm-tools/lib/include/conf.h @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 2002-2018 VMware, Inc. All rights reserved. + * Copyright (C) 2002-2019 VMware, Inc. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -163,6 +163,13 @@ */ #define CONFNAME_DISKINFO_INCLUDERESERVED "diskinfo-include-reserved" +/** + * Report UUID of disks for vmdk mapping via vim. + * + * @param boolean Set to true to report UUID to VMX. + */ +#define CONFNAME_DISKINFO_REPORT_UUID "diskinfo-report-uuid" + /* * END GuestInfo goodies. ****************************************************************************** diff --git a/open-vm-tools/services/plugins/guestInfo/guestInfoInt.h b/open-vm-tools/services/plugins/guestInfo/guestInfoInt.h index 81cc8faec..068e48244 100644 --- a/open-vm-tools/services/plugins/guestInfo/guestInfoInt.h +++ b/open-vm-tools/services/plugins/guestInfo/guestInfoInt.h @@ -32,6 +32,9 @@ #include "nicInfo.h" #include "dynbuf.h" +/* Default for whether to query and report disk UUIDs */ +#define CONFIG_GUESTINFO_REPORT_UUID_DEFAULT FALSE + /* * Plugin-specific data structures for the DiskGuestInfo. * diff --git a/open-vm-tools/services/plugins/guestInfo/guestInfoServer.c b/open-vm-tools/services/plugins/guestInfo/guestInfoServer.c index bc843e25f..92320b1d7 100644 --- a/open-vm-tools/services/plugins/guestInfo/guestInfoServer.c +++ b/open-vm-tools/services/plugins/guestInfo/guestInfoServer.c @@ -1217,6 +1217,13 @@ GuestInfoSendDiskInfoV1(ToolsAppCtx *ctx, // IN char *reply = NULL; size_t replyLen; Bool status; +#ifdef _WIN32 + Bool reportUUID = VMTools_ConfigGetBoolean(ctx->config, + CONFGROUPNAME_GUESTINFO, + CONFNAME_DISKINFO_REPORT_UUID, + CONFIG_GUESTINFO_REPORT_UUID_DEFAULT); +#endif + /* * Currently this format is fixed; the order should not be changed. * If a change is required, then DISK_INFO_VERSION must be bumped. @@ -1229,13 +1236,18 @@ GuestInfoSendDiskInfoV1(ToolsAppCtx *ctx, // IN static char jsonPerDiskFmt[] = "{" "\"name\":\"%s\"," "\"free\":\"%"FMT64"u\"," - "\"size\":\"%"FMT64"u\"},\n"; + "\"size\":\"%"FMT64"u\""; +#ifdef _WIN32 + static char jsonPerDiskUUIDFmt[] = ",\"uuid\":\"%s\""; +#endif + static char jsonPerDiskFmtFooter[] = "},\n"; static char jsonSuffix[] = "]}"; int i; // 20 bytes per number for ascii representation + // PARTITION_NAME_SIZE * 2 for name and (optional) uuid ASSERT_ON_COMPILE(sizeof tmpBuf > sizeof jsonPerDiskFmt + - PARTITION_NAME_SIZE + 20 + 20); + PARTITION_NAME_SIZE * 2 + 20 + 20); DynBuf_Init(&dynBuffer); @@ -1243,13 +1255,33 @@ GuestInfoSendDiskInfoV1(ToolsAppCtx *ctx, // IN GUEST_DISK_INFO_COMMAND, DISK_INFO_VERSION_1); DynBuf_Append(&dynBuffer, tmpBuf, len); for (i = 0; i < pdi->numEntries; i++) { + /* + * The '\' in Windows drive names needs escaping for json, + * so use base64 since its simple and will cover other weird + * cases like quotes, as well as avoid any utf-8 concerns. + */ + gchar *b64name = g_base64_encode(pdi->partitionList[i].name, + strlen(pdi->partitionList[i].name)); + len = Str_Snprintf(tmpBuf, sizeof tmpBuf, jsonPerDiskFmt, - pdi->partitionList[i].name, + b64name, pdi->partitionList[i].freeBytes, pdi->partitionList[i].totalBytes); DynBuf_Append(&dynBuffer, tmpBuf, len); + g_free(b64name); +#ifdef _WIN32 + if (reportUUID) { + if (pdi->partitionList[i].uuid[0] != '\0') { + len = Str_Snprintf(tmpBuf, sizeof tmpBuf, jsonPerDiskUUIDFmt, + pdi->partitionList[i].uuid); + DynBuf_Append(&dynBuffer, tmpBuf, len); + } + } +#endif + DynBuf_Append(&dynBuffer, jsonPerDiskFmtFooter, + sizeof jsonPerDiskFmtFooter - 1); } - DynBuf_Append(&dynBuffer, jsonSuffix, sizeof jsonSuffix); + DynBuf_Append(&dynBuffer, jsonSuffix, sizeof jsonSuffix - 1); infoReq = DynBuf_GetString(&dynBuffer); -- 2.47.3