]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LunCmd/Istgt.pm
1 package PVE
::Storage
::LunCmd
::Istgt
;
4 # Create initial target and LUN if target is missing ?
5 # Create and use list of free LUNs
9 use PVE
::Tools
qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
13 '/usr/local/etc/istgt/istgt.conf', # FreeBSD, FreeNAS
14 '/var/etc/iscsi/istgt.conf' # NAS4Free
17 '/usr/local/etc/rc.d/istgt', # FreeBSD, FreeNAS
18 '/var/etc/rc.d/istgt' # NAS4Free
21 # A logical unit can max have 63 LUNs
22 # https://code.google.com/p/istgt/source/browse/src/istgt_lu.h#39
25 my $CONFIG_FILE = undef;
29 my $OLD_CONFIG = undef;
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);
35 #Current SIGHUP reload limitations (http://www.peach.ne.jp/archives/istgt/):
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.
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)
61 my $read_config = sub {
62 my ($scfg, $timeout, $method) = @_;
68 $timeout = 10 if !$timeout;
80 $target = 'root@' . $scfg->{portal
};
83 foreach my $config (@CONFIG_FILES) {
85 my $cmd = [@ssh_cmd, $target, $luncmd, $config];
87 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
91 $DAEMON = $DAEMONS[$daemon];
92 $CONFIG_FILE = $config;
97 die $err if ($err && $err !~ /No such file or directory/);
98 die "No configuration found. Install istgt on $scfg->{portal}" if $msg eq '';
103 my $get_config = sub {
107 my $config = $read_config->($scfg, undef, 'get_config');
108 die "Missing config file" unless $config;
110 $OLD_CONFIG = $config;
115 my $parse_size = sub {
120 if ($text =~ m/^(\d+(\.\d+)?)([TGMK]B)?$/) {
121 my ($size, $reminder, $unit) = ($1, $2, $3);
122 return $size if !$unit;
125 } elsif ($unit eq 'MB') {
127 } elsif ($unit eq 'GB') {
128 $size *= 1024*1024*1024;
129 } elsif ($unit eq 'TB') {
130 $size *= 1024*1024*1024*1024;
136 } elsif ($text =~ /^auto$/i) {
143 my $size_with_unit = sub {
144 my ($size, $n) = (shift, 0);
146 return '0KB' if !$size;
148 return $size if $size eq 'AUTO';
150 if ($size =~ m/^\d+$/) {
151 ++$n and $size /= 1024 until $size < 1024;
153 return sprintf "%.2f%s", $size, ( qw[bytes KB MB GB TB] )[ $n ];
155 return sprintf "%d%s", $size, ( qw[bytes KB MB GB TB] )[ $n ];
158 die "$size: Not a number";
161 my $lun_dumper = sub {
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";
172 foreach my $conf (@{$SETTINGS->{$lun}->{luns
}}) {
173 $config .= "$conf->{lun} Storage " . $conf->{Storage
};
174 $config .= ' ' . $size_with_unit->($conf->{Size
}) . "\n";
181 my $get_lu_name = sub {
186 if (! exists $SETTINGS->{$target}->{used
}) {
187 for ($i = 0; $i < $MAX_LUNS; $i++) {
190 foreach my $lun (@{$SETTINGS->{$target}->{luns
}}) {
191 $lun->{lun
} =~ /^LUN(\d+)$/;
194 $SETTINGS->{$target}->{used
} = $used;
197 $used = $SETTINGS->{$target}->{used
};
198 for ($i = 0; $i < $MAX_LUNS; $i++) {
199 last unless $used->{$i};
201 $SETTINGS->{$target}->{used
}->{$i} = 1;
206 my $init_lu_name = sub {
210 if (! exists($SETTINGS->{$target}->{used
})) {
211 for (my $i = 0; $i < $MAX_LUNS; $i++) {
214 $SETTINGS->{$target}->{used
} = $used;
216 foreach my $lun (@{$SETTINGS->{$target}->{luns
}}) {
217 $lun->{lun
} =~ /^LUN(\d+)$/;
218 $SETTINGS->{$target}->{used
}->{$1} = 1;
222 my $free_lu_name = sub {
223 my ($target, $lu_name) = @_;
226 $lu_name =~ /^LUN(\d+)$/;
227 $SETTINGS->{$target}->{used
}->{$1} = 0;
233 my $target = $SETTINGS->{current
};
234 die 'Maximum number of LUNs per target is 63' if scalar @{$SETTINGS->{$target}->{luns
}} >= $MAX_LUNS;
236 my $lun = $get_lu_name->($target);
242 push @{$SETTINGS->{$target}->{luns
}}, $conf;
253 my $config = $get_config->($scfg);
254 my @cfgfile = split "\n", $config;
258 if ($_ =~ /^\s*\[(PortalGroup\d+)\]\s*/) {
260 $SETTINGS->{$1} = ();
261 } elsif ($_ =~ /^\s*\[(InitiatorGroup\d+)\]\s*/) {
263 $SETTINGS->{$1} = ();
264 } elsif ($_ =~ /^\s*PidFile\s+"?([\w\/\
.]+)"?\s*/) {
266 $SETTINGS->{pidfile} = $1;
267 } elsif ($_ =~ /^\s*NodeBase\s+"?
([\w\
-\
.]+)"?\s*/) {
269 $SETTINGS->{nodebase} = $1;
270 } elsif ($_ =~ /^\s*\[(LogicalUnit\d+)\]\s*/) {
272 $SETTINGS->{$lun} = ();
273 $SETTINGS->{targets}++;
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;
281 die "$line: parse error
[$_]";
284 $CONFIG .= "$_\n" unless $lun;
288 die "$scfg->{target
}: Target
not found
" unless $SETTINGS->{targets};
289 my $max = $SETTINGS->{targets};
292 for (my $i = 1; $i <= $max; $i++) {
293 my $target = $SETTINGS->{nodebase}.':'.$SETTINGS->{"LogicalUnit
$i"}->{TargetName};
294 if ($target eq $scfg->{target}) {
296 while ((my $key, my $val) = each(%{$SETTINGS->{"LogicalUnit
$i"}})) {
297 if ($key =~ /^LUN\d+/) {
298 if ($val =~ /^Storage\s+([\w\/\-]+)\s+(\w+)/) {
300 my $size = $parse_size->($2);
302 if ($storage =~ /^$base\/$scfg->{pool}\/([\w\-]+)$/) {
309 push @$lu, $conf if $conf;
311 delete $SETTINGS->{"LogicalUnit
$i"}->{$key};
314 $SETTINGS->{"LogicalUnit
$i"}->{luns} = $lu;
315 $SETTINGS->{current} = "LogicalUnit
$i";
316 $init_lu_name->("LogicalUnit
$i");
318 $CONFIG .= $lun_dumper->("LogicalUnit
$i");
319 delete $SETTINGS->{"LogicalUnit
$i"};
320 $SETTINGS->{targets}--;
323 die "$scfg->{target
}: Target
not found
" unless $SETTINGS->{targets} > 0;
327 my ($scfg, $timeout, $method, @params) = @_;
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};
343 my $create_lun = sub {
344 my ($scfg, $timeout, $method, @params) = @_;
346 my $file = "/tmp/config
$$";
348 if ($list_lun->($scfg, $timeout, $method, @params)) {
349 die "$params[0]: LUN
exists";
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' $!";
359 @params = ($CONFIG_FILE);
373 my $delete_lun = sub {
374 my ($scfg, $timeout, $method, @params) = @_;
376 my $file = "/tmp/config
$$";
378 my $target = $SETTINGS->{current};
381 foreach my $conf (@{$SETTINGS->{$target}->{luns}}) {
382 if ($conf->{Storage} =~ /^$params[0]$/) {
383 $free_lu_name->($target, $conf->{lun});
388 $SETTINGS->{$target}->{luns} = $luns;
390 my $config = $lun_dumper->($SETTINGS->{current});
391 open(my $fh, '>', $file) or die "Could
not open file
'$file' $!";
396 @params = ($CONFIG_FILE);
403 run_lun_command($scfg, undef, 'add_view', 'restart');
410 my $import_lun = sub {
411 my ($scfg, $timeout, $method, @params) = @_;
413 my $res = $create_lun->($scfg, $timeout, $method, @params);
419 my ($scfg, $timeout, $method, @params) = @_;
422 if (@params && $params[0] eq 'restart') {
423 @params = ('restart', '1>&2', '>', '/dev/null');
430 @params = ('-HUP', '$(cat '. "$SETTINGS->{pidfile
})");
441 my $modify_lun = sub {
442 my ($scfg, $timeout, $method, @params) = @_;
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);
451 return $add_view->($scfg, $timeout, $method, @params);
454 my $list_view = sub {
455 my ($scfg, $timeout, $method, @params) = @_;
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+)/) {
466 die "$lun->{Storage
}: Missing LUN
";
474 my $get_lun_cmd_map = sub {
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 },
487 die "unknown command
'$method'" unless exists $cmdmap->{$method};
489 return $cmdmap->{$method};
492 sub run_lun_command {
493 my ($scfg, $timeout, $method, @params) = @_;
500 $timeout = 10 if !$timeout;
508 $target = 'root@' . $scfg->{portal};
510 $parser->($scfg) unless $SETTINGS;
511 my $cmdmap = $get_lun_cmd_map->($method);
512 if ($method eq 'add_view') {
516 if (ref $cmdmap->{cmd} eq 'CODE') {
517 $res = $cmdmap->{cmd}->($scfg, $timeout, $method, @params);
519 $method = $res->{method};
520 @params = @{$res->{params}};
521 if ($res->{cmd} eq 'scp') {
522 $cmd = [@scp_cmd, $method, "$target:$params[0]"];
524 $cmd = [@ssh_cmd, $target, $method, @params];
530 $luncmd = $cmdmap->{cmd};
531 $method = $cmdmap->{method};
532 $cmd = [@ssh_cmd, $target, $luncmd, $method, @params];
536 run_command($cmd, outfunc => $output, timeout => $timeout);
538 if ($@ && $is_add_view) {
542 my $file = "/tmp/config
$$";
543 open(my $fh, '>', $file) or die "Could
not open file
'$file' $!";
544 print $fh $OLD_CONFIG;
546 $cmd = [@scp_cmd, $file, $CONFIG_FILE];
548 run_command($cmd, outfunc => $output, timeout => $timeout);
552 die "$err\n$err1" if $err1;
554 run_lun_command($scfg, undef, 'add_view', 'restart');
556 die "$err\n$@" if ($@);
561 } elsif ($is_add_view) {
565 if ($res->{post_exe} && ref $res->{post_exe} eq 'CODE') {
566 $res->{post_exe}->();