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