]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ISCSIDirectPlugin.pm
add Storage::get_bandwidth_limit helper
[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 };
40 }
41 });
42 };
43
44 my $err = $@;
45 die $err if $err && $err !~ m/TESTUNITREADY failed with SENSE KEY/ ;
46
47 return $list;
48
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 select_existing => 1,
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 bwlimit => { optional => 1 },
72 };
73 }
74
75 # Storage implementation
76
77 sub parse_volname {
78 my ($class, $volname) = @_;
79
80
81 if ($volname =~ m/^lun(\d+)$/) {
82 return ('images', $1, undef, undef, undef, undef, 'raw');
83 }
84
85 die "unable to parse iscsi volume name '$volname'\n";
86
87 }
88
89 sub path {
90 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
91
92 die "volume snapshot is not possible on iscsi device"
93 if defined($snapname);
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
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 {
112 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
113
114 die "can't clone images in iscsi storage\n";
115 }
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 {
124 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
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 {
189 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
190
191 die "volume snapshot is not possible on iscsi device" if $snapname;
192
193 return 1;
194 }
195
196 sub deactivate_volume {
197 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
198
199 die "volume snapshot is not possible on iscsi device" if $snapname;
200
201 return 1;
202 }
203
204 sub volume_size_info {
205 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
206
207 my $vollist = iscsi_ls($scfg,$storeid);
208 my $info = $vollist->{$storeid}->{$volname};
209
210 return $info->{size};
211 }
212
213 sub volume_resize {
214 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
215 die "volume resize is not possible on iscsi device";
216 }
217
218 sub volume_snapshot {
219 my ($class, $scfg, $storeid, $volname, $snap) = @_;
220 die "volume snapshot is not possible on iscsi device";
221 }
222
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
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
233 sub volume_has_feature {
234 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
235
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){
245 $key = 'snap';
246 }else{
247 $key = $isBase ? 'base' : 'current';
248 }
249 return 1 if $features->{$feature}->{$key};
250
251 return undef;
252 }
253
254 1;