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