[pve-devel] [PATCH 2/2] Add wwn-name mapping via /etc/pve/multipath.list. Files that not in this list are skipped.

Dmitry Petuhov mityapetuhov at gmail.com
Thu Jun 30 12:21:30 CEST 2016


Signed-off-by: Dmitry Petuhov <mityapetuhov at gmail.com>
---
 PVE/Storage/MPDirectPlugin.pm | 63 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 50 insertions(+), 13 deletions(-)

diff --git a/PVE/Storage/MPDirectPlugin.pm b/PVE/Storage/MPDirectPlugin.pm
index 76ced94..e9f30f2 100644
--- a/PVE/Storage/MPDirectPlugin.pm
+++ b/PVE/Storage/MPDirectPlugin.pm
@@ -4,12 +4,47 @@ use strict;
 use warnings;
 use Data::Dumper;
 use IO::File;
-use PVE::Tools qw(run_command trim);
+use PVE::Tools qw(run_command trim file_read_firstline);
 use PVE::Storage::Plugin;
 use PVE::JSONSchema qw(get_standard_option);
 
 use base qw(PVE::Storage::Plugin);
 
+# Utility functions
+sub mp_get_name {
+    my ($wwn) = @_;
+    my $ret = undef;
+
+    open my $list, '<', '/etc/pve/multipath.list' || return undef;
+    while ( my $line = <$list> ) {
+	next if $line !~ m/(\S+)\s+(\S+)\s*/;
+	if ($1 eq $wwn) {
+	    $ret = $2;
+	    last;
+	}
+    }
+    close $list;
+
+    return $ret;
+}
+
+sub mp_get_wwn {
+    my ($name) = @_;
+    my $ret = undef;
+
+    open my $list, '<', '/etc/pve/multipath.list' ||  return undef;
+    while ( my $line = <$list> ) {
+	next if $line !~ m/(\S+)\s+(\S+)\s*/;
+	if ($2 eq $name) {
+	    $ret = $1;
+	    last;
+	}
+    }
+    close $list;
+
+    return $ret;
+}
+
 # Configuration
 
 sub type {
@@ -30,6 +65,7 @@ sub properties {
 sub options {
     return {
 	content => { optional => 1 },
+        nodes => { optional => 1 },
     };
 }
 
@@ -38,7 +74,11 @@ sub options {
 sub parse_volname {
     my ($class, $volname) = @_;
 
-    return ('images', $volname, undef, undef, undef, undef, 'raw');
+    if ($volname =~ m/vm-(\d+)-disk-\S+/) {
+	return ('images', $volname, $1, undef, undef, undef, 'raw');
+    } else {
+	die "Invalid volume $volname";
+    }
 }
 
 sub filesystem_path {
@@ -47,8 +87,9 @@ sub filesystem_path {
     die "Direct attached device snapshot is not implemented" if defined($snapname);
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+    die "Cannot find WWN for volume $volname" unless my $wwn = mp_get_wwn($name);
 
-    my $path = "/dev/disk/by-id/wwn-0x$name";
+    my $path = "/dev/disk/by-id/wwn-0x$wwn";
 
     return wantarray ? ($path, $vmid, $vtype) : $path;
 }
@@ -93,7 +134,9 @@ sub list_images {
 	next if $disk !~ m/^wwn-0x(.+)$/;
 	my $wwid = $1;
 
-	my $volid = "$storeid:$wwid";
+	next unless my $name = mp_get_name($wwid);
+
+	my $volid = "$storeid:$name";
 
 	if ($vollist) {
 	    my $found = grep { $_ eq $volid } @$vollist;
@@ -103,16 +146,10 @@ sub list_images {
 	}
 
 	my $actualdev = readlink "/dev/disk/by-id/$disk";
-	$actualdev =~ s/..\/..\///;
+	$actualdev =~ s!../../!!;
 
-	my $size;
-	if (open SZ, "/sys/block/$actualdev/size" ) {
-	    $size = <SZ>;
-	    $size *= 512;
-	    close SZ;
-	} else {
-	    $size = undef;
-	}
+	my $size = file_read_firstline("/sys/block/$actualdev/size");
+	$size *= 512 if (defined $size);
 
 	push @$res, {
 	    volid => $volid, format => 'raw', size => $size,
-- 
2.1.4



More information about the pve-devel mailing list