improve snapshot delete - add force option to API

This commit is contained in:
Dietmar Maurer 2012-09-12 07:19:38 +02:00
parent d788cea673
commit 3ee28e388a
2 changed files with 22 additions and 5 deletions

View File

@ -1925,6 +1925,7 @@ __PACKAGE__->register_method({
description => $d->{description} || '', description => $d->{description} || '',
}; };
$item->{parent} = $d->{parent} if $d->{parent}; $item->{parent} = $d->{parent} if $d->{parent};
$item->{snapstate} = $d->{snapstate} if $d->{snapstate};
push @$res, $item; push @$res, $item;
} }
@ -2185,6 +2186,11 @@ __PACKAGE__->register_method({
node => get_standard_option('pve-node'), node => get_standard_option('pve-node'),
vmid => get_standard_option('pve-vmid'), vmid => get_standard_option('pve-vmid'),
snapname => get_standard_option('pve-snapshot-name'), snapname => get_standard_option('pve-snapshot-name'),
force => {
optional => 1,
type => 'boolean',
description => "For removal from config file, even if removing disk snapshots fails.",
},
}, },
}, },
returns => { returns => {
@ -2206,7 +2212,7 @@ __PACKAGE__->register_method({
my $realcmd = sub { my $realcmd = sub {
PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname"); PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
PVE::QemuServer::snapshot_delete($vmid, $snapname); PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force});
}; };
return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd); return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);

View File

@ -3755,6 +3755,8 @@ sub snapshot_create {
my $snap = &$snapshot_prepare($vmid, $snapname, $comment); my $snap = &$snapshot_prepare($vmid, $snapname, $comment);
my $drivehash = {};
eval { eval {
# create internal snapshots of all drives # create internal snapshots of all drives
@ -3773,6 +3775,7 @@ sub snapshot_create {
my $device = "drive-$ds"; my $device = "drive-$ds";
qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname); qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname);
$drivehash->{$ds} = 1;
}); });
}; };
my $err = $@; my $err = $@;
@ -3785,7 +3788,7 @@ sub snapshot_create {
if ($err) { if ($err) {
warn "snapshot create failed: starting cleanup\n"; warn "snapshot create failed: starting cleanup\n";
eval { snapshot_delete($vmid, $snapname, 1); }; eval { snapshot_delete($vmid, $snapname, 0, $drivehash); };
warn $@ if $@; warn $@ if $@;
die $err; die $err;
} }
@ -3793,8 +3796,9 @@ sub snapshot_create {
&$snapshot_commit($vmid, $snapname); &$snapshot_commit($vmid, $snapname);
} }
# Note: $drivehash is only set when called from snapshot_create.
sub snapshot_delete { sub snapshot_delete {
my ($vmid, $snapname, $force) = @_; my ($vmid, $snapname, $force, $drivehash) = @_;
my $prepare = 1; my $prepare = 1;
@ -3805,7 +3809,7 @@ sub snapshot_delete {
my $conf = load_config($vmid); my $conf = load_config($vmid);
check_lock($conf) if !$force; check_lock($conf) if !$drivehash;
$snap = $conf->{snapshots}->{$snapname}; $snap = $conf->{snapshots}->{$snapname};
@ -3829,6 +3833,7 @@ sub snapshot_delete {
} else { } else {
delete $conf->{parent} if $conf->{parent} && $conf->{parent} eq $snapname; delete $conf->{parent} if $conf->{parent} && $conf->{parent} eq $snapname;
delete $conf->{snapshots}->{$snapname}; delete $conf->{snapshots}->{$snapname};
delete $conf->{lock} if $drivehash;
foreach my $volid (@$unused) { foreach my $volid (@$unused) {
add_unused_volume($conf, $volid); add_unused_volume($conf, $volid);
} }
@ -3847,10 +3852,16 @@ sub snapshot_delete {
my ($ds, $drive) = @_; my ($ds, $drive) = @_;
return if drive_is_cdrom($drive); return if drive_is_cdrom($drive);
return if $drivehash && !$drivehash->{$ds};
my $volid = $drive->{file}; my $volid = $drive->{file};
my $device = "drive-$ds"; my $device = "drive-$ds";
qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname); eval { qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname); };
if (my $err = $@) {
die $err if !$force;
warn $err;
}
push @$unused, $volid; push @$unused, $volid;
}); });