Add labelling of mounted btrfs (#163)

Btrfs supports labelling of the file system while it is mounted.  This
was added into Linux kernel 3.10 [1] and btrfs-progs 3.12 [2].  As the
oldest supported distributions have the needed versions or newer,
unconditionally enable without any checking for availability.

    Distro             EOL        Linux kernel   btrfs-progs
    Debian 9           2022-Jun   4.19           4.7.3
    RHEL / CentOS 7    2024-Jun   3.10.0         4.9.1
    Ubuntu 18.04 LTS   2023-Apr   4.15.0         4.15.1

Unmounted btrfs is labelled via the block device containing it, where as
a mounted btrfs is labelled via it's mount point.

    # mkfs.btrfs -L initial_label /dev/sdb1
    # blkid /dev/sdb1
    /dev/sdb1: LABEL="initial_label" ...

    # btrfs filesystem label /dev/sdb1 unmounted_label_2
    # blkid /dev/sdb1
    /dev/sdb1: LABEL="unmounted_label_2" ...

    # mount /dev/sdb1 /mnt/1
    # btrfs filesystem label /dev/sdb1 mounted_label_3
    # blkid /dev/sdb1
    /dev/sdb1: LABEL="mounted_label_3" ...

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a8bfd4abea3da0e28f215e2a2b8c2f1ca27ebe80
    Btrfs: set/change the label of a mounted file system
[2] https://git.kernel.org/pub/scm/linux/kernel/git/kdave/btrfs-progs.git/commit/?id=619dc61cae1420da2dec48f689d49b9b346abc15
    Btrfs-progs: Change the label of a mounted file system

Closes #163 - Feature request: set label on a mounted btrfs
This commit is contained in:
Mike Fleetwood 2021-07-22 08:04:12 +01:00 committed by Curtis Gedak
parent 74bd376a50
commit eb034b1759
4 changed files with 29 additions and 5 deletions

View File

@ -79,12 +79,13 @@ struct FS
Support online_read; // Can and how to read sector usage while active Support online_read; // Can and how to read sector usage while active
Support online_grow; Support online_grow;
Support online_shrink; Support online_shrink;
Support online_write_label;
FS(FSType fstype_ = FS_UNSUPPORTED) : fstype(fstype_) FS(FSType fstype_ = FS_UNSUPPORTED) : fstype(fstype_)
{ {
busy = read = read_label = write_label = read_uuid = write_uuid = create = busy = read = read_label = write_label = read_uuid = write_uuid = create =
create_with_label = grow = shrink = move = check = copy = remove = online_read = create_with_label = grow = shrink = move = check = copy = remove = online_read =
online_grow = online_shrink = NONE; online_grow = online_shrink = online_write_label = NONE;
} }
}; };

View File

@ -2053,7 +2053,9 @@ bool GParted_Core::label_filesystem( const Partition & partition, OperationDetai
bool succes = false ; bool succes = false ;
FileSystem* p_filesystem = NULL ; FileSystem* p_filesystem = NULL ;
switch (get_fs(partition.fstype).write_label) const FS& fs_cap = get_fs(partition.fstype);
FS::Support support = (partition.busy) ? fs_cap.online_write_label : fs_cap.write_label;
switch (support)
{ {
case FS::EXTERNAL: case FS::EXTERNAL:
succes = (p_filesystem = get_filesystem_object(partition.fstype)) succes = (p_filesystem = get_filesystem_object(partition.fstype))

View File

@ -1299,6 +1299,14 @@ void Win_GParted::set_valid_operations()
} }
#endif #endif
// Allow labelling of mounted file systems that support it.
if (selected_filesystem.busy &&
selected_partition_ptr->status == STAT_REAL &&
fs_cap.online_write_label )
{
allow_label_filesystem(true);
}
// Only unmount/swapoff/VG deactivate or online actions allowed if busy // Only unmount/swapoff/VG deactivate or online actions allowed if busy
if ( selected_filesystem.busy ) if ( selected_filesystem.busy )
return ; return ;

View File

@ -66,6 +66,7 @@ FS btrfs::get_filesystem_support()
fs .read_uuid = FS::EXTERNAL ; fs .read_uuid = FS::EXTERNAL ;
fs.check = FS::EXTERNAL; fs.check = FS::EXTERNAL;
fs.write_label = FS::EXTERNAL; fs.write_label = FS::EXTERNAL;
fs.online_write_label = FS::EXTERNAL;
//Resizing of btrfs requires mount, umount and kernel //Resizing of btrfs requires mount, umount and kernel
// support as well as btrfs filesystem resize // support as well as btrfs filesystem resize
@ -261,13 +262,25 @@ void btrfs::set_used_sectors( Partition & partition )
} }
} }
bool btrfs::write_label( const Partition & partition, OperationDetail & operationdetail ) bool btrfs::write_label( const Partition & partition, OperationDetail & operationdetail )
{ {
return ! execute_command( "btrfs filesystem label " + Glib::shell_quote( partition.get_path() ) + // Use the mount point when labelling a mounted btrfs, or block device containing
" " + Glib::shell_quote( partition.get_filesystem_label() ), // the unmounted btrfs.
operationdetail, EXEC_CHECK_STATUS ); // btrfs filesystem label '/dev/PTN' 'NEWLABEL'
// btrfs filesystem label '/MNTPNT' 'NEWLABEL'
Glib::ustring path;
if (partition.busy)
path = partition.get_mountpoint();
else
path = partition.get_path();
return ! execute_command("btrfs filesystem label " + Glib::shell_quote(path) +
" " + Glib::shell_quote(partition.get_filesystem_label()),
operationdetail, EXEC_CHECK_STATUS);
} }
bool btrfs::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition ) bool btrfs::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
{ {
bool success = true ; bool success = true ;