From: Dietmar Maurer Date: Wed, 12 Aug 2015 04:04:41 +0000 (+0200) Subject: add subvol support for directory storage X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=35533c68fbcc6b57f1f3e2436360722f2382ca79;p=pve-storage.git add subvol support for directory storage This storage type does not support subvol quotas, so we only allow this if vdisk_alloc is called with size=0. --- diff --git a/PVE/Storage.pm b/PVE/Storage.pm index 1c622c4..1d953ef 100755 --- a/PVE/Storage.pm +++ b/PVE/Storage.pm @@ -613,7 +613,7 @@ sub vdisk_free { # lock shared storage $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { - my ($vtype, $name, $vmid, undef, undef, $isBase) = + my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = $plugin->parse_volname($volname); if ($isBase) { my $vollist = $plugin->list_images($storeid, $scfg); @@ -633,7 +633,7 @@ sub vdisk_free { } } } - $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase); + $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); }); return if !$cleanup_worker; diff --git a/PVE/Storage/DirPlugin.pm b/PVE/Storage/DirPlugin.pm index 41842f2..bc3c61f 100644 --- a/PVE/Storage/DirPlugin.pm +++ b/PVE/Storage/DirPlugin.pm @@ -18,7 +18,7 @@ sub plugindata { return { content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, none => 1 }, { images => 1, rootdir => 1 }], - format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ], + format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ], }; } diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm index 310a658..df02a33 100644 --- a/PVE/Storage/Plugin.pm +++ b/PVE/Storage/Plugin.pm @@ -16,6 +16,8 @@ cfs_register_file ('storage.cfg', sub { __PACKAGE__->parse_config(@_); }, sub { __PACKAGE__->write_config(@_); }); +# fixme: remove rootdir code (we now use subvols) + my $defaultData = { propertyList => { type => { description => "Storage type." }, @@ -157,7 +159,7 @@ PVE::JSONSchema::register_format('pve-storage-format', \&verify_format); sub verify_format { my ($fmt, $noerr) = @_; - if ($fmt !~ m/(raw|qcow2|vmdk)/) { + if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) { return undef if $noerr; die "invalid format '$fmt'\n"; } @@ -342,8 +344,8 @@ sub cluster_lock_storage { sub parse_name_dir { my $name = shift; - if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) { - return ($1, $3, $2); + if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) { + return ($1, $3, $2); # (name, format, isBase) } die "unable to parse volume filename '$name'\n"; @@ -356,12 +358,12 @@ sub parse_volname { my ($basedvmid, $basename) = ($1, $2); parse_name_dir($basename); my ($vmid, $name) = ($3, $4); - my (undef, undef, $isBase) = parse_name_dir($name); - return ('images', $name, $vmid, $basename, $basedvmid, $isBase); + my (undef, $format, $isBase) = parse_name_dir($name); + return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format); } elsif ($volname =~ m!^(\d+)/(\S+)$!) { my ($vmid, $name) = ($1, $2); - my (undef, undef, $isBase) = parse_name_dir($name); - return ('images', $name, $vmid, undef, undef, $isBase); + my (undef, $format, $isBase) = parse_name_dir($name); + return ('images', $name, $vmid, undef, undef, $isBase, $format); } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) { return ('iso', $1); } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) { @@ -427,7 +429,7 @@ sub create_base { # this only works for file based storage types die "storage definintion has no path\n" if !$scfg->{path}; - my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname); die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images'; @@ -436,8 +438,8 @@ sub create_base { my $path = $class->filesystem_path($scfg, $volname); - my ($size, $format, $used, $parent) = file_size_info($path); - die "file_size_info on '$volname' failed\n" if !($format && $size); + my ($size, undef, $used, $parent) = file_size_info($path); + die "file_size_info on '$volname' failed\n" if !($format && defined($size)); die "volname '$volname' contains wrong information about parent\n" if $basename && (!$parent || $parent ne "../$basevmid/$basename"); @@ -492,13 +494,15 @@ sub clone_image { # this only works for file based storage types die "storage definintion has no path\n" if !$scfg->{path}; - my ($vtype, $basename, $basevmid, undef, undef, $isBase) = + my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = $class->parse_volname($volname); die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; die "this storage type does not support clone_image on snapshot\n" if $snap; + die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol'; + die "clone_image only works on base images\n" if !$isBase; my $imagedir = $class->get_subdir($scfg, 'images'); @@ -549,41 +553,57 @@ sub alloc_image { die "disk image '$path' already exists\n" if -e $path; - my $cmd = ['/usr/bin/qemu-img', 'create']; - - push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; - - push @$cmd, '-f', $fmt, $path, "${size}K"; + if ($fmt eq 'subvol') { + # only allow this if size = 0, so that user knows what he is doing + die "storage does not support subvol quotas\n" if $size != 0; + + (mkdir $path) || die "unable to create subvol '$path' - $!\n"; + } else { + my $cmd = ['/usr/bin/qemu-img', 'create']; - run_command($cmd, errmsg => "unable to create image"); + push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; + + push @$cmd, '-f', $fmt, $path, "${size}K"; + run_command($cmd, errmsg => "unable to create image"); + } + return "$vmid/$name"; } sub free_image { - my ($class, $storeid, $scfg, $volname, $isBase) = @_; + my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_; my $path = $class->filesystem_path($scfg, $volname); - if (! -f $path) { - warn "disk image '$path' does not exists\n"; - return undef; - } - - if ($isBase) { - # try to remove immutable flag - eval { run_command(['/usr/bin/chattr', '-i', $path]); }; - warn $@ if $@; - } + if ($format eq 'subvol') { + File::Path::remove_tree($path); + } else { + + if (! -f $path) { + warn "disk image '$path' does not exists\n"; + return undef; + } - unlink($path) || die "unlink '$path' failed - $!\n"; + if ($isBase) { + # try to remove immutable flag + eval { run_command(['/usr/bin/chattr', '-i', $path]); }; + warn $@ if $@; + } + unlink($path) || die "unlink '$path' failed - $!\n"; + } + return undef; } sub file_size_info { my ($filename, $timeout) = @_; + if (-d $filename) { + return wantarray ? (0, 'subvol', 0, undef) : 1; + } + my $cmd = ['/usr/bin/qemu-img', 'info', $filename]; my $format; @@ -696,17 +716,15 @@ sub volume_has_feature { my $features = { snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} }, clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} }, - template => { current => {qcow2 => 1, raw => 1, vmdk => 1} }, + template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} }, copy => { base => {qcow2 => 1, raw => 1, vmdk => 1}, current => {qcow2 => 1, raw => 1, vmdk => 1}, snap => {qcow2 => 1} }, }; - my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname); - my (undef, $format) = parse_name_dir($name); - my $key = undef; if($snapname){ $key = 'snap'; @@ -740,7 +758,7 @@ sub list_images { next if !$vollist && defined($vmid) && ($owner ne $vmid); my ($size, $format, $used, $parent) = file_size_info($fn); - next if !($format && $size); + next if !($format && defined($size)); my $volid; if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {