]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
workaround zfs create -V error for unaligned sizes
[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
532 if ($@ || !defined($res) || $res !~ $pool) {
533 eval {
534 @param = ('-d', '/dev/disk/by-id/', "$pool");
535 $class->zfs_request($scfg, undef, 'zpool_import', @param);
536 };
537 die "could not activate storage '$storeid', $@\n" if $@;
538 }
539 return 1;
540 }
541
542 sub deactivate_storage {
543 my ($class, $storeid, $scfg, $cache) = @_;
544 return 1;
545 }
546
547 sub activate_volume {
548 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
549 return 1;
550 }
551
552 sub deactivate_volume {
553 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
554 return 1;
555 }
556
557 sub clone_image {
558 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
559
560 $snap ||= '__base__';
561
562 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
563 $class->parse_volname($volname);
564
565 die "clone_image only works on base images\n" if !$isBase;
566
567 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
568
569 if ($format eq 'subvol') {
570 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
571 chomp($size);
572 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
573 } else {
574 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
575 }
576
577 return "$basename/$name";
578 }
579
580 sub create_base {
581 my ($class, $storeid, $scfg, $volname) = @_;
582
583 my $snap = '__base__';
584
585 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
586 $class->parse_volname($volname);
587
588 die "create_base not possible with base image\n" if $isBase;
589
590 my $newname = $name;
591 if ( $format eq 'subvol' ) {
592 $newname =~ s/^subvol-/basevol-/;
593 } else {
594 $newname =~ s/^vm-/base-/;
595 }
596 my $newvolname = $basename ? "$basename/$newname" : "$newname";
597
598 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
599
600 my $running = undef; #fixme : is create_base always offline ?
601
602 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
603
604 return $newvolname;
605 }
606
607 sub volume_resize {
608 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
609
610 my $new_size = int($size/1024);
611
612 my (undef, $vname, undef, undef, undef, undef, $format) =
613 $class->parse_volname($volname);
614
615 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
616
617 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
618
619 return $new_size;
620 }
621
622 sub storage_can_replicate {
623 my ($class, $scfg, $storeid, $format) = @_;
624
625 return 1 if $format eq 'raw' || $format eq 'subvol';
626
627 return 0;
628 }
629
630 sub volume_has_feature {
631 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
632
633 my $features = {
634 snapshot => { current => 1, snap => 1},
635 clone => { base => 1},
636 template => { current => 1},
637 copy => { base => 1, current => 1},
638 sparseinit => { base => 1, current => 1},
639 replicate => { base => 1, current => 1},
640 };
641
642 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
643 $class->parse_volname($volname);
644
645 my $key = undef;
646
647 if ($snapname) {
648 $key = 'snap';
649 } else {
650 $key = $isBase ? 'base' : 'current';
651 }
652
653 return 1 if $features->{$feature}->{$key};
654
655 return undef;
656 }
657
658 sub volume_export {
659 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
660
661 die "unsupported export stream format for $class: $format\n"
662 if $format ne 'zfs';
663
664 die "$class storage can only export snapshots\n"
665 if !defined($snapshot);
666
667 my $dataset = ($class->parse_volname($volname))[1];
668
669 my $fd = fileno($fh);
670 die "internal error: invalid file handle for volume_export\n"
671 if !defined($fd);
672 $fd = ">&$fd";
673
674 # For zfs we always create a replication stream (-R) which means the remote
675 # side will always delete non-existing source snapshots. This should work
676 # for all our use cases.
677 my $cmd = ['zfs', 'send', '-Rpv'];
678 if (defined($base_snapshot)) {
679 my $arg = $with_snapshots ? '-I' : '-i';
680 push @$cmd, $arg, $base_snapshot;
681 }
682 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
683
684 run_command($cmd, output => $fd);
685
686 return;
687 }
688
689 sub volume_export_formats {
690 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
691
692 my @formats = ('zfs');
693 # TODOs:
694 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
695 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
696 return @formats;
697 }
698
699 sub volume_import {
700 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
701
702 die "unsupported import stream format for $class: $format\n"
703 if $format ne 'zfs';
704
705 my $fd = fileno($fh);
706 die "internal error: invalid file handle for volume_import\n"
707 if !defined($fd);
708
709 my $dataset = ($class->parse_volname($volname))[1];
710 my $zfspath = "$scfg->{pool}/$dataset";
711 my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
712 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
713 noerr => 1, errfunc => sub {});
714 if (defined($base_snapshot)) {
715 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
716 } else {
717 die "volume '$zfspath' already exists\n" if $exists;
718 }
719
720 eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
721 if (my $err = $@) {
722 if (defined($base_snapshot)) {
723 eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
724 } else {
725 eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
726 }
727 die $err;
728 }
729
730 return;
731 }
732
733 sub volume_import_formats {
734 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
735
736 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
737 }
738
739 1;