diff mbox series

[2/4] new command: btrfs filesystem info [<path>...]

Message ID c86c826dcc398a49fc6d3afb68683b104763eceb.1701891165.git.kreijack@inwind.it (mailing list archive)
State New, archived
Headers show
Series RFC: add the btrfs_info helper | expand

Commit Message

Goffredo Baroncelli Dec. 6, 2023, 7:32 p.m. UTC
From: Goffredo Baroncelli <kreijack@inwind.it>

This command shows all the main info of a (or all) btrfs filesystem.
The argument may be in any of the following form:
- btrfs filesystem path
- btrfs device
- UUID=<uuid>
- UUID_sub=<uuid_sub>
- PARTUUID=<partuuid>

If no argument is passed, the informaton of all the filesystem are showed.

Typical output
---------------------------------------------------
$ btrfs fi info /mnt/btrfs-raid1
UUID:    b39b0b27-ff80-4cb4-bf48-0be939ff0788
LABEL:   raid1
devices:
        dev:      /dev/sda2
        UUID_SUB: 65a4f76b-abaa-4d98-a1d5-4c586ffdcbf0
        PARTUUID: fb9c0cc8-02
        major:    8
        minor:    2
        devid:    1

        dev:      /dev/sdb2
        UUID_SUB: 5f7f141f-0bdf-44fb-a910-fdc502a6fa7d
        PARTUUID:
        major:    8
        minor:    18
        devid:    2

mounts:
        dest:     /mnt/btrfs-raid1
        rootpath: /
        subvol:   /
        options:  rw,noatime,nodiratime,rw,space_cache,subvolid=5,subvol=/
        seq:      93
        over:     30

        dest:     /var/log
        rootpath: /@log
        subvol:   /@log
        options:  rw,noatime,nodiratime,rw,nossd,discard=async,space_cache,subvolid=447,subvol=/@log
        seq:      64
        over:     30

-----------------------------------------------------

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
---
 cmds/filesystem.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
diff mbox series

Patch

diff --git a/cmds/filesystem.c b/cmds/filesystem.c
index 1b444b8f..38a0787f 100644
--- a/cmds/filesystem.c
+++ b/cmds/filesystem.c
@@ -55,6 +55,7 @@ 
 #include "common/parse-utils.h"
 #include "common/filesystem-utils.h"
 #include "common/format-output.h"
+#include "common/btrfs-info.h"
 #include "cmds/commands.h"
 #include "cmds/filesystem-usage.h"
 
@@ -1503,6 +1504,48 @@  static int cmd_filesystem_label(const struct cmd_struct *cmd,
 }
 static DEFINE_SIMPLE_COMMAND(filesystem_label, "label");
 
+static const char * const cmd_filesystem_info_usage[] = {
+	"btrfs filesystem info [UUID=<uuid>|UUID_SUB=<subuuid>|PARTUUID=<partuuid>|LABEL=<label>|<device>|<mount_point>] ...",
+	"Show the information of a btrfs filesystem.",
+	NULL
+};
+
+static int cmd_filesystem_info(const struct cmd_struct *cmd,
+				int argc, char **argv)
+{
+	clean_args_no_options(cmd, argc, argv);
+
+	if (argc - optind == 0) {
+		const struct btrfs_info *bi;
+		int r = btrfs_info_get(&bi);
+		if (r < 0) {
+			error("Cannot get info: %d - %s\n", -r, strerror(-r));
+			return 2;
+		}
+		btrfs_info_dump(bi, stdout);
+	} else {
+		for (int i = 1 ; i < argc ; i++) {
+			printf("Target: %s\n", argv[i]);
+
+			const struct btrfs_info *bi;
+			int r = btrfs_info_find(argv[i], &bi);
+			if (r < 0) {
+				error("Cannot get info for '%s': %d - %s\n",
+				      argv[i], -r, strerror(-r));
+				return 2;
+			}
+
+			btrfs_info_dump_single_entry(bi, stdout);
+
+			if (i < argc - 1)
+				printf("\n");
+		}
+	}
+
+	return 0;
+}
+static DEFINE_SIMPLE_COMMAND(filesystem_info, "info");
+
 static const char * const cmd_filesystem_balance_usage[] = {
 	"btrfs filesystem balance [args...] (alias of \"btrfs balance\")",
 	"Please see \"btrfs balance --help\" for more information.",
@@ -1707,6 +1750,7 @@  static const struct cmd_group filesystem_cmd_group = {
 		&cmd_struct_filesystem_balance,
 		&cmd_struct_filesystem_resize,
 		&cmd_struct_filesystem_label,
+		&cmd_struct_filesystem_info,
 		&cmd_struct_filesystem_usage,
 		&cmd_struct_filesystem_mkswapfile,
 		NULL