]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LunCmd/Istgt.pm
Module to support istgt lun commands Istgt is used on any *BSD. As of FreeBSD 10...
[pve-storage.git] / PVE / Storage / LunCmd / Istgt.pm
1 package PVE::Storage::LunCmd::Istgt;
2
3 # TODO
4 # Create initial target and LUN if target is missing ?
5 # Create and use list of free LUNs
6
7 use strict;
8 use warnings;
9 use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
10 use Data::Dumper;
11
12 my @CONFIG_FILES = (
13 '/usr/local/etc/istgt/istgt.conf', # FreeBSD, FreeNAS
14 '/var/etc/iscsi/istgt.conf' # NAS4Free
15 );
16 my @DAEMONS = (
17 '/usr/local/etc/rc.d/istgt', # FreeBSD, FreeNAS
18 '/var/etc/rc.d/istgt' # NAS4Free
19 );
20
21 # A logical unit can max have 63 LUNs
22 # https://code.google.com/p/istgt/source/browse/src/istgt_lu.h#39
23 my $MAX_LUNS = 64;
24
25 my $CONFIG_FILE = undef;
26 my $DAEMON = undef;
27 my $SETTINGS = undef;
28 my $CONFIG = undef;
29 my $OLD_CONFIG = undef;
30
31 my @ssh_opts = ('-o', 'BatchMode=yes');
32 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
33 my @scp_cmd = ('/usr/bin/scp', @ssh_opts);
34
35 #Current SIGHUP reload limitations (http://www.peach.ne.jp/archives/istgt/):
36 #
37 # The parameters other than PG, IG, and LU are not reloaded by SIGHUP.
38 # LU connected by the initiator can't be reloaded by SIGHUP.
39 # PG and IG mapped to LU can't be deleted by SIGHUP.
40 # If you delete an active LU, all connections of the LU are closed by SIGHUP.
41 # Updating IG is not affected until the next login.
42 #
43 # FreeBSD
44 # 1. Alt-F2 to change to native shell (zfsguru)
45 # 2. pw mod user root -w yes (change password for root to root)
46 # 3. vi /etc/ssh/sshd_config
47 # 4. uncomment PermitRootLogin yes
48 # 5. change PasswordAuthentication no to PasswordAuthentication yes
49 # 5. /etc/rc.d/sshd restart
50 # 6. On one of the proxmox nodes login as root and run: ssh-copy-id ip_freebsd_host
51 # 7. vi /etc/ssh/sshd_config
52 # 8. comment PermitRootLogin yes
53 # 9. change PasswordAuthentication yes to PasswordAuthentication no
54 # 10. /etc/rc.d/sshd restart
55 # 11. Reset passwd -> pw mod user root -w no
56 # 12. Alt-Ctrl-F1 to return to zfsguru shell (zfsguru)
57
58 sub get_base;
59 sub run_lun_command;
60
61 my $read_config = sub {
62 my ($scfg, $timeout, $method) = @_;
63
64 my $msg = '';
65 my $err = undef;
66 my $luncmd = 'cat';
67 my $target;
68 $timeout = 10 if !$timeout;
69
70 my $output = sub {
71 my $line = shift;
72 $msg .= "$line\n";
73 };
74
75 my $errfunc = sub {
76 my $line = shift;
77 $err .= "$line";
78 };
79
80 $target = 'root@' . $scfg->{portal};
81
82 my $daemon = 0;
83 foreach my $config (@CONFIG_FILES) {
84 $err = undef;
85 my $cmd = [@ssh_cmd, $target, $luncmd, $config];
86 eval {
87 run_command($cmd, outfunc => $output, errfunc => $errfunc, timeout => $timeout);
88 };
89 do {
90 $err = undef;
91 $DAEMON = $DAEMONS[$daemon];
92 $CONFIG_FILE = $config;
93 last;
94 } unless $@;
95 $daemon++;
96 }
97 die $err if ($err && $err !~ /No such file or directory/);
98 die "No configuration found. Install istgt on $scfg->{portal}" if $msg eq '';
99
100 return $msg;
101 };
102
103 my $get_config = sub {
104 my ($scfg) = @_;
105 my @conf = undef;
106
107 my $config = $read_config->($scfg, undef, 'get_config');
108 die "Missing config file" unless $config;
109
110 $OLD_CONFIG = $config;
111
112 return $config;
113 };
114
115 my $parse_size = sub {
116 my ($text) = @_;
117
118 return 0 if !$text;
119
120 if ($text =~ m/^(\d+(\.\d+)?)([TGMK]B)?$/) {
121 my ($size, $reminder, $unit) = ($1, $2, $3);
122 return $size if !$unit;
123 if ($unit eq 'KB') {
124 $size *= 1024;
125 } elsif ($unit eq 'MB') {
126 $size *= 1024*1024;
127 } elsif ($unit eq 'GB') {
128 $size *= 1024*1024*1024;
129 } elsif ($unit eq 'TB') {
130 $size *= 1024*1024*1024*1024;
131 }
132 if ($reminder) {
133 $size = ceil($size);
134 }
135 return $size;
136 } elsif ($text =~ /^auto$/i) {
137 return 'AUTO';
138 } else {
139 return 0;
140 }
141 };
142
143 my $size_with_unit = sub {
144 my ($size, $n) = (shift, 0);
145
146 return '0KB' if !$size;
147
148 return $size if $size eq 'AUTO';
149
150 if ($size =~ m/^\d+$/) {
151 ++$n and $size /= 1024 until $size < 1024;
152 if ($size =~ /\./) {
153 return sprintf "%.2f%s", $size, ( qw[bytes KB MB GB TB] )[ $n ];
154 } else {
155 return sprintf "%d%s", $size, ( qw[bytes KB MB GB TB] )[ $n ];
156 }
157 }
158 die "$size: Not a number";
159 };
160
161 my $lun_dumper = sub {
162 my ($lun) = @_;
163 my $config = '';
164
165 $config .= "\n[$lun]\n";
166 $config .= 'TargetName ' . $SETTINGS->{$lun}->{TargetName} . "\n";
167 $config .= 'Mapping ' . $SETTINGS->{$lun}->{Mapping} . "\n";
168 $config .= 'AuthGroup ' . $SETTINGS->{$lun}->{AuthGroup} . "\n";
169 $config .= 'UnitType ' . $SETTINGS->{$lun}->{UnitType} . "\n";
170 $config .= 'QueueDepth ' . $SETTINGS->{$lun}->{QueueDepth} . "\n";
171
172 foreach my $conf (@{$SETTINGS->{$lun}->{luns}}) {
173 $config .= "$conf->{lun} Storage " . $conf->{Storage};
174 $config .= ' ' . $size_with_unit->($conf->{Size}) . "\n";
175 }
176 $config .= "\n";
177
178 return $config;
179 };
180
181 my $get_lu_name = sub {
182 my ($target) = @_;
183 my $used = ();
184 my $i;
185
186 if (! exists $SETTINGS->{$target}->{used}) {
187 for ($i = 0; $i < $MAX_LUNS; $i++) {
188 $used->{$i} = 0;
189 }
190 foreach my $lun (@{$SETTINGS->{$target}->{luns}}) {
191 $lun->{lun} =~ /^LUN(\d+)$/;
192 $used->{$1} = 1;
193 }
194 $SETTINGS->{$target}->{used} = $used;
195 }
196
197 $used = $SETTINGS->{$target}->{used};
198 for ($i = 0; $i < $MAX_LUNS; $i++) {
199 last unless $used->{$i};
200 }
201 $SETTINGS->{$target}->{used}->{$i} = 1;
202
203 return "LUN$i";
204 };
205
206 my $init_lu_name = sub {
207 my ($target) = @_;
208 my $used = ();
209
210 if (! exists($SETTINGS->{$target}->{used})) {
211 for (my $i = 0; $i < $MAX_LUNS; $i++) {
212 $used->{$i} = 0;
213 }
214 $SETTINGS->{$target}->{used} = $used;
215 }
216 foreach my $lun (@{$SETTINGS->{$target}->{luns}}) {
217 $lun->{lun} =~ /^LUN(\d+)$/;
218 $SETTINGS->{$target}->{used}->{$1} = 1;
219 }
220 };
221
222 my $free_lu_name = sub {
223 my ($target, $lu_name) = @_;
224 my $used = ();
225
226 $lu_name =~ /^LUN(\d+)$/;
227 $SETTINGS->{$target}->{used}->{$1} = 0;
228 };
229
230 my $make_lun = sub {
231 my ($path) = @_;
232
233 my $target = $SETTINGS->{current};
234 die 'Maximum number of LUNs per target is 63' if scalar @{$SETTINGS->{$target}->{luns}} >= $MAX_LUNS;
235
236 my $lun = $get_lu_name->($target);
237 my $conf = {
238 lun => $lun,
239 Storage => $path,
240 Size => 'AUTO',
241 };
242 push @{$SETTINGS->{$target}->{luns}}, $conf;
243
244 return $conf->{lun};
245 };
246
247 my $parser = sub {
248 my ($scfg) = @_;
249
250 my $lun = undef;
251 my $line = 0;
252
253 my $config = $get_config->($scfg);
254 my @cfgfile = split "\n", $config;
255
256 foreach (@cfgfile) {
257 $line++;
258 if ($_ =~ /^\s*\[(PortalGroup\d+)\]\s*/) {
259 $lun = undef;
260 $SETTINGS->{$1} = ();
261 } elsif ($_ =~ /^\s*\[(InitiatorGroup\d+)\]\s*/) {
262 $lun = undef;
263 $SETTINGS->{$1} = ();
264 } elsif ($_ =~ /^\s*PidFile\s+"?([\w\/\.]+)"?\s*/) {
265 $lun = undef;
266 $SETTINGS->{pidfile} = $1;
267 } elsif ($_ =~ /^\s*NodeBase\s+"?([\w\-\.]+)"?\s*/) {
268 $lun = undef;
269 $SETTINGS->{nodebase} = $1;
270 } elsif ($_ =~ /^\s*\[(LogicalUnit\d+)\]\s*/) {
271 $lun = $1;
272 $SETTINGS->{$lun} = ();
273 $SETTINGS->{targets}++;
274 } elsif ($lun) {
275 next if (($_ =~ /^\s*#/) || ($_ =~ /^\s*$/));
276 if ($_ =~ /^\s*(\w+)\s+(.+)\s*/) {
277 #next if $2 =~ /^Option.*/;
278 $SETTINGS->{$lun}->{$1} = $2;
279 $SETTINGS->{$lun}->{$1} =~ s/^\s+|\s+$|"\s*//g;
280 } else {
281 die "$line: parse error [$_]";
282 }
283 }
284 $CONFIG .= "$_\n" unless $lun;
285 }
286
287 $CONFIG =~ s/\n$//;
288 die "$scfg->{target}: Target not found" unless $SETTINGS->{targets};
289 my $max = $SETTINGS->{targets};
290 my $base = get_base;
291 my $n;
292 for (my $i = 1; $i <= $max; $i++) {
293 my $target = $SETTINGS->{nodebase}.':'.$SETTINGS->{"LogicalUnit$i"}->{TargetName};
294 if ($target eq $scfg->{target}) {
295 my $lu = ();
296 while ((my $key, my $val) = each(%{$SETTINGS->{"LogicalUnit$i"}})) {
297 if ($key =~ /^LUN\d+/) {
298 if ($val =~ /^Storage\s+([\w\/\-]+)\s+(\w+)/) {
299 my $storage = $1;
300 my $size = $parse_size->($2);
301 my $conf = undef;
302 if ($storage =~ /^$base\/$scfg->{pool}\/([\w\-]+)$/) {
303 $conf = {
304 lun => $key,
305 Storage => $storage,
306 Size => $size,
307 };
308 }
309 push @$lu, $conf if $conf;
310 }
311 delete $SETTINGS->{"LogicalUnit$i"}->{$key};
312 }
313 }
314 $SETTINGS->{"LogicalUnit$i"}->{luns} = $lu;
315 $SETTINGS->{current} = "LogicalUnit$i";
316 $init_lu_name->("LogicalUnit$i");
317 } else {
318 $CONFIG .= $lun_dumper->("LogicalUnit$i");
319 delete $SETTINGS->{"LogicalUnit$i"};
320 $SETTINGS->{targets}--;
321 }
322 }
323 die "$scfg->{target}: Target not found" unless $SETTINGS->{targets} > 0;
324 };
325
326 my $list_lun = sub {
327 my ($scfg, $timeout, $method, @params) = @_;
328 my $name = undef;
329
330 my $object = $params[0];
331 for my $key (keys %$SETTINGS) {
332 next unless $key =~ /^LogicalUnit\d+$/;
333 foreach my $lun (@{$SETTINGS->{$key}->{luns}}) {
334 if ($lun->{Storage} =~ /^$object$/) {
335 return $lun->{Storage};
336 }
337 }
338 }
339
340 return $name;
341 };
342
343 my $create_lun = sub {
344 my ($scfg, $timeout, $method, @params) = @_;
345 my $res = ();
346 my $file = "/tmp/config$$";
347
348 if ($list_lun->($scfg, $timeout, $method, @params)) {
349 die "$params[0]: LUN exists";
350 }
351 my $lun = $params[0];
352 $lun = $make_lun->($lun);
353 my $config = $lun_dumper->($SETTINGS->{current});
354 open(my $fh, '>', $file) or die "Could not open file '$file' $!";
355
356 print $fh $CONFIG;
357 print $fh $config;
358 close $fh;
359 @params = ($CONFIG_FILE);
360 $res = {
361 cmd => 'scp',
362 method => $file,
363 params => \@params,
364 msg => $lun,
365 post_exe => sub {
366 unlink $file;
367 },
368 };
369
370 return $res;
371 };
372
373 my $delete_lun = sub {
374 my ($scfg, $timeout, $method, @params) = @_;
375 my $res = ();
376 my $file = "/tmp/config$$";
377
378 my $target = $SETTINGS->{current};
379 my $luns = ();
380
381 foreach my $conf (@{$SETTINGS->{$target}->{luns}}) {
382 if ($conf->{Storage} =~ /^$params[0]$/) {
383 $free_lu_name->($target, $conf->{lun});
384 } else {
385 push @$luns, $conf;
386 }
387 }
388 $SETTINGS->{$target}->{luns} = $luns;
389
390 my $config = $lun_dumper->($SETTINGS->{current});
391 open(my $fh, '>', $file) or die "Could not open file '$file' $!";
392
393 print $fh $CONFIG;
394 print $fh $config;
395 close $fh;
396 @params = ($CONFIG_FILE);
397 $res = {
398 cmd => 'scp',
399 method => $file,
400 params => \@params,
401 post_exe => sub {
402 unlink $file;
403 run_lun_command($scfg, undef, 'add_view', 'restart');
404 },
405 };
406
407 return $res;
408 };
409
410 my $import_lun = sub {
411 my ($scfg, $timeout, $method, @params) = @_;
412
413 my $res = $create_lun->($scfg, $timeout, $method, @params);
414
415 return $res;
416 };
417
418 my $add_view = sub {
419 my ($scfg, $timeout, $method, @params) = @_;
420 my $cmdmap;
421
422 if (@params && $params[0] eq 'restart') {
423 @params = ('restart', '1>&2', '>', '/dev/null');
424 $cmdmap = {
425 cmd => 'ssh',
426 method => $DAEMON,
427 params => \@params,
428 };
429 } else {
430 @params = ('-HUP', '$(cat '. "$SETTINGS->{pidfile})");
431 $cmdmap = {
432 cmd => 'ssh',
433 method => 'kill',
434 params => \@params,
435 };
436 }
437
438 return $cmdmap;
439 };
440
441 my $modify_lun = sub {
442 my ($scfg, $timeout, $method, @params) = @_;
443
444 # Current SIGHUP reload limitations
445 # LU connected by the initiator can't be reloaded by SIGHUP.
446 # Until above limitation persists modifying a LUN will require
447 # a restart of the daemon breaking all current connections
448 #die 'Modify a connected LUN is not currently supported by istgt';
449 @params = ('restart', @params);
450
451 return $add_view->($scfg, $timeout, $method, @params);
452 };
453
454 my $list_view = sub {
455 my ($scfg, $timeout, $method, @params) = @_;
456 my $lun = undef;
457
458 my $object = $params[0];
459 for my $key (keys %$SETTINGS) {
460 next unless $key =~ /^LogicalUnit\d+$/;
461 foreach my $lun (@{$SETTINGS->{$key}->{luns}}) {
462 if ($lun->{Storage} =~ /^$object$/) {
463 if ($lun->{lun} =~ /^LUN(\d+)/) {
464 return $1;
465 }
466 die "$lun->{Storage}: Missing LUN";
467 }
468 }
469 }
470
471 return $lun;
472 };
473
474 my $get_lun_cmd_map = sub {
475 my ($method) = @_;
476
477 my $cmdmap = {
478 create_lu => { cmd => $create_lun },
479 delete_lu => { cmd => $delete_lun },
480 import_lu => { cmd => $import_lun },
481 modify_lu => { cmd => $modify_lun },
482 add_view => { cmd => $add_view },
483 list_view => { cmd => $list_view },
484 list_lu => { cmd => $list_lun },
485 };
486
487 die "unknown command '$method'" unless exists $cmdmap->{$method};
488
489 return $cmdmap->{$method};
490 };
491
492 sub run_lun_command {
493 my ($scfg, $timeout, $method, @params) = @_;
494
495 my $msg = '';
496 my $luncmd;
497 my $target;
498 my $cmd;
499 my $res;
500 $timeout = 10 if !$timeout;
501 my $is_add_view = 0;
502
503 my $output = sub {
504 my $line = shift;
505 $msg .= "$line\n";
506 };
507
508 $target = 'root@' . $scfg->{portal};
509
510 $parser->($scfg) unless $SETTINGS;
511 my $cmdmap = $get_lun_cmd_map->($method);
512 if ($method eq 'add_view') {
513 $is_add_view = 1 ;
514 $timeout = 15;
515 }
516 if (ref $cmdmap->{cmd} eq 'CODE') {
517 $res = $cmdmap->{cmd}->($scfg, $timeout, $method, @params);
518 if (ref $res) {
519 $method = $res->{method};
520 @params = @{$res->{params}};
521 if ($res->{cmd} eq 'scp') {
522 $cmd = [@scp_cmd, $method, "$target:$params[0]"];
523 } else {
524 $cmd = [@ssh_cmd, $target, $method, @params];
525 }
526 } else {
527 return $res;
528 }
529 } else {
530 $luncmd = $cmdmap->{cmd};
531 $method = $cmdmap->{method};
532 $cmd = [@ssh_cmd, $target, $luncmd, $method, @params];
533 }
534
535 eval {
536 run_command($cmd, outfunc => $output, timeout => $timeout);
537 };
538 if ($@ && $is_add_view) {
539 my $err = $@;
540 if ($OLD_CONFIG) {
541 my $err1 = undef;
542 my $file = "/tmp/config$$";
543 open(my $fh, '>', $file) or die "Could not open file '$file' $!";
544 print $fh $OLD_CONFIG;
545 close $fh;
546 $cmd = [@scp_cmd, $file, $CONFIG_FILE];
547 eval {
548 run_command($cmd, outfunc => $output, timeout => $timeout);
549 };
550 $err1 = $@ if $@;
551 unlink $file;
552 die "$err\n$err1" if $err1;
553 eval {
554 run_lun_command($scfg, undef, 'add_view', 'restart');
555 };
556 die "$err\n$@" if ($@);
557 }
558 die $err;
559 } elsif ($@) {
560 die $@;
561 } elsif ($is_add_view) {
562 $OLD_CONFIG = undef;
563 }
564
565 if ($res->{post_exe} && ref $res->{post_exe} eq 'CODE') {
566 $res->{post_exe}->();
567 }
568
569 if ($res->{msg}) {
570 $msg = $res->{msg};
571 }
572
573 return $msg;
574 }
575
576 sub get_base {
577 return '/dev/zvol';
578 }
579
580 1;