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