]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
ZFS Pool: improve error output from activate_storage
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
1 package PVE::Storage::ZFSPoolPlugin;
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 use PVE::RPCEnvironment;
10 use Net::IP;
11
12 use base qw(PVE::Storage::Plugin);
13
14 sub type {
15 return 'zfspool';
16 }
17
18 sub plugindata {
19 return {
20 content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}],
21 format => [ { raw => 1, subvol => 1 } , 'raw' ],
22 };
23 }
24
25 sub properties {
26 return {
27 blocksize => {
28 description => "block size",
29 type => 'string',
30 },
31 sparse => {
32 description => "use sparse volumes",
33 type => 'boolean',
34 },
35 };
36 }
37
38 sub options {
39 return {
40 pool => { fixed => 1 },
41 blocksize => { optional => 1 },
42 sparse => { optional => 1 },
43 nodes => { optional => 1 },
44 disable => { optional => 1 },
45 content => { optional => 1 },
46 bwlimit => { optional => 1 },
47 };
48 }
49
50 # static zfs helper methods
51
52 sub zfs_parse_size {
53 my ($text) = @_;
54
55 return 0 if !$text;
56
57 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
58
59 my ($size, $reminder, $unit) = ($1, $2, $3);
60
61 if ($unit) {
62 if ($unit eq 'K') {
63 $size *= 1024;
64 } elsif ($unit eq 'M') {
65 $size *= 1024*1024;
66 } elsif ($unit eq 'G') {
67 $size *= 1024*1024*1024;
68 } elsif ($unit eq 'T') {
69 $size *= 1024*1024*1024*1024;
70 } else {
71 die "got unknown zfs size unit '$unit'\n";
72 }
73 }
74
75 if ($reminder) {
76 $size = ceil($size);
77 }
78
79 return $size;
80
81 }
82
83 warn "unable to parse zfs size '$text'\n";
84
85 return 0;
86 }
87
88 sub zfs_parse_zvol_list {
89 my ($text) = @_;
90
91 my $list = ();
92
93 return $list if !$text;
94
95 my @lines = split /\n/, $text;
96 foreach my $line (@lines) {
97 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
98 next if !($type eq 'volume' || $type eq 'filesystem');
99
100 my $zvol = {};
101 my @parts = split /\//, $dataset;
102 next if scalar(@parts) < 2; # we need pool/name
103 my $name = pop @parts;
104 my $pool = join('/', @parts);
105
106 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
107 $zvol->{owner} = $2;
108
109 $zvol->{pool} = $pool;
110 $zvol->{name} = $name;
111 if ($type eq 'filesystem') {
112 if ($refquota eq 'none') {
113 $zvol->{size} = 0;
114 } else {
115 $zvol->{size} = zfs_parse_size($refquota);
116 }
117 $zvol->{format} = 'subvol';
118 } else {
119 $zvol->{size} = zfs_parse_size($size);
120 $zvol->{format} = 'raw';
121 }
122 if ($origin !~ /^-$/) {
123 $zvol->{origin} = $origin;
124 }
125 push @$list, $zvol;
126 }
127
128 return $list;
129 }
130
131 sub parse_volname {
132 my ($class, $volname) = @_;
133
134 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) {
135 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw';
136 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
137 return ('images', $5, $7, $2, $4, $isBase, $format);
138 }
139
140 die "unable to parse zfs volume name '$volname'\n";
141 }
142
143 # virtual zfs methods (subclass can overwrite them)
144
145 sub path {
146 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
147
148 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
149
150 my $path = '';
151
152 if ($vtype eq "images") {
153 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
154 # fixme: we currently assume standard mount point?!
155 $path = "/$scfg->{pool}/$name";
156 } else {
157 $path = "/dev/zvol/$scfg->{pool}/$name";
158 }
159 $path .= "\@$snapname" if defined($snapname);
160 } else {
161 die "$vtype is not allowed in ZFSPool!";
162 }
163
164 return ($path, $vmid, $vtype);
165 }
166
167 sub zfs_request {
168 my ($class, $scfg, $timeout, $method, @params) = @_;
169
170 my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5;
171
172 my $cmd = [];
173
174 if ($method eq 'zpool_list') {
175 push @$cmd, 'zpool', 'list';
176 } elsif ($method eq 'zpool_import') {
177 push @$cmd, 'zpool', 'import';
178 $default_timeout = 15 if $default_timeout < 15;
179 } else {
180 push @$cmd, 'zfs', $method;
181 }
182
183 push @$cmd, @params;
184
185 my $msg = '';
186
187 my $output = sub {
188 my $line = shift;
189 $msg .= "$line\n";
190 };
191
192 $timeout = $default_timeout if !$timeout;
193
194 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
195
196 return $msg;
197 }
198
199 sub alloc_image {
200 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
201
202 my $volname = $name;
203
204 if ($fmt eq 'raw') {
205
206 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
207 if $volname && $volname !~ m/^vm-$vmid-/;
208 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
209 if !$volname;
210
211 $class->zfs_create_zvol($scfg, $volname, $size);
212 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
213
214 my $timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10;
215 for (my $i = 1; $i <= $timeout; $i++) {
216 last if -b $devname;
217 die "Timeout: no zvol after $timeout sec found.\n"
218 if $i == $timeout;
219
220 sleep(1);
221 }
222 } elsif ( $fmt eq 'subvol') {
223
224 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
225 if $volname && $volname !~ m/^subvol-$vmid-/;
226 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
227 if !$volname;
228
229 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
230 if $volname !~ m/^subvol-$vmid-/;
231
232 $class->zfs_create_subvol($scfg, $volname, $size);
233
234 } else {
235 die "unsupported format '$fmt'";
236 }
237
238 return $volname;
239 }
240
241 sub free_image {
242 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
243
244 my (undef, $name, undef) = $class->parse_volname($volname);
245
246 $class->zfs_delete_zvol($scfg, $name);
247
248 return undef;
249 }
250
251 sub list_images {
252 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
253
254 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
255 my $zfspool = $scfg->{pool};
256 my $res = [];
257
258 if (my $dat = $cache->{zfs}->{$zfspool}) {
259
260 foreach my $image (keys %$dat) {
261
262 my $info = $dat->{$image};
263
264 my $volname = $info->{name};
265 my $parent = $info->{parent};
266 my $owner = $info->{vmid};
267
268 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
269 my ($basename) = ($1);
270 $info->{volid} = "$storeid:$basename/$volname";
271 } else {
272 $info->{volid} = "$storeid:$volname";
273 }
274
275 if ($vollist) {
276 my $found = grep { $_ eq $info->{volid} } @$vollist;
277 next if !$found;
278 } else {
279 next if defined ($vmid) && ($owner ne $vmid);
280 }
281
282 push @$res, $info;
283 }
284 }
285 return $res;
286 }
287
288 sub zfs_get_pool_stats {
289 my ($class, $scfg) = @_;
290
291 my $available = 0;
292 my $used = 0;
293
294 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
295 'available,used', $scfg->{pool});
296
297 my @lines = split /\n/, $text;
298
299 if($lines[0] =~ /^(\d+)$/) {
300 $available = $1;
301 }
302
303 if($lines[1] =~ /^(\d+)$/) {
304 $used = $1;
305 }
306
307 return ($available, $used);
308 }
309
310 sub zfs_create_zvol {
311 my ($class, $scfg, $zvol, $size) = @_;
312
313 # always align size to 1M as workaround until
314 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
315 my $padding = (1024 - $size % 1024) % 1024;
316 $size = $size + $padding;
317
318 my $cmd = ['create'];
319
320 push @$cmd, '-s' if $scfg->{sparse};
321
322 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
323
324 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
325
326 $class->zfs_request($scfg, undef, @$cmd);
327 }
328
329 sub zfs_create_subvol {
330 my ($class, $scfg, $volname, $size) = @_;
331
332 my $dataset = "$scfg->{pool}/$volname";
333
334 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
335 '-o', "refquota=${size}k", $dataset];
336
337 $class->zfs_request($scfg, undef, @$cmd);
338 }
339
340 sub zfs_delete_zvol {
341 my ($class, $scfg, $zvol) = @_;
342
343 my $err;
344
345 for (my $i = 0; $i < 6; $i++) {
346
347 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
348 if ($err = $@) {
349 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
350 sleep(1);
351 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
352 $err = undef;
353 last;
354 } else {
355 die $err;
356 }
357 } else {
358 last;
359 }
360 }
361
362 die $err if $err;
363 }
364
365 sub zfs_list_zvol {
366 my ($class, $scfg) = @_;
367
368 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
369 my $zvols = zfs_parse_zvol_list($text);
370 return undef if !$zvols;
371
372 my $list = ();
373 foreach my $zvol (@$zvols) {
374 my $pool = $zvol->{pool};
375 my $name = $zvol->{name};
376 my $parent = $zvol->{origin};
377 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
378 $parent = $1;
379 }
380
381 $list->{$pool}->{$name} = {
382 name => $name,
383 size => $zvol->{size},
384 parent => $parent,
385 format => $zvol->{format},
386 vmid => $zvol->{owner},
387 };
388 }
389
390 return $list;
391 }
392
393 sub zfs_find_free_diskname {
394 my ($class, $storeid, $scfg, $vmid, $format) = @_;
395
396 my $volumes = $class->zfs_list_zvol($scfg);
397 my $dat = $volumes->{$scfg->{pool}};
398
399 my $disk_list = [ keys %$dat ];
400 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $format, $scfg);
401 }
402
403 sub zfs_get_latest_snapshot {
404 my ($class, $scfg, $volname) = @_;
405
406 my $vname = ($class->parse_volname($volname))[1];
407
408 # abort rollback if snapshot is not the latest
409 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
410 my $text = $class->zfs_request($scfg, undef, 'list', @params);
411 my @snapshots = split(/\n/, $text);
412
413 my $recentsnap;
414 foreach (@snapshots) {
415 if (/$scfg->{pool}\/$vname/) {
416 s/^.*@//;
417 $recentsnap = $_;
418 }
419 }
420
421 return $recentsnap;
422 }
423
424 sub status {
425 my ($class, $storeid, $scfg, $cache) = @_;
426
427 my $total = 0;
428 my $free = 0;
429 my $used = 0;
430 my $active = 0;
431
432 eval {
433 ($free, $used) = $class->zfs_get_pool_stats($scfg);
434 $active = 1;
435 $total = $free + $used;
436 };
437 warn $@ if $@;
438
439 return ($total, $free, $used, $active);
440 }
441
442 sub volume_size_info {
443 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
444
445 my (undef, $vname, undef, undef, undef, undef, $format) =
446 $class->parse_volname($volname);
447
448 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
449 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
450 if ($text =~ /\s$attr\s(\d+)\s/) {
451 return $1;
452 }
453
454 die "Could not get zfs volume size\n";
455 }
456
457 sub volume_snapshot {
458 my ($class, $scfg, $storeid, $volname, $snap) = @_;
459
460 my $vname = ($class->parse_volname($volname))[1];
461
462 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
463 }
464
465 sub volume_snapshot_delete {
466 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
467
468 my $vname = ($class->parse_volname($volname))[1];
469
470 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
471 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
472 }
473
474 sub volume_snapshot_rollback {
475 my ($class, $scfg, $storeid, $volname, $snap) = @_;
476
477 my $vname = ($class->parse_volname($volname))[1];
478
479 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
480 }
481
482 sub volume_rollback_is_possible {
483 my ($class, $scfg, $storeid, $volname, $snap) = @_;
484
485 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
486 if ($snap ne $recentsnap) {
487 die "can't rollback, more recent snapshots exist\n";
488 }
489
490 return 1;
491 }
492
493 sub volume_snapshot_list {
494 my ($class, $scfg, $storeid, $volname) = @_;
495
496 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
497
498 my $zpath = "$scfg->{pool}/$name";
499
500 my $snaps = [];
501
502 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
503 'name', $zpath];
504
505 my $outfunc = sub {
506 my $line = shift;
507
508 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
509 push @$snaps, $1;
510 }
511 };
512
513 eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
514
515 # return an empty array if dataset does not exist.
516 return $snaps;
517 }
518
519 sub activate_storage {
520 my ($class, $storeid, $scfg, $cache) = @_;
521
522 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
523 my $pool = $scfg->{pool};
524 $pool =~ s!/.*$!!;
525
526 my @param = ('-o', 'name', '-H', "$pool");
527 my $res;
528 eval {
529 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
530 };
531 my $err = $@;
532 if ($err || !defined($res) || $res !~ $pool) {
533 eval {
534 @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
535 $class->zfs_request($scfg, undef, 'zpool_import', @param);
536 };
537 if ($@) {
538 warn "$err\n";
539 die "could not activate storage '$storeid', $@\n";
540 }
541 }
542 return 1;
543 }
544
545 sub deactivate_storage {
546 my ($class, $storeid, $scfg, $cache) = @_;
547 return 1;
548 }
549
550 sub activate_volume {
551 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
552 return 1;
553 }
554
555 sub deactivate_volume {
556 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
557 return 1;
558 }
559
560 sub clone_image {
561 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
562
563 $snap ||= '__base__';
564
565 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
566 $class->parse_volname($volname);
567
568 die "clone_image only works on base images\n" if !$isBase;
569
570 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
571
572 if ($format eq 'subvol') {
573 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
574 chomp($size);
575 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
576 } else {
577 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
578 }
579
580 return "$basename/$name";
581 }
582
583 sub create_base {
584 my ($class, $storeid, $scfg, $volname) = @_;
585
586 my $snap = '__base__';
587
588 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
589 $class->parse_volname($volname);
590
591 die "create_base not possible with base image\n" if $isBase;
592
593 my $newname = $name;
594 if ( $format eq 'subvol' ) {
595 $newname =~ s/^subvol-/basevol-/;
596 } else {
597 $newname =~ s/^vm-/base-/;
598 }
599 my $newvolname = $basename ? "$basename/$newname" : "$newname";
600
601 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
602
603 my $running = undef; #fixme : is create_base always offline ?
604
605 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
606
607 return $newvolname;
608 }
609
610 sub volume_resize {
611 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
612
613 my $new_size = int($size/1024);
614
615 my (undef, $vname, undef, undef, undef, undef, $format) =
616 $class->parse_volname($volname);
617
618 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
619
620 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
621
622 return $new_size;
623 }
624
625 sub storage_can_replicate {
626 my ($class, $scfg, $storeid, $format) = @_;
627
628 return 1 if $format eq 'raw' || $format eq 'subvol';
629
630 return 0;
631 }
632
633 sub volume_has_feature {
634 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
635
636 my $features = {
637 snapshot => { current => 1, snap => 1},
638 clone => { base => 1},
639 template => { current => 1},
640 copy => { base => 1, current => 1},
641 sparseinit => { base => 1, current => 1},
642 replicate => { base => 1, current => 1},
643 };
644
645 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
646 $class->parse_volname($volname);
647
648 my $key = undef;
649
650 if ($snapname) {
651 $key = 'snap';
652 } else {
653 $key = $isBase ? 'base' : 'current';
654 }
655
656 return 1 if $features->{$feature}->{$key};
657
658 return undef;
659 }
660
661 sub volume_export {
662 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
663
664 die "unsupported export stream format for $class: $format\n"
665 if $format ne 'zfs';
666
667 die "$class storage can only export snapshots\n"
668 if !defined($snapshot);
669
670 my $dataset = ($class->parse_volname($volname))[1];
671
672 my $fd = fileno($fh);
673 die "internal error: invalid file handle for volume_export\n"
674 if !defined($fd);
675 $fd = ">&$fd";
676
677 # For zfs we always create a replication stream (-R) which means the remote
678 # side will always delete non-existing source snapshots. This should work
679 # for all our use cases.
680 my $cmd = ['zfs', 'send', '-Rpv'];
681 if (defined($base_snapshot)) {
682 my $arg = $with_snapshots ? '-I' : '-i';
683 push @$cmd, $arg, $base_snapshot;
684 }
685 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
686
687 run_command($cmd, output => $fd);
688
689 return;
690 }
691
692 sub volume_export_formats {
693 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
694
695 my @formats = ('zfs');
696 # TODOs:
697 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
698 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
699 return @formats;
700 }
701
702 sub volume_import {
703 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
704
705 die "unsupported import stream format for $class: $format\n"
706 if $format ne 'zfs';
707
708 my $fd = fileno($fh);
709 die "internal error: invalid file handle for volume_import\n"
710 if !defined($fd);
711
712 my $dataset = ($class->parse_volname($volname))[1];
713 my $zfspath = "$scfg->{pool}/$dataset";
714 my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
715 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
716 noerr => 1, errfunc => sub {});
717 if (defined($base_snapshot)) {
718 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
719 } else {
720 die "volume '$zfspath' already exists\n" if $exists;
721 }
722
723 eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
724 if (my $err = $@) {
725 if (defined($base_snapshot)) {
726 eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
727 } else {
728 eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
729 }
730 die $err;
731 }
732
733 return;
734 }
735
736 sub volume_import_formats {
737 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
738
739 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
740 }
741
742 1;