From: Dietmar Maurer Date: Fri, 23 Jan 2015 11:38:21 +0000 (+0100) Subject: zfs: implement zfs_get_latest_snapshot X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=2fc59177c99d03a33466f155ccf0e7829cd5dd13;p=pve-storage.git zfs: implement zfs_get_latest_snapshot To improve code sharing. --- diff --git a/PVE/Storage/ZFSDirPlugin.pm b/PVE/Storage/ZFSDirPlugin.pm index 11ee783..cac6a72 100644 --- a/PVE/Storage/ZFSDirPlugin.pm +++ b/PVE/Storage/ZFSDirPlugin.pm @@ -352,6 +352,25 @@ sub zfs_find_free_diskname { 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 @snapshots = split(/\n/, $text); + + my $recentsnap; + foreach (@snapshots) { + if (/$scfg->{pool}\/$volname/) { + s/^.*@//; + $recentsnap = $_; + } + } + + return $recentsnap; +} + sub status { my ($class, $storeid, $scfg, $cache) = @_; @@ -392,22 +411,12 @@ sub volume_snapshot_rollback { my ($class, $scfg, $storeid, $volname, $snap) = @_; # 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 @snapshots = split(/\n/, $text); - my $recentsnap = undef; - foreach (@snapshots) { - if (/$scfg->{pool}\/$volname/) { - s/^.*@//; - $recentsnap = $_; - } - } + my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); if ($snap ne $recentsnap) { die "cannot rollback, more recent snapshots exist\n"; } zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap"); - } sub activate_volume { diff --git a/PVE/Storage/ZFSPlugin.pm b/PVE/Storage/ZFSPlugin.pm index 3595913..22d8cca 100644 --- a/PVE/Storage/ZFSPlugin.pm +++ b/PVE/Storage/ZFSPlugin.pm @@ -324,16 +324,7 @@ sub volume_snapshot_rollback { my ($class, $scfg, $storeid, $volname, $snap) = @_; # abort rollback if snapshot is not the latest - my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation'); - my $text = $class->zfs_request($class, $scfg, undef, 'list', @params); - my @snapshots = split(/\n/, $text); - my $recentsnap = undef; - foreach (@snapshots) { - if (/$scfg->{pool}\/$volname/) { - s/^.*@//; - $recentsnap = $_; - } - } + my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname); if ($snap ne $recentsnap) { die "cannot rollback, more recent snapshots exist\n"; }