]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
6e0845752a002d02f8da50978de4330ba104ce8b
[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 my $cmd = ['create'];
314
315 push @$cmd, '-s' if $scfg->{sparse};
316
317 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
318
319 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
320
321 $class->zfs_request($scfg, undef, @$cmd);
322 }
323
324 sub zfs_create_subvol {
325 my ($class, $scfg, $volname, $size) = @_;
326
327 my $dataset = "$scfg->{pool}/$volname";
328
329 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
330 '-o', "refquota=${size}k", $dataset];
331
332 $class->zfs_request($scfg, undef, @$cmd);
333 }
334
335 sub zfs_delete_zvol {
336 my ($class, $scfg, $zvol) = @_;
337
338 my $err;
339
340 for (my $i = 0; $i < 6; $i++) {
341
342 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
343 if ($err = $@) {
344 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
345 sleep(1);
346 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
347 $err = undef;
348 last;
349 } else {
350 die $err;
351 }
352 } else {
353 last;
354 }
355 }
356
357 die $err if $err;
358 }
359
360 sub zfs_list_zvol {
361 my ($class, $scfg) = @_;
362
363 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
364 my $zvols = zfs_parse_zvol_list($text);
365 return undef if !$zvols;
366
367 my $list = ();
368 foreach my $zvol (@$zvols) {
369 my $pool = $zvol->{pool};
370 my $name = $zvol->{name};
371 my $parent = $zvol->{origin};
372 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
373 $parent = $1;
374 }
375
376 $list->{$pool}->{$name} = {
377 name => $name,
378 size => $zvol->{size},
379 parent => $parent,
380 format => $zvol->{format},
381 vmid => $zvol->{owner},
382 };
383 }
384
385 return $list;
386 }
387
388 sub zfs_find_free_diskname {
389 my ($class, $storeid, $scfg, $vmid, $format) = @_;
390
391 my $volumes = $class->zfs_list_zvol($scfg);
392 my $dat = $volumes->{$scfg->{pool}};
393
394 my $disk_list = [ keys %$dat ];
395 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $format, $scfg);
396 }
397
398 sub zfs_get_latest_snapshot {
399 my ($class, $scfg, $volname) = @_;
400
401 my $vname = ($class->parse_volname($volname))[1];
402
403 # abort rollback if snapshot is not the latest
404 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
405 my $text = $class->zfs_request($scfg, undef, 'list', @params);
406 my @snapshots = split(/\n/, $text);
407
408 my $recentsnap;
409 foreach (@snapshots) {
410 if (/$scfg->{pool}\/$vname/) {
411 s/^.*@//;
412 $recentsnap = $_;
413 }
414 }
415
416 return $recentsnap;
417 }
418
419 sub status {
420 my ($class, $storeid, $scfg, $cache) = @_;
421
422 my $total = 0;
423 my $free = 0;
424 my $used = 0;
425 my $active = 0;
426
427 eval {
428 ($free, $used) = $class->zfs_get_pool_stats($scfg);
429 $active = 1;
430 $total = $free + $used;
431 };
432 warn $@ if $@;
433
434 return ($total, $free, $used, $active);
435 }
436
437 sub volume_size_info {
438 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
439
440 my (undef, $vname, undef, undef, undef, undef, $format) =
441 $class->parse_volname($volname);
442
443 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
444 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
445 if ($text =~ /\s$attr\s(\d+)\s/) {
446 return $1;
447 }
448
449 die "Could not get zfs volume size\n";
450 }
451
452 sub volume_snapshot {
453 my ($class, $scfg, $storeid, $volname, $snap) = @_;
454
455 my $vname = ($class->parse_volname($volname))[1];
456
457 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
458 }
459
460 sub volume_snapshot_delete {
461 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
462
463 my $vname = ($class->parse_volname($volname))[1];
464
465 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
466 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
467 }
468
469 sub volume_snapshot_rollback {
470 my ($class, $scfg, $storeid, $volname, $snap) = @_;
471
472 my $vname = ($class->parse_volname($volname))[1];
473
474 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
475 }
476
477 sub volume_rollback_is_possible {
478 my ($class, $scfg, $storeid, $volname, $snap) = @_;
479
480 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
481 if ($snap ne $recentsnap) {
482 die "can't rollback, more recent snapshots exist\n";
483 }
484
485 return 1;
486 }
487
488 sub volume_snapshot_list {
489 my ($class, $scfg, $storeid, $volname) = @_;
490
491 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
492
493 my $zpath = "$scfg->{pool}/$name";
494
495 my $snaps = [];
496
497 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
498 'name', $zpath];
499
500 my $outfunc = sub {
501 my $line = shift;
502
503 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
504 push @$snaps, $1;
505 }
506 };
507
508 eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
509
510 # return an empty array if dataset does not exist.
511 return $snaps;
512 }
513
514 sub activate_storage {
515 my ($class, $storeid, $scfg, $cache) = @_;
516
517 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
518 my $pool = $scfg->{pool};
519 $pool =~ s!/.*$!!;
520
521 my @param = ('-o', 'name', '-H', "$pool");
522 my $res;
523 eval {
524 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
525 };
526
527 if ($@ || !defined($res) || $res !~ $pool) {
528 eval {
529 @param = ('-d', '/dev/disk/by-id/', "$pool");
530 $class->zfs_request($scfg, undef, 'zpool_import', @param);
531 };
532 die "could not activate storage '$storeid', $@\n" if $@;
533 }
534 return 1;
535 }
536
537 sub deactivate_storage {
538 my ($class, $storeid, $scfg, $cache) = @_;
539 return 1;
540 }
541
542 sub activate_volume {
543 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
544 return 1;
545 }
546
547 sub deactivate_volume {
548 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
549 return 1;
550 }
551
552 sub clone_image {
553 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
554
555 $snap ||= '__base__';
556
557 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
558 $class->parse_volname($volname);
559
560 die "clone_image only works on base images\n" if !$isBase;
561
562 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
563
564 if ($format eq 'subvol') {
565 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
566 chomp($size);
567 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
568 } else {
569 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
570 }
571
572 return "$basename/$name";
573 }
574
575 sub create_base {
576 my ($class, $storeid, $scfg, $volname) = @_;
577
578 my $snap = '__base__';
579
580 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
581 $class->parse_volname($volname);
582
583 die "create_base not possible with base image\n" if $isBase;
584
585 my $newname = $name;
586 if ( $format eq 'subvol' ) {
587 $newname =~ s/^subvol-/basevol-/;
588 } else {
589 $newname =~ s/^vm-/base-/;
590 }
591 my $newvolname = $basename ? "$basename/$newname" : "$newname";
592
593 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
594
595 my $running = undef; #fixme : is create_base always offline ?
596
597 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
598
599 return $newvolname;
600 }
601
602 sub volume_resize {
603 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
604
605 my $new_size = int($size/1024);
606
607 my (undef, $vname, undef, undef, undef, undef, $format) =
608 $class->parse_volname($volname);
609
610 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
611
612 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
613
614 return $new_size;
615 }
616
617 sub storage_can_replicate {
618 my ($class, $scfg, $storeid, $format) = @_;
619
620 return 1 if $format eq 'raw' || $format eq 'subvol';
621
622 return 0;
623 }
624
625 sub volume_has_feature {
626 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
627
628 my $features = {
629 snapshot => { current => 1, snap => 1},
630 clone => { base => 1},
631 template => { current => 1},
632 copy => { base => 1, current => 1},
633 sparseinit => { base => 1, current => 1},
634 replicate => { base => 1, current => 1},
635 };
636
637 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
638 $class->parse_volname($volname);
639
640 my $key = undef;
641
642 if ($snapname) {
643 $key = 'snap';
644 } else {
645 $key = $isBase ? 'base' : 'current';
646 }
647
648 return 1 if $features->{$feature}->{$key};
649
650 return undef;
651 }
652
653 sub volume_export {
654 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
655
656 die "unsupported export stream format for $class: $format\n"
657 if $format ne 'zfs';
658
659 die "$class storage can only export snapshots\n"
660 if !defined($snapshot);
661
662 my $dataset = ($class->parse_volname($volname))[1];
663
664 my $fd = fileno($fh);
665 die "internal error: invalid file handle for volume_export\n"
666 if !defined($fd);
667 $fd = ">&$fd";
668
669 # For zfs we always create a replication stream (-R) which means the remote
670 # side will always delete non-existing source snapshots. This should work
671 # for all our use cases.
672 my $cmd = ['zfs', 'send', '-Rpv'];
673 if (defined($base_snapshot)) {
674 my $arg = $with_snapshots ? '-I' : '-i';
675 push @$cmd, $arg, $base_snapshot;
676 }
677 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
678
679 run_command($cmd, output => $fd);
680
681 return;
682 }
683
684 sub volume_export_formats {
685 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
686
687 my @formats = ('zfs');
688 # TODOs:
689 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
690 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
691 return @formats;
692 }
693
694 sub volume_import {
695 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
696
697 die "unsupported import stream format for $class: $format\n"
698 if $format ne 'zfs';
699
700 my $fd = fileno($fh);
701 die "internal error: invalid file handle for volume_import\n"
702 if !defined($fd);
703
704 my $dataset = ($class->parse_volname($volname))[1];
705 my $zfspath = "$scfg->{pool}/$dataset";
706 my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
707 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
708 noerr => 1, errfunc => sub {});
709 if (defined($base_snapshot)) {
710 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
711 } else {
712 die "volume '$zfspath' already exists\n" if $exists;
713 }
714
715 eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
716 if (my $err = $@) {
717 if (defined($base_snapshot)) {
718 eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
719 } else {
720 eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
721 }
722 die $err;
723 }
724
725 return;
726 }
727
728 sub volume_import_formats {
729 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
730
731 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
732 }
733
734 1;