[PATCH pve-storage 03/13] plugin: add qemu_img_info

Alexandre Derumier alexandre.derumier at groupe-cyllene.com
Wed Jul 9 18:21:50 CEST 2025


Signed-off-by: Alexandre Derumier <alexandre.derumier at groupe-cyllene.com>
---
 src/PVE/Storage/Common.pm | 33 ++++++++++++++++++++++++++++++++
 src/PVE/Storage/Plugin.pm | 40 +++++++++++++++++++--------------------
 2 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm
index 89a70f4..aa89e68 100644
--- a/src/PVE/Storage/Common.pm
+++ b/src/PVE/Storage/Common.pm
@@ -5,6 +5,7 @@ use warnings;
 
 use PVE::JSONSchema;
 use PVE::Syscall;
+use PVE::Tools qw(run_command);
 
 use constant {
     FALLOC_FL_KEEP_SIZE => 0x01, # see linux/falloc.h
@@ -110,4 +111,36 @@ sub deallocate : prototype($$$) {
     }
 }
 
+=pod
+
+=head3 run_qemu_img_json
+
+    run_qemu_img_json($cmd, $timeout)
+
+Execute qemu_img command C<$cmd> with a timeout C<$timeout>.
+Parse the output result and return a json.
+
+=cut
+
+sub run_qemu_img_json {
+    my ($cmd, $timeout) = @_;
+    my $json = '';
+    my $err_output = '';
+    eval {
+        run_command(
+            $cmd,
+            timeout => $timeout,
+            outfunc => sub { $json .= shift },
+            errfunc => sub { $err_output .= shift . "\n" },
+        );
+    };
+    warn $@ if $@;
+    if ($err_output) {
+        # if qemu did not output anything to stdout we die with stderr as an error
+        die $err_output if !$json;
+        # otherwise we warn about it and try to parse the json
+        warn $err_output;
+    }
+    return $json;
+}
 1;
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index c4b4cd3..9c79439 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -691,6 +691,25 @@ sub qemu_img_create_qcow2_backed {
     run_command($cmd, errmsg => "unable to create image");
 }
 
+=pod
+
+=head3 qemu_img_info
+
+    qemu_img_info($filename, $file_format, $timeout)
+
+Returns a json with qemu image C<$filename> informations with format <$file_format>.
+
+=cut
+
+sub qemu_img_info {
+    my ($filename, $file_format, $timeout) = @_;
+
+    my $cmd = ['/usr/bin/qemu-img', 'info', '--output=json', $filename];
+    push $cmd->@*, '-f', $file_format if $file_format;
+
+    return PVE::Storage::Common::run_qemu_img_json($cmd, $timeout);
+}
+
 # Storage implementation
 
 # called during addition of storage (before the new storage config got written)
@@ -1127,26 +1146,7 @@ sub file_size_info {
             "file_size_info: '$filename': falling back to 'raw' from unknown format '$file_format'\n";
         $file_format = 'raw';
     }
-    my $cmd = ['/usr/bin/qemu-img', 'info', '--output=json', $filename];
-    push $cmd->@*, '-f', $file_format if $file_format;
-
-    my $json = '';
-    my $err_output = '';
-    eval {
-        run_command(
-            $cmd,
-            timeout => $timeout,
-            outfunc => sub { $json .= shift },
-            errfunc => sub { $err_output .= shift . "\n" },
-        );
-    };
-    warn $@ if $@;
-    if ($err_output) {
-        # if qemu did not output anything to stdout we die with stderr as an error
-        die $err_output if !$json;
-        # otherwise we warn about it and try to parse the json
-        warn $err_output;
-    }
+    my $json = qemu_img_info($filename, $file_format, $timeout);
     if (!$json) {
         die "failed to query file information with qemu-img\n" if $untrusted;
         # skip decoding if there was no output, e.g. if there was a timeout.
-- 
2.39.5




More information about the pve-devel mailing list