--- Begin Message ---
Signed-off-by: Alexandre Derumier <alexandre.derum...@groupe-cyllene.com>
---
src/PVE/Storage/Common.pm | 53 ++++++++++++++++++++++++++++++
src/PVE/Storage/GlusterfsPlugin.pm | 2 +-
src/PVE/Storage/Plugin.pm | 47 +-------------------------
3 files changed, 55 insertions(+), 47 deletions(-)
diff --git a/src/PVE/Storage/Common.pm b/src/PVE/Storage/Common.pm
index bd9c951..0770d70 100644
--- a/src/PVE/Storage/Common.pm
+++ b/src/PVE/Storage/Common.pm
@@ -5,12 +5,26 @@ 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
FALLOC_FL_PUNCH_HOLE => 0x02, # see linux/falloc.h
};
+our $QCOW2_PREALLOCATION = {
+ off => 1,
+ metadata => 1,
+ falloc => 1,
+ full => 1,
+};
+
+our $RAW_PREALLOCATION = {
+ off => 1,
+ falloc => 1,
+ full => 1,
+};
+
=pod
=head1 NAME
@@ -107,4 +121,43 @@ sub deallocate : prototype($$$) {
}
}
+
+sub preallocation_cmd_option {
+ my ($scfg, $fmt) = @_;
+
+ my $prealloc = $scfg->{preallocation};
+
+ if ($fmt eq 'qcow2') {
+ $prealloc = $prealloc // 'metadata';
+
+ die "preallocation mode '$prealloc' not supported by format '$fmt'\n"
+ if !$QCOW2_PREALLOCATION->{$prealloc};
+
+ return "preallocation=$prealloc";
+ } elsif ($fmt eq 'raw') {
+ $prealloc = $prealloc // 'off';
+ $prealloc = 'off' if $prealloc eq 'metadata';
+
+ die "preallocation mode '$prealloc' not supported by format '$fmt'\n"
+ if !$RAW_PREALLOCATION->{$prealloc};
+
+ return "preallocation=$prealloc";
+ }
+
+ return;
+}
+
+sub qemu_img_create {
+ my ($scfg, $fmt, $size, $path) = @_;
+
+ my $cmd = ['/usr/bin/qemu-img', 'create'];
+
+ my $prealloc_opt = preallocation_cmd_option($scfg, $fmt);
+ push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
+
+ push @$cmd, '-f', $fmt, $path, "${size}K";
+
+ run_command($cmd, errmsg => "unable to create image");
+}
+
1;
diff --git a/src/PVE/Storage/GlusterfsPlugin.pm
b/src/PVE/Storage/GlusterfsPlugin.pm
index 18493cb..b1a541d 100644
--- a/src/PVE/Storage/GlusterfsPlugin.pm
+++ b/src/PVE/Storage/GlusterfsPlugin.pm
@@ -265,7 +265,7 @@ sub alloc_image {
my $cmd = ['/usr/bin/qemu-img', 'create'];
- my $prealloc_opt = PVE::Storage::Plugin::preallocation_cmd_option($scfg,
$fmt);
+ my $prealloc_opt = PVE::Storage::Common::preallocation_cmd_option($scfg,
$fmt);
push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
push @$cmd, '-f', $fmt, $volumepath, "${size}K";
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index 4e16420..7030e4e 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -47,19 +47,6 @@ our @SHARED_STORAGE = (
'pbs',
);
-our $QCOW2_PREALLOCATION = {
- off => 1,
- metadata => 1,
- falloc => 1,
- full => 1,
-};
-
-our $RAW_PREALLOCATION = {
- off => 1,
- falloc => 1,
- full => 1,
-};
-
our $MAX_VOLUMES_PER_GUEST = 1024;
cfs_register_file ('storage.cfg',
@@ -577,31 +564,6 @@ sub parse_config {
return $cfg;
}
-sub preallocation_cmd_option {
- my ($scfg, $fmt) = @_;
-
- my $prealloc = $scfg->{preallocation};
-
- if ($fmt eq 'qcow2') {
- $prealloc = $prealloc // 'metadata';
-
- die "preallocation mode '$prealloc' not supported by format '$fmt'\n"
- if !$QCOW2_PREALLOCATION->{$prealloc};
-
- return "preallocation=$prealloc";
- } elsif ($fmt eq 'raw') {
- $prealloc = $prealloc // 'off';
- $prealloc = 'off' if $prealloc eq 'metadata';
-
- die "preallocation mode '$prealloc' not supported by format '$fmt'\n"
- if !$RAW_PREALLOCATION->{$prealloc};
-
- return "preallocation=$prealloc";
- }
-
- return;
-}
-
# Storage implementation
# called during addition of storage (before the new storage config got written)
@@ -926,14 +888,7 @@ sub alloc_image {
umask $old_umask;
die $err if $err;
} else {
- my $cmd = ['/usr/bin/qemu-img', 'create'];
-
- my $prealloc_opt = preallocation_cmd_option($scfg, $fmt);
- push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
-
- push @$cmd, '-f', $fmt, $path, "${size}K";
-
- eval { run_command($cmd, errmsg => "unable to create image"); };
+ eval { PVE::Storage::Common::qemu_img_create($scfg, $fmt, $size, $path)
};
if ($@) {
unlink $path;
rmdir $imagedir;
--
2.39.5
--- End Message ---