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