]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
fixup error messages when getting file size info
[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
138 sub path {
139 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
140
141 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
142
143 my $path = '';
144 my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}";
145
146 if ($vtype eq "images") {
147 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
148 $path = "$mountpoint/$name";
149 } else {
150 $path = "/dev/zvol/$scfg->{pool}/$name";
151 }
152 $path .= "\@$snapname" if defined($snapname);
153 } else {
154 die "$vtype is not allowed in ZFSPool!";
155 }
156
157 return ($path, $vmid, $vtype);
158 }
159
160 sub zfs_request {
161 my ($class, $scfg, $timeout, $method, @params) = @_;
162
163 my $cmd = [];
164
165 if ($method eq 'zpool_list') {
166 push @$cmd, 'zpool', 'list';
167 } elsif ($method eq 'zpool_import') {
168 push @$cmd, 'zpool', 'import';
169 $timeout = 15 if !$timeout || $timeout < 15;
170 } else {
171 push @$cmd, 'zfs', $method;
172 }
173 push @$cmd, @params;
174
175 my $msg = '';
176 my $output = sub { $msg .= "$_[0]\n" };
177
178 $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout;
179
180 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
181
182 return $msg;
183 }
184
185 sub zfs_wait_for_zvol_link {
186 my ($class, $scfg, $volname, $timeout) = @_;
187
188 my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10;
189 $timeout = $default_timeout if !defined($timeout);
190
191 my ($devname, undef, undef) = $class->path($scfg, $volname);
192
193 for (my $i = 1; $i <= $timeout; $i++) {
194 last if -b $devname;
195 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
196 if $i == $timeout;
197
198 sleep(1);
199 }
200 }
201
202 sub alloc_image {
203 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
204
205 my $volname = $name;
206
207 if ($fmt eq 'raw') {
208
209 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
210 if $volname && $volname !~ m/^vm-$vmid-/;
211 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
212 if !$volname;
213
214 $class->zfs_create_zvol($scfg, $volname, $size);
215 $class->zfs_wait_for_zvol_link($scfg, $volname);
216
217 } elsif ( $fmt eq 'subvol') {
218
219 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
220 if $volname && $volname !~ m/^subvol-$vmid-/;
221 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
222 if !$volname;
223
224 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
225 if $volname !~ m/^subvol-$vmid-/;
226
227 $class->zfs_create_subvol($scfg, $volname, $size);
228
229 } else {
230 die "unsupported format '$fmt'";
231 }
232
233 return $volname;
234 }
235
236 sub free_image {
237 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
238
239 my (undef, $name, undef) = $class->parse_volname($volname);
240
241 $class->zfs_delete_zvol($scfg, $name);
242
243 return undef;
244 }
245
246 sub list_images {
247 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
248
249 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
250 my $zfspool = $scfg->{pool};
251 my $res = [];
252
253 if (my $dat = $cache->{zfs}->{$zfspool}) {
254
255 foreach my $image (keys %$dat) {
256
257 my $info = $dat->{$image};
258
259 my $volname = $info->{name};
260 my $parent = $info->{parent};
261 my $owner = $info->{vmid};
262
263 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
264 my ($basename) = ($1);
265 $info->{volid} = "$storeid:$basename/$volname";
266 } else {
267 $info->{volid} = "$storeid:$volname";
268 }
269
270 if ($vollist) {
271 my $found = grep { $_ eq $info->{volid} } @$vollist;
272 next if !$found;
273 } else {
274 next if defined ($vmid) && ($owner ne $vmid);
275 }
276
277 push @$res, $info;
278 }
279 }
280 return $res;
281 }
282
283 sub zfs_get_properties {
284 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
285
286 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
287 '-Hp', $properties, $dataset);
288 my @values = split /\n/, $result;
289 return wantarray ? @values : $values[0];
290 }
291
292 sub zfs_get_pool_stats {
293 my ($class, $scfg) = @_;
294
295 my $available = 0;
296 my $used = 0;
297
298 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool});
299
300 if($lines[0] =~ /^(\d+)$/) {
301 $available = $1;
302 }
303
304 if($lines[1] =~ /^(\d+)$/) {
305 $used = $1;
306 }
307
308 return ($available, $used);
309 }
310
311 sub zfs_create_zvol {
312 my ($class, $scfg, $zvol, $size) = @_;
313
314 # always align size to 1M as workaround until
315 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
316 my $padding = (1024 - $size % 1024) % 1024;
317 $size = $size + $padding;
318
319 my $cmd = ['create'];
320
321 push @$cmd, '-s' if $scfg->{sparse};
322
323 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
324
325 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
326
327 $class->zfs_request($scfg, undef, @$cmd);
328 }
329
330 sub zfs_create_subvol {
331 my ($class, $scfg, $volname, $size) = @_;
332
333 my $dataset = "$scfg->{pool}/$volname";
334
335 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
336 '-o', "refquota=${size}k", $dataset];
337
338 $class->zfs_request($scfg, undef, @$cmd);
339 }
340
341 sub zfs_delete_zvol {
342 my ($class, $scfg, $zvol) = @_;
343
344 my $err;
345
346 for (my $i = 0; $i < 6; $i++) {
347
348 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
349 if ($err = $@) {
350 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
351 sleep(1);
352 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
353 $err = undef;
354 last;
355 } else {
356 die $err;
357 }
358 } else {
359 last;
360 }
361 }
362
363 die $err if $err;
364 }
365
366 sub zfs_list_zvol {
367 my ($class, $scfg) = @_;
368
369 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
370 my $zvols = zfs_parse_zvol_list($text);
371 return undef if !$zvols;
372
373 my $list = ();
374 foreach my $zvol (@$zvols) {
375 my $pool = $zvol->{pool};
376 my $name = $zvol->{name};
377 my $parent = $zvol->{origin};
378 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
379 $parent = $1;
380 }
381
382 $list->{$pool}->{$name} = {
383 name => $name,
384 size => $zvol->{size},
385 parent => $parent,
386 format => $zvol->{format},
387 vmid => $zvol->{owner},
388 };
389 }
390
391 return $list;
392 }
393
394 sub zfs_get_latest_snapshot {
395 my ($class, $scfg, $volname) = @_;
396
397 my $vname = ($class->parse_volname($volname))[1];
398
399 # abort rollback if snapshot is not the latest
400 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
401 my $text = $class->zfs_request($scfg, undef, 'list', @params);
402 my @snapshots = split(/\n/, $text);
403
404 my $recentsnap;
405 foreach (@snapshots) {
406 if (/$scfg->{pool}\/$vname/) {
407 s/^.*@//;
408 $recentsnap = $_;
409 }
410 }
411
412 return $recentsnap;
413 }
414
415 sub status {
416 my ($class, $storeid, $scfg, $cache) = @_;
417
418 my $total = 0;
419 my $free = 0;
420 my $used = 0;
421 my $active = 0;
422
423 eval {
424 ($free, $used) = $class->zfs_get_pool_stats($scfg);
425 $active = 1;
426 $total = $free + $used;
427 };
428 warn $@ if $@;
429
430 return ($total, $free, $used, $active);
431 }
432
433 sub volume_size_info {
434 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
435
436 my (undef, $vname, undef, undef, undef, undef, $format) =
437 $class->parse_volname($volname);
438
439 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
440 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
441 if ($value =~ /^(\d+)$/) {
442 return $1;
443 }
444
445 die "Could not get zfs volume size\n";
446 }
447
448 sub volume_snapshot {
449 my ($class, $scfg, $storeid, $volname, $snap) = @_;
450
451 my $vname = ($class->parse_volname($volname))[1];
452
453 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
454 }
455
456 sub volume_snapshot_delete {
457 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
458
459 my $vname = ($class->parse_volname($volname))[1];
460
461 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
462 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
463 }
464
465 sub volume_snapshot_rollback {
466 my ($class, $scfg, $storeid, $volname, $snap) = @_;
467
468 my $vname = ($class->parse_volname($volname))[1];
469
470 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
471 }
472
473 sub volume_rollback_is_possible {
474 my ($class, $scfg, $storeid, $volname, $snap) = @_;
475
476 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
477
478 die "can't rollback, no snapshots exist at all\n"
479 if !defined($recentsnap);
480
481 die "can't rollback, '$snap' is not most recent snapshot\n"
482 if $snap ne $recentsnap;
483
484 return 1;
485 }
486
487 sub volume_snapshot_list {
488 my ($class, $scfg, $storeid, $volname) = @_;
489
490 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
491
492 my $zpath = "$scfg->{pool}/$name";
493
494 my $snaps = [];
495
496 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
497 'name', $zpath];
498
499 my $outfunc = sub {
500 my $line = shift;
501
502 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
503 push @$snaps, $1;
504 }
505 };
506
507 eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
508
509 # return an empty array if dataset does not exist.
510 return $snaps;
511 }
512
513 sub activate_storage {
514 my ($class, $storeid, $scfg, $cache) = @_;
515
516 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
517 my $pool = $scfg->{pool};
518 $pool =~ s!/.*$!!;
519
520 my $pool_imported = sub {
521 my @param = ('-o', 'name', '-H', "$pool");
522 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
523 if ($@) {
524 warn "$@\n";
525 return undef;
526 }
527 return defined($res) && $res =~ m/$pool/;
528 };
529
530 if (!$pool_imported->()) {
531 # import can only be done if not yet imported!
532 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
533 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
534 if (my $err = $@) {
535 # just could've raced with another import, so recheck if it is imported
536 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
537 }
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
550 return 1 if defined($snapname);
551
552 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
553
554 if ($format eq 'raw') {
555 $class->zfs_wait_for_zvol_link($scfg, $volname);
556 } elsif ($format eq 'subvol') {
557 my ($path, undef, undef) = $class->path($scfg, $volname, $storeid);
558 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$path");
559 if ($mounted !~ m/^yes$/) {
560 $class->zfs_request($scfg, undef, 'mount', "$path");
561 }
562 }
563
564 return 1;
565 }
566
567 sub deactivate_volume {
568 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
569 return 1;
570 }
571
572 sub clone_image {
573 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
574
575 $snap ||= '__base__';
576
577 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
578 $class->parse_volname($volname);
579
580 die "clone_image only works on base images\n" if !$isBase;
581
582 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
583
584 if ($format eq 'subvol') {
585 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
586 chomp($size);
587 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
588 } else {
589 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
590 }
591
592 return "$basename/$name";
593 }
594
595 sub create_base {
596 my ($class, $storeid, $scfg, $volname) = @_;
597
598 my $snap = '__base__';
599
600 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
601 $class->parse_volname($volname);
602
603 die "create_base not possible with base image\n" if $isBase;
604
605 my $newname = $name;
606 if ( $format eq 'subvol' ) {
607 $newname =~ s/^subvol-/basevol-/;
608 } else {
609 $newname =~ s/^vm-/base-/;
610 }
611 my $newvolname = $basename ? "$basename/$newname" : "$newname";
612
613 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
614
615 my $running = undef; #fixme : is create_base always offline ?
616
617 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
618
619 return $newvolname;
620 }
621
622 sub volume_resize {
623 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
624
625 my $new_size = int($size/1024);
626
627 my (undef, $vname, undef, undef, undef, undef, $format) =
628 $class->parse_volname($volname);
629
630 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
631
632 # align size to 1M so we always have a valid multiple of the volume block size
633 if ($format eq 'raw') {
634 my $padding = (1024 - $new_size % 1024) % 1024;
635 $new_size = $new_size + $padding;
636 }
637
638 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
639
640 return $new_size;
641 }
642
643 sub storage_can_replicate {
644 my ($class, $scfg, $storeid, $format) = @_;
645
646 return 1 if $format eq 'raw' || $format eq 'subvol';
647
648 return 0;
649 }
650
651 sub volume_has_feature {
652 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
653
654 my $features = {
655 snapshot => { current => 1, snap => 1},
656 clone => { base => 1},
657 template => { current => 1},
658 copy => { base => 1, current => 1},
659 sparseinit => { base => 1, current => 1},
660 replicate => { base => 1, current => 1},
661 };
662
663 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
664 $class->parse_volname($volname);
665
666 my $key = undef;
667
668 if ($snapname) {
669 $key = 'snap';
670 } else {
671 $key = $isBase ? 'base' : 'current';
672 }
673
674 return 1 if $features->{$feature}->{$key};
675
676 return undef;
677 }
678
679 sub volume_export {
680 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
681
682 die "unsupported export stream format for $class: $format\n"
683 if $format ne 'zfs';
684
685 die "$class storage can only export snapshots\n"
686 if !defined($snapshot);
687
688 my $dataset = ($class->parse_volname($volname))[1];
689
690 my $fd = fileno($fh);
691 die "internal error: invalid file handle for volume_export\n"
692 if !defined($fd);
693 $fd = ">&$fd";
694
695 # For zfs we always create a replication stream (-R) which means the remote
696 # side will always delete non-existing source snapshots. This should work
697 # for all our use cases.
698 my $cmd = ['zfs', 'send', '-Rpv'];
699 if (defined($base_snapshot)) {
700 my $arg = $with_snapshots ? '-I' : '-i';
701 push @$cmd, $arg, $base_snapshot;
702 }
703 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
704
705 run_command($cmd, output => $fd);
706
707 return;
708 }
709
710 sub volume_export_formats {
711 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
712
713 my @formats = ('zfs');
714 # TODOs:
715 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
716 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
717 return @formats;
718 }
719
720 sub volume_import {
721 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
722
723 die "unsupported import stream format for $class: $format\n"
724 if $format ne 'zfs';
725
726 my $fd = fileno($fh);
727 die "internal error: invalid file handle for volume_import\n"
728 if !defined($fd);
729
730 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
731 my $zfspath = "$scfg->{pool}/$dataset";
732 my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
733 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
734 noerr => 1, errfunc => sub {});
735 if (defined($base_snapshot)) {
736 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
737 } elsif ($exists) {
738 die "volume '$zfspath' already exists\n" if !$allow_rename;
739 warn "volume '$zfspath' already exists - importing with a different name\n";
740 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
741 $zfspath = "$scfg->{pool}/$dataset";
742 }
743
744 eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
745 if (my $err = $@) {
746 if (defined($base_snapshot)) {
747 eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
748 } else {
749 eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
750 }
751 die $err;
752 }
753
754 return "$storeid:$dataset";
755 }
756
757 sub volume_import_formats {
758 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
759
760 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
761 }
762
763 1;