Add reading of bcachefs usage when mounted (!123)

Currently bcachefs-tools only provides a method to report the file
system usage while it is mounted.  We won't make GParted mount a
bcachefs to read it's usage as we want to keep GParted's scanning as a
read-only activity.  Therefore GParted can't report the usage of an
unmounted bcachefs.

    # bcachefs format /dev/sdb1
    # bcachefs fs usage /dev/sdb1
    error opening /dev/sdb1: not a bcachefs filesystem
    # echo $?
    1
    # bcachefs fs usage --help
    bcachefs fs usage - display detailed filesystem usage
    Usage: bcachefs fs usage [OPTION]... <mountpoint>
    ...
    # mount /dev/sdb1 /mnt/1
    # bcachefs fs usage /mnt/1
    Filesystem: a61a8302-9a79-4c24-a9e6-486e7fcc78f5
    Size:                      987842560
    Used:                       12713984
    Online reserved:                   0

    Data type       Required/total  Durability    Devices
    btree:          1/1             1             [sdb1]               1048576

    (no label) (device 0):          sdb1              rw
                                    data         buckets    fragmented
      free:                   1061027840            8095
      sb:                        3149824              25        126976
      journal:                   8388608              64
      btree:                     1048576               8
      user:                            0               0
      cached:                          0               0
      parity:                          0               0
      stripe:                          0               0
      need_gc_gens:                    0               0
      need_discard:                    0               0
      capacity:               1073741824            8192
    # echo $?
    0

Closes !123 - Add support for bcachefs, single device file systems only
This commit is contained in:
Mike Fleetwood 2024-03-20 08:06:15 +00:00
parent 7ca99a1577
commit ccdf14ddb4
2 changed files with 38 additions and 0 deletions

View file

@ -32,6 +32,7 @@ class bcachefs : public FileSystem
{
public:
FS get_filesystem_support();
void set_used_sectors(Partition& partition);
bool create(const Partition& new_partition, OperationDetail& operationdetail);
};

View file

@ -40,6 +40,7 @@ FS bcachefs::get_filesystem_support()
if (! Glib::find_program_in_path("bcachefs").empty())
{
fs.online_read = FS::EXTERNAL;
fs.create = FS::EXTERNAL;
fs.create_with_label = FS::EXTERNAL;
}
@ -50,6 +51,42 @@ FS bcachefs::get_filesystem_support()
}
void bcachefs::set_used_sectors(Partition& partition)
{
// 'bcachefs fs usage' only reports usage for mounted file systems.
exit_status = Utils::execute_command("bcachefs fs usage " + Glib::shell_quote(partition.get_mountpoint()),
output, error, true);
if (exit_status != 0)
{
if (! output.empty())
partition.push_back_message(output);
if (! error.empty())
partition.push_back_message(error);
return;
}
// Device specific free space in bytes
long long dev_free_bytes = -1;
Glib::ustring::size_type index = output.find("free:");
if (index < output.length())
sscanf(output.substr(index).c_str(), "free: %lld", &dev_free_bytes);
// Device specific size in bytes
long long dev_capacity_bytes = -1;
index = output.find("capacity:");
if (index < output.length())
sscanf(output.substr(index).c_str(), "capacity: %lld", &dev_capacity_bytes);
if (dev_free_bytes > -1 && dev_capacity_bytes > -1)
{
Sector fs_size = dev_capacity_bytes / partition.sector_size;
Sector fs_free = dev_free_bytes / partition.sector_size;
partition.set_sector_usage(fs_size, fs_free);
// Block size is not available in 'bcachefs fs usage' output.
}
}
bool bcachefs::create(const Partition& new_partition, OperationDetail& operationdetail)
{
return ! execute_command("bcachefs format -L " + Glib::shell_quote(new_partition.get_filesystem_label()) +