Fedora Account System
Red Hat Associate
Red Hat Customer
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.
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"} ] } ] }
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"} ] } ] }