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