Bug 1334659 - Make it possible to report output from lvm2 reporting commands in JSON format
Summary: Make it possible to report output from lvm2 reporting commands in JSON format
Keywords:
Status: CLOSED UPSTREAM
Alias: None
Product: LVM and device-mapper
Classification: Community
Component: lvm2
Version: unspecified
Hardware: All
OS: Linux
medium
low
Target Milestone: ---
: ---
Assignee: Peter Rajnoha
QA Contact: cluster-qe@redhat.com
URL:
Whiteboard:
Depends On: 1334655
Blocks: 1334682 1346079
TreeView+ depends on / blocked
 
Reported: 2016-05-10 09:12 UTC by Peter Rajnoha
Modified: 2017-08-24 08:39 UTC (History)
6 users (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2017-08-24 08:39:42 UTC
Embargoed:
prajnoha: lvm-technical-solution+
rule-engine: lvm-test-coverage?


Attachments (Terms of Use)

Description Peter Rajnoha 2016-05-10 09:12:31 UTC
Use new features introduced in libdevmapper to support report groups and different formats (bug #1334655) and make it possible for all lvm2 reporting commands to switch the output easily to JSON format.

This applies to all reporting commands using libdevmapper's reporting infrastructure, that is: pvs, vgs, lvs, pvdisplay -C, vgdisplay -C, lvdisplay -C, lvm devtypes command.

Comment 1 Peter Rajnoha 2016-05-11 13:50:07 UTC
Specifics for JSON format and libdevmapper's report group API:

struct dm_report_group *dm_report_group_create(dm_report_group_type_t type, void *data)
  --> "type" is DM_REPORT_GROUP_JSON, "data" is not used


int dm_report_group_push(struct dm_report_group *group, struct dm_report *report, void *data)
  --> "data" is a name of the array under which the report is done in JSON format (each report is an array of rows for JSON format). If "report" is NULL, it's just a named wrapping array for the reports that follow. If "report" is NULL and "data" is NULL at the same time, it's unnamed wrapping object in JSON.


int dm_report_group_pop(struct dm_report_group *group) 
  --> Adds closing brackets (to end object) or square brackets (to end array), based on what the appropriate "push" operation was


int dm_report_group_destroy(struct dm_report_group *group)
  --> Adds closing brackets for the whole JSON output.


Example:

# lvs
  LV    VG     Attr       LSize   
  root  fedora -wi-ao----  19.00g
  swap  fedora -wi-ao---- 500.00m
  lvol0 vg     -wi-a-----   4.00m

# lvs --reportformat json
  {
      "report": [
          {
              "lv": [
                  {"lv_name":"root", "vg_name":"fedora", "lv_attr":"-wi-ao----", "lv_size":"19.00g"},
                  {"lv_name":"swap", "vg_name":"fedora", "lv_attr":"-wi-ao----", "lv_size":"500.00m"},
                  {"lv_name":"lvol0", "vg_name":"vg", "lv_attr":"-wi-a-----", "lv_size":"4.00m"}
              ]
          }
      ]
  }

Comment 2 Peter Rajnoha 2016-09-22 11:19:05 UTC
It's possible to switch report output from all lvm2 reporting command into JSON format since lvm2 version 2.02.158.

There's global report/output_format lvm.conf setting as well as --reportformat command line options (which takes precedence over report/output_format lvm.conf setting) to set the output format.

See also man 7 lvmreport for more information.

Example:

[0] rhel7-a/~ # lvmconfig --type diff
report {
	compact_output=1
}

[0] rhel7-a/~ # lvs
  LV   VG   Attr       LSize   
  root rhel -wi-ao----   9.02g
  swap rhel -wi-ao---- 500.00m

[0] rhel7-a/~ # vim /etc/lvm/lvm.conf 

[0] rhel7-a/~ # lvmconfig --type diff
report {
	output_format="json"
	compact_output=1
}

[0] rhel7-a/~ # lvs
  {
      "report": [
          {
              "lv": [
                  {"lv_name":"root", "vg_name":"rhel", "lv_attr":"-wi-ao----", "lv_size":"9.02g"},
                  {"lv_name":"swap", "vg_name":"rhel", "lv_attr":"-wi-ao----", "lv_size":"500.00m"}
              ]
          }
      ]
  }

[0] rhel7-a/~ # lvs --reportformat basic
  LV   VG   Attr       LSize   
  root rhel -wi-ao----   9.02g
  swap rhel -wi-ao---- 500.00m

[0] rhel7-a/~ # lvs --reportformat json 
  {
      "report": [
          {
              "lv": [
                  {"lv_name":"root", "vg_name":"rhel", "lv_attr":"-wi-ao----", "lv_size":"9.02g"},
                  {"lv_name":"swap", "vg_name":"rhel", "lv_attr":"-wi-ao----", "lv_size":"500.00m"}
              ]
          }
      ]
  }


Note You need to log in before you can comment on or make changes to this bug.