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