[gparted] Use btrfs inspect-internal dump-super to read UUID (!105)



commit be895fb98eec6f505c9ce45f16d5f1f28750bcda
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Fri Jul 22 17:59:24 2022 +0100

    Use btrfs inspect-internal dump-super to read UUID (!105)
    
    GParted so far uses 'btrfs filesystem show' to read the file system
    UUID.  But this doesn't work on a file system image so doesn't work in
    the GitLab CI test jobs, as discussed in the earlier commit "Use btrfs
    filesystem label to read the FS label (!105)".
        $ truncate -s 256M /tmp/test.img
        $ mkfs.btrfs /tmp/test.img
        ...
        UUID:               5ea62f88-fef3-4ece-a726-b88f3d81fe1c
        ...
        $ btrfs filesystem show /tmp/test.img
        ERROR: not a valid btrfs filesystem: /tmp/test.img
    
    Instead use 'btrfs inspect-internal dump-super' which works on image
    files too.
        $ btrfs inspect-internal dump-super /tmp/test.img
        ...
        fsid                    5ea62f88-fef3-4ece-a726-b88f3d81fe1c
        ...
    
    'btrfs inspect-internal dump-super' was added in btrfs-progs 4.5 [1][2]
    so is available in the oldest supported distributions and can be used
    unconditionally.
        Distro            EOL        btrfs --version
        Debian 10         2022-Jun   v4.20
        RHEL / CentOS 7   2024-Jun   v4.9.1
        Ubuntu 18.04 LTS  2023-Apr   v4.15.1
        SLES 12 SP5       2024-Oct   v4.5.3    [3][4]
    
    Unfortunately it returns 0 status on failure so use non-empty stderr to
    identify failure.
        $ rm /tmp/test.img
        $ truncate -s 256M /tmp/test.img
        $ btrfs inspect-internal dump-super /tmp/test.img 1> /dev/null
        ERROR: bad magic on superblock on /tmp/test.img at 65536
        $ echo $?
        0
    
    [1] btrfs-progs: introduce inspect-internal dump-super
        
https://git.kernel.org/pub/scm/linux/kernel/git/kdave/btrfs-progs.git/commit/?id=eaa93e3b0295fc94c774ec73056559a6b8c78b42
    [2] Btrfs progs release 4.5
        https://lore.kernel.org/linux-btrfs/20160320235330 GG21722 suse cz/
        "* new/moved commands
           * btrfs-show-super -> btrfs inspect-internal dump-super
        "
    [3] SUSE Long Term Service Pack Support
        
https://links.imagerelay.com/cdn/3404/ql/f3a083e9bcd34c76addd096d7f60ec00/long_term_service_pack_support_flyer.pdf
    [4] SUSE package search
        
https://scc.suse.com/packages?name=SUSE%20Linux%20Enterprise%20Server&version=12.5&arch=x86_64&query=btrfsprogs&module=
    
    Closes !105 - Update used btrfs file system commands, new minimum is
                  btrfs-progs 4.5

 src/btrfs.cc | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
---
diff --git a/src/btrfs.cc b/src/btrfs.cc
index bb80d529..2d279ffb 100644
--- a/src/btrfs.cc
+++ b/src/btrfs.cc
@@ -61,7 +61,8 @@ FS btrfs::get_filesystem_support()
                //     btrfs filesystem label
                //     btrfs filesystem resize
                //     btrfs filesystem show
-               // as they are all available in btrfs-progs >= 3.12.
+               //     btrfs inspect-internal dump-super
+               // as they are all available in btrfs-progs >= 4.5.
 
                fs.read = FS::EXTERNAL;
                fs .read_label = FS::EXTERNAL ;
@@ -361,26 +362,25 @@ void btrfs::read_label(Partition& partition)
 }
 
 
-void btrfs::read_uuid( Partition & partition )
+void btrfs::read_uuid(Partition& partition)
 {
-       Utils::execute_command("btrfs filesystem show " + Glib::shell_quote(partition.get_path()),
+       Utils::execute_command("btrfs inspect-internal dump-super " + Glib::shell_quote(partition.get_path()),
                               output, error, true);
-       //In many cases the exit status doesn't reflect valid output or an error condition
-       //  so rely on parsing the output to determine success.
-
-       Glib::ustring uuid_str = Utils::regexp_label( output, "uuid:[[:blank:]]*(" 
RFC4122_NONE_NIL_UUID_REGEXP ")" ) ;
-       if ( ! uuid_str .empty() )
-               partition .uuid = uuid_str ;
-       else
+       // btrfs inspect-internal dump-super returns zero exit status for both success and
+       // failure.  Instead use non-empty stderr to identify failure.
+       if (! error.empty())
        {
-               if ( ! output .empty() )
-                       partition.push_back_message( output );
-
-               if ( ! error .empty() )
-                       partition.push_back_message( error );
+               if (! output.empty())
+                       partition.push_back_message(output);
+               if (! error.empty())
+                       partition.push_back_message(error);
+               return;
        }
+
+       partition.uuid = Utils::regexp_label(output, "^fsid[[:blank:]]*(" RFC4122_NONE_NIL_UUID_REGEXP ")");
 }
 
+
 bool btrfs::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
        return ! execute_command( "btrfstune -f -u " + Glib::shell_quote( partition.get_path() ),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]