Parsing configuration strings

May 20, 2017
174
18
83
Netherlands
cyberfusion.io
VM configs (`/nodes/{node}/qemu/{vmid}/config`) contain configuration strings, such as:

Code:
local-zfs:vm-100-disk-1,aio=native,cache=unsafe,discard=on,iops_rd=20,iops_rd_max=30,iops_wr=20,iops_wr_max=30,iothread=1,mbps_rd=5,mbps_rd_max=10,mbps_wr=5,mbps_wr_max=10,replicate=0,ro=1,size=20G,ssd=1

Parsing this seems straightforward, i.e.:

Code:
string = 'local-zfs:vm-100-disk-1,aio=native,cache=unsafe,discard=on,iops_rd=20,iops_rd_max=30,iops_wr=20,iops_wr_max=30,iothread=1,mbps_rd=5,mbps_rd_max=10,mbps_wr=5,mbps_wr_max=10,replicate=0,ro=1,size=20G,ssd=1'

for kv in string.split(','):
  if '=' not in kv:
    continue

  k, v = kv.split('=')

  print(k, v)

... yields:

Code:
aio native
cache unsafe
discard on
iops_rd 20
iops_rd_max 30
iops_wr 20
iops_wr_max 30
iothread 1
mbps_rd 5
mbps_rd_max 10
mbps_wr 5
mbps_wr_max 10
replicate 0
ro 1
size 20G
ssd 1

However, I can imagine there being some gotchas, for example:

* Some items in the comma-separated configuration string do not contain a colon, such as `local-zfs:vm-100-disk-1`.
* Do any configuration string items contain multiple commas? That would break splitting on `,`.
* Do any configuration string items contain multiple `=` characters? That would break splitting on `=`. Although this is fixable by splitting only on the first occurrence (assuming that is the option name).

Is a Proxmox developer able to share how such configuration strings should be parsed?

Alternatively, is there an endpoint which parses these? For example: an endpoint that returns a list of disks with human-readable attributes, including their sizes. (I guess not, as only the `config` endpoint is called by the Proxmox GUI.)