zfs: implement zfs_get_latest_snapshot

To improve code sharing.
This commit is contained in:
Dietmar Maurer
2015-01-23 12:38:21 +01:00
parent 2b40ffaea9
commit 2fc59177c9
2 changed files with 21 additions and 21 deletions

View File

@ -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 {

View File

@ -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";
}