RFC: add the btrfs_info helper
Subsystems (2)
cover RFC: add the btrfs_info helper
From: Goffredo Baroncelli <[email protected]>
The aim of this patches set is to add some helper to query the layout of a btrfs filesystem.
A btrfs filesystem may be composed by multiple disks, and several mountpoints on which different subvolumes are mounted.
Sometime we need to know the devices list starting from a path, or a path starting from a device.
These helpers allow to know all the information of a filesystem like
- UUID
- Label
- Mountpoints list (if mounted), and for each mountpoint
- subvolume
- options
- Devices list, and for each device
- major, minor
- device name
- partuuid, uuid_sub
- devid (if the user is root)
And it is possible to build these information giving any of:
- a path (if mounted)
- UUID
- Label
- partuuid
- uuid_sub
These helpers uses only libblkid and /proc/self/mountinfo to extract all these information. It is not required to be root; if the user is root it is returned also the devid of a device.
The first patch add the helpers. From the second patch there are two new commands ('btrfs fi info' and 'btrfs fi get-info') and an enachement of 'btrfs fi label', which can be used passing both a device or a path,
or UUID=<uuid>, LABEL=<label>... ; before the user has to pass a path
if the filesystem is mounted, and a device otherwise. This command refuse to work with a device when the fielsystem is mounted, which is not very user friendly.
The goal is to enanche some btrfs commands that now force the user to pass a device or a path even when it is possible to pass any valid reference to a btrfs filesystem (like UUID or Label).
BR
Goffredo Baroncelli (4):
btrfs-info: some utility to query a filesystem info new command: btrfs filesystem info [<path>...] new command: btrfs filesystem get-info ...
btrfs-progs: 'btrfs fi label' using a dev instead of a path
Makefile | 1 + cmds/filesystem.c | 141 +++++ common/btrfs-info.c | 1036 +++++++++++++++++++++++++++++++++++++ common/btrfs-info.h | 220 ++++++++ common/filesystem-utils.c | 51 +- 5 files changed, 1437 insertions(+), 12 deletions(-) create mode 100644 common/btrfs-info.c create mode 100644 common/btrfs-info.h
Signature · 1 line
2.43.0
PATCH 1/4 btrfs-info: some utility to query a filesystem info
From: Goffredo Baroncelli <[email protected]>
btrfs_info_XXX are a set of function to query the layout of a btrfs filesystem.
Example:
const struct btrfs_info *bi;
int r = btrfs_info_find(path, &bi);
if (r < 0) {
printf("Error: %d - %s\n", -r, strerror(-r));
return;
}
print("Label: %s\n", bi->label);
print("UUID: %s\n", bi->uuid);
printf("Devices:");
for (const struct btrfs_info_device *bid = bi->devices ;
bid ; bid = bid->next)
printf("\t%s\n", bid->name);
if (bi->mounts) {
printf("Mounts:")
for (const btrfs_info_mount *bim = bi->mounts ;
bim ; bim = bim->next )
print("\t%s\n", bim->dest);
}The client doesn't have to manage any memory allocation/deletion. The functions return an internal list build on the basis of the information returned by /proc/self/mountinfo and libblkid. It is not required to be root; however only root can access the devid of the devices.
--- Makefile | 1 + common/btrfs-info.c | 1036 +++++++++++++++++++++++++++++++++++++++++++ common/btrfs-info.h | 220 +++++++++ 3 files changed, 1257 insertions(+) create mode 100644 common/btrfs-info.c create mode 100644 common/btrfs-info.h
Makefile +1 -0
@@ -213,6 +213,7 @@ objects = \ common/parse-utils.o \ common/path-utils.o \ common/rbtree-utils.o \ + common/btrfs-info.o \ common/send-stream.o \ common/send-utils.o \ common/sort-utils.o \
common/btrfs-info.c new file +1036 -0
1,036 lines not on this page · raw message
common/btrfs-info.h new file +220 -0
220 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/4 new command: btrfs filesystem info [<path>...]
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
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: 2mounts:
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
-----------------------------------------------------
--- cmds/filesystem.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
cmds/filesystem.c +44 -0
@@ -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
Signature · 1 line
2.43.0
PATCH 3/4 new command: btrfs filesystem get-info ...
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
usage: btrfs filesystem get-info <options> <filesystemspec>
This new command returns specific info of a btrfs filesystem.
<filesystemspec> is in the form of:
- UUID=<uuid>
- UUID_SUB=<subuuid>
- PARTUUID=<partuuid>
- LABEL=<label>
- btrfs <device>
- a valid btrfs path
and <option> is one of the following:
-d show one accessible device of the filesystem passed as argument.
-D show all the devices of the filesystem passed as argument.
-l show the label of the filesystem passed as argument.
-m show one accessible mountpoint of the filesystem passed as argument.
-M show all the mountpoints of the filesystem passed as argument.
-r show the rootpath name of the argument (which must be a path).
-s show the subvolume name of the argument (which must be a path).
-U show the UUID of the filesystem passed as argument. --is-mounted the 0 exit code is returned if the filesystem passed
as argument is mounted.
The intended use is as helper in the script.
Typical usage:
-----------------------------
$ ./btrfs fi get-info -D / /dev/sdd3 -----------------------------
--- cmds/filesystem.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+)
cmds/filesystem.c +97 -0
@@ -1546,6 +1546,102 @@ static int cmd_filesystem_info(const struct cmd_struct *cmd, } static DEFINE_SIMPLE_COMMAND(filesystem_info, "info"); +static const char *const cmd_filesystem_get_info_usage[] = { +"btrfs filesystem get-info -m|-M|-d|-s|-r|-D|-U|-l|--is-mounted UUID=<uuid>|UUID_SUB=<subuuid>|PARTUUID=<partuuid>|LABEL=<label>|<device>|<mount_point>" , +"Get some information of a btrfs filesystem." , +"One of the following option must be passed:" , +"-d show one accessible device of the filesystem passed as argument." , +"-D show all the devices of the filesystem passed as argument." , +"-l show the label of the filesystem passed as argument." , +"-m show one accessible mountpoint of the filesystem passed as argument." , +"-M show all the mountpoints of the filesystem passed as argument." , +"-r show the rootpath name of the argument (which must be a path)." , +"-s show the subvolume name of the argument (which must be a path)." , +"-U show the UUID of the filesystem passed as argument." , +"--is-mounted the 0 exit code is returned if the filesystem passed" , +" as argument is mounted." , +NULL +}; + +static int cmd_filesystem_get_info (const struct cmd_struct *cmd, +int argc,char **argv) +{ +if (check_argc_min (argc - optind,2 ) ||check_argc_max (argc - optind,2 )) +return 1 ; + +const struct btrfs_info *bi; +int r =btrfs_info_find (argv[2 ], &bi); +if (r <0 ) { +error ("Cannot get info for '%s': %d - %s\n" , argv[2 ], + -r,strerror (-r)); +return 2 ; + } + +if (!strcmp (argv[1 ],"-m" )) { +const char *p =btrfs_info_find_valid_path (bi); +if (!p) +return 100 ; +printf ("%s\n" , p); + }else if (!strcmp (argv[1 ],"-d" )) { +const char *p =btrfs_info_find_valid_dev (bi); +if (!p) +return 100 ; +printf ("%s\n" , p); + }else if (!strcmp (argv[1 ],"-s" ) || !strcmp (argv[1 ],"-r" )) { +const struct btrfs_info_mount *bm, *res =NULL ; +const char *winner =NULL ; +int winner_length =0 ; +char *path =realpath (argv[2 ],NULL ); +if (!path) { +fprintf (stderr,"ERROR: in realpath(%s)\n" , argv[2 ]); +return -EINVAL ; + } +int lpath =strlen (path); +for (bm = bi->mounts; bm; bm = bm->next) { +int l =strlen (bm->dest); +if (l > lpath) +continue ; +if (strncmp (bm->dest, path, lpath)) +continue ; +if (!winner || l > winner_length) { + winner = bm->dest; + winner_length = l; + res = bm; + } + } +free (path); +if (!res) +return 100 ; +if (!strcmp (argv[1 ],"-s" )) +printf ("%s\n" , res->subvol); +else /* -r */ +printf ("%s\n" , res->rootpath); + }else if (!strcmp (argv[1 ],"-M" )) { +const struct btrfs_info_mount *bm = bi->mounts; +while (bm) { +printf ("%s\n" , bm->dest); + bm = bm->next; + } + }else if (!strcmp (argv[1 ],"-D" )) { +const struct btrfs_info_device *bd = bi->devices; +while (bd) { +printf ("%s\n" , bd->name); + bd = bd->next; + } + }else if (!strcmp (argv[1 ],"-U" )) { +printf ("%s\n" , bi->uuid); + }else if (!strcmp (argv[1 ],"-l" )) { +printf ("%s\n" , bi->label); + }else if (!strcmp (argv[1 ],"--is-mounted" )) { +return btrfs_info_is_mounted (bi) ?0 :100 ; + }else { +error ("Unknown parameter '%s'" , argv[1 ]); + } + +return 0 ; +} +static DEFINE_SIMPLE_COMMAND(filesystem_get_info, "get-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." ,
@@ -1751,6 +1847,7 @@ static const struct cmd_group filesystem_cmd_group = { &cmd_struct_filesystem_resize, &cmd_struct_filesystem_label, &cmd_struct_filesystem_info, + &cmd_struct_filesystem_get_info, &cmd_struct_filesystem_usage, &cmd_struct_filesystem_mkswapfile,NULL
Signature · 1 line
2.43.0
PATCH 4/4 btrfs-progs: 'btrfs fi label' using a dev instead of a path
In reply to Goffredo BaroncelliFrom: Goffredo Baroncelli <[email protected]>
'btrfs fi label' has a strange behaviour: when the filesystem is unmounted, it has to be invoked against a device. When the filesystem is mounted, it has to be invoked against a filesystem path.
Even tough it has a technical explanation, this may confuses the users.
This patch allow to pass a device even in the latter cases. Alternatevely, it is possible to pass a UUID=xxx or a LABEL=zzz identification.
--- common/filesystem-utils.c | 51 ++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-)
common/filesystem-utils.c +39 -12
@@ -29,6 +29,7 @@#include "common/messages.h" #include "common/open-utils.h" #include "common/path-utils.h" +#include "common/btrfs-info.h" /* * For a given:
@@ -189,31 +190,57 @@ int get_label_mounted(const char *mount_path, char *labelp)return 0 ; } -int get_label (const char *btrfs_dev,char *label) +int get_label (const char *path,char *label) {int ret; +const struct btrfs_info *bi; - ret =path_is_reg_or_block_device (btrfs_dev); -if (!ret) - ret =get_label_mounted (btrfs_dev, label); -else if (ret >0 ) - ret =get_label_unmounted (btrfs_dev, label); + ret =btrfs_info_find (path, &bi); +if (ret == -ENOENT ) { +error ("'%s' is not a btrfs filesystem" , path); +return -ENOENT ; + } +if (ret <0 ) { +error ("unable to access btrfs filesystem info" ); +return -EINVAL ; + } + +if (btrfs_info_is_mounted (bi)) { + path =btrfs_info_find_valid_path (bi); + ret =get_label_mounted (path, label); + }else { + path =btrfs_info_find_valid_dev (bi); + ret =get_label_unmounted (path, label); + }return ret; } -int set_label (const char *btrfs_dev,const char *label) +int set_label (const char *path,const char *label) {int ret; +const struct btrfs_info *bi;if (check_label (label))return -1 ; - ret =path_is_reg_or_block_device (btrfs_dev); -if (!ret) - ret =set_label_mounted (btrfs_dev, label); -else if (ret >0 ) - ret =set_label_unmounted (btrfs_dev, label); + ret =btrfs_info_find (path, &bi); +if (ret == -ENOENT ) { +error ("'%s' is not a btrfs filesystem" , path); +return -ENOENT ; + } +if (ret <0 ) { +error ("unable to access btrfs filesystem info" ); +return -EINVAL ; + } + +if (btrfs_info_is_mounted (bi)) { + path =btrfs_info_find_valid_path (bi); + ret =set_label_mounted (path, label); + }else { + path =btrfs_info_find_valid_dev (bi); + ret =set_label_unmounted (path, label); + }return ret; }
Signature · 1 line
2.43.0