]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LunCmd/LIO.pm
a2050d4364391eb3c9cb4fbd242ee4dd4de857a6
1 package PVE
::Storage
::LunCmd
::LIO
;
3 # lightly based on code from Iet.pm
6 # -----------------------------------------------------------------
7 # Copyright (c) 2018 BestSolution.at EDV Systemhaus GmbH
10 # This software is released under the terms of the
12 # "GNU Affero General Public License"
14 # and may only be distributed and used under the terms of the
15 # mentioned license. You should have received a copy of the license
16 # along with this software product, if not you can download it from
17 # https://www.gnu.org/licenses/agpl-3.0.en.html
19 # Author: udo.rader@bestsolution.at
20 # -----------------------------------------------------------------
24 use PVE
::Tools
qw(run_command);
30 # config file location differs from distro to distro
32 '/etc/rtslib-fb-target/saveconfig.json', # Debian 9.x et al
33 '/etc/target/saveconfig.json' , # ArchLinux, CentOS
35 my $BACKSTORE = '/backstores/block';
38 my $SETTINGS_TIMESTAMP = 0;
39 my $SETTINGS_MAXAGE = 15; # in seconds
41 my @ssh_opts = ('-o', 'BatchMode=yes');
42 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
43 my $id_rsa_path = '/etc/pve/priv/zfs';
44 my $targetcli = '/usr/bin/targetcli';
46 my $execute_remote_command = sub {
47 my ($scfg, $timeout, $remote_command, @params) = @_;
55 $timeout = 10 if !$timeout;
67 $target = 'root@' . $scfg->{portal
};
68 $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, '--', $remote_command, @params];
71 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
88 # fetch targetcli configuration from the portal
89 my $read_config = sub {
90 my ($scfg, $timeout) = @_;
98 $timeout = 10 if !$timeout;
110 $target = 'root@' . $scfg->{portal
};
112 foreach my $oneFile (@CONFIG_FILES) {
113 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $luncmd, $oneFile];
115 run_command
($cmd, outfunc
=> $output, errfunc
=> $errfunc, timeout
=> $timeout);
118 die $err if ($err !~ /No such file or directory/);
120 return $msg if $msg ne '';
123 die "No configuration found. Install targetcli on $scfg->{portal}\n" if $msg eq '';
128 my $get_config = sub {
132 my $config = $read_config->($scfg, undef);
133 die "Missing config file" unless $config;
138 # fetches and parses targetcli config from the portal
141 my $tpg = $scfg->{lio_tpg
} || die "Target Portal Group not set, aborting!\n";
144 if ($tpg =~ /^tpg(\d+)$/) {
147 die "Target Portal Group has invalid value, must contain string 'tpg' and a suffix number, eg 'tpg17'\n";
152 my $config = $get_config->($scfg);
153 my $jsonconfig = JSON-
>new->utf8->decode($config);
156 foreach my $oneTarget (@{$jsonconfig->{targets
}}) {
157 # only interested in iSCSI targets
158 if ($oneTarget->{fabric
} eq 'iscsi' && $oneTarget->{wwn
} eq $scfg->{target
}) {
160 foreach my $oneTpg (@{$oneTarget->{tpgs
}}) {
161 if ($oneTpg->{tag
} == $tpg_tag) {
162 $SETTINGS->{target
} = $oneTpg;
170 # seriously unhappy if the target server lacks iSCSI target configuration ...
172 die "target portal group tpg$tpg_tag not found!\n";
176 # removes the given lu_name from the local list of luns
177 my $free_lu_name = sub {
181 foreach my $lun (@{$SETTINGS->{target
}->{luns
}}) {
182 if ($lun->{storage_object
} ne "$BACKSTORE/$lu_name") {
187 $SETTINGS->{target
}->{luns
} = $new;
190 # locally registers a new lun
191 my $register_lun = sub {
192 my ($scfg, $idx, $volname) = @_;
196 storage_object
=> "$BACKSTORE/$volname",
199 push @{$SETTINGS->{target
}->{luns
}}, $conf;
204 # extracts the ZFS volume name from a device path
205 my $extract_volname = sub {
206 my ($scfg, $lunpath) = @_;
210 if ($lunpath =~ /^$base\/$scfg
->{pool
}\
/([\w\-]+)$/) {
217 # retrieves the LUN index for a particular object
218 my $list_view = sub {
219 my ($scfg, $timeout, $method, @params) = @_;
222 my $object = $params[0];
223 my $volname = $extract_volname->($scfg, $params[0]);
225 foreach my $lun (@{$SETTINGS->{target
}->{luns
}}) {
226 if ($lun->{storage_object
} eq "$BACKSTORE/$volname") {
227 return $lun->{index};
234 # determines, if the given object exists on the portal
236 my ($scfg, $timeout, $method, @params) = @_;
239 my $object = $params[0];
240 my $volname = $extract_volname->($scfg, $params[0]);
242 foreach my $lun (@{$SETTINGS->{target
}->{luns
}}) {
243 if ($lun->{storage_object
} eq "$BACKSTORE/$volname") {
251 # adds a new LUN to the target
252 my $create_lun = sub {
253 my ($scfg, $timeout, $method, @params) = @_;
255 if ($list_lun->($scfg, $timeout, $method, @params)) {
256 die "$params[0]: LUN already exists!";
259 my $device = $params[0];
260 my $volname = $extract_volname->($scfg, $device);
261 my $tpg = $scfg->{lio_tpg
} || die "Target Portal Group not set, aborting!\n";
263 # step 1: create backstore for device
264 my @cliparams = ($BACKSTORE, 'create', "name=$volname", "dev=$device" );
265 my $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
266 die $res->{msg
} if !$res->{result
};
268 # step 2: register lun with target
269 # targetcli /iscsi/iqn.2018-04.at.bestsolution.somehost:target/tpg1/luns/ create /backstores/block/foobar
270 @cliparams = ("/iscsi/$scfg->{target}/$tpg/luns/", 'create', "$BACKSTORE/$volname" );
271 $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
272 die $res->{msg
} if !$res->{result
};
274 # targetcli responds with "Created LUN 99"
275 # not calculating the index ourselves, because the index at the portal might have
276 # changed without our knowledge, so relying on the number that targetcli returns
278 if ($res->{msg
} =~ /LUN (\d+)/) {
281 die "unable to determine new LUN index: $res->{msg}";
284 $register_lun->($scfg, $lun_idx, $volname);
286 # step 3: unfortunately, targetcli doesn't always save changes, no matter
287 # if auto_save_on_exit is true or not. So saving to be safe ...
288 $execute_remote_command->($scfg, $timeout, $targetcli, 'saveconfig');
293 my $delete_lun = sub {
294 my ($scfg, $timeout, $method, @params) = @_;
295 my $res = {msg
=> undef};
297 my $tpg = $scfg->{lio_tpg
} || die "Target Portal Group not set, aborting!\n";
299 my $path = $params[0];
300 my $volname = $extract_volname->($scfg, $params[0]);
302 foreach my $lun (@{$SETTINGS->{target
}->{luns
}}) {
303 if ($lun->{storage_object
} eq "$BACKSTORE/$volname") {
304 # step 1: delete the lun
305 my @cliparams = ("/iscsi/$scfg->{target}/$tpg/luns/", 'delete', "lun$lun->{index}" );
306 my $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
309 } unless $res->{result
};
311 # step 2: delete the backstore
312 @cliparams = ($BACKSTORE, 'delete', $volname);
313 $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
316 } unless $res->{result
};
318 # step 3: save to be safe ...
319 $execute_remote_command->($scfg, $timeout, $targetcli, 'saveconfig');
321 # update interal cache
322 $free_lu_name->($volname);
331 my $import_lun = sub {
332 my ($scfg, $timeout, $method, @params) = @_;
334 return $create_lun->($scfg, $timeout, $method, @params);
337 # needed for example when the underlying ZFS volume has been resized
338 my $modify_lun = sub {
339 my ($scfg, $timeout, $method, @params) = @_;
342 $msg = $delete_lun->($scfg, $timeout, $method, @params);
344 $msg = $create_lun->($scfg, $timeout, $method, @params);
351 my ($scfg, $timeout, $method, @params) = @_;
357 create_lu
=> $create_lun,
358 delete_lu
=> $delete_lun,
359 import_lu
=> $import_lun,
360 modify_lu
=> $modify_lun,
361 add_view
=> $add_view,
362 list_view
=> $list_view,
363 list_lu
=> $list_lun,
366 sub run_lun_command
{
367 my ($scfg, $timeout, $method, @params) = @_;
369 # fetch configuration from target if we haven't yet
370 # or if our configuration is stale
371 my $timediff = time - $SETTINGS_TIMESTAMP;
372 if ( ! $SETTINGS || $timediff > $SETTINGS_MAXAGE ) {
373 $SETTINGS_TIMESTAMP = time;
377 die "unknown command '$method'" unless exists $lun_cmd_map{$method};
378 my $msg = $lun_cmd_map{$method}->($scfg, $timeout, $method, @params);