]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LunCmd/LIO.pm
lio: list_lun: return early if volname cannot be parsed
[pve-storage.git] / PVE / Storage / LunCmd / LIO.pm
1 package PVE::Storage::LunCmd::LIO;
2
3 # lightly based on code from Iet.pm
4 #
5 # additional changes:
6 # -----------------------------------------------------------------
7 # Copyright (c) 2018 BestSolution.at EDV Systemhaus GmbH
8 # All Rights Reserved.
9 #
10 # This software is released under the terms of the
11 #
12 # "GNU Affero General Public License"
13 #
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
18 #
19 # Author: udo.rader@bestsolution.at
20 # -----------------------------------------------------------------
21
22 use strict;
23 use warnings;
24 use PVE::Tools qw(run_command);
25 use JSON;
26
27 sub get_base;
28
29 # targetcli constants
30 # config file location differs from distro to distro
31 my @CONFIG_FILES = (
32 '/etc/rtslib-fb-target/saveconfig.json', # Debian 9.x et al
33 '/etc/target/saveconfig.json' , # ArchLinux, CentOS
34 );
35 my $BACKSTORE = '/backstores/block';
36
37 my $SETTINGS = undef;
38 my $SETTINGS_TIMESTAMP = 0;
39 my $SETTINGS_MAXAGE = 15; # in seconds
40
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';
45
46 my $execute_remote_command = sub {
47 my ($scfg, $timeout, $remote_command, @params) = @_;
48
49 my $msg = '';
50 my $err = undef;
51 my $target;
52 my $cmd;
53 my $res = ();
54
55 $timeout = 10 if !$timeout;
56
57 my $output = sub { $msg .= "$_[0]\n" };
58 my $errfunc = sub { $err .= "$_[0]\n" };
59
60 $target = 'root@' . $scfg->{portal};
61 $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, '--', $remote_command, @params];
62
63 eval {
64 run_command($cmd, outfunc => $output, errfunc => $errfunc, timeout => $timeout);
65 };
66 if ($@) {
67 $res = {
68 result => 0,
69 msg => $err,
70 }
71 } else {
72 $res = {
73 result => 1,
74 msg => $msg,
75 }
76 }
77
78 return $res;
79 };
80
81 # fetch targetcli configuration from the portal
82 my $read_config = sub {
83 my ($scfg, $timeout) = @_;
84
85 my $msg = '';
86 my $err = undef;
87 my $luncmd = 'cat';
88 my $target;
89 my $retry = 1;
90
91 $timeout = 10 if !$timeout;
92
93 my $output = sub { $msg .= "$_[0]\n" };
94 my $errfunc = sub { $err .= "$_[0]\n" };
95
96 $target = 'root@' . $scfg->{portal};
97
98 foreach my $oneFile (@CONFIG_FILES) {
99 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $luncmd, $oneFile];
100 eval {
101 run_command($cmd, outfunc => $output, errfunc => $errfunc, timeout => $timeout);
102 };
103 if ($@) {
104 die $err if ($err !~ /No such file or directory/);
105 }
106 return $msg if $msg ne '';
107 }
108
109 die "No configuration found. Install targetcli on $scfg->{portal}\n" if $msg eq '';
110
111 return $msg;
112 };
113
114 my $get_config = sub {
115 my ($scfg) = @_;
116 my @conf = undef;
117
118 my $config = $read_config->($scfg, undef);
119 die "Missing config file" unless $config;
120
121 return $config;
122 };
123
124 # fetches and parses targetcli config from the portal
125 my $parser = sub {
126 my ($scfg) = @_;
127 my $tpg = $scfg->{lio_tpg} || die "Target Portal Group not set, aborting!\n";
128 my $tpg_tag;
129
130 if ($tpg =~ /^tpg(\d+)$/) {
131 $tpg_tag = $1;
132 } else {
133 die "Target Portal Group has invalid value, must contain string 'tpg' and a suffix number, eg 'tpg17'\n";
134 }
135
136 my $base = get_base;
137
138 my $config = $get_config->($scfg);
139 my $jsonconfig = JSON->new->utf8->decode($config);
140
141 my $haveTarget = 0;
142 foreach my $target (@{$jsonconfig->{targets}}) {
143 # only interested in iSCSI targets
144 next if !($target->{fabric} eq 'iscsi' && $target->{wwn} eq $scfg->{target});
145 # find correct TPG
146 foreach my $tpg (@{$target->{tpgs}}) {
147 if ($tpg->{tag} == $tpg_tag) {
148 $SETTINGS->{target} = $tpg;
149 $haveTarget = 1;
150 last;
151 }
152 }
153 }
154
155 # seriously unhappy if the target server lacks iSCSI target configuration ...
156 if (!$haveTarget) {
157 die "target portal group tpg$tpg_tag not found!\n";
158 }
159 };
160
161 # removes the given lu_name from the local list of luns
162 my $free_lu_name = sub {
163 my ($lu_name) = @_;
164
165 my $new = [];
166 foreach my $lun (@{$SETTINGS->{target}->{luns}}) {
167 if ($lun->{storage_object} ne "$BACKSTORE/$lu_name") {
168 push @$new, $lun;
169 }
170 }
171
172 $SETTINGS->{target}->{luns} = $new;
173 };
174
175 # locally registers a new lun
176 my $register_lun = sub {
177 my ($scfg, $idx, $volname) = @_;
178
179 my $conf = {
180 index => $idx,
181 storage_object => "$BACKSTORE/$volname",
182 is_new => 1,
183 };
184 push @{$SETTINGS->{target}->{luns}}, $conf;
185
186 return $conf;
187 };
188
189 # extracts the ZFS volume name from a device path
190 my $extract_volname = sub {
191 my ($scfg, $lunpath) = @_;
192 my $volname = undef;
193
194 my $base = get_base;
195 if ($lunpath =~ /^$base\/$scfg->{pool}\/([\w\-]+)$/) {
196 $volname = $1;
197 }
198
199 return $volname;
200 };
201
202 # retrieves the LUN index for a particular object
203 my $list_view = sub {
204 my ($scfg, $timeout, $method, @params) = @_;
205 my $lun = undef;
206
207 my $object = $params[0];
208 my $volname = $extract_volname->($scfg, $object);
209
210 return undef if !defined($volname); # nothing to search for..
211
212 foreach my $lun (@{$SETTINGS->{target}->{luns}}) {
213 if ($lun->{storage_object} eq "$BACKSTORE/$volname") {
214 return $lun->{index};
215 }
216 }
217
218 return $lun;
219 };
220
221 # determines, if the given object exists on the portal
222 my $list_lun = sub {
223 my ($scfg, $timeout, $method, @params) = @_;
224 my $name = undef;
225
226 my $object = $params[0];
227 my $volname = $extract_volname->($scfg, $params[0]);
228
229 foreach my $lun (@{$SETTINGS->{target}->{luns}}) {
230 if ($lun->{storage_object} eq "$BACKSTORE/$volname") {
231 return $object;
232 }
233 }
234
235 return $name;
236 };
237
238 # adds a new LUN to the target
239 my $create_lun = sub {
240 my ($scfg, $timeout, $method, @params) = @_;
241
242 if ($list_lun->($scfg, $timeout, $method, @params)) {
243 die "$params[0]: LUN already exists!";
244 }
245
246 my $device = $params[0];
247 my $volname = $extract_volname->($scfg, $device);
248 my $tpg = $scfg->{lio_tpg} || die "Target Portal Group not set, aborting!\n";
249
250 # step 1: create backstore for device
251 my @cliparams = ($BACKSTORE, 'create', "name=$volname", "dev=$device" );
252 my $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
253 die $res->{msg} if !$res->{result};
254
255 # step 2: enable unmap support on the backstore
256 @cliparams = ($BACKSTORE . '/' . $volname, 'set', 'attribute', 'emulate_tpu=1' );
257 $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
258 die $res->{msg} if !$res->{result};
259
260 # step 3: register lun with target
261 # targetcli /iscsi/iqn.2018-04.at.bestsolution.somehost:target/tpg1/luns/ create /backstores/block/foobar
262 @cliparams = ("/iscsi/$scfg->{target}/$tpg/luns/", 'create', "$BACKSTORE/$volname" );
263 $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
264 die $res->{msg} if !$res->{result};
265
266 # targetcli responds with "Created LUN 99"
267 # not calculating the index ourselves, because the index at the portal might have
268 # changed without our knowledge, so relying on the number that targetcli returns
269 my $lun_idx;
270 if ($res->{msg} =~ /LUN (\d+)/) {
271 $lun_idx = $1;
272 } else {
273 die "unable to determine new LUN index: $res->{msg}";
274 }
275
276 $register_lun->($scfg, $lun_idx, $volname);
277
278 # step 3: unfortunately, targetcli doesn't always save changes, no matter
279 # if auto_save_on_exit is true or not. So saving to be safe ...
280 $execute_remote_command->($scfg, $timeout, $targetcli, 'saveconfig');
281
282 return $res->{msg};
283 };
284
285 my $delete_lun = sub {
286 my ($scfg, $timeout, $method, @params) = @_;
287 my $res = {msg => undef};
288
289 my $tpg = $scfg->{lio_tpg} || die "Target Portal Group not set, aborting!\n";
290
291 my $path = $params[0];
292 my $volname = $extract_volname->($scfg, $params[0]);
293
294 foreach my $lun (@{$SETTINGS->{target}->{luns}}) {
295 next if $lun->{storage_object} ne "$BACKSTORE/$volname";
296
297 # step 1: delete the lun
298 my @cliparams = ("/iscsi/$scfg->{target}/$tpg/luns/", 'delete', "lun$lun->{index}" );
299 my $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
300 do {
301 die $res->{msg};
302 } unless $res->{result};
303
304 # step 2: delete the backstore
305 @cliparams = ($BACKSTORE, 'delete', $volname);
306 $res = $execute_remote_command->($scfg, $timeout, $targetcli, @cliparams);
307 do {
308 die $res->{msg};
309 } unless $res->{result};
310
311 # step 3: save to be safe ...
312 $execute_remote_command->($scfg, $timeout, $targetcli, 'saveconfig');
313
314 # update interal cache
315 $free_lu_name->($volname);
316
317 last;
318 }
319
320 return $res->{msg};
321 };
322
323 my $import_lun = sub {
324 my ($scfg, $timeout, $method, @params) = @_;
325
326 return $create_lun->($scfg, $timeout, $method, @params);
327 };
328
329 # needed for example when the underlying ZFS volume has been resized
330 my $modify_lun = sub {
331 my ($scfg, $timeout, $method, @params) = @_;
332 # Nothing to do on volume modification for LIO
333 return undef;
334 };
335
336 my $add_view = sub {
337 my ($scfg, $timeout, $method, @params) = @_;
338
339 return '';
340 };
341
342 my %lun_cmd_map = (
343 create_lu => $create_lun,
344 delete_lu => $delete_lun,
345 import_lu => $import_lun,
346 modify_lu => $modify_lun,
347 add_view => $add_view,
348 list_view => $list_view,
349 list_lu => $list_lun,
350 );
351
352 sub run_lun_command {
353 my ($scfg, $timeout, $method, @params) = @_;
354
355 # fetch configuration from target if we haven't yet or if it is stale
356 my $timediff = time - $SETTINGS_TIMESTAMP;
357 if (!$SETTINGS || $timediff > $SETTINGS_MAXAGE) {
358 $SETTINGS_TIMESTAMP = time;
359 $parser->($scfg);
360 }
361
362 die "unknown command '$method'" unless exists $lun_cmd_map{$method};
363 my $msg = $lun_cmd_map{$method}->($scfg, $timeout, $method, @params);
364
365 return $msg;
366 }
367
368 sub get_base {
369 return '/dev';
370 }
371
372 1;