]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LunCmd/Iet.pm
1 package PVE
::Storage
::LunCmd
::Iet
;
3 # iscsi storage running Debian
4 # 1) apt-get install iscsitarget iscsitarget-dkms
5 # 2) Create target like (/etc/iet/ietd.conf):
6 # Target iqn.2001-04.com.example:tank
8 # 3) Activate daemon (/etc/default/iscsitarget)
9 # ISCSITARGET_ENABLE=true
10 # 4) service iscsitarget start
12 # On one of the proxmox nodes:
14 # 2) ssh-copy-id <ip_of_iscsi_storage>
18 use PVE
::Tools
qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
23 # A logical unit can max have 16864 LUNs
24 # http://manpages.ubuntu.com/manpages/precise/man5/ietd.conf.5.html
27 my $CONFIG_FILE = '/etc/iet/ietd.conf';
28 my $DAEMON = '/usr/sbin/ietadm';
31 my $OLD_CONFIG = undef;
33 my @ssh_opts = ('-o', 'BatchMode=yes');
34 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
35 my @scp_cmd = ('/usr/bin/scp', @ssh_opts);
36 my $ietadm = '/usr/sbin/ietadm';
38 my $execute_command = sub {
39 my ($scfg, $exec, $timeout, $method, @params) = @_;
47 $timeout = 10 if !$timeout;
59 $target = 'root@' . $scfg->{portal
};
62 $cmd = [@scp_cmd, $method, "$target:$params[0]"];
64 $cmd = [@ssh_cmd, $target, $method, @params];
68 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
85 my $read_config = sub {
86 my ($scfg, $timeout) = @_;
92 $timeout = 10 if !$timeout;
104 $target = 'root@' . $scfg->{portal
};
106 my $cmd = [@ssh_cmd, $target, $luncmd, $CONFIG_FILE];
108 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
111 die $err if ($err !~ /No such file or directory/);
112 die "No configuration found. Install iet on $scfg->{portal}" if $msg eq '';
118 my $get_config = sub {
122 my $config = $read_config->($scfg, undef);
123 die "Missing config file" unless $config;
125 $OLD_CONFIG = $config;
136 my $config = $get_config->($scfg);
137 my @cfgfile = split "\n", $config;
142 if ($_ =~ /^\s*Target\s*([\w\-\:\.]+)\s*$/) {
143 if ($1 eq $scfg->{target
} && ! $cfg_target) {
145 die "$line: Parse error [$_]" if $SETTINGS;
146 $SETTINGS->{target
} = $1;
148 } elsif ($1 eq $scfg->{target
} && $cfg_target) {
149 die "$line: Parse error [$_]";
150 } elsif ($cfg_target) {
158 $SETTINGS->{text
} .= "$_\n";
159 next if ($_ =~ /^\s*#/ || ! $_);
161 if ($_ =~ /^(\w+)\s*#/) {
164 if ($option =~ /^\s*(\w+)\s+(\w+)\s*$/) {
166 die "$line: Parse error [$_]";
168 $SETTINGS->{$1} = $2;
169 } elsif ($option =~ /^\s*(\w+)\s+(\d+)\s+([\w\-\/=,]+)\s
*$/) {
170 die "$line: Parse error [$option]" unless ($1 eq 'Lun');
173 my @lun = split ',', $3;
174 die "$line: Parse error [$option]" unless (scalar(@lun) > 1);
176 my @lun_opt = split '=', $_;
177 die "$line: Parse error [$option]" unless (scalar(@lun_opt) == 2);
178 $conf->{$lun_opt[0]} = $lun_opt[1];
180 if ($conf->{Path
} && $conf->{Path
} =~ /^$base\/$scfg
->{pool
}\
/([\w\-]+)$/) {
181 $conf->{include
} = 1;
183 $conf->{include
} = 0;
186 push @{$SETTINGS->{luns
}}, $conf;
188 die "$line: Parse error [$option]";
195 $CONFIG =~ s/^\s+|\s+$|"\s*//g;
198 my $update_config = sub {
200 my $file = "/tmp/config$$";
203 while ((my $option, my $value) = each(%$SETTINGS)) {
204 next if ($option eq 'include' || $option eq 'luns' || $option eq 'Path' || $option eq 'text' || $option eq 'used');
205 if ($option eq 'target') {
206 $config = "\n\nTarget " . $SETTINGS->{target
} . "\n" . $config;
208 $config .= "\t$option\t\t\t$value\n";
211 foreach my $lun (@{$SETTINGS->{luns
}}) {
213 while ((my $option, my $value) = each(%$lun)) {
214 next if ($option eq 'include' || $option eq 'lun' || $option eq 'Path');
215 if ($lun_opt eq '') {
216 $lun_opt = $option . '=' . $value;
218 $lun_opt .= ',' . $option . '=' . $value;
221 $config .= "\tLun $lun->{lun} Path=$lun->{Path},$lun_opt\n";
223 open(my $fh, '>', $file) or die "Could not open file '$file' $!";
229 my @params = ($CONFIG_FILE);
230 my $res = $execute_command->($scfg, 'scp', undef, $file, @params);
233 die $res->{msg
} unless $res->{result
};
236 my $get_target_tid = sub {
238 my $proc = '/proc/net/iet/volume';
241 my @params = ($proc);
242 my $res = $execute_command->($scfg, 'ssh', undef, 'cat', @params);
243 die $res->{msg
} unless $res->{result
};
244 my @cfg = split "\n", $res->{msg
};
247 if ($_ =~ /^\s*tid:(\d+)\s+name:([\w\-\:\.]+)\s*$/) {
248 if ($2 && $2 eq $scfg->{target
}) {
258 my $get_lu_name = sub {
262 if (! exists $SETTINGS->{used
}) {
263 for ($i = 0; $i < $MAX_LUNS; $i++) {
266 foreach my $lun (@{$SETTINGS->{luns
}}) {
267 $used->{$lun->{lun
}} = 1;
269 $SETTINGS->{used
} = $used;
272 $used = $SETTINGS->{used
};
273 for ($i = 0; $i < $MAX_LUNS; $i++) {
274 last unless $used->{$i};
276 $SETTINGS->{used
}->{$i} = 1;
281 my $init_lu_name = sub {
284 if (! exists($SETTINGS->{used
})) {
285 for (my $i = 0; $i < $MAX_LUNS; $i++) {
288 $SETTINGS->{used
} = $used;
290 foreach my $lun (@{$SETTINGS->{luns
}}) {
291 $SETTINGS->{used
}->{$lun->{lun
}} = 1;
295 my $free_lu_name = sub {
299 foreach my $lun (@{$SETTINGS->{luns
}}) {
300 if ($lun->{lun
} != $lu_name) {
305 $SETTINGS->{luns
} = $new;
306 $SETTINGS->{used
}->{$lu_name} = 0;
310 my ($scfg, $path) = @_;
312 die 'Maximum number of LUNs per target is 16384' if scalar @{$SETTINGS->{luns
}} >= $MAX_LUNS;
314 my $lun = $get_lu_name->();
321 push @{$SETTINGS->{luns
}}, $conf;
326 my $list_view = sub {
327 my ($scfg, $timeout, $method, @params) = @_;
330 my $object = $params[0];
331 foreach my $lun (@{$SETTINGS->{luns
}}) {
332 next unless $lun->{include
} == 1;
333 if ($lun->{Path
} =~ /^$object$/) {
334 return $lun->{lun
} if (defined($lun->{lun
}));
335 die "$lun->{Path}: Missing LUN";
343 my ($scfg, $timeout, $method, @params) = @_;
346 my $object = $params[0];
347 foreach my $lun (@{$SETTINGS->{luns
}}) {
348 next unless $lun->{include
} == 1;
349 if ($lun->{Path
} =~ /^$object$/) {
357 my $create_lun = sub {
358 my ($scfg, $timeout, $method, @params) = @_;
360 if ($list_lun->($scfg, $timeout, $method, @params)) {
361 die "$params[0]: LUN exists";
363 my $lun = $params[0];
364 $lun = $make_lun->($scfg, $lun);
365 my $tid = $get_target_tid->($scfg);
366 $update_config->($scfg);
368 my $path = "Path=$lun->{Path},Type=$lun->{Type}";
370 @params = ('--op', 'new', "--tid=$tid", "--lun=$lun->{lun}", '--params', $path);
371 my $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
373 $free_lu_name->($lun->{lun
});
374 $update_config->($scfg);
376 } unless $res->{result
};
381 my $delete_lun = sub {
382 my ($scfg, $timeout, $method, @params) = @_;
383 my $res = {msg
=> undef};
385 my $path = $params[0];
386 my $tid = $get_target_tid->($scfg);
388 foreach my $lun (@{$SETTINGS->{luns
}}) {
389 if ($lun->{Path
} eq $path) {
390 @params = ('--op', 'delete', "--tid=$tid", "--lun=$lun->{lun}");
391 $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
392 if ($res->{result
}) {
393 $free_lu_name->($lun->{lun
});
394 $update_config->($scfg);
405 my $import_lun = sub {
406 my ($scfg, $timeout, $method, @params) = @_;
408 return $create_lun->($scfg, $timeout, $method, @params);
411 my $modify_lun = sub {
412 my ($scfg, $timeout, $method, @params) = @_;
416 my $path = $params[1];
417 my $tid = $get_target_tid->($scfg);
419 foreach my $cfg (@{$SETTINGS->{luns
}}) {
420 if ($cfg->{Path
} eq $path) {
426 @params = ('--op', 'delete', "--tid=$tid", "--lun=$lun->{lun}");
427 $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
428 die $res->{msg
} unless $res->{result
};
430 $path = "Path=$lun->{Path},Type=$lun->{Type}";
431 @params = ('--op', 'new', "--tid=$tid", "--lun=$lun->{lun}", '--params', $path);
432 $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
433 die $res->{msg
} unless $res->{result
};
439 my ($scfg, $timeout, $method, @params) = @_;
444 my $get_lun_cmd_map = sub {
448 create_lu
=> { cmd
=> $create_lun },
449 delete_lu
=> { cmd
=> $delete_lun },
450 import_lu
=> { cmd
=> $import_lun },
451 modify_lu
=> { cmd
=> $modify_lun },
452 add_view
=> { cmd
=> $add_view },
453 list_view
=> { cmd
=> $list_view },
454 list_lu
=> { cmd
=> $list_lun },
457 die "unknown command '$method'" unless exists $cmdmap->{$method};
459 return $cmdmap->{$method};
462 sub run_lun_command
{
463 my ($scfg, $timeout, $method, @params) = @_;
465 $parser->($scfg) unless $SETTINGS;
466 my $cmdmap = $get_lun_cmd_map->($method);
467 my $msg = $cmdmap->{cmd
}->($scfg, $timeout, $method, @params);