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