diff --git a/PVE/Storage/BTRFSPlugin.pm b/PVE/Storage/BTRFSPlugin.pm index 1407f44..63d307d 100644 --- a/PVE/Storage/BTRFSPlugin.pm +++ b/PVE/Storage/BTRFSPlugin.pm @@ -73,6 +73,7 @@ sub options { is_mountpoint => { optional => 1 }, nocow => { optional => 1 }, mkdir => { optional => 1 }, + preallocation => { optional => 1 }, # TODO: The new variant of mkdir with `populate` vs `create`... }; } diff --git a/PVE/Storage/CIFSPlugin.pm b/PVE/Storage/CIFSPlugin.pm index c5f3894..3a7e638 100644 --- a/PVE/Storage/CIFSPlugin.pm +++ b/PVE/Storage/CIFSPlugin.pm @@ -142,6 +142,7 @@ sub options { smbversion => { optional => 1}, mkdir => { optional => 1 }, bwlimit => { optional => 1 }, + preallocation => { optional => 1 }, }; } diff --git a/PVE/Storage/DirPlugin.pm b/PVE/Storage/DirPlugin.pm index 2267f11..3eeec98 100644 --- a/PVE/Storage/DirPlugin.pm +++ b/PVE/Storage/DirPlugin.pm @@ -59,6 +59,7 @@ sub options { mkdir => { optional => 1 }, is_mountpoint => { optional => 1 }, bwlimit => { optional => 1 }, + preallocation => { optional => 1 }, }; } diff --git a/PVE/Storage/GlusterfsPlugin.pm b/PVE/Storage/GlusterfsPlugin.pm index ea4df82..d8d2b88 100644 --- a/PVE/Storage/GlusterfsPlugin.pm +++ b/PVE/Storage/GlusterfsPlugin.pm @@ -137,6 +137,7 @@ sub options { format => { optional => 1 }, mkdir => { optional => 1 }, bwlimit => { optional => 1 }, + preallocation => { optional => 1 }, }; } @@ -260,7 +261,8 @@ sub alloc_image { my $cmd = ['/usr/bin/qemu-img', 'create']; - push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; + my $prealloc_opt = PVE::Storage::Plugin::preallocation_cmd_option($scfg, $fmt); + push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt); push @$cmd, '-f', $fmt, $volumepath, "${size}K"; diff --git a/PVE/Storage/NFSPlugin.pm b/PVE/Storage/NFSPlugin.pm index 39bf15a..21b288a 100644 --- a/PVE/Storage/NFSPlugin.pm +++ b/PVE/Storage/NFSPlugin.pm @@ -90,6 +90,7 @@ sub options { format => { optional => 1 }, mkdir => { optional => 1 }, bwlimit => { optional => 1 }, + preallocation => { optional => 1 }, }; } diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm index fab2316..aeb4fff 100644 --- a/PVE/Storage/Plugin.pm +++ b/PVE/Storage/Plugin.pm @@ -41,6 +41,19 @@ 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', @@ -152,6 +165,13 @@ my $defaultData = { type => 'string', format => 'pve-storage-format', optional => 1, }, + preallocation => { + description => "Preallocation mode for raw and qcow2 images. " . + "Using 'metadata' on raw images results in preallocation=off.", + type => 'string', enum => ['off', 'metadata', 'falloc', 'full'], + default => 'metadata', + optional => 1, + }, }, }; @@ -444,6 +464,31 @@ 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) @@ -764,7 +809,8 @@ sub alloc_image { } else { my $cmd = ['/usr/bin/qemu-img', 'create']; - push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; + my $prealloc_opt = preallocation_cmd_option($scfg, $fmt); + push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt); push @$cmd, '-f', $fmt, $path, "${size}K";