[pve-devel] [PATCH 1/3] Refactoring code

Wolfgang Link w.link at proxmox.com
Tue Aug 25 13:28:06 CEST 2015


We can use this also, if we mount snapshots at backup
---
 src/PVE/VZDump/LXC.pm | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/PVE/VZDump/LXC.pm b/src/PVE/VZDump/LXC.pm
index 12aae78..a6a0639 100644
--- a/src/PVE/VZDump/LXC.pm
+++ b/src/PVE/VZDump/LXC.pm
@@ -66,6 +66,25 @@ sub vm_status {
     return wantarray ? ($running, $running ? 'running' : 'stopped') : $running; 
 }
 
+my $mount_dev = sub {
+    my ($dev, $mountpoint, $fs) = @_;
+
+    eval {
+	File::Path::mkpath($mountpoint);
+	PVE::Tools::run_command(['mount', '-t', $fs, $dev, $mountpoint]);
+    };
+    if (my $err = $@) {
+	eval { PVE::Tools::run_command(['umount', '-d', $mountpoint]) };
+	warn $@ if $@;
+	eval { File::Path::remove_tree($mountpoint) };
+	warn $@ if $@;
+
+	die $err;
+    }
+
+    return 1;
+};
+
 my $loop_mount_image = sub {
     my ($image_path, $mountpoint) = @_;
     
@@ -78,15 +97,10 @@ my $loop_mount_image = sub {
 	};
 	PVE::Tools::run_command(['losetup', '--find', '--show', $image_path], outfunc => $parser);
 
-	File::Path::mkpath($mountpoint);
-	PVE::Tools::run_command(['mount', '-t', 'ext4', $loopdev, $mountpoint]);
-	$mounted = 1;
+	$mounted = &$mount_dev($loopdev, $mountpoint, 'ext4');
     };
     if (my $err = $@) {
-	if ($mounted) {
-	    eval { PVE::Tools::run_command(['umount', '-d', $mountpoint]) };
-	    warn $@ if $@;
-	} else {
+	if (!$mounted) {
 	    eval { PVE::Tools::run_command(['losetup', '-d', $loopdev]) if $loopdev; };
 	    warn $@ if $@;
 	}
-- 
2.1.4





More information about the pve-devel mailing list