]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ISCSIPlugin.pm
iSCSI ipv6 support
[pve-storage.git] / PVE / Storage / ISCSIPlugin.pm
1 package PVE::Storage::ISCSIPlugin;
2
3 use strict;
4 use warnings;
5 use File::stat;
6 use IO::Dir;
7 use IO::File;
8 use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV4RE $IPV6RE);
9 use PVE::Storage::Plugin;
10 use PVE::JSONSchema qw(get_standard_option);
11
12 use base qw(PVE::Storage::Plugin);
13
14 # iscsi helper function
15
16 my $ISCSIADM = '/usr/bin/iscsiadm';
17 $ISCSIADM = undef if ! -X $ISCSIADM;
18
19 sub check_iscsi_support {
20 my $noerr = shift;
21
22 if (!$ISCSIADM) {
23 my $msg = "no iscsi support - please install open-iscsi";
24 if ($noerr) {
25 warn "warning: $msg\n";
26 return 0;
27 }
28
29 die "error: $msg\n";
30 }
31
32 return 1;
33 }
34
35 sub iscsi_session_list {
36
37 check_iscsi_support ();
38
39 my $cmd = [$ISCSIADM, '--mode', 'session'];
40
41 my $res = {};
42
43 eval {
44 run_command($cmd, errmsg => 'iscsi session scan failed', outfunc => sub {
45 my $line = shift;
46
47 if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)\s*$/) {
48 my ($session, $target) = ($1, $2);
49 # there can be several sessions per target (multipath)
50 push @{$res->{$target}}, $session;
51 }
52 });
53 };
54 if (my $err = $@) {
55 die $err if $err !~ m/: No active sessions.$/i;
56 }
57
58 return $res;
59 }
60
61 sub iscsi_test_portal {
62 my ($portal) = @_;
63
64 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
65 return 0 if !$server;
66 return PVE::Network::tcp_ping($server, $port || 3260, 2);
67 }
68
69 sub iscsi_discovery {
70 my ($portal) = @_;
71
72 check_iscsi_support ();
73
74 my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
75 '--portal', $portal];
76
77 my $res = {};
78
79 return $res if !iscsi_test_portal($portal); # fixme: raise exception here?
80
81 run_command($cmd, outfunc => sub {
82 my $line = shift;
83
84 if ($line =~ m/^((?:$IPV4RE|\[$IPV6RE\]):\d+)\,\S+\s+(\S+)\s*$/) {
85 my $portal = $1;
86 my $target = $2;
87 # one target can have more than one portal (multipath).
88 push @{$res->{$target}}, $portal;
89 }
90 });
91
92 return $res;
93 }
94
95 sub iscsi_login {
96 my ($target, $portal_in) = @_;
97
98 check_iscsi_support ();
99
100 eval { iscsi_discovery ($portal_in); };
101 warn $@ if $@;
102
103 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
104 run_command($cmd);
105 }
106
107 sub iscsi_logout {
108 my ($target, $portal) = @_;
109
110 check_iscsi_support ();
111
112 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
113 run_command($cmd);
114 }
115
116 my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
117
118 sub iscsi_session_rescan {
119 my $session_list = shift;
120
121 check_iscsi_support();
122
123 my $rstat = stat($rescan_filename);
124
125 if (!$rstat) {
126 if (my $fh = IO::File->new($rescan_filename, "a")) {
127 utime undef, undef, $fh;
128 close($fh);
129 }
130 } else {
131 my $atime = $rstat->atime;
132 my $tdiff = time() - $atime;
133 # avoid frequent rescans
134 return if !($tdiff < 0 || $tdiff > 10);
135 utime undef, undef, $rescan_filename;
136 }
137
138 foreach my $session (@$session_list) {
139 my $cmd = [$ISCSIADM, '--mode', 'session', '-r', $session, '-R'];
140 eval { run_command($cmd, outfunc => sub {}); };
141 warn $@ if $@;
142 }
143 }
144
145 sub load_stable_scsi_paths {
146
147 my $stable_paths = {};
148
149 my $stabledir = "/dev/disk/by-id";
150
151 if (my $dh = IO::Dir->new($stabledir)) {
152 while (defined(my $tmp = $dh->read)) {
153 # exclude filenames with part in name (same disk but partitions)
154 # use only filenames with scsi(with multipath i have the same device
155 # with dm-uuid-mpath , dm-name and scsi in name)
156 if($tmp !~ m/-part\d+$/ && $tmp =~ m/^scsi-/) {
157 my $path = "$stabledir/$tmp";
158 my $bdevdest = readlink($path);
159 if ($bdevdest && $bdevdest =~ m|^../../([^/]+)|) {
160 $stable_paths->{$1}=$tmp;
161 }
162 }
163 }
164 $dh->close;
165 }
166 return $stable_paths;
167 }
168
169 sub iscsi_device_list {
170
171 my $res = {};
172
173 my $dirname = '/sys/class/iscsi_session';
174
175 my $stable_paths = load_stable_scsi_paths();
176
177 dir_glob_foreach($dirname, 'session(\d+)', sub {
178 my ($ent, $session) = @_;
179
180 my $target = file_read_firstline("$dirname/$ent/targetname");
181 return if !$target;
182
183 my (undef, $host) = dir_glob_regex("$dirname/$ent/device", 'target(\d+):.*');
184 return if !defined($host);
185
186 dir_glob_foreach("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
187 my ($tmp, $channel, $id, $lun) = @_;
188
189 my $type = file_read_firstline("/sys/bus/scsi/devices/$tmp/type");
190 return if !defined($type) || $type ne '0'; # list disks only
191
192 my $bdev;
193 if (-d "/sys/bus/scsi/devices/$tmp/block") { # newer kernels
194 (undef, $bdev) = dir_glob_regex("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
195 } else {
196 (undef, $bdev) = dir_glob_regex("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
197 }
198 return if !$bdev;
199
200 #check multipath
201 if (-d "/sys/block/$bdev/holders") {
202 my $multipathdev = dir_glob_regex("/sys/block/$bdev/holders", '[A-Za-z]\S*');
203 $bdev = $multipathdev if $multipathdev;
204 }
205
206 my $blockdev = $stable_paths->{$bdev};
207 return if !$blockdev;
208
209 my $size = file_read_firstline("/sys/block/$bdev/size");
210 return if !$size;
211
212 my $volid = "$channel.$id.$lun.$blockdev";
213
214 $res->{$target}->{$volid} = {
215 'format' => 'raw',
216 'size' => int($size * 512),
217 'vmid' => 0, # not assigned to any vm
218 'channel' => int($channel),
219 'id' => int($id),
220 'lun' => int($lun),
221 };
222
223 #print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
224 });
225
226 });
227
228 return $res;
229 }
230
231 # Configuration
232
233 sub type {
234 return 'iscsi';
235 }
236
237 sub plugindata {
238 return {
239 content => [ {images => 1, none => 1}, { images => 1 }],
240 };
241 }
242
243 sub properties {
244 return {
245 target => {
246 description => "iSCSI target.",
247 type => 'string',
248 },
249 portal => {
250 description => "iSCSI portal (IP or DNS name with optional port).",
251 type => 'string', format => 'pve-storage-portal-dns',
252 },
253 };
254 }
255
256 sub options {
257 return {
258 portal => { fixed => 1 },
259 target => { fixed => 1 },
260 nodes => { optional => 1},
261 disable => { optional => 1},
262 content => { optional => 1},
263 };
264 }
265
266 # Storage implementation
267
268 sub parse_volname {
269 my ($class, $volname) = @_;
270
271 if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
272 return ('images', $1, undef, undef, undef, undef, 'raw');
273 }
274
275 die "unable to parse iscsi volume name '$volname'\n";
276 }
277
278 sub filesystem_path {
279 my ($class, $scfg, $volname, $snapname) = @_;
280
281 die "snapshot is not possible on iscsi storage\n" if defined($snapname);
282
283 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
284
285 my $path = "/dev/disk/by-id/$name";
286
287 return wantarray ? ($path, $vmid, $vtype) : $path;
288 }
289
290 sub create_base {
291 my ($class, $storeid, $scfg, $volname) = @_;
292
293 die "can't create base images in iscsi storage\n";
294 }
295
296 sub clone_image {
297 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
298
299 die "can't clone images in iscsi storage\n";
300 }
301
302 sub alloc_image {
303 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
304
305 die "can't allocate space in iscsi storage\n";
306 }
307
308 sub free_image {
309 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
310
311 die "can't free space in iscsi storage\n";
312 }
313
314 sub list_images {
315 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
316
317 my $res = [];
318
319 $cache->{iscsi_devices} = iscsi_device_list() if !$cache->{iscsi_devices};
320
321 # we have no owner for iscsi devices
322
323 my $target = $scfg->{target};
324
325 if (my $dat = $cache->{iscsi_devices}->{$target}) {
326
327 foreach my $volname (keys %$dat) {
328
329 my $volid = "$storeid:$volname";
330
331 if ($vollist) {
332 my $found = grep { $_ eq $volid } @$vollist;
333 next if !$found;
334 } else {
335 # we have no owner for iscsi devices
336 next if defined($vmid);
337 }
338
339 my $info = $dat->{$volname};
340 $info->{volid} = $volid;
341
342 push @$res, $info;
343 }
344 }
345
346 return $res;
347 }
348
349 sub status {
350 my ($class, $storeid, $scfg, $cache) = @_;
351
352 $cache->{iscsi_sessions} = iscsi_session_list() if !$cache->{iscsi_sessions};
353
354 my $active = defined($cache->{iscsi_sessions}->{$scfg->{target}});
355
356 return (0, 0, 0, $active);
357 }
358
359 sub activate_storage {
360 my ($class, $storeid, $scfg, $cache) = @_;
361
362 return if !check_iscsi_support(1);
363
364 $cache->{iscsi_sessions} = iscsi_session_list() if !$cache->{iscsi_sessions};
365
366 my $iscsi_sess = $cache->{iscsi_sessions}->{$scfg->{target}};
367 if (!defined ($iscsi_sess)) {
368 eval { iscsi_login($scfg->{target}, $scfg->{portal}); };
369 warn $@ if $@;
370 } else {
371 # make sure we get all devices
372 iscsi_session_rescan($iscsi_sess);
373 }
374 }
375
376 sub deactivate_storage {
377 my ($class, $storeid, $scfg, $cache) = @_;
378
379 return if !check_iscsi_support(1);
380
381 $cache->{iscsi_sessions} = iscsi_session_list() if !$cache->{iscsi_sessions};
382
383 my $iscsi_sess = $cache->{iscsi_sessions}->{$scfg->{target}};
384
385 if (defined ($iscsi_sess)) {
386 iscsi_logout($scfg->{target}, $scfg->{portal});
387 }
388 }
389
390 sub check_connection {
391 my ($class, $storeid, $scfg) = @_;
392
393 my $portal = $scfg->{portal};
394 return iscsi_test_portal($portal);
395 }
396
397 sub volume_resize {
398 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
399 die "volume resize is not possible on iscsi device";
400 }
401
402 sub volume_has_feature {
403 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
404
405 my $features = {
406 copy => { current => 1},
407 };
408
409 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
410 $class->parse_volname($volname);
411
412 my $key = undef;
413 if($snapname){
414 $key = 'snap';
415 }else{
416 $key = $isBase ? 'base' : 'current';
417 }
418 return 1 if $features->{$feature}->{$key};
419
420 return undef;
421 }
422
423
424 1;