[pve-devel] [PATCH pve-storage 06/10] common: add qemu-img measure

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri Jul 4 13:51:56 CEST 2025


> Alexandre Derumier via pve-devel <pve-devel at lists.proxmox.com> hat am 04.07.2025 08:45 CEST geschrieben:

> ---
>  src/PVE/Storage/Common.pm | 28 ++++++++++++++++++++++++++++
>  1 file changed, 28 insertions(+)
> 
> diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm
> index c15cc88..e73eeab 100644
> --- a/src/PVE/Storage/Common.pm
> +++ b/src/PVE/Storage/Common.pm
> @@ -197,4 +197,32 @@ sub qemu_img_info {
>      return $json;
>  }
>  
> +sub qemu_img_measure {
> +    my ($size, $fmt, $timeout, $options) = @_;
> +
> +    die "format is missing" if !$fmt;
> +
> +    my $cmd = ['/usr/bin/qemu-img', 'measure', '--output=json', '--size', "${size}K", '-O', $fmt];
> +    push $cmd->@*, '-o', join(',', @$options) if @$options > 0;
> +
> +    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;

this is identical to qemu_img_info modulo the generated command, so I'd add a
follow-up to extract the output handling into a helper:

commit 9053bf5593b097d484ce52c3ffe831138c2bb208
Author:     Fabian Grünbichler <f.gruenbichler at proxmox.com>
AuthorDate: Fri Jul 4 11:01:13 2025 +0200
Commit:     Fabian Grünbichler <f.gruenbichler at proxmox.com>
CommitDate: Fri Jul 4 11:01:13 2025 +0200

    helpers: add `qemu-img .. --json` run helper
    
    Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>

diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm
index 43f3f15..ffcdab4 100644
--- a/src/PVE/Storage/Common.pm
+++ b/src/PVE/Storage/Common.pm
@@ -171,12 +171,8 @@ sub qemu_img_create {
     run_command($cmd, errmsg => "unable to create image");
 }
 
-sub qemu_img_info {
-    my ($filename, $file_format, $timeout, $follow_backing_files) = @_;
-
-    my $cmd = ['/usr/bin/qemu-img', 'info', '--output=json', $filename];
-    push $cmd->@*, '-f', $file_format if $file_format;
-    push $cmd->@*, '--backing-chain' if $follow_backing_files;
+my sub run_qemu_img_json {
+    my ($cmd, $timeout) = @_;
 
     my $json = '';
     my $err_output = '';
@@ -198,6 +194,16 @@ sub qemu_img_info {
     return $json;
 }
 
+sub qemu_img_info {
+    my ($filename, $file_format, $timeout, $follow_backing_files) = @_;
+
+    my $cmd = ['/usr/bin/qemu-img', 'info', '--output=json', $filename];
+    push $cmd->@*, '-f', $file_format if $file_format;
+    push $cmd->@*, '--backing-chain' if $follow_backing_files;
+
+    return run_qemu_img_json($cmd, $timeout);
+}
+
 sub qemu_img_measure {
     my ($size, $fmt, $timeout, $options) = @_;
 
@@ -206,24 +212,7 @@ sub qemu_img_measure {
     my $cmd = ['/usr/bin/qemu-img', 'measure', '--output=json', '--size', "${size}K", '-O', $fmt];
     push $cmd->@*, '-o', join(',', @$options) if @$options > 0;
 
-    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;
+    return run_qemu_img_json($cmd, $timeout);
 }
 
 1;


> +}
> +
>  1;
> -- 
> 2.39.5




More information about the pve-devel mailing list