5
0
mirror of git://git.proxmox.com/git/pve-storage.git synced 2025-07-12 04:58:59 +03:00

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.
This commit is contained in:
Dietmar Maurer
2015-08-12 06:04:41 +02:00
parent dec97937f2
commit 35533c68fb
3 changed files with 56 additions and 38 deletions

View File

@ -613,7 +613,7 @@ sub vdisk_free {
# lock shared storage # lock shared storage
$plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { $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); $plugin->parse_volname($volname);
if ($isBase) { if ($isBase) {
my $vollist = $plugin->list_images($storeid, $scfg); 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; return if !$cleanup_worker;

View File

@ -18,7 +18,7 @@ sub plugindata {
return { return {
content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, none => 1 }, content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, none => 1 },
{ images => 1, rootdir => 1 }], { images => 1, rootdir => 1 }],
format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ], format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
}; };
} }

View File

@ -16,6 +16,8 @@ cfs_register_file ('storage.cfg',
sub { __PACKAGE__->parse_config(@_); }, sub { __PACKAGE__->parse_config(@_); },
sub { __PACKAGE__->write_config(@_); }); sub { __PACKAGE__->write_config(@_); });
# fixme: remove rootdir code (we now use subvols)
my $defaultData = { my $defaultData = {
propertyList => { propertyList => {
type => { description => "Storage type." }, type => { description => "Storage type." },
@ -157,7 +159,7 @@ PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
sub verify_format { sub verify_format {
my ($fmt, $noerr) = @_; my ($fmt, $noerr) = @_;
if ($fmt !~ m/(raw|qcow2|vmdk)/) { if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
return undef if $noerr; return undef if $noerr;
die "invalid format '$fmt'\n"; die "invalid format '$fmt'\n";
} }
@ -342,8 +344,8 @@ sub cluster_lock_storage {
sub parse_name_dir { sub parse_name_dir {
my $name = shift; my $name = shift;
if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) { if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
return ($1, $3, $2); return ($1, $3, $2); # (name, format, isBase)
} }
die "unable to parse volume filename '$name'\n"; die "unable to parse volume filename '$name'\n";
@ -356,12 +358,12 @@ sub parse_volname {
my ($basedvmid, $basename) = ($1, $2); my ($basedvmid, $basename) = ($1, $2);
parse_name_dir($basename); parse_name_dir($basename);
my ($vmid, $name) = ($3, $4); my ($vmid, $name) = ($3, $4);
my (undef, undef, $isBase) = parse_name_dir($name); my (undef, $format, $isBase) = parse_name_dir($name);
return ('images', $name, $vmid, $basename, $basedvmid, $isBase); return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
} elsif ($volname =~ m!^(\d+)/(\S+)$!) { } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
my ($vmid, $name) = ($1, $2); my ($vmid, $name) = ($1, $2);
my (undef, undef, $isBase) = parse_name_dir($name); my (undef, $format, $isBase) = parse_name_dir($name);
return ('images', $name, $vmid, undef, undef, $isBase); return ('images', $name, $vmid, undef, undef, $isBase, $format);
} elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) { } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
return ('iso', $1); return ('iso', $1);
} elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) { } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
@ -427,7 +429,7 @@ sub create_base {
# this only works for file based storage types # this only works for file based storage types
die "storage definintion has no path\n" if !$scfg->{path}; 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); $class->parse_volname($volname);
die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images'; 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 $path = $class->filesystem_path($scfg, $volname);
my ($size, $format, $used, $parent) = file_size_info($path); my ($size, undef, $used, $parent) = file_size_info($path);
die "file_size_info on '$volname' failed\n" if !($format && $size); die "file_size_info on '$volname' failed\n" if !($format && defined($size));
die "volname '$volname' contains wrong information about parent\n" die "volname '$volname' contains wrong information about parent\n"
if $basename && (!$parent || $parent ne "../$basevmid/$basename"); if $basename && (!$parent || $parent ne "../$basevmid/$basename");
@ -492,13 +494,15 @@ sub clone_image {
# this only works for file based storage types # this only works for file based storage types
die "storage definintion has no path\n" if !$scfg->{path}; 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); $class->parse_volname($volname);
die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; 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 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; die "clone_image only works on base images\n" if !$isBase;
my $imagedir = $class->get_subdir($scfg, 'images'); my $imagedir = $class->get_subdir($scfg, 'images');
@ -549,34 +553,46 @@ sub alloc_image {
die "disk image '$path' already exists\n" if -e $path; die "disk image '$path' already exists\n" if -e $path;
my $cmd = ['/usr/bin/qemu-img', 'create']; 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;
push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; (mkdir $path) || die "unable to create subvol '$path' - $!\n";
} else {
my $cmd = ['/usr/bin/qemu-img', 'create'];
push @$cmd, '-f', $fmt, $path, "${size}K"; push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
run_command($cmd, errmsg => "unable to create image"); push @$cmd, '-f', $fmt, $path, "${size}K";
run_command($cmd, errmsg => "unable to create image");
}
return "$vmid/$name"; return "$vmid/$name";
} }
sub free_image { sub free_image {
my ($class, $storeid, $scfg, $volname, $isBase) = @_; my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
my $path = $class->filesystem_path($scfg, $volname); my $path = $class->filesystem_path($scfg, $volname);
if (! -f $path) { if ($format eq 'subvol') {
warn "disk image '$path' does not exists\n"; File::Path::remove_tree($path);
return undef; } else {
}
if ($isBase) { if (! -f $path) {
# try to remove immutable flag warn "disk image '$path' does not exists\n";
eval { run_command(['/usr/bin/chattr', '-i', $path]); }; return undef;
warn $@ if $@; }
}
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; return undef;
} }
@ -584,6 +600,10 @@ sub free_image {
sub file_size_info { sub file_size_info {
my ($filename, $timeout) = @_; my ($filename, $timeout) = @_;
if (-d $filename) {
return wantarray ? (0, 'subvol', 0, undef) : 1;
}
my $cmd = ['/usr/bin/qemu-img', 'info', $filename]; my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
my $format; my $format;
@ -696,17 +716,15 @@ sub volume_has_feature {
my $features = { my $features = {
snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} }, snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
clone => { base => {qcow2 => 1, raw => 1, vmdk => 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}, copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
current => {qcow2 => 1, raw => 1, vmdk => 1}, current => {qcow2 => 1, raw => 1, vmdk => 1},
snap => {qcow2 => 1} }, snap => {qcow2 => 1} },
}; };
my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
$class->parse_volname($volname); $class->parse_volname($volname);
my (undef, $format) = parse_name_dir($name);
my $key = undef; my $key = undef;
if($snapname){ if($snapname){
$key = 'snap'; $key = 'snap';
@ -740,7 +758,7 @@ sub list_images {
next if !$vollist && defined($vmid) && ($owner ne $vmid); next if !$vollist && defined($vmid) && ($owner ne $vmid);
my ($size, $format, $used, $parent) = file_size_info($fn); my ($size, $format, $used, $parent) = file_size_info($fn);
next if !($format && $size); next if !($format && defined($size));
my $volid; my $volid;
if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) { if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {