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