X-Git-Url: https://git.proxmox.com/?p=pve-storage.git;a=blobdiff_plain;f=PVE%2FStorage%2FZFSPoolPlugin.pm;h=5f6befd23d57bc28b28f69eeca989aa826e19784;hp=6b095f1b62bceaccf5d8118ab0354b9563346b60;hb=95dfa44ca1a41bd5913c5b7c66adcf8291fac6d4;hpb=d6d924d0bf9485329d15bfc29ddc177c3370dc30 diff --git a/PVE/Storage/ZFSPoolPlugin.pm b/PVE/Storage/ZFSPoolPlugin.pm index 6b095f1..5f6befd 100644 --- a/PVE/Storage/ZFSPoolPlugin.pm +++ b/PVE/Storage/ZFSPoolPlugin.pm @@ -2,11 +2,15 @@ package PVE::Storage::ZFSPoolPlugin; use strict; use warnings; + use IO::File; +use Net::IP; use POSIX; -use PVE::Tools qw(run_command); -use PVE::Storage::Plugin; +use PVE::ProcFSTools; +use PVE::RPCEnvironment; +use PVE::Storage::Plugin; +use PVE::Tools qw(run_command); use base qw(PVE::Storage::Plugin); @@ -31,6 +35,10 @@ sub properties { description => "use sparse volumes", type => 'boolean', }, + mountpoint => { + description => "mount point", + type => 'string', format => 'pve-storage-path', + }, }; } @@ -41,49 +49,14 @@ sub options { sparse => { optional => 1 }, nodes => { optional => 1 }, disable => { optional => 1 }, - maxfiles => { optional => 1 }, content => { optional => 1 }, + bwlimit => { optional => 1 }, + mountpoint => { optional => 1 }, }; } # static zfs helper methods -sub zfs_parse_size { - my ($text) = @_; - - return 0 if !$text; - - if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) { - - my ($size, $reminder, $unit) = ($1, $2, $3); - - if ($unit) { - if ($unit eq 'K') { - $size *= 1024; - } elsif ($unit eq 'M') { - $size *= 1024*1024; - } elsif ($unit eq 'G') { - $size *= 1024*1024*1024; - } elsif ($unit eq 'T') { - $size *= 1024*1024*1024*1024; - } else { - die "got unknown zfs size unit '$unit'\n"; - } - } - - if ($reminder) { - $size = ceil($size); - } - - return $size; - - } - - warn "unable to parse zfs size '$text'\n"; - - return 0; -} - sub zfs_parse_zvol_list { my ($text) = @_; @@ -98,25 +71,24 @@ sub zfs_parse_zvol_list { my $zvol = {}; my @parts = split /\//, $dataset; + next if scalar(@parts) < 2; # we need pool/name my $name = pop @parts; my $pool = join('/', @parts); - next unless $name =~ m!^(vm|base|subvol)-(\d+)-(\S+)$!; + next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!; $zvol->{owner} = $2; - $name = $pool . '/' . $name; - $zvol->{pool} = $pool; $zvol->{name} = $name; if ($type eq 'filesystem') { if ($refquota eq 'none') { $zvol->{size} = 0; } else { - $zvol->{size} = zfs_parse_size($refquota); + $zvol->{size} = $refquota + 0; } $zvol->{format} = 'subvol'; } else { - $zvol->{size} = zfs_parse_size($size); + $zvol->{size} = $size + 0; $zvol->{format} = 'raw'; } if ($origin !~ /^-$/) { @@ -131,8 +103,10 @@ sub zfs_parse_zvol_list { sub parse_volname { my ($class, $volname) = @_; - if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm|subvol)?-(\d+)-\S+)$/) { - return ('images', $5, $8, $2, $4, $6); + if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) { + my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw'; + my $isBase = ($6 eq 'base' || $6 eq 'basevol'); + return ('images', $5, $7, $2, $4, $isBase, $format); } die "unable to parse zfs volume name '$volname'\n"; @@ -140,20 +114,45 @@ sub parse_volname { # virtual zfs methods (subclass can overwrite them) +sub on_add_hook { + my ($class, $storeid, $scfg, %param) = @_; + + my $cfg_mountpoint = $scfg->{mountpoint}; + + # ignore failure, pool might currently not be imported + my $mountpoint; + eval { + my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool}, 1); + $mountpoint = PVE::Storage::Plugin::verify_path($res, 1) if defined($res); + }; + + if (defined($cfg_mountpoint)) { + if (defined($mountpoint) && !($cfg_mountpoint =~ m|^\Q$mountpoint\E/?$|)) { + warn "warning for $storeid - mountpoint: $cfg_mountpoint " . + "does not match current mount point: $mountpoint\n"; + } + } else { + $scfg->{mountpoint} = $mountpoint; + } + + return; +} + sub path { - my ($class, $scfg, $volname) = @_; + my ($class, $scfg, $volname, $storeid, $snapname) = @_; my ($vtype, $name, $vmid) = $class->parse_volname($volname); my $path = ''; + my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}"; if ($vtype eq "images") { - if ($volname =~ m/^subvol-/) { - # fixme: we currently assume standard mount point?! - $path = "/$scfg->{pool}/$volname"; + if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) { + $path = "$mountpoint/$name"; } else { - $path = "/dev/zvol/$scfg->{pool}/$volname"; + $path = "/dev/zvol/$scfg->{pool}/$name"; } + $path .= "\@$snapname" if defined($snapname); } else { die "$vtype is not allowed in ZFSPool!"; } @@ -164,56 +163,72 @@ sub path { sub zfs_request { my ($class, $scfg, $timeout, $method, @params) = @_; - $timeout = 5 if !$timeout; - my $cmd = []; if ($method eq 'zpool_list') { push @$cmd, 'zpool', 'list'; + } elsif ($method eq 'zpool_import') { + push @$cmd, 'zpool', 'import'; + $timeout = 15 if !$timeout || $timeout < 15; } else { push @$cmd, 'zfs', $method; } - push @$cmd, @params; - + my $msg = ''; + my $output = sub { $msg .= "$_[0]\n" }; - my $output = sub { - my $line = shift; - $msg .= "$line\n"; - }; + $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout; run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout); return $msg; } +sub zfs_wait_for_zvol_link { + my ($class, $scfg, $volname, $timeout) = @_; + + my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10; + $timeout = $default_timeout if !defined($timeout); + + my ($devname, undef, undef) = $class->path($scfg, $volname); + + for (my $i = 1; $i <= $timeout; $i++) { + last if -b $devname; + die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n" + if $i == $timeout; + + sleep(1); + } +} + sub alloc_image { my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; my $volname = $name; - + if ($fmt eq 'raw') { - die "illegal name '$volname' - sould be 'vm-$vmid-*'\n" + die "illegal name '$volname' - should be 'vm-$vmid-*'\n" if $volname && $volname !~ m/^vm-$vmid-/; - $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) + $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname; $class->zfs_create_zvol($scfg, $volname, $size); - my $devname = "/dev/zvol/$scfg->{pool}/$volname"; - - run_command("udevadm trigger --subsystem-match block"); - system("udevadm settle --timeout 10 --exit-if-exists=${devname}"); + $class->zfs_wait_for_zvol_link($scfg, $volname); } elsif ( $fmt eq 'subvol') { - - die "subvolume allocation without name\n" if !$volname; - die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n" + + die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" + if $volname && $volname !~ m/^subvol-$vmid-/; + $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) + if !$volname; + + die "illegal name '$volname' - should be 'subvol-$vmid-*'\n" if $volname !~ m/^subvol-$vmid-/; - $class->zfs_create_subvol($scfg, $volname, $size); - + $class->zfs_create_subvol($scfg, $volname, $size); + } else { die "unsupported format '$fmt'"; } @@ -242,43 +257,48 @@ sub list_images { foreach my $image (keys %$dat) { - my $volname = $dat->{$image}->{name}; - my $parent = $dat->{$image}->{parent}; + my $info = $dat->{$image}; - my $volid = undef; - if ($parent && $parent =~ m/^(\S+)@(\S+)$/) { + my $volname = $info->{name}; + my $parent = $info->{parent}; + my $owner = $info->{vmid}; + + if ($parent && $parent =~ m/^(\S+)\@__base__$/) { my ($basename) = ($1); - $volid = "$storeid:$basename/$volname"; + $info->{volid} = "$storeid:$basename/$volname"; } else { - $volid = "$storeid:$volname"; + $info->{volid} = "$storeid:$volname"; } - my $owner = $dat->{$volname}->{vmid}; if ($vollist) { - my $found = grep { $_ eq $volid } @$vollist; + my $found = grep { $_ eq $info->{volid} } @$vollist; next if !$found; } else { next if defined ($vmid) && ($owner ne $vmid); } - my $info = $dat->{$volname}; - $info->{volid} = $volid; push @$res, $info; } } return $res; } +sub zfs_get_properties { + my ($class, $scfg, $properties, $dataset, $timeout) = @_; + + my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value', + '-Hp', $properties, $dataset); + my @values = split /\n/, $result; + return wantarray ? @values : $values[0]; +} + sub zfs_get_pool_stats { my ($class, $scfg) = @_; my $available = 0; my $used = 0; - my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp', - 'available,used', $scfg->{pool}); - - my @lines = split /\n/, $text; + my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool}); if($lines[0] =~ /^(\d+)$/) { $available = $1; @@ -291,21 +311,14 @@ sub zfs_get_pool_stats { return ($available, $used); } -sub zfs_get_zvol_size { - my ($class, $scfg, $zvol) = @_; - - my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol"); - - if ($text =~ /volsize\s(\d+)/) { - return $1; - } - - die "Could not get zvol size"; -} - sub zfs_create_zvol { my ($class, $scfg, $zvol, $size) = @_; - + + # always align size to 1M as workaround until + # https://github.com/zfsonlinux/zfs/issues/8541 is solved + my $padding = (1024 - $size % 1024) % 1024; + $size = $size + $padding; + my $cmd = ['create']; push @$cmd, '-s' if $scfg->{sparse}; @@ -321,8 +334,10 @@ sub zfs_create_subvol { my ($class, $scfg, $volname, $size) = @_; my $dataset = "$scfg->{pool}/$volname"; - - my $cmd = ['create', '-o', "refquota=${size}k", $dataset]; + my $quota = $size ? "${size}k" : "none"; + + my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa', + '-o', "refquota=${quota}", $dataset]; $class->zfs_request($scfg, undef, @$cmd); } @@ -338,6 +353,9 @@ sub zfs_delete_zvol { if ($err = $@) { if ($err =~ m/^zfs error:(.*): dataset is busy.*/) { sleep(1); + } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) { + $err = undef; + last; } else { die $err; } @@ -352,7 +370,7 @@ sub zfs_delete_zvol { sub zfs_list_zvol { my ($class, $scfg) = @_; - my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr'); + my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp'); my $zvols = zfs_parse_zvol_list($text); return undef if !$zvols; @@ -377,48 +395,21 @@ sub zfs_list_zvol { return $list; } -sub zfs_find_free_diskname { - my ($class, $storeid, $scfg, $vmid) = @_; - - my $name = undef; - my $volumes = $class->zfs_list_zvol($scfg); +sub zfs_get_sorted_snapshot_list { + my ($class, $scfg, $volname, $sort_params) = @_; - my $disk_ids = {}; - my $dat = $volumes->{$scfg->{pool}}; + my $vname = ($class->parse_volname($volname))[1]; - foreach my $image (keys %$dat) { - my $volname = $dat->{$image}->{name}; - if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){ - $disk_ids->{$2} = 1; - } - } - - for (my $i = 1; $i < 100; $i++) { - if (!$disk_ids->{$i}) { - return "vm-$vmid-disk-$i"; - } - } - - die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"; -} - -sub zfs_get_latest_snapshot { - my ($class, $scfg, $volname) = @_; - - # abort rollback if snapshot is not the latest - my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation'); - my $text = zfs_request($class, $scfg, undef, 'list', @params); + my @params = ('-H', '-t', 'snapshot', '-o', 'name', $sort_params->@*, "$scfg->{pool}\/$vname"); + my $text = $class->zfs_request($scfg, undef, 'list', @params); my @snapshots = split(/\n/, $text); - my $recentsnap; - foreach (@snapshots) { - if (/$scfg->{pool}\/$volname/) { - s/^.*@//; - $recentsnap = $_; - } + my $snap_names = []; + for my $snapshot (@snapshots) { + (my $snap_name = $snapshot) =~ s/^.*@//; + push $snap_names->@*, $snap_name; } - - return $recentsnap; + return $snap_names; } sub status { @@ -442,52 +433,148 @@ sub status { sub volume_size_info { my ($class, $scfg, $storeid, $volname, $timeout) = @_; - return $class->zfs_get_zvol_size($scfg, $volname); + my (undef, $vname, undef, undef, undef, undef, $format) = + $class->parse_volname($volname); + + my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; + my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname"); + if ($value =~ /^(\d+)$/) { + return $1; + } + + die "Could not get zfs volume size\n"; } sub volume_snapshot { my ($class, $scfg, $storeid, $volname, $snap) = @_; - $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap"); + my $vname = ($class->parse_volname($volname))[1]; + + $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap"); } sub volume_snapshot_delete { my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; - $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap"); + my $vname = ($class->parse_volname($volname))[1]; + + $class->deactivate_volume($storeid, $scfg, $vname, $snap, {}); + $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap"); } sub volume_snapshot_rollback { my ($class, $scfg, $storeid, $volname, $snap) = @_; - zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap"); + my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname); + + my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap"); + + # we have to unmount rollbacked subvols, to invalidate wrong kernel + # caches, they get mounted in activate volume again + # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931 + if ($format eq 'subvol') { + eval { $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); }; + if (my $err = $@) { + die $err if $err !~ m/not currently mounted$/; + } + } + + return $msg; } sub volume_rollback_is_possible { - my ($class, $scfg, $storeid, $volname, $snap) = @_; - - my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); - if ($snap ne $recentsnap) { - die "can't rollback, more recent snapshots exist\n"; + my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_; + + # can't use '-S creation', because zfs list won't reverse the order when the + # creation time is the same second, breaking at least our tests. + my $snapshots = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-s', 'creation']); + + my $found; + $blockers //= []; # not guaranteed to be set by caller + for my $snapshot ($snapshots->@*) { + if ($snapshot eq $snap) { + $found = 1; + } elsif ($found) { + push $blockers->@*, $snapshot; + } } - return 1; + my $volid = "${storeid}:${volname}"; + + die "can't rollback, snapshot '$snap' does not exist on '$volid'\n" + if !$found; + + die "can't rollback, '$snap' is not most recent snapshot on '$volid'\n" + if scalar($blockers->@*) > 0; + + return 1; +} + +sub volume_snapshot_info { + my ($class, $scfg, $storeid, $volname) = @_; + + my $vname = ($class->parse_volname($volname))[1]; + + my @params = ('-Hp', '-t', 'snapshot', '-o', 'name,guid,creation', "$scfg->{pool}\/$vname"); + my $text = $class->zfs_request($scfg, undef, 'list', @params); + my @lines = split(/\n/, $text); + + my $info = {}; + for my $line (@lines) { + my ($snapshot, $guid, $creation) = split(/\s+/, $line); + (my $snap_name = $snapshot) =~ s/^.*@//; + + $info->{$snap_name} = { + id => $guid, + timestamp => $creation, + }; + } + return $info; +} + +my sub dataset_mounted_heuristic { + my ($dataset) = @_; + + my $mounts = PVE::ProcFSTools::parse_proc_mounts(); + for my $mp (@$mounts) { + my ($what, $dir, $fs) = $mp->@*; + next if $fs ne 'zfs'; + # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off') + # If any child is mounted heuristically assume that `zfs mount -a` was successful + next if $what !~ m!^$dataset(?:/|$)!; + return 1; + } + return 0; } sub activate_storage { my ($class, $storeid, $scfg, $cache) = @_; - my @param = ('-o', 'name', '-H'); + # Note: $scfg->{pool} can include dataset / + my $dataset = $scfg->{pool}; + my $pool = ($dataset =~ s!/.*$!!r); + + return 1 if dataset_mounted_heuristic($dataset); # early return - my $text = zfs_request($class, $scfg, undef, 'zpool_list', @param); + my $pool_imported = sub { + my @param = ('-o', 'name', '-H', $pool); + my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) }; + warn "$@\n" if $@; - # Note: $scfg->{pool} can include dataset / - my $pool = $scfg->{pool}; - $pool =~ s!/.*$!!; + return defined($res) && $res =~ m/$pool/; + }; - if ($text !~ $pool) { - run_command("zpool import -d /dev/disk/by-id/ -a"); + if (!$pool_imported->()) { + # import can only be done if not yet imported! + my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool); + eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) }; + if (my $err = $@) { + # just could've raced with another import, so recheck if it is imported + die "could not activate storage '$storeid', $err\n" if !$pool_imported->(); + } } + eval { $class->zfs_request($scfg, undef, 'mount', '-a') }; + die "could not activate storage '$storeid', $@\n" if $@; return 1; } @@ -497,12 +584,26 @@ sub deactivate_storage { } sub activate_volume { - my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; + my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; + + return 1 if defined($snapname); + + my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname); + + if ($format eq 'raw') { + $class->zfs_wait_for_zvol_link($scfg, $volname); + } elsif ($format eq 'subvol') { + my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset"); + if ($mounted !~ m/^yes$/) { + $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset"); + } + } + return 1; } sub deactivate_volume { - my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; + my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; return 1; } @@ -511,16 +612,22 @@ sub clone_image { $snap ||= '__base__'; - my ($vtype, $basename, $basevmid, undef, undef, $isBase) = + my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = $class->parse_volname($volname); die "clone_image only works on base images\n" if !$isBase; - my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid); + my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format); - $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name"); + if ($format eq 'subvol') { + my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename"); + chomp($size); + $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size"); + } else { + $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name"); + } - return $name; + return "$basename/$name"; } sub create_base { @@ -528,14 +635,17 @@ sub create_base { my $snap = '__base__'; - my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname); die "create_base not possible with base image\n" if $isBase; my $newname = $name; - $newname =~ s/^vm-/base-/; - + if ( $format eq 'subvol' ) { + $newname =~ s/^subvol-/basevol-/; + } else { + $newname =~ s/^vm-/base-/; + } my $newvolname = $basename ? "$basename/$newname" : "$newname"; $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname"); @@ -550,13 +660,32 @@ sub create_base { sub volume_resize { my ($class, $scfg, $storeid, $volname, $size, $running) = @_; - my $new_size = ($size/1024); + my $new_size = int($size/1024); + + my (undef, $vname, undef, undef, undef, undef, $format) = + $class->parse_volname($volname); + + my $attr = $format eq 'subvol' ? 'refquota' : 'volsize'; + + # align size to 1M so we always have a valid multiple of the volume block size + if ($format eq 'raw') { + my $padding = (1024 - $new_size % 1024) % 1024; + $new_size = $new_size + $padding; + } - $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname"); + $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname"); return $new_size; } +sub storage_can_replicate { + my ($class, $scfg, $storeid, $format) = @_; + + return 1 if $format eq 'raw' || $format eq 'subvol'; + + return 0; +} + sub volume_has_feature { my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; @@ -565,6 +694,9 @@ sub volume_has_feature { clone => { base => 1}, template => { current => 1}, copy => { base => 1, current => 1}, + sparseinit => { base => 1, current => 1}, + replicate => { base => 1, current => 1}, + rename => {current => 1}, }; my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = @@ -583,4 +715,118 @@ sub volume_has_feature { return undef; } +sub volume_export { + my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; + + die "unsupported export stream format for $class: $format\n" + if $format ne 'zfs'; + + die "$class storage can only export snapshots\n" + if !defined($snapshot); + + my $dataset = ($class->parse_volname($volname))[1]; + + my $fd = fileno($fh); + die "internal error: invalid file handle for volume_export\n" + if !defined($fd); + $fd = ">&$fd"; + + # For zfs we always create a replication stream (-R) which means the remote + # side will always delete non-existing source snapshots. This should work + # for all our use cases. + my $cmd = ['zfs', 'send', '-Rpv']; + if (defined($base_snapshot)) { + my $arg = $with_snapshots ? '-I' : '-i'; + push @$cmd, $arg, $base_snapshot; + } + push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot"; + + run_command($cmd, output => $fd); + + return; +} + +sub volume_export_formats { + my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; + + my @formats = ('zfs'); + # TODOs: + # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/; + # push @formats, 'raw' if !$base_snapshot && !$with_snapshots; + return @formats; +} + +sub volume_import { + my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_; + + die "unsupported import stream format for $class: $format\n" + if $format ne 'zfs'; + + my $fd = fileno($fh); + die "internal error: invalid file handle for volume_import\n" + if !defined($fd); + + my (undef, $dataset, $vmid) = $class->parse_volname($volname); + my $zfspath = "$scfg->{pool}/$dataset"; + my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : ''; + my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix], + noerr => 1, quiet => 1); + if (defined($base_snapshot)) { + die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists; + } elsif ($exists) { + die "volume '$zfspath' already exists\n" if !$allow_rename; + warn "volume '$zfspath' already exists - importing with a different name\n"; + $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format); + $zfspath = "$scfg->{pool}/$dataset"; + } + + eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") }; + if (my $err = $@) { + if (defined($base_snapshot)) { + eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) }; + } else { + eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) }; + } + die $err; + } + + return "$storeid:$dataset"; +} + +sub volume_import_formats { + my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; + + return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots); +} + +sub rename_volume { + my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_; + + my ( + undef, + $source_image, + $source_vmid, + $base_name, + $base_vmid, + undef, + $format + ) = $class->parse_volname($source_volname); + $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format) + if !$target_volname; + + my $pool = $scfg->{pool}; + my $source_zfspath = "${pool}/${source_image}"; + my $target_zfspath = "${pool}/${target_volname}"; + + my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $target_zfspath], + noerr => 1, quiet => 1); + die "target volume '${target_volname}' already exists\n" if $exists; + + $class->zfs_request($scfg, 5, 'rename', ${source_zfspath}, ${target_zfspath}); + + $base_name = $base_name ? "${base_name}/" : ''; + + return "${storeid}:${base_name}${target_volname}"; +} + 1;