[PATCH pve-storage 05/10] common: add qemu_img_info helper

Alexandre Derumier alexandre.derumier at groupe-cyllene.com
Fri Jul 4 08:45:02 CEST 2025


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

diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm
index 78e5320..c15cc88 100644
--- a/src/PVE/Storage/Common.pm
+++ b/src/PVE/Storage/Common.pm
@@ -171,4 +171,30 @@ sub qemu_img_create {
     run_command($cmd, errmsg => "unable to create image");
 }
 
+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;
+
+    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 5afe29b..81443aa 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -1031,26 +1031,8 @@ 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 = PVE::Storage::Common::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