]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
ZFSPoolPlugin: fix #2662 get volume size correctly
[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
060ef890
DM
57sub zfs_parse_size {
58 my ($text) = @_;
59
60 return 0 if !$text;
3824ba88 61
060ef890
DM
62 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
63
64 my ($size, $reminder, $unit) = ($1, $2, $3);
3824ba88 65
060ef890
DM
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 }
3824ba88 83
d99de0f8 84 return $size + 0;
3824ba88 85
060ef890
DM
86 }
87
88 warn "unable to parse zfs size '$text'\n";
89
90 return 0;
91}
92
7730694e
DM
93sub 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) {
1ccae449
DM
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;
dec97937 107 next if scalar(@parts) < 2; # we need pool/name
1ccae449
DM
108 my $name = pop @parts;
109 my $pool = join('/', @parts);
110
851658c3 111 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
1ccae449
DM
112 $zvol->{owner} = $2;
113
1ccae449
DM
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);
7730694e 121 }
1ccae449
DM
122 $zvol->{format} = 'subvol';
123 } else {
124 $zvol->{size} = zfs_parse_size($size);
125 $zvol->{format} = 'raw';
7730694e 126 }
1ccae449
DM
127 if ($origin !~ /^-$/) {
128 $zvol->{origin} = $origin;
129 }
130 push @$list, $zvol;
7730694e
DM
131 }
132
133 return $list;
134}
135
cc80ed9c
WL
136sub parse_volname {
137 my ($class, $volname) = @_;
138
8e5b96ca
DM
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);
cc80ed9c
WL
143 }
144
145 die "unable to parse zfs volume name '$volname'\n";
146}
147
7730694e
DM
148# virtual zfs methods (subclass can overwrite them)
149
dcefd9dd
FE
150sub on_add_hook {
151 my ($class, $storeid, $scfg, %param) = @_;
152
153 my $cfg_mountpoint = $scfg->{mountpoint};
dcefd9dd
FE
154
155 # ignore failure, pool might currently not be imported
75815bf5
FE
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);
dcefd9dd
FE
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
f3e632d0 172sub path {
e67069eb 173 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f3e632d0
WL
174
175 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
176
177 my $path = '';
dcefd9dd 178 my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}";
f3e632d0 179
1ccae449 180 if ($vtype eq "images") {
74b724a6 181 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
dcefd9dd 182 $path = "$mountpoint/$name";
1ccae449 183 } else {
851658c3 184 $path = "/dev/zvol/$scfg->{pool}/$name";
1ccae449 185 }
f482231e 186 $path .= "\@$snapname" if defined($snapname);
f3e632d0 187 } else {
85fda4dd 188 die "$vtype is not allowed in ZFSPool!";
f3e632d0
WL
189 }
190
191 return ($path, $vmid, $vtype);
192}
193
7730694e 194sub zfs_request {
44257d2e 195 my ($class, $scfg, $timeout, $method, @params) = @_;
7730694e 196
7730694e
DM
197 my $cmd = [];
198
199 if ($method eq 'zpool_list') {
86d47239 200 push @$cmd, 'zpool', 'list';
e2e63801
FG
201 } elsif ($method eq 'zpool_import') {
202 push @$cmd, 'zpool', 'import';
e9ab8ea3 203 $timeout = 15 if !$timeout || $timeout < 15;
7730694e
DM
204 } else {
205 push @$cmd, 'zfs', $method;
206 }
7730694e 207 push @$cmd, @params;
7730694e 208
a10695b4
TL
209 my $msg = '';
210 my $output = sub { $msg .= "$_[0]\n" };
7730694e 211
a10695b4 212 $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout;
72bdeea1 213
1f390a30 214 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
7730694e
DM
215
216 return $msg;
217}
218
56362cfb
FG
219sub 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
b3ba95e4
WL
236sub alloc_image {
237 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
238
1ccae449 239 my $volname = $name;
3824ba88 240
1ccae449 241 if ($fmt eq 'raw') {
b3ba95e4 242
fc05c9a0 243 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
1ccae449 244 if $volname && $volname !~ m/^vm-$vmid-/;
a44c0147 245 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
1ccae449 246 if !$volname;
b3ba95e4 247
1ccae449 248 $class->zfs_create_zvol($scfg, $volname, $size);
56362cfb 249 $class->zfs_wait_for_zvol_link($scfg, $volname);
82e08809 250
1ccae449 251 } elsif ( $fmt eq 'subvol') {
55525ad2 252
fc05c9a0 253 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
55525ad2 254 if $volname && $volname !~ m/^subvol-$vmid-/;
a44c0147 255 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
55525ad2
DM
256 if !$volname;
257
fc05c9a0 258 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
1ccae449 259 if $volname !~ m/^subvol-$vmid-/;
76fd7dc7 260
3824ba88
FE
261 $class->zfs_create_subvol($scfg, $volname, $size);
262
1ccae449
DM
263 } else {
264 die "unsupported format '$fmt'";
265 }
b3ba95e4 266
82e08809 267 return $volname;
b3ba95e4
WL
268}
269
e9565df5
WL
270sub 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
ca04180f
WL
280sub 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
1b83c3d9 291 my $info = $dat->{$image};
ca04180f 292
1b83c3d9
FG
293 my $volname = $info->{name};
294 my $parent = $info->{parent};
295 my $owner = $info->{vmid};
296
297 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
ca04180f 298 my ($basename) = ($1);
1b83c3d9 299 $info->{volid} = "$storeid:$basename/$volname";
ca04180f 300 } else {
1b83c3d9 301 $info->{volid} = "$storeid:$volname";
ca04180f
WL
302 }
303
ca04180f 304 if ($vollist) {
1b83c3d9 305 my $found = grep { $_ eq $info->{volid} } @$vollist;
ca04180f
WL
306 next if !$found;
307 } else {
308 next if defined ($vmid) && ($owner ne $vmid);
309 }
310
ca04180f
WL
311 push @$res, $info;
312 }
313 }
ca04180f
WL
314 return $res;
315}
316
4966c886
FE
317sub 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
7730694e
DM
326sub zfs_get_pool_stats {
327 my ($class, $scfg) = @_;
328
329 my $available = 0;
330 my $used = 0;
331
4966c886 332 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool});
7730694e
DM
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
7730694e
DM
345sub zfs_create_zvol {
346 my ($class, $scfg, $zvol, $size) = @_;
cdef3abb
ML
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
7730694e
DM
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
44257d2e 361 $class->zfs_request($scfg, undef, @$cmd);
7730694e
DM
362}
363
1ccae449
DM
364sub zfs_create_subvol {
365 my ($class, $scfg, $volname, $size) = @_;
366
367 my $dataset = "$scfg->{pool}/$volname";
3824ba88 368
efaf4017
DM
369 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
370 '-o', "refquota=${size}k", $dataset];
1ccae449 371
44257d2e 372 $class->zfs_request($scfg, undef, @$cmd);
1ccae449
DM
373}
374
7730694e
DM
375sub zfs_delete_zvol {
376 my ($class, $scfg, $zvol) = @_;
377
1f390a30
WL
378 my $err;
379
380 for (my $i = 0; $i < 6; $i++) {
381
44257d2e 382 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
1f390a30
WL
383 if ($err = $@) {
384 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
385 sleep(1);
27ff0e99
WL
386 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
387 $err = undef;
388 last;
1f390a30
WL
389 } else {
390 die $err;
391 }
392 } else {
393 last;
394 }
395 }
396
397 die $err if $err;
7730694e
DM
398}
399
400sub zfs_list_zvol {
401 my ($class, $scfg) = @_;
402
d99de0f8 403 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
7730694e
DM
404 my $zvols = zfs_parse_zvol_list($text);
405 return undef if !$zvols;
406
407 my $list = ();
408 foreach my $zvol (@$zvols) {
1ccae449
DM
409 my $pool = $zvol->{pool};
410 my $name = $zvol->{name};
7730694e
DM
411 my $parent = $zvol->{origin};
412 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
413 $parent = $1;
414 }
415
1ccae449
DM
416 $list->{$pool}->{$name} = {
417 name => $name,
7730694e
DM
418 size => $zvol->{size},
419 parent => $parent,
1ccae449
DM
420 format => $zvol->{format},
421 vmid => $zvol->{owner},
7730694e
DM
422 };
423 }
424
425 return $list;
426}
427
2fc59177
DM
428sub zfs_get_latest_snapshot {
429 my ($class, $scfg, $volname) = @_;
430
851658c3
WL
431 my $vname = ($class->parse_volname($volname))[1];
432
2fc59177
DM
433 # abort rollback if snapshot is not the latest
434 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
44257d2e 435 my $text = $class->zfs_request($scfg, undef, 'list', @params);
2fc59177
DM
436 my @snapshots = split(/\n/, $text);
437
438 my $recentsnap;
439 foreach (@snapshots) {
851658c3 440 if (/$scfg->{pool}\/$vname/) {
2fc59177
DM
441 s/^.*@//;
442 $recentsnap = $_;
443 }
444 }
445
446 return $recentsnap;
447}
448
b5e5f7e3
DM
449sub 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
467sub volume_size_info {
468 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
469
851658c3 470 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
471 $class->parse_volname($volname);
472
473 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
4966c886
FE
474 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
475 if ($value =~ /^(\d+)$/) {
79f2b938
DM
476 return $1;
477 }
478
479 die "Could not get zfs volume size\n";
b5e5f7e3
DM
480}
481
482sub volume_snapshot {
f5640e7d 483 my ($class, $scfg, $storeid, $volname, $snap) = @_;
b5e5f7e3 484
851658c3
WL
485 my $vname = ($class->parse_volname($volname))[1];
486
44257d2e 487 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
b5e5f7e3
DM
488}
489
490sub volume_snapshot_delete {
491 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
492
851658c3
WL
493 my $vname = ($class->parse_volname($volname))[1];
494
495 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
44257d2e 496 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
b5e5f7e3
DM
497}
498
2b40ffae
WL
499sub volume_snapshot_rollback {
500 my ($class, $scfg, $storeid, $volname, $snap) = @_;
501
851658c3
WL
502 my $vname = ($class->parse_volname($volname))[1];
503
44257d2e 504 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
1597f1f9
WL
505}
506
507sub volume_rollback_is_possible {
3824ba88
FE
508 my ($class, $scfg, $storeid, $volname, $snap) = @_;
509
2fc59177 510 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
2b40ffae 511 if ($snap ne $recentsnap) {
1597f1f9 512 die "can't rollback, more recent snapshots exist\n";
2b40ffae
WL
513 }
514
3824ba88 515 return 1;
2b40ffae
WL
516}
517
aefe82ea 518sub volume_snapshot_list {
8b622c2d 519 my ($class, $scfg, $storeid, $volname) = @_;
aefe82ea
WL
520
521 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
522
523 my $zpath = "$scfg->{pool}/$name";
524
aefe82ea
WL
525 my $snaps = [];
526
527 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
528 'name', $zpath];
529
aefe82ea
WL
530 my $outfunc = sub {
531 my $line = shift;
532
8b622c2d 533 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
aefe82ea
WL
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
0a3d992f
DM
544sub activate_storage {
545 my ($class, $storeid, $scfg, $cache) = @_;
86d47239 546
93124ef4
DM
547 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
548 my $pool = $scfg->{pool};
549 $pool =~ s!/.*$!!;
550
b5c8278a
TL
551 my $pool_imported = sub {
552 my @param = ('-o', 'name', '-H', "$pool");
553 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
dc18abe0 554 if ($@) {
b5c8278a
TL
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->();
dc18abe0 568 }
86d47239 569 }
0a3d992f
DM
570 return 1;
571}
572
573sub deactivate_storage {
574 my ($class, $storeid, $scfg, $cache) = @_;
575 return 1;
576}
577
d4c63dc1 578sub activate_volume {
02e797b8 579 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
56362cfb
FG
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
d4c63dc1
WL
589 return 1;
590}
591
592sub deactivate_volume {
02e797b8 593 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
594 return 1;
595}
5bb8e010 596
d3a282e8
WL
597sub clone_image {
598 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
599
600 $snap ||= '__base__';
601
55525ad2 602 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
d3a282e8
WL
603 $class->parse_volname($volname);
604
605 die "clone_image only works on base images\n" if !$isBase;
606
a44c0147 607 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
d3a282e8 608
851658c3 609 if ($format eq 'subvol') {
44257d2e 610 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
851658c3 611 chomp($size);
44257d2e 612 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
851658c3 613 } else {
44257d2e 614 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
851658c3 615 }
d3a282e8 616
851658c3 617 return "$basename/$name";
d3a282e8
WL
618}
619
620sub create_base {
621 my ($class, $storeid, $scfg, $volname) = @_;
622
623 my $snap = '__base__';
624
851658c3 625 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
d3a282e8
WL
626 $class->parse_volname($volname);
627
628 die "create_base not possible with base image\n" if $isBase;
629
630 my $newname = $name;
851658c3
WL
631 if ( $format eq 'subvol' ) {
632 $newname =~ s/^subvol-/basevol-/;
633 } else {
634 $newname =~ s/^vm-/base-/;
635 }
d3a282e8
WL
636 my $newvolname = $basename ? "$basename/$newname" : "$newname";
637
44257d2e 638 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
d3a282e8
WL
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
a4034b9f
WL
647sub volume_resize {
648 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
649
79f2b938
DM
650 my $new_size = int($size/1024);
651
851658c3 652 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
653 $class->parse_volname($volname);
654
655 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
a4034b9f 656
d3e3e5d6
FE
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
44257d2e 663 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
a4034b9f
WL
664
665 return $new_size;
666}
667
7118dd91
DM
668sub 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
2b40ffae
WL
676sub 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},
baafddbd 684 sparseinit => { base => 1, current => 1},
f189504c 685 replicate => { base => 1, current => 1},
2b40ffae
WL
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
47f37b53
WB
704sub 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
9a75947b
WL
713 my $dataset = ($class->parse_volname($volname))[1];
714
47f37b53
WB
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 }
9a75947b 728 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
47f37b53
WB
729
730 run_command($cmd, output => $fd);
731
732 return;
733}
734
d390328b
WB
735sub 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
47f37b53 745sub volume_import {
a97d3ee4 746 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
47f37b53
WB
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
a97d3ee4 755 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
9a75947b 756 my $zfspath = "$scfg->{pool}/$dataset";
47f37b53
WB
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;
a97d3ee4
FE
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";
47f37b53
WB
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
a97d3ee4 779 return "$storeid:$dataset";
47f37b53
WB
780}
781
d390328b
WB
782sub 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
5bb8e010 7881;