]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
951d027c4aa47a6dde15d1985f496ec34c3fffdb
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
1 package PVE::Storage::ZFSPoolPlugin;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7 use Net::IP;
8 use POSIX;
9
10 use PVE::ProcFSTools;
11 use PVE::RPCEnvironment;
12 use PVE::Storage::Plugin;
13 use PVE::Tools qw(run_command);
14
15 use base qw(PVE::Storage::Plugin);
16
17 sub type {
18 return 'zfspool';
19 }
20
21 sub plugindata {
22 return {
23 content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}],
24 format => [ { raw => 1, subvol => 1 } , 'raw' ],
25 };
26 }
27
28 sub properties {
29 return {
30 blocksize => {
31 description => "block size",
32 type => 'string',
33 },
34 sparse => {
35 description => "use sparse volumes",
36 type => 'boolean',
37 },
38 mountpoint => {
39 description => "mount point",
40 type => 'string', format => 'pve-storage-path',
41 },
42 };
43 }
44
45 sub options {
46 return {
47 pool => { fixed => 1 },
48 blocksize => { optional => 1 },
49 sparse => { optional => 1 },
50 nodes => { optional => 1 },
51 disable => { optional => 1 },
52 content => { optional => 1 },
53 bwlimit => { optional => 1 },
54 mountpoint => { optional => 1 },
55 };
56 }
57
58 # static zfs helper methods
59
60 sub zfs_parse_zvol_list {
61 my ($text, $pool) = @_;
62
63 my $list = ();
64
65 return $list if !$text;
66
67 my @lines = split /\n/, $text;
68 foreach my $line (@lines) {
69 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
70 next if !($type eq 'volume' || $type eq 'filesystem');
71
72 my $zvol = {};
73 my @parts = split /\//, $dataset;
74 next if scalar(@parts) < 2; # we need pool/name
75 my $name = pop @parts;
76 my $parsed_pool = join('/', @parts);
77 next if $parsed_pool ne $pool;
78
79 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
80 $zvol->{owner} = $2;
81
82 $zvol->{name} = $name;
83 if ($type eq 'filesystem') {
84 if ($refquota eq 'none') {
85 $zvol->{size} = 0;
86 } else {
87 $zvol->{size} = $refquota + 0;
88 }
89 $zvol->{format} = 'subvol';
90 } else {
91 $zvol->{size} = $size + 0;
92 $zvol->{format} = 'raw';
93 }
94 if ($origin !~ /^-$/) {
95 $zvol->{origin} = $origin;
96 }
97 push @$list, $zvol;
98 }
99
100 return $list;
101 }
102
103 sub parse_volname {
104 my ($class, $volname) = @_;
105
106 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) {
107 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw';
108 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
109 return ('images', $5, $7, $2, $4, $isBase, $format);
110 }
111
112 die "unable to parse zfs volume name '$volname'\n";
113 }
114
115 # virtual zfs methods (subclass can overwrite them)
116
117 sub on_add_hook {
118 my ($class, $storeid, $scfg, %param) = @_;
119
120 my $cfg_mountpoint = $scfg->{mountpoint};
121
122 # ignore failure, pool might currently not be imported
123 my $mountpoint;
124 eval {
125 my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool}, 1);
126 $mountpoint = PVE::Storage::Plugin::verify_path($res, 1) if defined($res);
127 };
128
129 if (defined($cfg_mountpoint)) {
130 if (defined($mountpoint) && !($cfg_mountpoint =~ m|^\Q$mountpoint\E/?$|)) {
131 warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
132 "does not match current mount point: $mountpoint\n";
133 }
134 } else {
135 $scfg->{mountpoint} = $mountpoint;
136 }
137
138 return;
139 }
140
141 sub path {
142 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
143
144 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
145
146 my $path = '';
147 my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}";
148
149 if ($vtype eq "images") {
150 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
151 $path = "$mountpoint/$name";
152 } else {
153 $path = "/dev/zvol/$scfg->{pool}/$name";
154 }
155 $path .= "\@$snapname" if defined($snapname);
156 } else {
157 die "$vtype is not allowed in ZFSPool!";
158 }
159
160 return ($path, $vmid, $vtype);
161 }
162
163 sub zfs_request {
164 my ($class, $scfg, $timeout, $method, @params) = @_;
165
166 my $cmd = [];
167
168 if ($method eq 'zpool_list') {
169 push @$cmd, 'zpool', 'list';
170 } elsif ($method eq 'zpool_import') {
171 push @$cmd, 'zpool', 'import';
172 $timeout = 15 if !$timeout || $timeout < 15;
173 } else {
174 push @$cmd, 'zfs', $method;
175 }
176 push @$cmd, @params;
177
178 my $msg = '';
179 my $output = sub { $msg .= "$_[0]\n" };
180
181 if (PVE::RPCEnvironment->is_worker()) {
182 $timeout = 60*60 if !$timeout;
183 $timeout = 60*5 if $timeout < 60*5;
184 } else {
185 $timeout = 10 if !$timeout;
186 }
187
188 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
189
190 return $msg;
191 }
192
193 sub zfs_wait_for_zvol_link {
194 my ($class, $scfg, $volname, $timeout) = @_;
195
196 my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10;
197 $timeout = $default_timeout if !defined($timeout);
198
199 my ($devname, undef, undef) = $class->path($scfg, $volname);
200
201 for (my $i = 1; $i <= $timeout; $i++) {
202 last if -b $devname;
203 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
204 if $i == $timeout;
205
206 sleep(1);
207 }
208 }
209
210 sub alloc_image {
211 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
212
213 my $volname = $name;
214
215 if ($fmt eq 'raw') {
216
217 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
218 if $volname && $volname !~ m/^vm-$vmid-/;
219 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
220 if !$volname;
221
222 $class->zfs_create_zvol($scfg, $volname, $size);
223 $class->zfs_wait_for_zvol_link($scfg, $volname);
224
225 } elsif ( $fmt eq 'subvol') {
226
227 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
228 if $volname && $volname !~ m/^subvol-$vmid-/;
229 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
230 if !$volname;
231
232 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
233 if $volname !~ m/^subvol-$vmid-/;
234
235 $class->zfs_create_subvol($scfg, $volname, $size);
236
237 } else {
238 die "unsupported format '$fmt'";
239 }
240
241 return $volname;
242 }
243
244 sub free_image {
245 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
246
247 my (undef, $name, undef) = $class->parse_volname($volname);
248
249 $class->zfs_delete_zvol($scfg, $name);
250
251 return undef;
252 }
253
254 sub list_images {
255 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
256
257 my $zfs_list = $class->zfs_list_zvol($scfg);
258
259 my $res = [];
260
261 for my $info (values $zfs_list->%*) {
262 my $volname = $info->{name};
263 my $parent = $info->{parent};
264 my $owner = $info->{vmid};
265
266 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
267 my ($basename) = ($1);
268 $info->{volid} = "$storeid:$basename/$volname";
269 } else {
270 $info->{volid} = "$storeid:$volname";
271 }
272
273 if ($vollist) {
274 my $found = grep { $_ eq $info->{volid} } @$vollist;
275 next if !$found;
276 } else {
277 next if defined ($vmid) && ($owner ne $vmid);
278 }
279
280 push @$res, $info;
281 }
282 return $res;
283 }
284
285 sub zfs_get_properties {
286 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
287
288 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
289 '-Hp', $properties, $dataset);
290 my @values = split /\n/, $result;
291 return wantarray ? @values : $values[0];
292 }
293
294 sub zfs_get_pool_stats {
295 my ($class, $scfg) = @_;
296
297 my $available = 0;
298 my $used = 0;
299
300 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool});
301
302 if($lines[0] =~ /^(\d+)$/) {
303 $available = $1;
304 }
305
306 if($lines[1] =~ /^(\d+)$/) {
307 $used = $1;
308 }
309
310 return ($available, $used);
311 }
312
313 sub zfs_create_zvol {
314 my ($class, $scfg, $zvol, $size) = @_;
315
316 # always align size to 1M as workaround until
317 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
318 my $padding = (1024 - $size % 1024) % 1024;
319 $size = $size + $padding;
320
321 my $cmd = ['create'];
322
323 push @$cmd, '-s' if $scfg->{sparse};
324
325 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
326
327 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
328
329 $class->zfs_request($scfg, undef, @$cmd);
330 }
331
332 sub zfs_create_subvol {
333 my ($class, $scfg, $volname, $size) = @_;
334
335 my $dataset = "$scfg->{pool}/$volname";
336 my $quota = $size ? "${size}k" : "none";
337
338 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
339 '-o', "refquota=${quota}", $dataset];
340
341 $class->zfs_request($scfg, undef, @$cmd);
342 }
343
344 sub zfs_delete_zvol {
345 my ($class, $scfg, $zvol) = @_;
346
347 my $err;
348
349 for (my $i = 0; $i < 6; $i++) {
350
351 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
352 if ($err = $@) {
353 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
354 sleep(1);
355 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
356 $err = undef;
357 last;
358 } else {
359 die $err;
360 }
361 } else {
362 last;
363 }
364 }
365
366 die $err if $err;
367 }
368
369 sub zfs_list_zvol {
370 my ($class, $scfg) = @_;
371
372 my $text = $class->zfs_request(
373 $scfg,
374 10,
375 'list',
376 '-o',
377 'name,volsize,origin,type,refquota',
378 '-t',
379 'volume,filesystem',
380 '-d1',
381 '-Hp',
382 $scfg->{pool},
383 );
384 # It's still required to have zfs_parse_zvol_list filter by pool, because -d1 lists
385 # $scfg->{pool} too and while unlikely, it could be named to be mistaken for a volume.
386 my $zvols = zfs_parse_zvol_list($text, $scfg->{pool});
387 return {} if !$zvols;
388
389 my $list = {};
390 foreach my $zvol (@$zvols) {
391 my $name = $zvol->{name};
392 my $parent = $zvol->{origin};
393 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
394 $parent = $1;
395 }
396
397 $list->{$name} = {
398 name => $name,
399 size => $zvol->{size},
400 parent => $parent,
401 format => $zvol->{format},
402 vmid => $zvol->{owner},
403 };
404 }
405
406 return $list;
407 }
408
409 sub zfs_get_sorted_snapshot_list {
410 my ($class, $scfg, $volname, $sort_params) = @_;
411
412 my @params = ('-H', '-r', '-t', 'snapshot', '-o', 'name', $sort_params->@*);
413
414 my $vname = ($class->parse_volname($volname))[1];
415 push @params, "$scfg->{pool}\/$vname";
416
417 my $text = $class->zfs_request($scfg, undef, 'list', @params);
418 my @snapshots = split(/\n/, $text);
419
420 my $snap_names = [];
421 for my $snapshot (@snapshots) {
422 (my $snap_name = $snapshot) =~ s/^.*@//;
423 push $snap_names->@*, $snap_name;
424 }
425 return $snap_names;
426 }
427
428 sub status {
429 my ($class, $storeid, $scfg, $cache) = @_;
430
431 my $total = 0;
432 my $free = 0;
433 my $used = 0;
434 my $active = 0;
435
436 eval {
437 ($free, $used) = $class->zfs_get_pool_stats($scfg);
438 $active = 1;
439 $total = $free + $used;
440 };
441 warn $@ if $@;
442
443 return ($total, $free, $used, $active);
444 }
445
446 sub volume_size_info {
447 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
448
449 my (undef, $vname, undef, $parent, undef, undef, $format) =
450 $class->parse_volname($volname);
451
452 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
453 my ($size, $used) = $class->zfs_get_properties($scfg, "$attr,usedbydataset", "$scfg->{pool}/$vname");
454
455 $used = ($used =~ /^(\d+)$/) ? $1 : 0;
456
457 if ($size =~ /^(\d+)$/) {
458 return wantarray ? ($1, $format, $used, $parent) : $1;
459 }
460
461 die "Could not get zfs volume size\n";
462 }
463
464 sub volume_snapshot {
465 my ($class, $scfg, $storeid, $volname, $snap) = @_;
466
467 my $vname = ($class->parse_volname($volname))[1];
468
469 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
470 }
471
472 sub volume_snapshot_delete {
473 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
474
475 my $vname = ($class->parse_volname($volname))[1];
476
477 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
478 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
479 }
480
481 sub volume_snapshot_rollback {
482 my ($class, $scfg, $storeid, $volname, $snap) = @_;
483
484 my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
485
486 my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
487
488 # we have to unmount rollbacked subvols, to invalidate wrong kernel
489 # caches, they get mounted in activate volume again
490 # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931
491 if ($format eq 'subvol') {
492 eval { $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); };
493 if (my $err = $@) {
494 die $err if $err !~ m/not currently mounted$/;
495 }
496 }
497
498 return $msg;
499 }
500
501 sub volume_rollback_is_possible {
502 my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_;
503
504 # can't use '-S creation', because zfs list won't reverse the order when the
505 # creation time is the same second, breaking at least our tests.
506 my $snapshots = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-s', 'creation']);
507
508 my $found;
509 $blockers //= []; # not guaranteed to be set by caller
510 for my $snapshot ($snapshots->@*) {
511 if ($snapshot eq $snap) {
512 $found = 1;
513 } elsif ($found) {
514 push $blockers->@*, $snapshot;
515 }
516 }
517
518 my $volid = "${storeid}:${volname}";
519
520 die "can't rollback, snapshot '$snap' does not exist on '$volid'\n"
521 if !$found;
522
523 die "can't rollback, '$snap' is not most recent snapshot on '$volid'\n"
524 if scalar($blockers->@*) > 0;
525
526 return 1;
527 }
528
529 sub volume_snapshot_info {
530 my ($class, $scfg, $storeid, $volname) = @_;
531
532 my @params = ('-Hp', '-r', '-t', 'snapshot', '-o', 'name,guid,creation');
533
534 my $vname = ($class->parse_volname($volname))[1];
535 push @params, "$scfg->{pool}\/$vname";
536
537 my $text = $class->zfs_request($scfg, undef, 'list', @params);
538 my @lines = split(/\n/, $text);
539
540 my $info = {};
541 for my $line (@lines) {
542 my ($snapshot, $guid, $creation) = split(/\s+/, $line);
543 (my $snap_name = $snapshot) =~ s/^.*@//;
544
545 $info->{$snap_name} = {
546 id => $guid,
547 timestamp => $creation,
548 };
549 }
550 return $info;
551 }
552
553 my sub dataset_mounted_heuristic {
554 my ($dataset) = @_;
555
556 my $mounts = PVE::ProcFSTools::parse_proc_mounts();
557 for my $mp (@$mounts) {
558 my ($what, $dir, $fs) = $mp->@*;
559 next if $fs ne 'zfs';
560 # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off')
561 # If any child is mounted heuristically assume that `zfs mount -a` was successful
562 next if $what !~ m!^$dataset(?:/|$)!;
563 return 1;
564 }
565 return 0;
566 }
567
568 sub activate_storage {
569 my ($class, $storeid, $scfg, $cache) = @_;
570
571 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
572 my $dataset = $scfg->{pool};
573 my $pool = ($dataset =~ s!/.*$!!r);
574
575 return 1 if dataset_mounted_heuristic($dataset); # early return
576
577 my $pool_imported = sub {
578 my @param = ('-o', 'name', '-H', $pool);
579 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
580 warn "$@\n" if $@;
581
582 return defined($res) && $res =~ m/$pool/;
583 };
584
585 if (!$pool_imported->()) {
586 # import can only be done if not yet imported!
587 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool);
588 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
589 if (my $err = $@) {
590 # just could've raced with another import, so recheck if it is imported
591 die "could not activate storage '$storeid', $err\n" if !$pool_imported->();
592 }
593 }
594 eval { $class->zfs_request($scfg, undef, 'mount', '-a') };
595 die "could not activate storage '$storeid', $@\n" if $@;
596 return 1;
597 }
598
599 sub deactivate_storage {
600 my ($class, $storeid, $scfg, $cache) = @_;
601 return 1;
602 }
603
604 sub activate_volume {
605 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
606
607 return 1 if defined($snapname);
608
609 my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
610
611 if ($format eq 'raw') {
612 $class->zfs_wait_for_zvol_link($scfg, $volname);
613 } elsif ($format eq 'subvol') {
614 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset");
615 if ($mounted !~ m/^yes$/) {
616 $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset");
617 }
618 }
619
620 return 1;
621 }
622
623 sub deactivate_volume {
624 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
625 return 1;
626 }
627
628 sub clone_image {
629 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
630
631 $snap ||= '__base__';
632
633 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
634 $class->parse_volname($volname);
635
636 die "clone_image only works on base images\n" if !$isBase;
637
638 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
639
640 if ($format eq 'subvol') {
641 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
642 chomp($size);
643 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
644 } else {
645 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
646 }
647
648 return "$basename/$name";
649 }
650
651 sub create_base {
652 my ($class, $storeid, $scfg, $volname) = @_;
653
654 my $snap = '__base__';
655
656 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
657 $class->parse_volname($volname);
658
659 die "create_base not possible with base image\n" if $isBase;
660
661 my $newname = $name;
662 if ( $format eq 'subvol' ) {
663 $newname =~ s/^subvol-/basevol-/;
664 } else {
665 $newname =~ s/^vm-/base-/;
666 }
667 my $newvolname = $basename ? "$basename/$newname" : "$newname";
668
669 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
670
671 my $running = undef; #fixme : is create_base always offline ?
672
673 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
674
675 return $newvolname;
676 }
677
678 sub volume_resize {
679 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
680
681 my $new_size = int($size/1024);
682
683 my (undef, $vname, undef, undef, undef, undef, $format) =
684 $class->parse_volname($volname);
685
686 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
687
688 # align size to 1M so we always have a valid multiple of the volume block size
689 if ($format eq 'raw') {
690 my $padding = (1024 - $new_size % 1024) % 1024;
691 $new_size = $new_size + $padding;
692 }
693
694 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
695
696 return $new_size;
697 }
698
699 sub storage_can_replicate {
700 my ($class, $scfg, $storeid, $format) = @_;
701
702 return 1 if $format eq 'raw' || $format eq 'subvol';
703
704 return 0;
705 }
706
707 sub volume_has_feature {
708 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
709
710 my $features = {
711 snapshot => { current => 1, snap => 1},
712 clone => { base => 1},
713 template => { current => 1},
714 copy => { base => 1, current => 1},
715 sparseinit => { base => 1, current => 1},
716 replicate => { base => 1, current => 1},
717 rename => {current => 1},
718 };
719
720 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
721 $class->parse_volname($volname);
722
723 my $key = undef;
724
725 if ($snapname) {
726 $key = 'snap';
727 } else {
728 $key = $isBase ? 'base' : 'current';
729 }
730
731 return 1 if $features->{$feature}->{$key};
732
733 return undef;
734 }
735
736 sub volume_export {
737 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
738
739 die "unsupported export stream format for $class: $format\n"
740 if $format ne 'zfs';
741
742 die "$class storage can only export snapshots\n"
743 if !defined($snapshot);
744
745 my $dataset = ($class->parse_volname($volname))[1];
746
747 my $fd = fileno($fh);
748 die "internal error: invalid file handle for volume_export\n"
749 if !defined($fd);
750 $fd = ">&$fd";
751
752 # For zfs we always create a replication stream (-R) which means the remote
753 # side will always delete non-existing source snapshots. This should work
754 # for all our use cases.
755 my $cmd = ['zfs', 'send', '-Rpv'];
756 if (defined($base_snapshot)) {
757 my $arg = $with_snapshots ? '-I' : '-i';
758 push @$cmd, $arg, $base_snapshot;
759 }
760 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
761
762 run_command($cmd, output => $fd);
763
764 return;
765 }
766
767 sub volume_export_formats {
768 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
769
770 my @formats = ('zfs');
771 # TODOs:
772 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
773 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
774 return @formats;
775 }
776
777 sub volume_import {
778 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
779
780 die "unsupported import stream format for $class: $format\n"
781 if $format ne 'zfs';
782
783 my $fd = fileno($fh);
784 die "internal error: invalid file handle for volume_import\n"
785 if !defined($fd);
786
787 my (undef, $dataset, $vmid, undef, undef, undef, $volume_format) =
788 $class->parse_volname($volname);
789
790 my $zfspath = "$scfg->{pool}/$dataset";
791 my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
792 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
793 noerr => 1, quiet => 1);
794 if (defined($base_snapshot)) {
795 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
796 } elsif ($exists) {
797 die "volume '$zfspath' already exists\n" if !$allow_rename;
798 warn "volume '$zfspath' already exists - importing with a different name\n";
799 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format);
800 $zfspath = "$scfg->{pool}/$dataset";
801 }
802
803 eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
804 if (my $err = $@) {
805 if (defined($base_snapshot)) {
806 eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
807 } else {
808 eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
809 }
810 die $err;
811 }
812
813 return "$storeid:$dataset";
814 }
815
816 sub volume_import_formats {
817 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
818
819 return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
820 }
821
822 sub rename_volume {
823 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
824
825 my (
826 undef,
827 $source_image,
828 $source_vmid,
829 $base_name,
830 $base_vmid,
831 undef,
832 $format
833 ) = $class->parse_volname($source_volname);
834 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
835 if !$target_volname;
836
837 my $pool = $scfg->{pool};
838 my $source_zfspath = "${pool}/${source_image}";
839 my $target_zfspath = "${pool}/${target_volname}";
840
841 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $target_zfspath],
842 noerr => 1, quiet => 1);
843 die "target volume '${target_volname}' already exists\n" if $exists;
844
845 $class->zfs_request($scfg, 5, 'rename', ${source_zfspath}, ${target_zfspath});
846
847 $base_name = $base_name ? "${base_name}/" : '';
848
849 return "${storeid}:${base_name}${target_volname}";
850 }
851
852 1;