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