Parsing configuration strings

May 20, 2017
172
18
58
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.)
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!