]>
Commit | Line | Data |
---|---|---|
b345cad3 AD |
1 | package PVE::Storage::ISCSIDirectPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use HTTP::Request; | |
7 | use LWP::UserAgent; | |
8 | use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach); | |
9 | use PVE::Storage::Plugin; | |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | ||
12 | use base qw(PVE::Storage::Plugin); | |
13 | ||
2677f623 DM |
14 | sub iscsi_ls { |
15 | my ($scfg, $storeid) = @_; | |
b345cad3 AD |
16 | |
17 | my $portal = $scfg->{portal}; | |
18 | my $cmd = ['/usr/bin/iscsi-ls', '-s', 'iscsi://'.$portal ]; | |
19 | my $list = {}; | |
3a0c3246 AD |
20 | my %unittobytes = ( |
21 | "k" => 1024, | |
22 | "M" => 1024*1024, | |
23 | "G" => 1024*1024*1024, | |
24 | "T" => 1024*1024*1024*1024 | |
25 | ); | |
b345cad3 AD |
26 | eval { |
27 | ||
3a0c3246 | 28 | run_command($cmd, errmsg => "iscsi error", errfunc => sub {}, outfunc => sub { |
b345cad3 AD |
29 | my $line = shift; |
30 | $line = trim($line); | |
3a0c3246 AD |
31 | if( $line =~ /Lun:(\d+)\s+([A-Za-z0-9\-\_\.\:]*)\s+\(Size:([0-9\.]*)(k|M|G|T)\)/ ) { |
32 | my $image = "lun".$1; | |
b345cad3 | 33 | my $size = $3; |
3a0c3246 AD |
34 | my $unit = $4; |
35 | ||
b345cad3 AD |
36 | $list->{$storeid}->{$image} = { |
37 | name => $image, | |
3a0c3246 | 38 | size => $size * $unittobytes{$unit}, |
b345cad3 AD |
39 | }; |
40 | } | |
41 | }); | |
42 | }; | |
43 | ||
44 | my $err = $@; | |
45 | die $err if $err && $err !~ m/TESTUNITREADY failed with SENSE KEY/ ; | |
3a0c3246 | 46 | |
b345cad3 | 47 | return $list; |
3a0c3246 | 48 | |
b345cad3 AD |
49 | } |
50 | ||
51 | # Configuration | |
52 | ||
53 | sub type { | |
54 | return 'iscsidirect'; | |
55 | } | |
56 | ||
57 | sub plugindata { | |
58 | return { | |
59 | content => [ {images => 1, none => 1}, { images => 1 }], | |
60 | }; | |
61 | } | |
62 | ||
63 | sub options { | |
64 | return { | |
65 | portal => { fixed => 1 }, | |
66 | target => { fixed => 1 }, | |
67 | nodes => { optional => 1}, | |
68 | disable => { optional => 1}, | |
69 | content => { optional => 1}, | |
70 | }; | |
71 | } | |
72 | ||
b345cad3 AD |
73 | # Storage implementation |
74 | ||
75 | sub parse_volname { | |
76 | my ($class, $volname) = @_; | |
77 | ||
2677f623 | 78 | |
3a0c3246 | 79 | if ($volname =~ m/^lun(\d+)$/) { |
b345cad3 AD |
80 | return ('images', $1, undef); |
81 | } | |
82 | ||
83 | die "unable to parse iscsi volume name '$volname'\n"; | |
84 | ||
85 | } | |
86 | ||
87 | sub path { | |
88 | my ($class, $scfg, $volname) = @_; | |
89 | ||
90 | my ($vtype, $lun, $vmid) = $class->parse_volname($volname); | |
91 | ||
92 | my $target = $scfg->{target}; | |
93 | my $portal = $scfg->{portal}; | |
94 | ||
95 | my $path = "iscsi://$portal/$target/$lun"; | |
96 | ||
97 | return ($path, $vmid, $vtype); | |
98 | } | |
99 | ||
100 | ||
101 | sub alloc_image { | |
102 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
103 | ||
104 | die "can't allocate space in iscsi storage\n"; | |
105 | } | |
106 | ||
107 | sub free_image { | |
108 | my ($class, $storeid, $scfg, $volname) = @_; | |
109 | ||
110 | die "can't free space in iscsi storage\n"; | |
111 | } | |
112 | ||
113 | ||
114 | sub list_images { | |
115 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
116 | ||
117 | my $res = []; | |
118 | ||
119 | $cache->{directiscsi} = iscsi_ls($scfg,$storeid) if !$cache->{directiscsi}; | |
120 | ||
121 | # we have no owner for iscsi devices | |
122 | ||
123 | my $target = $scfg->{target}; | |
124 | ||
125 | if (my $dat = $cache->{directiscsi}->{$storeid}) { | |
126 | ||
127 | foreach my $volname (keys %$dat) { | |
128 | ||
129 | my $volid = "$storeid:$volname"; | |
130 | ||
131 | if ($vollist) { | |
132 | my $found = grep { $_ eq $volid } @$vollist; | |
133 | next if !$found; | |
134 | } else { | |
135 | # we have no owner for iscsi devices | |
136 | next if defined($vmid); | |
137 | } | |
138 | ||
139 | my $info = $dat->{$volname}; | |
140 | $info->{volid} = $volid; | |
141 | ||
142 | push @$res, $info; | |
143 | } | |
144 | } | |
145 | ||
146 | return $res; | |
147 | } | |
148 | ||
149 | ||
150 | sub status { | |
151 | my ($class, $storeid, $scfg, $cache) = @_; | |
152 | ||
153 | my $total = 0; | |
154 | my $free = 0; | |
155 | my $used = 0; | |
156 | my $active = 1; | |
157 | return ($total,$free,$used,$active); | |
158 | ||
159 | return undef; | |
160 | } | |
161 | ||
162 | sub activate_storage { | |
163 | my ($class, $storeid, $scfg, $cache) = @_; | |
164 | return 1; | |
165 | } | |
166 | ||
167 | sub deactivate_storage { | |
168 | my ($class, $storeid, $scfg, $cache) = @_; | |
169 | return 1; | |
170 | } | |
171 | ||
172 | sub activate_volume { | |
173 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
174 | return 1; | |
175 | } | |
176 | ||
177 | sub deactivate_volume { | |
178 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
179 | return 1; | |
180 | } | |
181 | ||
baf69659 AD |
182 | sub volume_size_info { |
183 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
184 | ||
6f714585 AD |
185 | my $vollist = iscsi_ls($scfg,$storeid); |
186 | my $info = $vollist->{$storeid}->{$volname}; | |
187 | ||
188 | return $info->{size}; | |
baf69659 AD |
189 | } |
190 | ||
3bac137c AD |
191 | sub volume_resize { |
192 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
193 | die "volume resize is not possible on iscsi device"; | |
194 | } | |
195 | ||
ec409fb4 AD |
196 | sub volume_snapshot { |
197 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
198 | die "volume snapshot is not possible on iscsi device"; | |
199 | } | |
200 | ||
9f02df83 AD |
201 | sub volume_snapshot_rollback { |
202 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
203 | die "volume snapshot rollback is not possible on iscsi device"; | |
204 | } | |
205 | ||
b2c1ab4b AD |
206 | sub volume_snapshot_delete { |
207 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
208 | die "volume snapshot delete is not possible on iscsi device"; | |
209 | } | |
210 | ||
b345cad3 | 211 | 1; |