From ee31ad019cebb0170a9afd401f3d7f898d36113b Mon Sep 17 00:00:00 2001 From: Michael Rasmussen Date: Thu, 17 Oct 2013 01:18:49 +0200 Subject: [PATCH] Comstar lun commands moved to separate module Signed-off-by: Michael Rasmussen --- PVE/Storage/LunCmd/Comstar.pm | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 PVE/Storage/LunCmd/Comstar.pm diff --git a/PVE/Storage/LunCmd/Comstar.pm b/PVE/Storage/LunCmd/Comstar.pm new file mode 100644 index 0000000..8c01d55 --- /dev/null +++ b/PVE/Storage/LunCmd/Comstar.pm @@ -0,0 +1,101 @@ +package PVE::Storage::LunCmd::Comstar; + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); +use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach); +use Data::Dumper; + +my @ssh_opts = ('-o', 'BatchMode=yes'); +my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts); + +my $get_lun_cmd_map = sub { + my ($method) = @_; + + my $stmfadmcmd = "/usr/sbin/stmfadm"; + my $sbdadmcmd = "/usr/sbin/sbdadm"; + + my $cmdmap = { + create_lu => { cmd => $stmfadmcmd, method => 'create-lu' }, + delete_lu => { cmd => $stmfadmcmd, method => 'delete-lu' }, + import_lu => { cmd => $stmfadmcmd, method => 'import-lu' }, + modify_lu => { cmd => $stmfadmcmd, method => 'modify-lu' }, + add_view => { cmd => $stmfadmcmd, method => 'add-view' }, + list_view => { cmd => $stmfadmcmd, method => 'list-view' }, + list_lu => { cmd => $sbdadmcmd, method => 'list-lu' }, + }; + + die "unknown command '$method'" unless exists $cmdmap->{$method}; + + return $cmdmap->{$method}; +}; + +sub get_base { + return '/dev/zvol/rdsk'; +} + +sub run_lun_command { + my ($scfg, $timeout, $method, @params) = @_; + + my $msg = ''; + my $luncmd; + my $target; + my $guid; + $timeout = 10 if !$timeout; + + my $output = sub { + my $line = shift; + $msg .= "$line\n"; + }; + + if ($method eq 'create_lu') { + my $prefix = '600144f'; + my $digest = md5_hex($params[0]); + $digest =~ /(\w{7}(.*))/; + $guid = "$prefix$2"; + @params = ('-p', 'wcd=false', '-p', "guid=$guid", @params); + } elsif ($method eq 'modify_lu') { + @params = ('-s', @params); + } elsif ($method eq 'list_view') { + @params = ('-l', @params); + } elsif ($method eq 'list_lu') { + $guid = $params[0]; + @params = undef; + } + + my $cmdmap = $get_lun_cmd_map->($method); + $luncmd = $cmdmap->{cmd}; + my $lunmethod = $cmdmap->{method}; + + $target = 'root@' . $scfg->{portal}; + + my $cmd = [@ssh_cmd, $target, $luncmd, $lunmethod, @params]; + + run_command($cmd, outfunc => $output, timeout => $timeout); + + if ($method eq 'list_view') { + my @lines = split /\n/, $msg; + $msg = undef; + foreach my $line (@lines) { + if ($line =~ /^\s*LUN\s*:\s*(\d+)$/) { + $msg = $1; + last; + } + } + } elsif ($method eq 'list_lu') { + my $object = $guid; + my @lines = split /\n/, $msg; + $msg = undef; + foreach my $line (@lines) { + if ($line =~ /(\w+)\s+\d+\s+$object$/) { + $msg = $1; + last; + } + } + } elsif ($method eq 'create_lu') { + $msg = $guid; + } + + return $msg; +} +