bump version to 7.0-13
[pve-storage.git] / PVE / Storage / LunCmd / Comstar.pm
CommitLineData
ee31ad01
MR
1package PVE::Storage::LunCmd::Comstar;
2
3use strict;
4use warnings;
074b2cb4 5
ee31ad01
MR
6use Digest::MD5 qw(md5_hex);
7use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
ee31ad01
MR
8
9my @ssh_opts = ('-o', 'BatchMode=yes');
10my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
3b219e80 11my $id_rsa_path = '/etc/pve/priv/zfs';
ee31ad01
MR
12
13my $get_lun_cmd_map = sub {
3b219e80
MR
14 my ($method) = @_;
15
16 my $stmfadmcmd = "/usr/sbin/stmfadm";
17 my $sbdadmcmd = "/usr/sbin/sbdadm";
18
19 my $cmdmap = {
20 create_lu => { cmd => $stmfadmcmd, method => 'create-lu' },
21 delete_lu => { cmd => $stmfadmcmd, method => 'delete-lu' },
22 import_lu => { cmd => $stmfadmcmd, method => 'import-lu' },
23 modify_lu => { cmd => $stmfadmcmd, method => 'modify-lu' },
24 add_view => { cmd => $stmfadmcmd, method => 'add-view' },
25 list_view => { cmd => $stmfadmcmd, method => 'list-view' },
26 list_lu => { cmd => $sbdadmcmd, method => 'list-lu' },
27 };
28
29 die "unknown command '$method'" unless exists $cmdmap->{$method};
30
31 return $cmdmap->{$method};
ee31ad01
MR
32};
33
34sub get_base {
3b219e80 35 return '/dev/zvol/rdsk';
ee31ad01
MR
36}
37
38sub run_lun_command {
3b219e80 39 my ($scfg, $timeout, $method, @params) = @_;
ee31ad01
MR
40
41 my $msg = '';
42 my $luncmd;
43 my $target;
44 my $guid;
45 $timeout = 10 if !$timeout;
3b219e80 46
ee31ad01
MR
47 my $output = sub {
48 my $line = shift;
3b219e80 49 $msg .= "$line\n";
ee31ad01
MR
50 };
51
3b219e80 52 if ($method eq 'create_lu') {
70986fd9
CA
53 my $wcd = 'false';
54 if ($scfg->{nowritecache}) {
55 $wcd = 'true';
56 }
3b219e80
MR
57 my $prefix = '600144f';
58 my $digest = md5_hex($params[0]);
59 $digest =~ /(\w{7}(.*))/;
60 $guid = "$prefix$2";
70986fd9 61 @params = ('-p', "wcd=$wcd", '-p', "guid=$guid", @params);
3b219e80
MR
62 } elsif ($method eq 'modify_lu') {
63 @params = ('-s', @params);
64 } elsif ($method eq 'list_view') {
65 @params = ('-l', @params);
ee31ad01 66 } elsif ($method eq 'list_lu') {
3b219e80
MR
67 $guid = $params[0];
68 @params = undef;
454c15e2
CA
69 } elsif ($method eq 'add_view') {
70 if ($scfg->{comstar_tg}) {
71 unshift @params, $scfg->{comstar_tg};
72 unshift @params, '--target-group';
73 }
74 if ($scfg->{comstar_hg}) {
75 unshift @params, $scfg->{comstar_hg};
76 unshift @params, '--host-group';
77 }
3b219e80
MR
78 }
79
80 my $cmdmap = $get_lun_cmd_map->($method);
81 $luncmd = $cmdmap->{cmd};
82 my $lunmethod = $cmdmap->{method};
83
84 $target = 'root@' . $scfg->{portal};
85
86 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $luncmd, $lunmethod, @params];
87
88 run_command($cmd, outfunc => $output, timeout => $timeout);
89
90 if ($method eq 'list_view') {
91 my @lines = split /\n/, $msg;
92 $msg = undef;
93 foreach my $line (@lines) {
94 if ($line =~ /^\s*LUN\s*:\s*(\d+)$/) {
95 $msg = $1;
96 last;
97 }
98 }
99 } elsif ($method eq 'list_lu') {
100 my $object = $guid;
101 my @lines = split /\n/, $msg;
102 $msg = undef;
103 foreach my $line (@lines) {
104 if ($line =~ /(\w+)\s+\d+\s+$object$/) {
105 $msg = $1;
106 last;
107 }
108 }
109 } elsif ($method eq 'create_lu') {
110 $msg = $guid;
111 }
112
113 return $msg;
ee31ad01
MR
114}
115