]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPlugin.pm
Added "nowritecache" option to ZFS storage plugin. Turns off write caching on Comstar...
[pve-storage.git] / PVE / Storage / ZFSPlugin.pm
CommitLineData
4f914e6e
MR
1package PVE::Storage::ZFSPlugin;
2
3use strict;
4use warnings;
5use IO::File;
6use POSIX;
5332e6c9 7use PVE::Tools qw(run_command);
4f914e6e 8use PVE::Storage::Plugin;
4f914e6e
MR
9
10use base qw(PVE::Storage::Plugin);
a7d56be6
MR
11use PVE::Storage::LunCmd::Comstar;
12use PVE::Storage::LunCmd::Istgt;
78a64432 13use PVE::Storage::LunCmd::Iet;
4f914e6e
MR
14
15my @ssh_opts = ('-o', 'BatchMode=yes');
16my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
3b219e80 17my $id_rsa_path = '/etc/pve/priv/zfs';
4f914e6e 18
a7d56be6 19my $lun_cmds = {
3b219e80
MR
20 create_lu => 1,
21 delete_lu => 1,
22 import_lu => 1,
23 modify_lu => 1,
24 add_view => 1,
25 list_view => 1,
26 list_lu => 1,
a7d56be6
MR
27};
28
29my $zfs_unknown_scsi_provider = sub {
3b219e80 30 my ($provider) = @_;
a7d56be6 31
3b219e80 32 die "$provider: unknown iscsi provider. Available [comstar, istgt, iet]";
a7d56be6
MR
33};
34
35my $zfs_get_base = sub {
3b219e80
MR
36 my ($scfg) = @_;
37
38 if ($scfg->{iscsiprovider} eq 'comstar') {
39 return PVE::Storage::LunCmd::Comstar::get_base;
40 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
41 return PVE::Storage::LunCmd::Istgt::get_base;
42 } elsif ($scfg->{iscsiprovider} eq 'iet') {
43 return PVE::Storage::LunCmd::Iet::get_base;
44 } else {
45 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
46 }
a7d56be6
MR
47};
48
4f914e6e
MR
49sub zfs_request {
50 my ($scfg, $timeout, $method, @params) = @_;
51
5332e6c9 52 my $cmdmap;
4f914e6e
MR
53 my $zfscmd;
54 my $target;
3b219e80 55 my $msg;
4f914e6e 56
5332e6c9 57 $timeout = 5 if !$timeout;
4f914e6e 58
3b219e80
MR
59 if ($lun_cmds->{$method}) {
60 if ($scfg->{iscsiprovider} eq 'comstar') {
61 $msg = PVE::Storage::LunCmd::Comstar::run_lun_command($scfg, $timeout, $method, @params);
62 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
63 $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params);
64 } elsif ($scfg->{iscsiprovider} eq 'iet') {
65 $msg = PVE::Storage::LunCmd::Iet::run_lun_command($scfg, $timeout, $method, @params);
66 } else {
67 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
68 }
69 } else {
70 if ($method eq 'zpool_list') {
71 $zfscmd = 'zpool';
72 $method = 'list',
73 } else {
74 $zfscmd = 'zfs';
75 }
76
77 $target = 'root@' . $scfg->{portal};
78
79 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $zfscmd, $method, @params];
80
81 $msg = '';
82
83 my $output = sub {
84 my $line = shift;
85 $msg .= "$line\n";
86 };
87
88 run_command($cmd, outfunc => $output, timeout => $timeout);
89 }
4f914e6e
MR
90
91 return $msg;
92}
93
94sub zfs_parse_size {
95 my ($text) = @_;
96
97 return 0 if !$text;
98
99 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
3b219e80
MR
100 my ($size, $reminder, $unit) = ($1, $2, $3);
101 return $size if !$unit;
102 if ($unit eq 'K') {
103 $size *= 1024;
104 } elsif ($unit eq 'M') {
105 $size *= 1024*1024;
106 } elsif ($unit eq 'G') {
107 $size *= 1024*1024*1024;
108 } elsif ($unit eq 'T') {
109 $size *= 1024*1024*1024*1024;
110 }
111
112 if ($reminder) {
113 $size = ceil($size);
114 }
115 return $size;
4f914e6e 116 } else {
3b219e80 117 return 0;
4f914e6e
MR
118 }
119}
120
121sub zfs_get_pool_stats {
5332e6c9 122 my ($scfg) = @_;
4f914e6e 123
1fca1464 124 my $available = 0;
5332e6c9 125 my $used = 0;
4f914e6e 126
5332e6c9 127 my $text = zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
3b219e80 128 'available,used', $scfg->{pool});
4f914e6e 129
5332e6c9 130 my @lines = split /\n/, $text;
4f914e6e 131
5332e6c9 132 if($lines[0] =~ /^(\d+)$/) {
3b219e80 133 $available = $1;
5332e6c9 134 }
4f914e6e 135
5332e6c9 136 if($lines[1] =~ /^(\d+)$/) {
3b219e80 137 $used = $1;
5332e6c9 138 }
4f914e6e 139
1fca1464 140 return ($available, $used);
4f914e6e
MR
141}
142
143sub zfs_parse_zvol_list {
144 my ($text) = @_;
145
146 my $list = ();
147
148 return $list if !$text;
149
150 my @lines = split /\n/, $text;
151 foreach my $line (@lines) {
3b219e80
MR
152 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
153 my $zvol = {};
5e479180
PRG
154 my @parts = split /\//, $1;
155 my $name = pop @parts;
156 my $pool = join('/', @parts);
157
158 if ($pool !~ /^rpool$/) {
159 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
160 $name = $pool . '/' . $name;
3b219e80 161 } else {
5e479180 162 next;
3b219e80
MR
163 }
164
5e479180 165 $zvol->{pool} = $pool;
3b219e80
MR
166 $zvol->{name} = $name;
167 $zvol->{size} = zfs_parse_size($2);
168 if ($3 !~ /^-$/) {
5e479180 169 $zvol->{origin} = $3;
3b219e80
MR
170 }
171 push @$list, $zvol;
172 }
4f914e6e
MR
173 }
174
175 return $list;
176}
177
178sub zfs_get_lu_name {
179 my ($scfg, $zvol) = @_;
180 my $object;
181
3b219e80 182 my $base = $zfs_get_base->($scfg);
4f914e6e 183 if ($zvol =~ /^.+\/.+/) {
a7d56be6 184 $object = "$base/$zvol";
5332e6c9 185 } else {
a7d56be6 186 $object = "$base/$scfg->{pool}/$zvol";
4f914e6e
MR
187 }
188
a7d56be6
MR
189 my $lu_name = zfs_request($scfg, undef, 'list_lu', $object);
190
3b219e80
MR
191 return $lu_name if $lu_name;
192
4f914e6e
MR
193 die "Could not find lu_name for zvol $zvol";
194}
195
196sub zfs_get_zvol_size {
197 my ($scfg, $zvol) = @_;
198
199 my $text = zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
200
201 if($text =~ /volsize\s(\d+)/){
3b219e80 202 return $1;
4f914e6e
MR
203 }
204
205 die "Could not get zvol size";
206}
207
208sub zfs_add_lun_mapping_entry {
209 my ($scfg, $zvol, $guid) = @_;
210
211 if (! defined($guid)) {
3b219e80 212 $guid = zfs_get_lu_name($scfg, $zvol);
4f914e6e 213 }
3b219e80 214
4f914e6e
MR
215 zfs_request($scfg, undef, 'add_view', $guid);
216}
217
218sub zfs_delete_lu {
219 my ($scfg, $zvol) = @_;
220
221 my $guid = zfs_get_lu_name($scfg, $zvol);
222
223 zfs_request($scfg, undef, 'delete_lu', $guid);
224}
225
226sub zfs_create_lu {
227 my ($scfg, $zvol) = @_;
228
3b219e80 229 my $base = $zfs_get_base->($scfg);
a7d56be6 230 my $guid = zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
4f914e6e
MR
231
232 return $guid;
233}
234
235sub zfs_import_lu {
236 my ($scfg, $zvol) = @_;
237
3b219e80 238 my $base = $zfs_get_base->($scfg);
a7d56be6 239 zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
4f914e6e
MR
240}
241
242sub zfs_resize_lu {
243 my ($scfg, $zvol, $size) = @_;
244
245 my $guid = zfs_get_lu_name($scfg, $zvol);
246
a7d56be6 247 zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid);
4f914e6e
MR
248}
249
250sub zfs_create_zvol {
251 my ($scfg, $zvol, $size) = @_;
a9bd7bdf
CA
252
253 my $sparse = '';
254 if ($scfg->{sparse}) {
255 $sparse = '-s';
256 }
4f914e6e 257
a9bd7bdf 258 zfs_request($scfg, undef, 'create', $sparse, '-b', $scfg->{blocksize}, '-V', "${size}k", "$scfg->{pool}/$zvol");
4f914e6e
MR
259}
260
261sub zfs_delete_zvol {
262 my ($scfg, $zvol) = @_;
263
264 zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
265}
266
267sub zfs_get_lun_number {
268 my ($scfg, $guid) = @_;
4f914e6e
MR
269
270 die "could not find lun_number for guid $guid" if !$guid;
271
a7d56be6 272 return zfs_request($scfg, undef, 'list_view', $guid);
4f914e6e
MR
273}
274
275sub zfs_list_zvol {
276 my ($scfg) = @_;
277
5e479180 278 my $text = zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
4f914e6e
MR
279 my $zvols = zfs_parse_zvol_list($text);
280 return undef if !$zvols;
281
282 my $list = ();
283 foreach my $zvol (@$zvols) {
3b219e80
MR
284 my @values = split('/', $zvol->{name});
285
5e479180
PRG
286 my $image = pop @values;
287 my $pool = join('/', @values);
3b219e80
MR
288
289 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
290 my $owner = $3;
291
292 my $parent = $zvol->{origin};
293 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
294 $parent = $1;
295 }
296
297 $list->{$pool}->{$image} = {
298 name => $image,
299 size => $zvol->{size},
300 parent => $parent,
301 format => 'raw',
302 vmid => $owner
303 };
4f914e6e
MR
304 }
305
306 return $list;
307}
308
309# Configuration
310
311sub type {
312 return 'zfs';
313}
314
315sub plugindata {
316 return {
3b219e80 317 content => [ {images => 1}, { images => 1 }],
4f914e6e
MR
318 };
319}
320
321sub properties {
322 return {
3b219e80
MR
323 iscsiprovider => {
324 description => "iscsi provider",
325 type => 'string',
326 },
a9bd7bdf
CA
327 blocksize => {
328 description => "block size",
329 type => 'string',
330 },
70986fd9
CA
331 # this will disable write caching on comstar and istgt.
332 # it is not implemented for iet. iet blockio always operates with
333 # writethrough caching when not in readonly mode
334 nowritecache => {
335 description => "disable write caching on the target",
336 type => 'boolean',
337 },
a9bd7bdf
CA
338 sparse => {
339 description => "use sparse volumes",
340 type => 'boolean',
a9bd7bdf 341 }
4f914e6e
MR
342 };
343}
344
345sub options {
346 return {
a7d56be6
MR
347 nodes => { optional => 1 },
348 disable => { optional => 1 },
349 portal => { fixed => 1 },
3b219e80 350 target => { fixed => 1 },
a7d56be6 351 pool => { fixed => 1 },
3b219e80
MR
352 blocksize => { fixed => 1 },
353 iscsiprovider => { fixed => 1 },
70986fd9 354 nowritecache => { optional => 1 },
a9bd7bdf 355 sparse => { optional => 1 },
3b219e80 356 content => { optional => 1 },
4f914e6e
MR
357 };
358}
359
360# Storage implementation
361
362sub parse_volname {
363 my ($class, $volname) = @_;
364
365 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
3b219e80 366 return ('images', $5, $8, $2, $4, $6);
4f914e6e
MR
367 }
368
369 die "unable to parse zfs volume name '$volname'\n";
370}
371
372sub path {
373 my ($class, $scfg, $volname) = @_;
374
375 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
376
377 my $target = $scfg->{target};
378 my $portal = $scfg->{portal};
379
380 my $guid = zfs_get_lu_name($scfg, $name);
381 my $lun = zfs_get_lun_number($scfg, $guid);
3b219e80 382
4f914e6e 383 my $path = "iscsi://$portal/$target/$lun";
3b219e80 384
4f914e6e
MR
385 return ($path, $vmid, $vtype);
386}
387
388my $find_free_diskname = sub {
389 my ($storeid, $scfg, $vmid) = @_;
390
391 my $name = undef;
392 my $volumes = zfs_list_zvol($scfg);
393
394 my $disk_ids = {};
395 my $dat = $volumes->{$scfg->{pool}};
396
397 foreach my $image (keys %$dat) {
398 my $volname = $dat->{$image}->{name};
399 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
400 $disk_ids->{$2} = 1;
401 }
402 }
403
404 for (my $i = 1; $i < 100; $i++) {
405 if (!$disk_ids->{$i}) {
406 return "vm-$vmid-disk-$i";
407 }
408 }
409
410 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
411};
412
413sub create_base {
414 my ($class, $storeid, $scfg, $volname) = @_;
415
416 my $snap = '__base__';
417
418 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
419 $class->parse_volname($volname);
420
421 die "create_base not possible with base image\n" if $isBase;
422
423 my $newname = $name;
424 $newname =~ s/^vm-/base-/;
425
426 my $newvolname = $basename ? "$basename/$newname" : "$newname";
427
428 zfs_delete_lu($scfg, $name);
429 zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
430
431 my $guid = zfs_create_lu($scfg, $newname);
432 zfs_add_lun_mapping_entry($scfg, $newname, $guid);
433
434 my $running = undef; #fixme : is create_base always offline ?
435
436 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
437
438 return $newvolname;
439}
440
441sub clone_image {
442 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
443
444 my $snap = '__base__';
445
446 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
447 $class->parse_volname($volname);
448
449 die "clone_image only works on base images\n" if !$isBase;
450
451 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
452
453 warn "clone $volname: $basename to $name\n";
454
455 zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
456
457 my $guid = zfs_create_lu($scfg, $name);
458 zfs_add_lun_mapping_entry($scfg, $name, $guid);
459
460 return $name;
461}
462
463sub alloc_image {
464 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
465
466 die "unsupported format '$fmt'" if $fmt ne 'raw';
467
468 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
3b219e80 469 if $name && $name !~ m/^vm-$vmid-/;
4f914e6e
MR
470
471 $name = &$find_free_diskname($storeid, $scfg, $vmid);
472
473 zfs_create_zvol($scfg, $name, $size);
474 my $guid = zfs_create_lu($scfg, $name);
475 zfs_add_lun_mapping_entry($scfg, $name, $guid);
476
477 return $name;
478}
479
480sub free_image {
481 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
482
483 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
484
485 zfs_delete_lu($scfg, $name);
486 eval {
487 zfs_delete_zvol($scfg, $name);
488 };
489 do {
490 my $err = $@;
491 my $guid = zfs_create_lu($scfg, $name);
492 zfs_add_lun_mapping_entry($scfg, $name, $guid);
493 die $err;
494 } if $@;
495
496 return undef;
497}
498
499sub list_images {
500 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
501
502 $cache->{zfs} = zfs_list_zvol($scfg) if !$cache->{zfs};
503 my $zfspool = $scfg->{pool};
504 my $res = [];
505
506 if (my $dat = $cache->{zfs}->{$zfspool}) {
507
3b219e80 508 foreach my $image (keys %$dat) {
4f914e6e 509
3b219e80
MR
510 my $volname = $dat->{$image}->{name};
511 my $parent = $dat->{$image}->{parent};
4f914e6e 512
3b219e80 513 my $volid = undef;
4f914e6e 514 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
3b219e80
MR
515 my ($basename) = ($1);
516 $volid = "$storeid:$basename/$volname";
517 } else {
518 $volid = "$storeid:$volname";
519 }
520
521 my $owner = $dat->{$volname}->{vmid};
522 if ($vollist) {
523 my $found = grep { $_ eq $volid } @$vollist;
524 next if !$found;
525 } else {
526 next if defined ($vmid) && ($owner ne $vmid);
527 }
528
529 my $info = $dat->{$volname};
530 $info->{volid} = $volid;
531 push @$res, $info;
532 }
4f914e6e
MR
533 }
534
535 return $res;
536}
537
538sub status {
539 my ($class, $storeid, $scfg, $cache) = @_;
540
541 my $total = 0;
542 my $free = 0;
543 my $used = 0;
544 my $active = 0;
545
546 eval {
3b219e80
MR
547 ($free, $used) = zfs_get_pool_stats($scfg);
548 $active = 1;
549 $total = $free + $used;
4f914e6e
MR
550 };
551 warn $@ if $@;
552
553 return ($total, $free, $used, $active);
554}
555
556sub activate_storage {
557 my ($class, $storeid, $scfg, $cache) = @_;
558 return 1;
559}
560
561sub deactivate_storage {
562 my ($class, $storeid, $scfg, $cache) = @_;
563 return 1;
564}
565
566sub activate_volume {
567 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
568 return 1;
569}
570
571sub deactivate_volume {
572 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
573 return 1;
574}
575
576sub volume_size_info {
577 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
578
579 return zfs_get_zvol_size($scfg, $volname);
580}
581
582sub volume_resize {
583 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
584
585 my $new_size = ($size/1024);
586
587 zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
588 zfs_resize_lu($scfg, $volname, $new_size);
589}
590
591sub volume_snapshot {
592 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
593
594 zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
595}
596
597sub volume_snapshot_rollback {
598 my ($class, $scfg, $storeid, $volname, $snap) = @_;
599
600 zfs_delete_lu($scfg, $volname);
601
602 zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
603
604 zfs_import_lu($scfg, $volname);
605
606 zfs_add_lun_mapping_entry($scfg, $volname);
607}
608
609sub volume_snapshot_delete {
610 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
611
612 zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
613}
614
615sub volume_has_feature {
616 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
617
618 my $features = {
3b219e80
MR
619 snapshot => { current => 1, snap => 1},
620 clone => { base => 1},
621 template => { current => 1},
622 copy => { base => 1, current => 1},
4f914e6e
MR
623 };
624
625 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
3b219e80 626 $class->parse_volname($volname);
4f914e6e
MR
627
628 my $key = undef;
5332e6c9
DM
629
630 if ($snapname) {
3b219e80 631 $key = 'snap';
4f914e6e 632 } else {
3b219e80 633 $key = $isBase ? 'base' : 'current';
4f914e6e 634 }
5332e6c9 635
4f914e6e
MR
636 return 1 if $features->{$feature}->{$key};
637
638 return undef;
639}
640
6411;