]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPlugin.pm
Linux LIO/targetcli support
[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 my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol" : "$base/$scfg->{pool}/$zvol";
105
106 my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
107
108 return $lu_name if $lu_name;
109
110 die "Could not find lu_name for zvol $zvol";
111 }
112
113 sub zfs_add_lun_mapping_entry {
114 my ($class, $scfg, $zvol, $guid) = @_;
115
116 if (!defined($guid)) {
117 $guid = $class->zfs_get_lu_name($scfg, $zvol);
118 }
119
120 $class->zfs_request($scfg, undef, 'add_view', $guid);
121 }
122
123 sub zfs_delete_lu {
124 my ($class, $scfg, $zvol) = @_;
125
126 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
127
128 $class->zfs_request($scfg, undef, 'delete_lu', $guid);
129 }
130
131 sub zfs_create_lu {
132 my ($class, $scfg, $zvol) = @_;
133
134 my $base = $zfs_get_base->($scfg);
135 my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
136
137 return $guid;
138 }
139
140 sub zfs_import_lu {
141 my ($class, $scfg, $zvol) = @_;
142
143 my $base = $zfs_get_base->($scfg);
144 $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
145 }
146
147 sub zfs_resize_lu {
148 my ($class, $scfg, $zvol, $size) = @_;
149
150 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
151
152 $class->zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid);
153 }
154
155 sub zfs_get_lun_number {
156 my ($class, $scfg, $guid) = @_;
157
158 die "could not find lun_number for guid $guid" if !$guid;
159
160 return $class->zfs_request($scfg, undef, 'list_view', $guid);
161 }
162
163 # Configuration
164
165 sub type {
166 return 'zfs';
167 }
168
169 sub plugindata {
170 return {
171 content => [ {images => 1}, { images => 1 }],
172 };
173 }
174
175 sub properties {
176 return {
177 iscsiprovider => {
178 description => "iscsi provider",
179 type => 'string',
180 },
181 # this will disable write caching on comstar and istgt.
182 # it is not implemented for iet. iet blockio always operates with
183 # writethrough caching when not in readonly mode
184 nowritecache => {
185 description => "disable write caching on the target",
186 type => 'boolean',
187 },
188 comstar_tg => {
189 description => "target group for comstar views",
190 type => 'string',
191 },
192 comstar_hg => {
193 description => "host group for comstar views",
194 type => 'string',
195 },
196 lio_tpg => {
197 description => "target portal group for Linux LIO targets",
198 type => 'string',
199 },
200 };
201 }
202
203 sub options {
204 return {
205 nodes => { optional => 1 },
206 disable => { optional => 1 },
207 portal => { fixed => 1 },
208 target => { fixed => 1 },
209 pool => { fixed => 1 },
210 blocksize => { fixed => 1 },
211 iscsiprovider => { fixed => 1 },
212 nowritecache => { optional => 1 },
213 sparse => { optional => 1 },
214 comstar_hg => { optional => 1 },
215 comstar_tg => { optional => 1 },
216 lio_tpg => { optional => 1 },
217 content => { optional => 1 },
218 bwlimit => { optional => 1 },
219 };
220 }
221
222 # Storage implementation
223
224 sub path {
225 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
226
227 die "direct access to snapshots not implemented"
228 if defined($snapname);
229
230 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
231
232 my $target = $scfg->{target};
233 my $portal = $scfg->{portal};
234
235 my $guid = $class->zfs_get_lu_name($scfg, $name);
236 my $lun = $class->zfs_get_lun_number($scfg, $guid);
237
238 my $path = "iscsi://$portal/$target/$lun";
239
240 return ($path, $vmid, $vtype);
241 }
242
243 sub create_base {
244 my ($class, $storeid, $scfg, $volname) = @_;
245
246 my $snap = '__base__';
247
248 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
249 $class->parse_volname($volname);
250
251 die "create_base not possible with base image\n" if $isBase;
252
253 my $newname = $name;
254 $newname =~ s/^vm-/base-/;
255
256 my $newvolname = $basename ? "$basename/$newname" : "$newname";
257
258 $class->zfs_delete_lu($scfg, $name);
259 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
260
261 my $guid = $class->zfs_create_lu($scfg, $newname);
262 $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
263
264 my $running = undef; #fixme : is create_base always offline ?
265
266 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
267
268 return $newvolname;
269 }
270
271 sub clone_image {
272 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
273
274 my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
275
276 # get ZFS dataset name from PVE volname
277 my (undef, $clonedname) = $class->parse_volname($name);
278
279 my $guid = $class->zfs_create_lu($scfg, $clonedname);
280 $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid);
281
282 return $name;
283 }
284
285 sub alloc_image {
286 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
287
288 die "unsupported format '$fmt'" if $fmt ne 'raw';
289
290 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
291 if $name && $name !~ m/^vm-$vmid-/;
292
293 my $volname = $name;
294
295 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname;
296
297 $class->zfs_create_zvol($scfg, $volname, $size);
298
299 my $guid = $class->zfs_create_lu($scfg, $volname);
300 $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
301
302 return $volname;
303 }
304
305 sub free_image {
306 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
307
308 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
309
310 $class->zfs_delete_lu($scfg, $name);
311
312 eval { $class->zfs_delete_zvol($scfg, $name); };
313 if (my $err = $@) {
314 my $guid = $class->zfs_create_lu($scfg, $name);
315 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
316 die $err;
317 }
318
319 return undef;
320 }
321
322 sub volume_resize {
323 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
324
325 my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
326
327 $class->zfs_resize_lu($scfg, $volname, $new_size);
328
329 return $new_size;
330 }
331
332 sub volume_snapshot_delete {
333 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
334
335 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
336 }
337
338 sub volume_snapshot_rollback {
339 my ($class, $scfg, $storeid, $volname, $snap) = @_;
340
341 $class->zfs_delete_lu($scfg, $volname);
342
343 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
344
345 $class->zfs_import_lu($scfg, $volname);
346
347 $class->zfs_add_lun_mapping_entry($scfg, $volname);
348 }
349
350 sub storage_can_replicate {
351 my ($class, $scfg, $storeid, $format) = @_;
352
353 return 0;
354 }
355
356 sub volume_has_feature {
357 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
358
359 my $features = {
360 snapshot => { current => 1, snap => 1},
361 clone => { base => 1},
362 template => { current => 1},
363 copy => { base => 1, current => 1},
364 };
365
366 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
367 $class->parse_volname($volname);
368
369 my $key = undef;
370
371 if ($snapname) {
372 $key = 'snap';
373 } else {
374 $key = $isBase ? 'base' : 'current';
375 }
376
377 return 1 if $features->{$feature}->{$key};
378
379 return undef;
380 }
381
382 sub volume_snapshot_list {
383 my ($class, $scfg, $storeid, $volname) = @_;
384 # return an empty array if dataset does not exist.
385 die "Volume_snapshot_list is not implemented for ZFS over iSCSI.\n";
386 }
387
388 sub activate_storage {
389 my ($class, $storeid, $scfg, $cache) = @_;
390
391 return 1;
392 }
393
394 sub deactivate_storage {
395 my ($class, $storeid, $scfg, $cache) = @_;
396
397 return 1;
398 }
399
400 sub activate_volume {
401 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
402
403 die "unable to activate snapshot from remote zfs storage" if $snapname;
404
405 return 1;
406 }
407
408 sub deactivate_volume {
409 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
410
411 die "unable to deactivate snapshot from remote zfs storage" if $snapname;
412
413 return 1;
414 }
415
416 1;