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