Skip to content

Instantly share code, notes, and snippets.

@nijave
Last active September 8, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nijave/f323eb1756933b968b70f0cb951ea619 to your computer and use it in GitHub Desktop.
Save nijave/f323eb1756933b968b70f0cb951ea619 to your computer and use it in GitHub Desktop.
Read Hyper-V key-value pair data on Linux guest
# Inspired by https://github.com/ejsiron/hvkvp
# explanation of format https://www.altaro.com/hyper-v/key-value-pair-data-exchange-3-linux/
# pool_3 host->guest (read-only)
with open("/var/lib/hyperv/.kvp_pool_3", "r") as f:
data = f.read()
# Each k, v is a nul terminated string. k is 512 bytes, v 2048 bytes
# so 2560 bytes is a single item
n = 2560
pairs = {
# It looks like the nul terminated strings may have non nul data so stop reading when
# first nul byte is found after the data
c[0:c[0:512].find("\x00")].replace("\x00",""): c[512:512+c[512:].find("\x00")].replace("\x00","")
for c in [data[i:i+n] for i in range(0, len(data), n)]
}
print(pairs)
@nijave
Copy link
Author

nijave commented Sep 8, 2020

{
  "HostName": "HYPERV01",
  "HostingSystemEditionId": "42",
  "HostingSystemOsMajor": "10",
  "HostingSystemOsMinor": "0",
  "HostingSystemProcessorArchitecture": "9",
  "HostingSystemSpMajor": "0",
  "HostingSystemSpMinor": "0",
  "PhysicalHostName": "HYPERV01",
  "PhysicalHostNameFullyQualified": "HYPERV01",
  "VirtualMachineId": "8049526A-4F67-46C1-B144-DFED29F9F432",
  "VirtualMachineName": "vmcent74docker",
  "HostingSystemNestedLevel": "0",
  "HostingSystemProcessorIdleStateMax": "0",
  "HostingSystemProcessorThrottleMax": "100",
  "HostingSystemProcessorThrottleMin": "5",
  "VirtualMachineDynamicMemoryBalancingEnabled": "1"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment