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