]>
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>
19 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 $id_rsa_path = '/etc/pve/priv/zfs';
37 my $ietadm = '/usr/sbin/ietadm';
39 my $execute_command = sub {
40 my ($scfg, $exec, $timeout, $method, @params) = @_;
48 $timeout = 10 if !$timeout;
61 $target = 'root@[' . $scfg->{portal
} . ']';
62 $cmd = [@scp_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", '--', $method, "$target:$params[0]"];
64 $target = 'root@' . $scfg->{portal
};
65 $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, '--', $method, @params];
69 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
86 my $read_config = sub {
87 my ($scfg, $timeout) = @_;
93 $timeout = 10 if !$timeout;
105 $target = 'root@' . $scfg->{portal
};
107 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $luncmd, $CONFIG_FILE];
109 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
112 die $err if ($err !~ /No such file or directory/);
113 die "No configuration found. Install iet on $scfg->{portal}" if $msg eq '';
119 my $get_config = sub {
123 my $config = $read_config->($scfg, undef);
124 die "Missing config file" unless $config;
126 $OLD_CONFIG = $config;
137 my $config = $get_config->($scfg);
138 my @cfgfile = split "\n", $config;
143 if ($_ =~ /^\s*Target\s*([\w\-\:\.]+)\s*$/) {
144 if ($1 eq $scfg->{target
} && ! $cfg_target) {
146 die "$line: Parse error [$_]" if $SETTINGS;
147 $SETTINGS->{target
} = $1;
149 } elsif ($1 eq $scfg->{target
} && $cfg_target) {
150 die "$line: Parse error [$_]";
151 } elsif ($cfg_target) {
159 $SETTINGS->{text
} .= "$_\n";
160 next if ($_ =~ /^\s*#/ || ! $_);
162 if ($_ =~ /^(\w+)\s*#/) {
165 if ($option =~ /^\s*(\w+)\s+(\w+)\s*$/) {
167 die "$line: Parse error [$_]";
169 $SETTINGS->{$1} = $2;
170 } elsif ($option =~ /^\s*(\w+)\s+(\d+)\s+([\w\-\/=,]+)\s
*$/) {
171 die "$line: Parse error [$option]" unless ($1 eq 'Lun');
174 my @lun = split ',', $3;
175 die "$line: Parse error [$option]" unless (scalar(@lun) > 1);
177 my @lun_opt = split '=', $_;
178 die "$line: Parse error [$option]" unless (scalar(@lun_opt) == 2);
179 $conf->{$lun_opt[0]} = $lun_opt[1];
181 if ($conf->{Path
} && $conf->{Path
} =~ /^$base\/$scfg
->{pool
}\
/([\w\-]+)$/) {
182 $conf->{include
} = 1;
184 $conf->{include
} = 0;
187 push @{$SETTINGS->{luns
}}, $conf;
189 die "$line: Parse error [$option]";
196 $CONFIG =~ s/^\s+|\s+$|"\s*//g;
199 my $update_config = sub {
201 my $file = "/tmp/config$$";
204 while ((my $option, my $value) = each(%$SETTINGS)) {
205 next if ($option eq 'include' || $option eq 'luns' || $option eq 'Path' || $option eq 'text' || $option eq 'used');
206 if ($option eq 'target') {
207 $config = "\n\nTarget " . $SETTINGS->{target
} . "\n" . $config;
209 $config .= "\t$option\t\t\t$value\n";
212 foreach my $lun (@{$SETTINGS->{luns
}}) {
214 while ((my $option, my $value) = each(%$lun)) {
215 next if ($option eq 'include' || $option eq 'lun' || $option eq 'Path');
216 if ($lun_opt eq '') {
217 $lun_opt = $option . '=' . $value;
219 $lun_opt .= ',' . $option . '=' . $value;
222 $config .= "\tLun $lun->{lun} Path=$lun->{Path},$lun_opt\n";
224 open(my $fh, '>', $file) or die "Could not open file '$file' $!";
230 my @params = ($CONFIG_FILE);
231 my $res = $execute_command->($scfg, 'scp', undef, $file, @params);
234 die $res->{msg
} unless $res->{result
};
237 my $get_target_tid = sub {
239 my $proc = '/proc/net/iet/volume';
242 my @params = ($proc);
243 my $res = $execute_command->($scfg, 'ssh', undef, 'cat', @params);
244 die $res->{msg
} unless $res->{result
};
245 my @cfg = split "\n", $res->{msg
};
248 if ($_ =~ /^\s*tid:(\d+)\s+name:([\w\-\:\.]+)\s*$/) {
249 if ($2 && $2 eq $scfg->{target
}) {
259 my $get_lu_name = sub {
263 if (! exists $SETTINGS->{used
}) {
264 for ($i = 0; $i < $MAX_LUNS; $i++) {
267 foreach my $lun (@{$SETTINGS->{luns
}}) {
268 $used->{$lun->{lun
}} = 1;
270 $SETTINGS->{used
} = $used;
273 $used = $SETTINGS->{used
};
274 for ($i = 0; $i < $MAX_LUNS; $i++) {
275 last unless $used->{$i};
277 $SETTINGS->{used
}->{$i} = 1;
282 my $init_lu_name = sub {
285 if (! exists($SETTINGS->{used
})) {
286 for (my $i = 0; $i < $MAX_LUNS; $i++) {
289 $SETTINGS->{used
} = $used;
291 foreach my $lun (@{$SETTINGS->{luns
}}) {
292 $SETTINGS->{used
}->{$lun->{lun
}} = 1;
296 my $free_lu_name = sub {
300 foreach my $lun (@{$SETTINGS->{luns
}}) {
301 if ($lun->{lun
} != $lu_name) {
306 $SETTINGS->{luns
} = $new;
307 $SETTINGS->{used
}->{$lu_name} = 0;
311 my ($scfg, $path) = @_;
313 die 'Maximum number of LUNs per target is 16384' if scalar @{$SETTINGS->{luns
}} >= $MAX_LUNS;
315 my $lun = $get_lu_name->();
322 push @{$SETTINGS->{luns
}}, $conf;
327 my $list_view = sub {
328 my ($scfg, $timeout, $method, @params) = @_;
331 my $object = $params[0];
332 foreach my $lun (@{$SETTINGS->{luns
}}) {
333 next unless $lun->{include
} == 1;
334 if ($lun->{Path
} =~ /^$object$/) {
335 return $lun->{lun
} if (defined($lun->{lun
}));
336 die "$lun->{Path}: Missing LUN";
344 my ($scfg, $timeout, $method, @params) = @_;
347 my $object = $params[0];
348 foreach my $lun (@{$SETTINGS->{luns
}}) {
349 next unless $lun->{include
} == 1;
350 if ($lun->{Path
} =~ /^$object$/) {
358 my $create_lun = sub {
359 my ($scfg, $timeout, $method, @params) = @_;
361 if ($list_lun->($scfg, $timeout, $method, @params)) {
362 die "$params[0]: LUN exists";
364 my $lun = $params[0];
365 $lun = $make_lun->($scfg, $lun);
366 my $tid = $get_target_tid->($scfg);
367 $update_config->($scfg);
369 my $path = "Path=$lun->{Path},Type=$lun->{Type}";
371 @params = ('--op', 'new', "--tid=$tid", "--lun=$lun->{lun}", '--params', $path);
372 my $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
374 $free_lu_name->($lun->{lun
});
375 $update_config->($scfg);
377 } unless $res->{result
};
382 my $delete_lun = sub {
383 my ($scfg, $timeout, $method, @params) = @_;
384 my $res = {msg
=> undef};
386 my $path = $params[0];
387 my $tid = $get_target_tid->($scfg);
389 foreach my $lun (@{$SETTINGS->{luns
}}) {
390 if ($lun->{Path
} eq $path) {
391 @params = ('--op', 'delete', "--tid=$tid", "--lun=$lun->{lun}");
392 $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
393 if ($res->{result
}) {
394 $free_lu_name->($lun->{lun
});
395 $update_config->($scfg);
406 my $import_lun = sub {
407 my ($scfg, $timeout, $method, @params) = @_;
409 return $create_lun->($scfg, $timeout, $method, @params);
412 my $modify_lun = sub {
413 my ($scfg, $timeout, $method, @params) = @_;
417 my $path = $params[1];
418 my $tid = $get_target_tid->($scfg);
420 foreach my $cfg (@{$SETTINGS->{luns
}}) {
421 if ($cfg->{Path
} eq $path) {
427 @params = ('--op', 'delete', "--tid=$tid", "--lun=$lun->{lun}");
428 $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
429 die $res->{msg
} unless $res->{result
};
431 $path = "Path=$lun->{Path},Type=$lun->{Type}";
432 @params = ('--op', 'new', "--tid=$tid", "--lun=$lun->{lun}", '--params', $path);
433 $res = $execute_command->($scfg, 'ssh', $timeout, $ietadm, @params);
434 die $res->{msg
} unless $res->{result
};
440 my ($scfg, $timeout, $method, @params) = @_;
445 my $get_lun_cmd_map = sub {
449 create_lu
=> { cmd
=> $create_lun },
450 delete_lu
=> { cmd
=> $delete_lun },
451 import_lu
=> { cmd
=> $import_lun },
452 modify_lu
=> { cmd
=> $modify_lun },
453 add_view
=> { cmd
=> $add_view },
454 list_view
=> { cmd
=> $list_view },
455 list_lu
=> { cmd
=> $list_lun },
458 die "unknown command '$method'" unless exists $cmdmap->{$method};
460 return $cmdmap->{$method};
463 sub run_lun_command
{
464 my ($scfg, $timeout, $method, @params) = @_;
466 $parser->($scfg) unless $SETTINGS;
467 my $cmdmap = $get_lun_cmd_map->($method);
468 my $msg = $cmdmap->{cmd
}->($scfg, $timeout, $method, @params);