]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
ZFS: mount subvols in activate_volume
[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);
c8eb0178
FG
475
476 die "can't rollback, no snapshots exist at all\n"
477 if !defined($recentsnap);
478
d0eaf185
FG
479 die "can't rollback, '$snap' is not most recent snapshot\n"
480 if $snap ne $recentsnap;
2b40ffae 481
3824ba88 482 return 1;
2b40ffae
WL
483}
484
aefe82ea 485sub volume_snapshot_list {
8b622c2d 486 my ($class, $scfg, $storeid, $volname) = @_;
aefe82ea
WL
487
488 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
489
490 my $zpath = "$scfg->{pool}/$name";
491
aefe82ea
WL
492 my $snaps = [];
493
494 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
495 'name', $zpath];
496
aefe82ea
WL
497 my $outfunc = sub {
498 my $line = shift;
499
8b622c2d 500 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
aefe82ea
WL
501 push @$snaps, $1;
502 }
503 };
504
505 eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
506
507 # return an empty array if dataset does not exist.
508 return $snaps;
509}
510
0a3d992f
DM
511sub activate_storage {
512 my ($class, $storeid, $scfg, $cache) = @_;
86d47239 513
93124ef4
DM
514 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
515 my $pool = $scfg->{pool};
516 $pool =~ s!/.*$!!;
517
b5c8278a
TL
518 my $pool_imported = sub {
519 my @param = ('-o', 'name', '-H', "$pool");
520 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
dc18abe0 521 if ($@) {
b5c8278a
TL
522 warn "$@\n";
523 return undef;
524 }
525 return defined($res) && $res =~ m/$pool/;
526 };
527
528 if (!$pool_imported->()) {
529 # import can only be done if not yet imported!
530 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
531 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
532 if (my $err = $@) {
533 # just could've raced with another import, so recheck if it is imported
534 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
dc18abe0 535 }
86d47239 536 }
0a3d992f
DM
537 return 1;
538}
539
540sub deactivate_storage {
541 my ($class, $storeid, $scfg, $cache) = @_;
542 return 1;
543}
544
d4c63dc1 545sub activate_volume {
02e797b8 546 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
56362cfb
FG
547
548 return 1 if defined($snapname);
549
550 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
551
815df2dd
FE
552 if ($format eq 'raw') {
553 $class->zfs_wait_for_zvol_link($scfg, $volname);
554 } elsif ($format eq 'subvol') {
555 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$volname");
556 if ($mounted !~ m/^yes$/) {
557 $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$volname");
558 }
559 }
56362cfb 560
d4c63dc1
WL
561 return 1;
562}
563
564sub deactivate_volume {
02e797b8 565 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
566 return 1;
567}
5bb8e010 568
d3a282e8
WL
569sub clone_image {
570 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
571
572 $snap ||= '__base__';
573
55525ad2 574 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
d3a282e8
WL
575 $class->parse_volname($volname);
576
577 die "clone_image only works on base images\n" if !$isBase;
578
a44c0147 579 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
d3a282e8 580
851658c3 581 if ($format eq 'subvol') {
e05113fb 582 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
851658c3 583 chomp($size);
44257d2e 584 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
851658c3 585 } else {
44257d2e 586 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
851658c3 587 }
d3a282e8 588
851658c3 589 return "$basename/$name";
d3a282e8
WL
590}
591
592sub create_base {
593 my ($class, $storeid, $scfg, $volname) = @_;
594
595 my $snap = '__base__';
596
851658c3 597 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
d3a282e8
WL
598 $class->parse_volname($volname);
599
600 die "create_base not possible with base image\n" if $isBase;
601
602 my $newname = $name;
851658c3
WL
603 if ( $format eq 'subvol' ) {
604 $newname =~ s/^subvol-/basevol-/;
605 } else {
606 $newname =~ s/^vm-/base-/;
607 }
d3a282e8
WL
608 my $newvolname = $basename ? "$basename/$newname" : "$newname";
609
44257d2e 610 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
d3a282e8
WL
611
612 my $running = undef; #fixme : is create_base always offline ?
613
614 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
615
616 return $newvolname;
617}
618
a4034b9f
WL
619sub volume_resize {
620 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
621
79f2b938
DM
622 my $new_size = int($size/1024);
623
851658c3 624 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
625 $class->parse_volname($volname);
626
627 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
a4034b9f 628
d3e3e5d6
FE
629 # align size to 1M so we always have a valid multiple of the volume block size
630 if ($format eq 'raw') {
631 my $padding = (1024 - $new_size % 1024) % 1024;
632 $new_size = $new_size + $padding;
633 }
634
44257d2e 635 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
a4034b9f
WL
636
637 return $new_size;
638}
639
7118dd91
DM
640sub storage_can_replicate {
641 my ($class, $scfg, $storeid, $format) = @_;
642
643 return 1 if $format eq 'raw' || $format eq 'subvol';
644
645 return 0;
646}
647
2b40ffae
WL
648sub volume_has_feature {
649 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
650
651 my $features = {
652 snapshot => { current => 1, snap => 1},
653 clone => { base => 1},
654 template => { current => 1},
655 copy => { base => 1, current => 1},
baafddbd 656 sparseinit => { base => 1, current => 1},
f189504c 657 replicate => { base => 1, current => 1},
2b40ffae
WL
658 };
659
660 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
661 $class->parse_volname($volname);
662
663 my $key = undef;
664
665 if ($snapname) {
666 $key = 'snap';
667 } else {
668 $key = $isBase ? 'base' : 'current';
669 }
670
671 return 1 if $features->{$feature}->{$key};
672
673 return undef;
674}
675
47f37b53
WB
676sub volume_export {
677 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
678
679 die "unsupported export stream format for $class: $format\n"
680 if $format ne 'zfs';
681
682 die "$class storage can only export snapshots\n"
683 if !defined($snapshot);
684
9a75947b
WL
685 my $dataset = ($class->parse_volname($volname))[1];
686
47f37b53
WB
687 my $fd = fileno($fh);
688 die "internal error: invalid file handle for volume_export\n"
689 if !defined($fd);
690 $fd = ">&$fd";
691
692 # For zfs we always create a replication stream (-R) which means the remote
693 # side will always delete non-existing source snapshots. This should work
694 # for all our use cases.
695 my $cmd = ['zfs', 'send', '-Rpv'];
696 if (defined($base_snapshot)) {
697 my $arg = $with_snapshots ? '-I' : '-i';
698 push @$cmd, $arg, $base_snapshot;
699 }
9a75947b 700 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
47f37b53
WB
701
702 run_command($cmd, output => $fd);
703
704 return;
705}
706
d390328b
WB
707sub volume_export_formats {
708 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
709
710 my @formats = ('zfs');
711 # TODOs:
712 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
713 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
714 return @formats;
715}
716
47f37b53 717sub volume_import {
a97d3ee4 718 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
47f37b53
WB
719
720 die "unsupported import stream format for $class: $format\n"
721 if $format ne 'zfs';
722
723 my $fd = fileno($fh);
724 die "internal error: invalid file handle for volume_import\n"
725 if !defined($fd);
726
a97d3ee4 727 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
9a75947b 728 my $zfspath = "$scfg->{pool}/$dataset";
47f37b53
WB
729 my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
730 my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
731 noerr => 1, errfunc => sub {});
732 if (defined($base_snapshot)) {
733 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
a97d3ee4
FE
734 } elsif ($exists) {
735 die "volume '$zfspath' already exists\n" if !$allow_rename;
736 warn "volume '$zfspath' already exists - importing with a different name\n";
737 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
738 $zfspath = "$scfg->{pool}/$dataset";
47f37b53
WB
739 }
740
741 eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
742 if (my $err = $@) {
743 if (defined($base_snapshot)) {
744 eval { run_command(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
745 } else {
746 eval { run_command(['zfs', 'destroy', '-r', '--', $zfspath]) };
747 }
748 die $err;
749 }
750
a97d3ee4 751 return "$storeid:$dataset";
47f37b53
WB
752}
753
d390328b
WB
754sub volume_import_formats {
755 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
756
757 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
758}
759
5bb8e010 7601;