[pve-devel] [PATCH pve-container 4/9] add mount_raw_file

Wolfgang Link w.link at proxmox.com
Wed Nov 18 09:30:01 CET 2015


this is a function to mount snapshots and images to access the image.
---
 src/PVE/LXC.pm | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 383a6da..d8bb81f 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -2240,6 +2240,72 @@ sub mountpoint_mount {
     die "unsupported storage";
 }
 
+
+sub mount_raw_file {
+    my ($scfg, $mountpoint, $snapname, $volid) = @_;
+
+    my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+
+    my $format = (PVE::Storage::parse_volname($scfg, $volid))[6];
+
+    if($scfg->{ids}->{$storage}->{type} eq 'zfspool' && $snapname) {
+
+	my @cmd = ['zfs', 'clone', '-o', 'readonly=on', "mountpoint=${mountpoint}",
+		"$scfg->{pool}/$volname\@$snapname", "$scfg->{pool}/$snapname"];
+
+	PVE::Tools::run_command(@cmd);
+	die $@ if $@;
+
+    } elsif ($scfg->{ids}->{$storage}->{type} eq 'rbd' ) {
+
+	my $dev = undef;
+	my $vol = $snapname ? "${volname}\@${snapname}" : $volname;
+
+	my @cmd = ['rbd', 'map', $vol];
+
+	PVE::Tools::run_command(@cmd,
+		    outfunc => sub {
+			my $line = shift;
+			$dev = $line if $line =~ m|^/dev/rbd\d+$|;
+		    }
+	    );
+
+	die $@ if $@;
+
+	eval {
+	    PVE::Tools::run_command(['mount', '-t', 'ext4', $dev, $mountpoint]);
+	};
+	if (my $err = $@) {
+	    umount_raw_file($mountpoint, $dev);
+	    die "$err\n";
+	}
+
+    } elsif ($format eq 'raw' && !$snapname) {
+
+	my $raw_path = PVE::Storage::path ($scfg, $volid);
+	my $loopdev = '';
+
+	PVE::Tools::run_command(['losetup', '--find', '--show', $raw_path],
+				outfunc => sub {
+				    my $line = shift;
+				    $loopdev = $line if $line =~m|^/dev/loop\d+$|;
+				});
+
+	eval {
+	    PVE::Tools::run_command(['mount', '-t', 'ext4', $loopdev,
+				     $mountpoint]);
+	};
+	if (my $err = $@) {
+	    umount_raw_file($mountpoint, $loopdev);
+	    die "$err\n";
+	}
+
+	return $loopdev;
+    } else {
+	die "Can't mount volid $volid\n";
+    }
+}
+
 sub get_vm_volumes {
     my ($conf, $excludes) = @_;
 
-- 
2.1.4





More information about the pve-devel mailing list