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