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