plugin: add qemu_img_info
Signed-off-by: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
This commit is contained in:
committed by
Wolfgang Bumiller
parent
24fe1bf621
commit
933736ad6d
@ -5,6 +5,7 @@ use warnings;
|
|||||||
|
|
||||||
use PVE::JSONSchema;
|
use PVE::JSONSchema;
|
||||||
use PVE::Syscall;
|
use PVE::Syscall;
|
||||||
|
use PVE::Tools qw(run_command);
|
||||||
|
|
||||||
use constant {
|
use constant {
|
||||||
FALLOC_FL_KEEP_SIZE => 0x01, # see linux/falloc.h
|
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;
|
1;
|
||||||
|
|||||||
@ -691,6 +691,25 @@ sub qemu_img_create_qcow2_backed {
|
|||||||
run_command($cmd, errmsg => "unable to create image");
|
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
|
# Storage implementation
|
||||||
|
|
||||||
# called during addition of storage (before the new storage config got written)
|
# 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_size_info: '$filename': falling back to 'raw' from unknown format '$file_format'\n";
|
||||||
$file_format = 'raw';
|
$file_format = 'raw';
|
||||||
}
|
}
|
||||||
my $cmd = ['/usr/bin/qemu-img', 'info', '--output=json', $filename];
|
my $json = qemu_img_info($filename, $file_format, $timeout);
|
||||||
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;
|
|
||||||
}
|
|
||||||
if (!$json) {
|
if (!$json) {
|
||||||
die "failed to query file information with qemu-img\n" if $untrusted;
|
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.
|
# skip decoding if there was no output, e.g. if there was a timeout.
|
||||||
|
|||||||
Reference in New Issue
Block a user