]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Storage::LunCmd::Comstar; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Digest::MD5 qw(md5_hex); | |
6 | use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach); | |
7 | use Data::Dumper; | |
8 | ||
9 | my @ssh_opts = ('-o', 'BatchMode=yes'); | |
10 | my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts); | |
11 | my $id_rsa_path = '/etc/pve/priv/zfs'; | |
12 | ||
13 | my $get_lun_cmd_map = sub { | |
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}; | |
32 | }; | |
33 | ||
34 | sub get_base { | |
35 | return '/dev/zvol/rdsk'; | |
36 | } | |
37 | ||
38 | sub run_lun_command { | |
39 | my ($scfg, $timeout, $method, @params) = @_; | |
40 | ||
41 | my $msg = ''; | |
42 | my $luncmd; | |
43 | my $target; | |
44 | my $guid; | |
45 | $timeout = 10 if !$timeout; | |
46 | ||
47 | my $output = sub { | |
48 | my $line = shift; | |
49 | $msg .= "$line\n"; | |
50 | }; | |
51 | ||
52 | if ($method eq 'create_lu') { | |
53 | my $wcd = 'false'; | |
54 | if ($scfg->{nowritecache}) { | |
55 | $wcd = 'true'; | |
56 | } | |
57 | my $prefix = '600144f'; | |
58 | my $digest = md5_hex($params[0]); | |
59 | $digest =~ /(\w{7}(.*))/; | |
60 | $guid = "$prefix$2"; | |
61 | @params = ('-p', "wcd=$wcd", '-p', "guid=$guid", @params); | |
62 | } elsif ($method eq 'modify_lu') { | |
63 | @params = ('-s', @params); | |
64 | } elsif ($method eq 'list_view') { | |
65 | @params = ('-l', @params); | |
66 | } elsif ($method eq 'list_lu') { | |
67 | $guid = $params[0]; | |
68 | @params = undef; | |
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 | } | |
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; | |
114 | } | |
115 |