]>
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}, |
3add8714 | 39 | format => 'raw', |
b345cad3 AD |
40 | }; |
41 | } | |
42 | }); | |
43 | }; | |
44 | ||
45 | my $err = $@; | |
46 | die $err if $err && $err !~ m/TESTUNITREADY failed with SENSE KEY/ ; | |
3a0c3246 | 47 | |
b345cad3 | 48 | return $list; |
3a0c3246 | 49 | |
b345cad3 AD |
50 | } |
51 | ||
52 | # Configuration | |
53 | ||
54 | sub type { | |
55 | return 'iscsidirect'; | |
56 | } | |
57 | ||
58 | sub plugindata { | |
59 | return { | |
5da48ca6 DC |
60 | content => [ {images => 1, none => 1}, { images => 1 }], |
61 | select_existing => 1, | |
b345cad3 AD |
62 | }; |
63 | } | |
64 | ||
65 | sub options { | |
66 | return { | |
67 | portal => { fixed => 1 }, | |
68 | target => { fixed => 1 }, | |
69 | nodes => { optional => 1}, | |
70 | disable => { optional => 1}, | |
71 | content => { optional => 1}, | |
9edb99a5 | 72 | bwlimit => { optional => 1 }, |
b345cad3 AD |
73 | }; |
74 | } | |
75 | ||
b345cad3 AD |
76 | # Storage implementation |
77 | ||
78 | sub parse_volname { | |
79 | my ($class, $volname) = @_; | |
80 | ||
2677f623 | 81 | |
3a0c3246 | 82 | if ($volname =~ m/^lun(\d+)$/) { |
7800e84d | 83 | return ('images', $1, undef, undef, undef, undef, 'raw'); |
b345cad3 AD |
84 | } |
85 | ||
86 | die "unable to parse iscsi volume name '$volname'\n"; | |
87 | ||
88 | } | |
89 | ||
90 | sub path { | |
e67069eb DM |
91 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
92 | ||
93 | die "volume snapshot is not possible on iscsi device" | |
94 | if defined($snapname); | |
b345cad3 AD |
95 | |
96 | my ($vtype, $lun, $vmid) = $class->parse_volname($volname); | |
97 | ||
98 | my $target = $scfg->{target}; | |
99 | my $portal = $scfg->{portal}; | |
100 | ||
101 | my $path = "iscsi://$portal/$target/$lun"; | |
102 | ||
103 | return ($path, $vmid, $vtype); | |
104 | } | |
105 | ||
5eab0272 DM |
106 | sub create_base { |
107 | my ($class, $storeid, $scfg, $volname) = @_; | |
108 | ||
109 | die "can't create base images in iscsi storage\n"; | |
110 | } | |
111 | ||
112 | sub clone_image { | |
f236eaf8 | 113 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
5eab0272 DM |
114 | |
115 | die "can't clone images in iscsi storage\n"; | |
116 | } | |
b345cad3 AD |
117 | |
118 | sub alloc_image { | |
119 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
120 | ||
121 | die "can't allocate space in iscsi storage\n"; | |
122 | } | |
123 | ||
124 | sub free_image { | |
32437ed2 | 125 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
b345cad3 AD |
126 | |
127 | die "can't free space in iscsi storage\n"; | |
128 | } | |
129 | ||
130 | ||
131 | sub list_images { | |
132 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
133 | ||
134 | my $res = []; | |
135 | ||
136 | $cache->{directiscsi} = iscsi_ls($scfg,$storeid) if !$cache->{directiscsi}; | |
137 | ||
138 | # we have no owner for iscsi devices | |
139 | ||
140 | my $target = $scfg->{target}; | |
141 | ||
142 | if (my $dat = $cache->{directiscsi}->{$storeid}) { | |
143 | ||
144 | foreach my $volname (keys %$dat) { | |
145 | ||
146 | my $volid = "$storeid:$volname"; | |
147 | ||
148 | if ($vollist) { | |
149 | my $found = grep { $_ eq $volid } @$vollist; | |
150 | next if !$found; | |
151 | } else { | |
152 | # we have no owner for iscsi devices | |
153 | next if defined($vmid); | |
154 | } | |
155 | ||
156 | my $info = $dat->{$volname}; | |
157 | $info->{volid} = $volid; | |
158 | ||
159 | push @$res, $info; | |
160 | } | |
161 | } | |
162 | ||
163 | return $res; | |
164 | } | |
165 | ||
166 | ||
167 | sub status { | |
168 | my ($class, $storeid, $scfg, $cache) = @_; | |
169 | ||
170 | my $total = 0; | |
171 | my $free = 0; | |
172 | my $used = 0; | |
173 | my $active = 1; | |
174 | return ($total,$free,$used,$active); | |
175 | ||
176 | return undef; | |
177 | } | |
178 | ||
179 | sub activate_storage { | |
180 | my ($class, $storeid, $scfg, $cache) = @_; | |
181 | return 1; | |
182 | } | |
183 | ||
184 | sub deactivate_storage { | |
185 | my ($class, $storeid, $scfg, $cache) = @_; | |
186 | return 1; | |
187 | } | |
188 | ||
189 | sub activate_volume { | |
02e797b8 WL |
190 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
191 | ||
192 | die "volume snapshot is not possible on iscsi device" if $snapname; | |
193 | ||
b345cad3 AD |
194 | return 1; |
195 | } | |
196 | ||
197 | sub deactivate_volume { | |
02e797b8 WL |
198 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
199 | ||
200 | die "volume snapshot is not possible on iscsi device" if $snapname; | |
201 | ||
b345cad3 AD |
202 | return 1; |
203 | } | |
204 | ||
baf69659 AD |
205 | sub volume_size_info { |
206 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
207 | ||
6f714585 AD |
208 | my $vollist = iscsi_ls($scfg,$storeid); |
209 | my $info = $vollist->{$storeid}->{$volname}; | |
210 | ||
211 | return $info->{size}; | |
baf69659 AD |
212 | } |
213 | ||
3bac137c AD |
214 | sub volume_resize { |
215 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
216 | die "volume resize is not possible on iscsi device"; | |
217 | } | |
218 | ||
ec409fb4 | 219 | sub volume_snapshot { |
f5640e7d | 220 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
ec409fb4 AD |
221 | die "volume snapshot is not possible on iscsi device"; |
222 | } | |
223 | ||
9f02df83 AD |
224 | sub volume_snapshot_rollback { |
225 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
226 | die "volume snapshot rollback is not possible on iscsi device"; | |
227 | } | |
228 | ||
b2c1ab4b AD |
229 | sub volume_snapshot_delete { |
230 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
231 | die "volume snapshot delete is not possible on iscsi device"; | |
232 | } | |
233 | ||
cbc6fef3 AD |
234 | sub volume_has_feature { |
235 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
236 | ||
39ab682a AD |
237 | my $features = { |
238 | copy => { current => 1}, | |
239 | }; | |
240 | ||
241 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
242 | $class->parse_volname($volname); | |
243 | ||
244 | my $key = undef; | |
245 | if($snapname){ | |
2c5a7097 | 246 | $key = 'snap'; |
39ab682a AD |
247 | }else{ |
248 | $key = $isBase ? 'base' : 'current'; | |
249 | } | |
250 | return 1 if $features->{$feature}->{$key}; | |
251 | ||
cbc6fef3 AD |
252 | return undef; |
253 | } | |
254 | ||
b345cad3 | 255 | 1; |