diff mbox series

btrfs-progs: scrub status: add json output format for -d (per device scrub status) option

Message ID 20250210202837.45565-1-racz.zoli@gmail.com (mailing list archive)
State New
Headers show
Series btrfs-progs: scrub status: add json output format for -d (per device scrub status) option | expand

Commit Message

Racz Zoltan Feb. 10, 2025, 8:28 p.m. UTC
This patch depends on the "[PATCH 0/2] btrfs-progs: scrub status: add json output format" patchset, 
and extends it with json output format for the per device scrub status (-d) option too.

When using the -d option the resulted json will have a slightly
different format containing the "devices" -> "device" keys and the "device" key will further 
contain the "info" and "scrub" subkeys. 

Example usage: 
btrfs --format json scrub status / -d

json output:
{
  "__header": {
    "version": "1"
  },
  "scrub-status": {
    "uuid": "1a7d1bc4-c212-42bf-b05c-73bd313d3ecd",
    "devices": [
      "device": {
        "dev": "/dev/nvme0n1p3",
        "id": 1
      }
    ]
  }
}


---
 cmds/scrub.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/cmds/scrub.c b/cmds/scrub.c
index 31b965fc..46a5ae77 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -128,6 +128,8 @@  struct format_ctx fctx;
 
 static const struct rowspec scrub_status_rowspec[] = {
 	{ .key = "uuid", .fmt = "%s", .out_json = "uuid"},
+	{ .key = "dev", .fmt = "%s", .out_json = "dev"},
+	{ .key = "id", .fmt = "%llu", .out_json = "id"},
 	{ .key = "status", .fmt = "%s", .out_json = "status"},
 	{ .key = "duration", .fmt = "%u:%s", .out_json = "duration"},
 	{ .key = "started_at", .fmt = "%s", .out_json = "started-at"},
@@ -402,8 +404,10 @@  static void _print_scrub_ss(struct scrub_stats *ss)
 
 	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
 
-	if ((!ss || !ss->t_start) && !json_output) {
-		pr_verbose(LOG_DEFAULT, "\tno stats available\n");
+	if (!ss || !ss->t_start) {
+		if (!json_output)
+			pr_verbose(LOG_DEFAULT, "\tno stats available\n");
+		
 		return;
 	}
 
@@ -455,10 +459,19 @@  static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 				struct btrfs_scrub_progress *p, int raw,
 				const char *append, struct scrub_stats *ss,
 				u64 limit)
-{
-	pr_verbose(LOG_DEFAULT, "\nScrub device %s (id %llu) %s\n", di->path, di->devid,
+{	
+	
+	const bool json_output = (bconf.output_format == CMD_FORMAT_JSON);
+
+	if (json_output) {
+		fmt_print_start_group(&fctx, "device", JSON_TYPE_MAP);
+		fmt_print(&fctx, "dev", di->path);
+		fmt_print(&fctx, "id", di->devid);
+	} else 	
+		pr_verbose(LOG_DEFAULT, "\nScrub device %s (id %llu) %s\n", di->path, di->devid,
 	       append ? append : "");
 
+
 	_print_scrub_ss(ss);
 
 	if (p) {
@@ -481,6 +494,10 @@  static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 			print_scrub_summary(p, ss, di->bytes_used, limit);
 		}
 	}
+
+	if (json_output) 
+		fmt_print_end_group(&fctx, "device");
+
 }
 
 /*
@@ -2032,6 +2049,9 @@  static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)
 		pr_verbose(LOG_DEFAULT, "UUID:             %s\n", fsid);
 
 	if (do_stats_per_dev) {
+		if (json_output) 
+			fmt_print_start_group(&fctx, "devices", JSON_TYPE_ARRAY);
+
 		for (i = 0; i < fi_args.num_devices; ++i) {
 			u64 limit;
 
@@ -2049,6 +2069,10 @@  static int cmd_scrub_status(const struct cmd_struct *cmd, int argc, char **argv)
 							"history" : "status",
 					&last_scrub->stats, limit);
 		}
+
+		if (json_output) 
+			fmt_print_end_group(&fctx, "devices");
+
 	} else {
 		u64 total_bytes_used = 0;
 		struct btrfs_ioctl_space_info *sp = si_args->spaces;