]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPlugin.pm
iscis: add iscsi_session helper
[pve-storage.git] / PVE / Storage / ZFSPlugin.pm
1 package PVE::Storage::ZFSPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use POSIX;
7 use PVE::Tools qw(run_command);
8 use PVE::Storage::ZFSPoolPlugin;
9 use PVE::RPCEnvironment;
10
11 use base qw(PVE::Storage::ZFSPoolPlugin);
12 use PVE::Storage::LunCmd::Comstar;
13 use PVE::Storage::LunCmd::Istgt;
14 use PVE::Storage::LunCmd::Iet;
15 use PVE::Storage::LunCmd::LIO;
16
17
18 my @ssh_opts = ('-o', 'BatchMode=yes');
19 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
20 my $id_rsa_path = '/etc/pve/priv/zfs';
21
22 my $lun_cmds = {
23 create_lu => 1,
24 delete_lu => 1,
25 import_lu => 1,
26 modify_lu => 1,
27 add_view => 1,
28 list_view => 1,
29 list_lu => 1,
30 };
31
32 my $zfs_unknown_scsi_provider = sub {
33 my ($provider) = @_;
34
35 die "$provider: unknown iscsi provider. Available [comstar, istgt, iet, LIO]";
36 };
37
38 my $zfs_get_base = sub {
39 my ($scfg) = @_;
40
41 if ($scfg->{iscsiprovider} eq 'comstar') {
42 return PVE::Storage::LunCmd::Comstar::get_base;
43 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
44 return PVE::Storage::LunCmd::Istgt::get_base;
45 } elsif ($scfg->{iscsiprovider} eq 'iet') {
46 return PVE::Storage::LunCmd::Iet::get_base;
47 } elsif ($scfg->{iscsiprovider} eq 'LIO') {
48 return PVE::Storage::LunCmd::LIO::get_base;
49 } else {
50 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
51 }
52 };
53
54 sub zfs_request {
55 my ($class, $scfg, $timeout, $method, @params) = @_;
56
57 $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 10
58 if !$timeout;
59
60 my $msg = '';
61
62 if ($lun_cmds->{$method}) {
63 if ($scfg->{iscsiprovider} eq 'comstar') {
64 $msg = PVE::Storage::LunCmd::Comstar::run_lun_command($scfg, $timeout, $method, @params);
65 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
66 $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params);
67 } elsif ($scfg->{iscsiprovider} eq 'iet') {
68 $msg = PVE::Storage::LunCmd::Iet::run_lun_command($scfg, $timeout, $method, @params);
69 } elsif ($scfg->{iscsiprovider} eq 'LIO') {
70 $msg = PVE::Storage::LunCmd::LIO::run_lun_command($scfg, $timeout, $method, @params);
71 } else {
72 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
73 }
74 } else {
75
76 my $target = 'root@' . $scfg->{portal};
77
78 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target];
79
80 if ($method eq 'zpool_list') {
81 push @$cmd, 'zpool', 'list';
82 } else {
83 push @$cmd, 'zfs', $method;
84 }
85
86 push @$cmd, @params;
87
88 my $output = sub {
89 my $line = shift;
90 $msg .= "$line\n";
91 };
92
93 run_command($cmd, outfunc => $output, timeout => $timeout);
94 }
95
96 return $msg;
97 }
98
99 sub zfs_get_lu_name {
100 my ($class, $scfg, $zvol) = @_;
101
102 my $base = $zfs_get_base->($scfg);
103
104 $zvol = ($class->parse_volname($zvol))[1];
105
106 my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol" : "$base/$scfg->{pool}/$zvol";
107
108 my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
109
110 return $lu_name if $lu_name;
111
112 die "Could not find lu_name for zvol $zvol";
113 }
114
115 sub zfs_add_lun_mapping_entry {
116 my ($class, $scfg, $zvol, $guid) = @_;
117
118 if (!defined($guid)) {
119 $guid = $class->zfs_get_lu_name($scfg, $zvol);
120 }
121
122 $class->zfs_request($scfg, undef, 'add_view', $guid);
123 }
124
125 sub zfs_delete_lu {
126 my ($class, $scfg, $zvol) = @_;
127
128 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
129
130 $class->zfs_request($scfg, undef, 'delete_lu', $guid);
131 }
132
133 sub zfs_create_lu {
134 my ($class, $scfg, $zvol) = @_;
135
136 my $base = $zfs_get_base->($scfg);
137 my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
138
139 return $guid;
140 }
141
142 sub zfs_import_lu {
143 my ($class, $scfg, $zvol) = @_;
144
145 my $base = $zfs_get_base->($scfg);
146 $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
147 }
148
149 sub zfs_resize_lu {
150 my ($class, $scfg, $zvol, $size) = @_;
151
152 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
153
154 $class->zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid);
155 }
156
157 sub zfs_get_lun_number {
158 my ($class, $scfg, $guid) = @_;
159
160 die "could not find lun_number for guid $guid" if !$guid;
161
162 return $class->zfs_request($scfg, undef, 'list_view', $guid);
163 }
164
165 # Configuration
166
167 sub type {
168 return 'zfs';
169 }
170
171 sub plugindata {
172 return {
173 content => [ {images => 1}, { images => 1 }],
174 };
175 }
176
177 sub properties {
178 return {
179 iscsiprovider => {
180 description => "iscsi provider",
181 type => 'string',
182 },
183 # this will disable write caching on comstar and istgt.
184 # it is not implemented for iet. iet blockio always operates with
185 # writethrough caching when not in readonly mode
186 nowritecache => {
187 description => "disable write caching on the target",
188 type => 'boolean',
189 },
190 comstar_tg => {
191 description => "target group for comstar views",
192 type => 'string',
193 },
194 comstar_hg => {
195 description => "host group for comstar views",
196 type => 'string',
197 },
198 lio_tpg => {
199 description => "target portal group for Linux LIO targets",
200 type => 'string',
201 },
202 };
203 }
204
205 sub options {
206 return {
207 nodes => { optional => 1 },
208 disable => { optional => 1 },
209 portal => { fixed => 1 },
210 target => { fixed => 1 },
211 pool => { fixed => 1 },
212 blocksize => { fixed => 1 },
213 iscsiprovider => { fixed => 1 },
214 nowritecache => { optional => 1 },
215 sparse => { optional => 1 },
216 comstar_hg => { optional => 1 },
217 comstar_tg => { optional => 1 },
218 lio_tpg => { optional => 1 },
219 content => { optional => 1 },
220 bwlimit => { optional => 1 },
221 };
222 }
223
224 # Storage implementation
225
226 sub path {
227 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
228
229 die "direct access to snapshots not implemented"
230 if defined($snapname);
231
232 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
233
234 my $target = $scfg->{target};
235 my $portal = $scfg->{portal};
236
237 my $guid = $class->zfs_get_lu_name($scfg, $name);
238 my $lun = $class->zfs_get_lun_number($scfg, $guid);
239
240 my $path = "iscsi://$portal/$target/$lun";
241
242 return ($path, $vmid, $vtype);
243 }
244
245 sub create_base {
246 my ($class, $storeid, $scfg, $volname) = @_;
247
248 my $snap = '__base__';
249
250 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
251 $class->parse_volname($volname);
252
253 die "create_base not possible with base image\n" if $isBase;
254
255 my $newname = $name;
256 $newname =~ s/^vm-/base-/;
257
258 my $newvolname = $basename ? "$basename/$newname" : "$newname";
259
260 $class->zfs_delete_lu($scfg, $name);
261 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
262
263 my $guid = $class->zfs_create_lu($scfg, $newname);
264 $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
265
266 my $running = undef; #fixme : is create_base always offline ?
267
268 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
269
270 return $newvolname;
271 }
272
273 sub clone_image {
274 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
275
276 my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
277
278 # get ZFS dataset name from PVE volname
279 my (undef, $clonedname) = $class->parse_volname($name);
280
281 my $guid = $class->zfs_create_lu($scfg, $clonedname);
282 $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid);
283
284 return $name;
285 }
286
287 sub alloc_image {
288 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
289
290 die "unsupported format '$fmt'" if $fmt ne 'raw';
291
292 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
293 if $name && $name !~ m/^vm-$vmid-/;
294
295 my $volname = $name;
296
297 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname;
298
299 $class->zfs_create_zvol($scfg, $volname, $size);
300
301 my $guid = $class->zfs_create_lu($scfg, $volname);
302 $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
303
304 return $volname;
305 }
306
307 sub free_image {
308 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
309
310 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
311
312 $class->zfs_delete_lu($scfg, $name);
313
314 eval { $class->zfs_delete_zvol($scfg, $name); };
315 if (my $err = $@) {
316 my $guid = $class->zfs_create_lu($scfg, $name);
317 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
318 die $err;
319 }
320
321 return undef;
322 }
323
324 sub volume_resize {
325 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
326
327 $volname = ($class->parse_volname($volname))[1];
328
329 my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
330
331 $class->zfs_resize_lu($scfg, $volname, $new_size);
332
333 return $new_size;
334 }
335
336 sub volume_snapshot_delete {
337 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
338
339 $volname = ($class->parse_volname($volname))[1];
340
341 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
342 }
343
344 sub volume_snapshot_rollback {
345 my ($class, $scfg, $storeid, $volname, $snap) = @_;
346
347 $volname = ($class->parse_volname($volname))[1];
348
349 $class->zfs_delete_lu($scfg, $volname);
350
351 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
352
353 $class->zfs_import_lu($scfg, $volname);
354
355 $class->zfs_add_lun_mapping_entry($scfg, $volname);
356 }
357
358 sub storage_can_replicate {
359 my ($class, $scfg, $storeid, $format) = @_;
360
361 return 0;
362 }
363
364 sub volume_has_feature {
365 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
366
367 my $features = {
368 snapshot => { current => 1, snap => 1},
369 clone => { base => 1},
370 template => { current => 1},
371 copy => { base => 1, current => 1},
372 };
373
374 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
375 $class->parse_volname($volname);
376
377 my $key = undef;
378
379 if ($snapname) {
380 $key = 'snap';
381 } else {
382 $key = $isBase ? 'base' : 'current';
383 }
384
385 return 1 if $features->{$feature}->{$key};
386
387 return undef;
388 }
389
390 sub volume_snapshot_list {
391 my ($class, $scfg, $storeid, $volname) = @_;
392 # return an empty array if dataset does not exist.
393 die "Volume_snapshot_list is not implemented for ZFS over iSCSI.\n";
394 }
395
396 sub activate_storage {
397 my ($class, $storeid, $scfg, $cache) = @_;
398
399 return 1;
400 }
401
402 sub deactivate_storage {
403 my ($class, $storeid, $scfg, $cache) = @_;
404
405 return 1;
406 }
407
408 sub activate_volume {
409 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
410
411 die "unable to activate snapshot from remote zfs storage" if $snapname;
412
413 return 1;
414 }
415
416 sub deactivate_volume {
417 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
418
419 die "unable to deactivate snapshot from remote zfs storage" if $snapname;
420
421 return 1;
422 }
423
424 1;