]> git.proxmox.com Git - pve-container.git/commitdiff
Improve error handling in snapshot_create
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Wed, 10 Feb 2016 12:04:11 +0000 (13:04 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 11 Feb 2016 05:40:30 +0000 (06:40 +0100)
Set unfreeze before trying to freeze, otherwise an aborted
or failed lxc-freeze will not be reversed by our error
handling, leaving the container in a (partially) frozen
state.

Make snapshot_create failure handling more resembling
to the QemuServer codebase and prepare for future code
convergence:
* use $drivehash parameter in snapshot_delete to bypass
check_lock() and delete config lock
* call $snapshot_commit last, it's only needed now if
there were no errors

src/PVE/LXC.pm

index df8bb28d6561aaf2a47e9b06e0d4432d01eec93e..f7f468a823d61b82cce2140fd5919906275c536b 100644 (file)
@@ -1795,11 +1795,13 @@ sub snapshot_create {
     my $running = check_running($vmid);
     
     my $unfreeze = 0;
-    
+
+    my $drivehash = {};
+
     eval {
        if ($running) {
-           PVE::Tools::run_command(['/usr/bin/lxc-freeze', '-n', $vmid]);
            $unfreeze = 1;
+           PVE::Tools::run_command(['/usr/bin/lxc-freeze', '-n', $vmid]);
            PVE::Tools::run_command(['/bin/sync']);
        };
 
@@ -1808,7 +1810,7 @@ sub snapshot_create {
        my $volid = $rootinfo->{volume};
 
        PVE::Storage::volume_snapshot($storecfg, $volid, $snapname);
-       &$snapshot_commit($vmid, $snapname);
+       $drivehash->{rootfs} = 1;
     };
     my $err = $@;
     
@@ -1818,13 +1820,17 @@ sub snapshot_create {
     }
     
     if ($err) {
-       snapshot_delete($vmid, $snapname, 1);
+       eval { snapshot_delete($vmid, $snapname, 1, $drivehash); };
+       warn "$@\n" if $@;
        die "$err\n";
     }
+
+    &$snapshot_commit($vmid, $snapname);
 }
 
+# Note: $drivehash is only set when called from snapshot_create.
 sub snapshot_delete {
-    my ($vmid, $snapname, $force) = @_;
+    my ($vmid, $snapname, $force, $drivehash) = @_;
 
     my $snap;
 
@@ -1839,7 +1845,9 @@ sub snapshot_delete {
 
        $snap = $conf->{snapshots}->{$snapname};
 
-       check_lock($conf);
+       if (!$drivehash) {
+           check_lock($conf);
+       }
 
        die "snapshot '$snapname' does not exist\n" if !defined($snap);
 
@@ -1867,7 +1875,13 @@ sub snapshot_delete {
 
     my $del_snap =  sub {
 
-       check_lock($conf);
+       $conf = load_config($vmid);
+
+       if ($drivehash) {
+           delete $conf->{lock};
+       } else {
+           check_lock($conf);
+       }
 
        my $parent = $conf->{snapshots}->{$snapname}->{parent};
        foreach my $snapkey (keys %{$conf->{snapshots}}) {