zfs: implement zfs_get_latest_snapshot
To improve code sharing.
This commit is contained in:
@ -352,6 +352,25 @@ sub zfs_find_free_diskname {
|
|||||||
die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
|
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 {
|
sub status {
|
||||||
my ($class, $storeid, $scfg, $cache) = @_;
|
my ($class, $storeid, $scfg, $cache) = @_;
|
||||||
|
|
||||||
@ -392,22 +411,12 @@ sub volume_snapshot_rollback {
|
|||||||
my ($class, $scfg, $storeid, $volname, $snap) = @_;
|
my ($class, $scfg, $storeid, $volname, $snap) = @_;
|
||||||
|
|
||||||
# abort rollback if snapshot is not the latest
|
# abort rollback if snapshot is not the latest
|
||||||
my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
|
my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
|
||||||
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 = $_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($snap ne $recentsnap) {
|
if ($snap ne $recentsnap) {
|
||||||
die "cannot rollback, more recent snapshots exist\n";
|
die "cannot rollback, more recent snapshots exist\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
|
zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub activate_volume {
|
sub activate_volume {
|
||||||
|
|||||||
@ -324,16 +324,7 @@ sub volume_snapshot_rollback {
|
|||||||
my ($class, $scfg, $storeid, $volname, $snap) = @_;
|
my ($class, $scfg, $storeid, $volname, $snap) = @_;
|
||||||
|
|
||||||
# abort rollback if snapshot is not the latest
|
# abort rollback if snapshot is not the latest
|
||||||
my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
|
my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
|
||||||
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 = $_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($snap ne $recentsnap) {
|
if ($snap ne $recentsnap) {
|
||||||
die "cannot rollback, more recent snapshots exist\n";
|
die "cannot rollback, more recent snapshots exist\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user