]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ISCSIDirectPlugin.pm
bump version to 8.2.0
[pve-storage.git] / PVE / Storage / ISCSIDirectPlugin.pm
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
14 sub iscsi_ls {
15 my ($scfg, $storeid) = @_;
16
17 my $portal = $scfg->{portal};
18 my $cmd = ['/usr/bin/iscsi-ls', '-s', 'iscsi://'.$portal ];
19 my $list = {};
20 my %unittobytes = (
21 "k" => 1024,
22 "M" => 1024*1024,
23 "G" => 1024*1024*1024,
24 "T" => 1024*1024*1024*1024
25 );
26 eval {
27
28 run_command($cmd, errmsg => "iscsi error", errfunc => sub {}, outfunc => sub {
29 my $line = shift;
30 $line = trim($line);
31 if( $line =~ /Lun:(\d+)\s+([A-Za-z0-9\-\_\.\:]*)\s+\(Size:([0-9\.]*)(k|M|G|T)\)/ ) {
32 my $image = "lun".$1;
33 my $size = $3;
34 my $unit = $4;
35
36 $list->{$storeid}->{$image} = {
37 name => $image,
38 size => $size * $unittobytes{$unit},
39 format => 'raw',
40 };
41 }
42 });
43 };
44
45 my $err = $@;
46 die $err if $err && $err !~ m/TESTUNITREADY failed with SENSE KEY/ ;
47
48 return $list;
49
50 }
51
52 # Configuration
53
54 sub type {
55 return 'iscsidirect';
56 }
57
58 sub plugindata {
59 return {
60 content => [ {images => 1, none => 1}, { images => 1 }],
61 select_existing => 1,
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},
72 bwlimit => { optional => 1 },
73 };
74 }
75
76 # Storage implementation
77
78 sub parse_volname {
79 my ($class, $volname) = @_;
80
81
82 if ($volname =~ m/^lun(\d+)$/) {
83 return ('images', $1, undef, undef, undef, undef, 'raw');
84 }
85
86 die "unable to parse iscsi volume name '$volname'\n";
87
88 }
89
90 sub path {
91 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
92
93 die "volume snapshot is not possible on iscsi device"
94 if defined($snapname);
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
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 {
113 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
114
115 die "can't clone images in iscsi storage\n";
116 }
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 {
125 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
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 {
190 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
191
192 die "volume snapshot is not possible on iscsi device" if $snapname;
193
194 return 1;
195 }
196
197 sub deactivate_volume {
198 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
199
200 die "volume snapshot is not possible on iscsi device" if $snapname;
201
202 return 1;
203 }
204
205 sub volume_size_info {
206 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
207
208 my $vollist = iscsi_ls($scfg,$storeid);
209 my $info = $vollist->{$storeid}->{$volname};
210
211 return $info->{size};
212 }
213
214 sub volume_resize {
215 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
216 die "volume resize is not possible on iscsi device";
217 }
218
219 sub volume_snapshot {
220 my ($class, $scfg, $storeid, $volname, $snap) = @_;
221 die "volume snapshot is not possible on iscsi device";
222 }
223
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
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
234 sub volume_has_feature {
235 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
236
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){
246 $key = 'snap';
247 }else{
248 $key = $isBase ? 'base' : 'current';
249 }
250 return 1 if $features->{$feature}->{$key};
251
252 return undef;
253 }
254
255 1;