[pve-devel] [PATCH 1/7] refactoring code to reuse it

Wolfgang Link w.link at proxmox.com
Mon Aug 24 10:48:30 CEST 2015


we can use this also, if we mount snapshots
---
 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 112bc84..24f81ef 100644
--- a/src/PVE/VZDump/LXC.pm
+++ b/src/PVE/VZDump/LXC.pm
@@ -63,6 +63,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) = @_;
     
@@ -75,15 +94,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