]>
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) = @_;
225 $lu_name =~ /^LUN(\d+)$/;
226 $SETTINGS->{$target}->{used
}->{$1} = 0;
232 my $target = $SETTINGS->{current
};
233 die 'Maximum number of LUNs per target is 63' if scalar @{$SETTINGS->{$target}->{luns
}} >= $MAX_LUNS;
235 my $lun = $get_lu_name->($target);
241 push @{$SETTINGS->{$target}->{luns
}}, $conf;
252 my $config = $get_config->($scfg);
253 my @cfgfile = split "\n", $config;
257 if ($_ =~ /^\s*\[(PortalGroup\d+)\]\s*/) {
259 $SETTINGS->{$1} = ();
260 } elsif ($_ =~ /^\s*\[(InitiatorGroup\d+)\]\s*/) {
262 $SETTINGS->{$1} = ();
263 } elsif ($_ =~ /^\s*PidFile\s+"?([\w\/\
.]+)"?\s*/) {
265 $SETTINGS->{pidfile} = $1;
266 } elsif ($_ =~ /^\s*NodeBase\s+"?
([\w\
-\
.]+)"?\s*/) {
268 $SETTINGS->{nodebase} = $1;
269 } elsif ($_ =~ /^\s*\[(LogicalUnit\d+)\]\s*/) {
271 $SETTINGS->{$lun} = ();
272 $SETTINGS->{targets}++;
274 next if (($_ =~ /^\s*#/) || ($_ =~ /^\s*$/));
275 if ($_ =~ /^\s*(\w+)\s+(.+)\s*/) {
276 #next if $2 =~ /^Option.*/;
277 $SETTINGS->{$lun}->{$1} = $2;
278 $SETTINGS->{$lun}->{$1} =~ s/^\s+|\s+$|"\s*//g;
280 die "$line: parse error
[$_]";
283 $CONFIG .= "$_\n" unless $lun;
287 die "$scfg->{target
}: Target
not found
" unless $SETTINGS->{targets};
288 my $max = $SETTINGS->{targets};
291 for (my $i = 1; $i <= $max; $i++) {
292 my $target = $SETTINGS->{nodebase}.':'.$SETTINGS->{"LogicalUnit
$i"}->{TargetName};
293 if ($target eq $scfg->{target}) {
295 while ((my $key, my $val) = each(%{$SETTINGS->{"LogicalUnit
$i"}})) {
296 if ($key =~ /^LUN\d+/) {
297 if ($val =~ /^Storage\s+([\w\/\-]+)\s+(\w+)/) {
299 my $size = $parse_size->($2);
301 if ($storage =~ /^$base\/$scfg->{pool}\/([\w\-]+)$/) {
308 push @$lu, $conf if $conf;
310 delete $SETTINGS->{"LogicalUnit
$i"}->{$key};
313 $SETTINGS->{"LogicalUnit
$i"}->{luns} = $lu;
314 $SETTINGS->{current} = "LogicalUnit
$i";
315 $init_lu_name->("LogicalUnit
$i");
317 $CONFIG .= $lun_dumper->("LogicalUnit
$i");
318 delete $SETTINGS->{"LogicalUnit
$i"};
319 $SETTINGS->{targets}--;
322 die "$scfg->{target
}: Target
not found
" unless $SETTINGS->{targets} > 0;
326 my ($scfg, $timeout, $method, @params) = @_;
329 my $object = $params[0];
330 for my $key (keys %$SETTINGS) {
331 next unless $key =~ /^LogicalUnit\d+$/;
332 foreach my $lun (@{$SETTINGS->{$key}->{luns}}) {
333 if ($lun->{Storage} =~ /^$object$/) {
334 return $lun->{Storage};
342 my $create_lun = sub {
343 my ($scfg, $timeout, $method, @params) = @_;
345 my $file = "/tmp/config
$$";
347 if ($list_lun->($scfg, $timeout, $method, @params)) {
348 die "$params[0]: LUN
exists";
350 my $lun = $params[0];
351 $lun = $make_lun->($lun);
352 my $config = $lun_dumper->($SETTINGS->{current});
353 open(my $fh, '>', $file) or die "Could
not open file
'$file' $!";
358 @params = ($CONFIG_FILE);
372 my $delete_lun = sub {
373 my ($scfg, $timeout, $method, @params) = @_;
375 my $file = "/tmp/config
$$";
377 my $target = $SETTINGS->{current};
380 foreach my $conf (@{$SETTINGS->{$target}->{luns}}) {
381 if ($conf->{Storage} =~ /^$params[0]$/) {
382 $free_lu_name->($target, $conf->{lun});
387 $SETTINGS->{$target}->{luns} = $luns;
389 my $config = $lun_dumper->($SETTINGS->{current});
390 open(my $fh, '>', $file) or die "Could
not open file
'$file' $!";
395 @params = ($CONFIG_FILE);
402 run_lun_command($scfg, undef, 'add_view', 'restart');
409 my $import_lun = sub {
410 my ($scfg, $timeout, $method, @params) = @_;
412 my $res = $create_lun->($scfg, $timeout, $method, @params);
418 my ($scfg, $timeout, $method, @params) = @_;
421 if (@params && $params[0] eq 'restart') {
422 @params = ('restart', '1>&2', '>', '/dev/null');
429 @params = ('-HUP', '$(cat '. "$SETTINGS->{pidfile
})");
440 my $modify_lun = sub {
441 my ($scfg, $timeout, $method, @params) = @_;
443 # Current SIGHUP reload limitations
444 # LU connected by the initiator can't be reloaded by SIGHUP.
445 # Until above limitation persists modifying a LUN will require
446 # a restart of the daemon breaking all current connections
447 #die 'Modify a connected LUN is not currently supported by istgt';
448 @params = ('restart', @params);
450 return $add_view->($scfg, $timeout, $method, @params);
453 my $list_view = sub {
454 my ($scfg, $timeout, $method, @params) = @_;
457 my $object = $params[0];
458 for my $key (keys %$SETTINGS) {
459 next unless $key =~ /^LogicalUnit\d+$/;
460 foreach my $lun (@{$SETTINGS->{$key}->{luns}}) {
461 if ($lun->{Storage} =~ /^$object$/) {
462 if ($lun->{lun} =~ /^LUN(\d+)/) {
465 die "$lun->{Storage
}: Missing LUN
";
473 my $get_lun_cmd_map = sub {
477 create_lu => { cmd => $create_lun },
478 delete_lu => { cmd => $delete_lun },
479 import_lu => { cmd => $import_lun },
480 modify_lu => { cmd => $modify_lun },
481 add_view => { cmd => $add_view },
482 list_view => { cmd => $list_view },
483 list_lu => { cmd => $list_lun },
486 die "unknown command
'$method'" unless exists $cmdmap->{$method};
488 return $cmdmap->{$method};
491 sub run_lun_command {
492 my ($scfg, $timeout, $method, @params) = @_;
499 $timeout = 10 if !$timeout;
507 $target = 'root@' . $scfg->{portal};
509 $parser->($scfg) unless $SETTINGS;
510 my $cmdmap = $get_lun_cmd_map->($method);
511 if ($method eq 'add_view') {
515 if (ref $cmdmap->{cmd} eq 'CODE') {
516 $res = $cmdmap->{cmd}->($scfg, $timeout, $method, @params);
518 $method = $res->{method};
519 @params = @{$res->{params}};
520 if ($res->{cmd} eq 'scp') {
521 $cmd = [@scp_cmd, $method, "$target:$params[0]"];
523 $cmd = [@ssh_cmd, $target, $method, @params];
529 $luncmd = $cmdmap->{cmd};
530 $method = $cmdmap->{method};
531 $cmd = [@ssh_cmd, $target, $luncmd, $method, @params];
535 run_command($cmd, outfunc => $output, timeout => $timeout);
537 if ($@ && $is_add_view) {
541 my $file = "/tmp/config
$$";
542 open(my $fh, '>', $file) or die "Could
not open file
'$file' $!";
543 print $fh $OLD_CONFIG;
545 $cmd = [@scp_cmd, $file, $CONFIG_FILE];
547 run_command($cmd, outfunc => $output, timeout => $timeout);
551 die "$err\n$err1" if $err1;
553 run_lun_command($scfg, undef, 'add_view', 'restart');
555 die "$err\n$@" if ($@);
560 } elsif ($is_add_view) {
564 if ($res->{post_exe} && ref $res->{post_exe} eq 'CODE') {
565 $res->{post_exe}->();