]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
Revert "Include pve-replica cronjob."
[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 },
35 };
36}
37
5bb8e010
DM
38sub options {
39 return {
7730694e
DM
40 pool => { fixed => 1 },
41 blocksize => { optional => 1 },
42 sparse => { optional => 1 },
43 nodes => { optional => 1 },
5bb8e010 44 disable => { optional => 1 },
5bb8e010
DM
45 content => { optional => 1 },
46 };
47}
48
7730694e
DM
49# static zfs helper methods
50
060ef890
DM
51sub zfs_parse_size {
52 my ($text) = @_;
53
54 return 0 if !$text;
55
56 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
57
58 my ($size, $reminder, $unit) = ($1, $2, $3);
59
60 if ($unit) {
61 if ($unit eq 'K') {
62 $size *= 1024;
63 } elsif ($unit eq 'M') {
64 $size *= 1024*1024;
65 } elsif ($unit eq 'G') {
66 $size *= 1024*1024*1024;
67 } elsif ($unit eq 'T') {
68 $size *= 1024*1024*1024*1024;
69 } else {
70 die "got unknown zfs size unit '$unit'\n";
71 }
72 }
73
74 if ($reminder) {
75 $size = ceil($size);
76 }
77
78 return $size;
79
80 }
81
82 warn "unable to parse zfs size '$text'\n";
83
84 return 0;
85}
86
7730694e
DM
87sub zfs_parse_zvol_list {
88 my ($text) = @_;
89
90 my $list = ();
91
92 return $list if !$text;
93
94 my @lines = split /\n/, $text;
95 foreach my $line (@lines) {
1ccae449
DM
96 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
97 next if !($type eq 'volume' || $type eq 'filesystem');
98
99 my $zvol = {};
100 my @parts = split /\//, $dataset;
dec97937 101 next if scalar(@parts) < 2; # we need pool/name
1ccae449
DM
102 my $name = pop @parts;
103 my $pool = join('/', @parts);
104
851658c3 105 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
1ccae449
DM
106 $zvol->{owner} = $2;
107
1ccae449
DM
108 $zvol->{pool} = $pool;
109 $zvol->{name} = $name;
110 if ($type eq 'filesystem') {
111 if ($refquota eq 'none') {
112 $zvol->{size} = 0;
113 } else {
114 $zvol->{size} = zfs_parse_size($refquota);
7730694e 115 }
1ccae449
DM
116 $zvol->{format} = 'subvol';
117 } else {
118 $zvol->{size} = zfs_parse_size($size);
119 $zvol->{format} = 'raw';
7730694e 120 }
1ccae449
DM
121 if ($origin !~ /^-$/) {
122 $zvol->{origin} = $origin;
123 }
124 push @$list, $zvol;
7730694e
DM
125 }
126
127 return $list;
128}
129
cc80ed9c
WL
130sub parse_volname {
131 my ($class, $volname) = @_;
132
8e5b96ca
DM
133 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?((base|basevol|vm|subvol)-(\d+)-\S+)$/) {
134 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ? 'subvol' : 'raw';
135 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
136 return ('images', $5, $7, $2, $4, $isBase, $format);
cc80ed9c
WL
137 }
138
139 die "unable to parse zfs volume name '$volname'\n";
140}
141
7730694e
DM
142# virtual zfs methods (subclass can overwrite them)
143
f3e632d0 144sub path {
e67069eb 145 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f3e632d0
WL
146
147 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
148
149 my $path = '';
150
1ccae449 151 if ($vtype eq "images") {
74b724a6 152 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
f482231e 153 # fixme: we currently assume standard mount point?!
851658c3 154 $path = "/$scfg->{pool}/$name";
1ccae449 155 } else {
851658c3 156 $path = "/dev/zvol/$scfg->{pool}/$name";
1ccae449 157 }
f482231e 158 $path .= "\@$snapname" if defined($snapname);
f3e632d0 159 } else {
85fda4dd 160 die "$vtype is not allowed in ZFSPool!";
f3e632d0
WL
161 }
162
163 return ($path, $vmid, $vtype);
164}
165
7730694e 166sub zfs_request {
c4bb4a3d 167 my ($class, $scfg, $ip, $timeout, $method, @params) = @_;
7730694e 168
ef881e10 169 my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5;
7730694e
DM
170
171 my $cmd = [];
172
173 if ($method eq 'zpool_list') {
86d47239 174 push @$cmd, 'zpool', 'list';
e2e63801
FG
175 } elsif ($method eq 'zpool_import') {
176 push @$cmd, 'zpool', 'import';
72bdeea1 177 $default_timeout = 15 if $default_timeout < 15;
7730694e
DM
178 } else {
179 push @$cmd, 'zfs', $method;
180 }
181
182 push @$cmd, @params;
4bd0b38f
WL
183 if ($ip) {
184 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
185 unshift @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
186 }
7730694e
DM
187 my $msg = '';
188
189 my $output = sub {
190 my $line = shift;
191 $msg .= "$line\n";
192 };
193
72bdeea1
FG
194 $timeout = $default_timeout if !$timeout;
195
1f390a30 196 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
7730694e
DM
197
198 return $msg;
199}
200
b3ba95e4
WL
201sub alloc_image {
202 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
203
1ccae449
DM
204 my $volname = $name;
205
206 if ($fmt eq 'raw') {
b3ba95e4 207
1ccae449
DM
208 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
209 if $volname && $volname !~ m/^vm-$vmid-/;
55525ad2 210 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
1ccae449 211 if !$volname;
b3ba95e4 212
1ccae449
DM
213 $class->zfs_create_zvol($scfg, $volname, $size);
214 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
82e08809 215
1ccae449 216 run_command("udevadm trigger --subsystem-match block");
602eacfe 217 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
76fd7dc7 218
1ccae449 219 } elsif ( $fmt eq 'subvol') {
55525ad2
DM
220
221 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
222 if $volname && $volname !~ m/^subvol-$vmid-/;
223 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
224 if !$volname;
225
1ccae449
DM
226 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
227 if $volname !~ m/^subvol-$vmid-/;
76fd7dc7 228
1ccae449
DM
229 $class->zfs_create_subvol($scfg, $volname, $size);
230
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
7730694e
DM
285sub zfs_get_pool_stats {
286 my ($class, $scfg) = @_;
287
288 my $available = 0;
289 my $used = 0;
290
c4bb4a3d 291 my $text = $class->zfs_request($scfg, undef, undef, 'get', '-o', 'value', '-Hp',
7730694e
DM
292 'available,used', $scfg->{pool});
293
294 my @lines = split /\n/, $text;
295
296 if($lines[0] =~ /^(\d+)$/) {
297 $available = $1;
298 }
299
300 if($lines[1] =~ /^(\d+)$/) {
301 $used = $1;
302 }
303
304 return ($available, $used);
305}
306
7730694e
DM
307sub zfs_create_zvol {
308 my ($class, $scfg, $zvol, $size) = @_;
309
310 my $cmd = ['create'];
311
312 push @$cmd, '-s' if $scfg->{sparse};
313
314 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
315
316 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
317
c4bb4a3d 318 $class->zfs_request($scfg, undef, undef, @$cmd);
7730694e
DM
319}
320
1ccae449
DM
321sub zfs_create_subvol {
322 my ($class, $scfg, $volname, $size) = @_;
323
324 my $dataset = "$scfg->{pool}/$volname";
325
efaf4017
DM
326 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
327 '-o', "refquota=${size}k", $dataset];
1ccae449 328
c4bb4a3d 329 $class->zfs_request($scfg, undef, undef, @$cmd);
1ccae449
DM
330}
331
7730694e
DM
332sub zfs_delete_zvol {
333 my ($class, $scfg, $zvol) = @_;
334
1f390a30
WL
335 my $err;
336
337 for (my $i = 0; $i < 6; $i++) {
338
c4bb4a3d 339 eval { $class->zfs_request($scfg, undef, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
1f390a30
WL
340 if ($err = $@) {
341 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
342 sleep(1);
27ff0e99
WL
343 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
344 $err = undef;
345 last;
1f390a30
WL
346 } else {
347 die $err;
348 }
349 } else {
350 last;
351 }
352 }
353
354 die $err if $err;
7730694e
DM
355}
356
357sub zfs_list_zvol {
358 my ($class, $scfg) = @_;
359
c4bb4a3d 360 my $text = $class->zfs_request($scfg, undef, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
7730694e
DM
361 my $zvols = zfs_parse_zvol_list($text);
362 return undef if !$zvols;
363
364 my $list = ();
365 foreach my $zvol (@$zvols) {
1ccae449
DM
366 my $pool = $zvol->{pool};
367 my $name = $zvol->{name};
7730694e
DM
368 my $parent = $zvol->{origin};
369 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
370 $parent = $1;
371 }
372
1ccae449
DM
373 $list->{$pool}->{$name} = {
374 name => $name,
7730694e
DM
375 size => $zvol->{size},
376 parent => $parent,
1ccae449
DM
377 format => $zvol->{format},
378 vmid => $zvol->{owner},
7730694e
DM
379 };
380 }
381
382 return $list;
383}
384
385sub zfs_find_free_diskname {
55525ad2 386 my ($class, $storeid, $scfg, $vmid, $format) = @_;
7730694e
DM
387
388 my $name = undef;
389 my $volumes = $class->zfs_list_zvol($scfg);
390
391 my $disk_ids = {};
392 my $dat = $volumes->{$scfg->{pool}};
393
394 foreach my $image (keys %$dat) {
395 my $volname = $dat->{$image}->{name};
851658c3 396 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
7730694e
DM
397 $disk_ids->{$2} = 1;
398 }
399 }
400
401 for (my $i = 1; $i < 100; $i++) {
402 if (!$disk_ids->{$i}) {
55525ad2 403 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
7730694e
DM
404 }
405 }
406
407 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
408}
409
2fc59177
DM
410sub zfs_get_latest_snapshot {
411 my ($class, $scfg, $volname) = @_;
412
851658c3
WL
413 my $vname = ($class->parse_volname($volname))[1];
414
2fc59177
DM
415 # abort rollback if snapshot is not the latest
416 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
c4bb4a3d 417 my $text = $class->zfs_request($scfg, undef, undef, 'list', @params);
2fc59177
DM
418 my @snapshots = split(/\n/, $text);
419
420 my $recentsnap;
421 foreach (@snapshots) {
851658c3 422 if (/$scfg->{pool}\/$vname/) {
2fc59177
DM
423 s/^.*@//;
424 $recentsnap = $_;
425 }
426 }
427
428 return $recentsnap;
429}
430
b5e5f7e3
DM
431sub status {
432 my ($class, $storeid, $scfg, $cache) = @_;
433
434 my $total = 0;
435 my $free = 0;
436 my $used = 0;
437 my $active = 0;
438
439 eval {
440 ($free, $used) = $class->zfs_get_pool_stats($scfg);
441 $active = 1;
442 $total = $free + $used;
443 };
444 warn $@ if $@;
445
446 return ($total, $free, $used, $active);
447}
448
449sub volume_size_info {
450 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
451
851658c3 452 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
453 $class->parse_volname($volname);
454
455 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
c4bb4a3d 456 my $text = $class->zfs_request($scfg, undef, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
79f2b938
DM
457 if ($text =~ /\s$attr\s(\d+)\s/) {
458 return $1;
459 }
460
461 die "Could not get zfs volume size\n";
b5e5f7e3
DM
462}
463
464sub volume_snapshot {
f5640e7d 465 my ($class, $scfg, $storeid, $volname, $snap) = @_;
b5e5f7e3 466
851658c3
WL
467 my $vname = ($class->parse_volname($volname))[1];
468
c4bb4a3d 469 $class->zfs_request($scfg, undef, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
b5e5f7e3
DM
470}
471
b76774e5
WL
472sub volume_send {
473 my ($class, $scfg, $storeid, $volname, $ip, $snap,
474 $incremental_snap, $verbose, $limit, $target_path) = @_;
475
476 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
477
478 my $zpath = "$scfg->{pool}/$name";
479
480 die "$vtype is not allowed in ZFSPool!" if ($vtype ne "images");
481
482 my $cmdsend = [];
483 my $cmdlimit = [];
484
485 push @$cmdsend, 'zfs', 'send', '-R';
486 push @$cmdsend, '-v' if defined($verbose);
487
488 if( defined($incremental_snap)) {
489 push @$cmdsend, '-I', "$zpath\@${incremental_snap}";
490 }
491
492 push @$cmdsend, '--', "$zpath\@${snap}";
493
494 # limit in kByte/s
495 if ($limit){
496 my $bwl = $limit * 1024;
497 push @$cmdlimit, 'cstream', '-t', $bwl;
498 }
499
500 my $cmdrecv = [];
501
aefe82ea
WL
502 if ($ip) {
503 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
504 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
505 }
506
b76774e5
WL
507 push @$cmdrecv, 'zfs', 'recv', '-F', '--';
508
509 $zpath = $target_path if defined($target_path);
510 push @$cmdrecv, $zpath;
511
b76774e5
WL
512 if ($limit) {
513 eval { run_command([$cmdsend, $cmdlimit, $cmdrecv]) };
514 } else {
515 eval { run_command([$cmdsend, $cmdrecv]) };
516 }
517
518 if (my $err = $@) {
519 die $err;
520 }
521}
522
4bd0b38f
WL
523sub volume_snapshot_delete_remote {
524 my ($class, $scfg, $storeid, $volname, $snap, $ip) = @_;
525
526 my $vname = ($class->parse_volname($volname))[1];
527 $class->zfs_request($scfg, $ip, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
528}
529
b5e5f7e3
DM
530sub volume_snapshot_delete {
531 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
532
851658c3
WL
533 my $vname = ($class->parse_volname($volname))[1];
534
535 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
4bd0b38f 536
c4bb4a3d 537 $class->zfs_request($scfg, undef, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
b5e5f7e3
DM
538}
539
2b40ffae
WL
540sub volume_snapshot_rollback {
541 my ($class, $scfg, $storeid, $volname, $snap) = @_;
542
851658c3
WL
543 my $vname = ($class->parse_volname($volname))[1];
544
c4bb4a3d 545 $class->zfs_request($scfg, undef, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
1597f1f9
WL
546}
547
548sub volume_rollback_is_possible {
549 my ($class, $scfg, $storeid, $volname, $snap) = @_;
550
2fc59177 551 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
2b40ffae 552 if ($snap ne $recentsnap) {
1597f1f9 553 die "can't rollback, more recent snapshots exist\n";
2b40ffae
WL
554 }
555
1597f1f9 556 return 1;
2b40ffae
WL
557}
558
aefe82ea
WL
559sub volume_snapshot_list {
560 my ($class, $scfg, $storeid, $volname, $prefix, $ip) = @_;
561
562 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
563
564 my $zpath = "$scfg->{pool}/$name";
565
566 $prefix = '' if !defined($prefix);
567 my $snaps = [];
568
569 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
570 'name', $zpath];
571
572 if ($ip) {
573 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
574 unshift @$cmd, 'ssh', '-o', ' BatchMode=yes', "root\@${ip}", '--';
575 }
576
577 my $outfunc = sub {
578 my $line = shift;
579
580 if ($line =~ m/^\Q$zpath\E@(\Q$prefix\E.*)$/) {
581 push @$snaps, $1;
582 }
583 };
584
585 eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
586
587 # return an empty array if dataset does not exist.
588 return $snaps;
589}
590
0a3d992f
DM
591sub activate_storage {
592 my ($class, $storeid, $scfg, $cache) = @_;
86d47239 593
93124ef4
DM
594 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
595 my $pool = $scfg->{pool};
596 $pool =~ s!/.*$!!;
597
e2e63801
FG
598 my @param = ('-o', 'name', '-H', "$pool");
599 my $res;
600 eval {
c4bb4a3d 601 $res = $class->zfs_request($scfg, undef, undef, 'zpool_list', @param);
e2e63801
FG
602 };
603
604 if ($@ || !defined($res) || $res !~ $pool) {
605 eval {
606 @param = ('-d', '/dev/disk/by-id/', "$pool");
c4bb4a3d 607 $class->zfs_request($scfg, undef, undef, 'zpool_import', @param);
e2e63801
FG
608 };
609 die "could not activate storage '$storeid', $@\n" if $@;
86d47239 610 }
0a3d992f
DM
611 return 1;
612}
613
614sub deactivate_storage {
615 my ($class, $storeid, $scfg, $cache) = @_;
616 return 1;
617}
618
d4c63dc1 619sub activate_volume {
02e797b8 620 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
621 return 1;
622}
623
624sub deactivate_volume {
02e797b8 625 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
626 return 1;
627}
5bb8e010 628
d3a282e8
WL
629sub clone_image {
630 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
631
632 $snap ||= '__base__';
633
55525ad2 634 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
d3a282e8
WL
635 $class->parse_volname($volname);
636
637 die "clone_image only works on base images\n" if !$isBase;
638
55525ad2 639 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
d3a282e8 640
851658c3 641 if ($format eq 'subvol') {
c4bb4a3d 642 my $size = $class->zfs_request($scfg, undef, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
851658c3 643 chomp($size);
c4bb4a3d 644 $class->zfs_request($scfg, undef, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
851658c3 645 } else {
c4bb4a3d 646 $class->zfs_request($scfg, undef, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
851658c3 647 }
d3a282e8 648
851658c3 649 return "$basename/$name";
d3a282e8
WL
650}
651
652sub create_base {
653 my ($class, $storeid, $scfg, $volname) = @_;
654
655 my $snap = '__base__';
656
851658c3 657 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
d3a282e8
WL
658 $class->parse_volname($volname);
659
660 die "create_base not possible with base image\n" if $isBase;
661
662 my $newname = $name;
851658c3
WL
663 if ( $format eq 'subvol' ) {
664 $newname =~ s/^subvol-/basevol-/;
665 } else {
666 $newname =~ s/^vm-/base-/;
667 }
d3a282e8
WL
668 my $newvolname = $basename ? "$basename/$newname" : "$newname";
669
c4bb4a3d 670 $class->zfs_request($scfg, undef, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
d3a282e8
WL
671
672 my $running = undef; #fixme : is create_base always offline ?
673
674 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
675
676 return $newvolname;
677}
678
a4034b9f
WL
679sub volume_resize {
680 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
681
79f2b938
DM
682 my $new_size = int($size/1024);
683
851658c3 684 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
685 $class->parse_volname($volname);
686
687 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
a4034b9f 688
c4bb4a3d 689 $class->zfs_request($scfg, undef, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
a4034b9f
WL
690
691 return $new_size;
692}
693
2b40ffae
WL
694sub volume_has_feature {
695 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
696
697 my $features = {
698 snapshot => { current => 1, snap => 1},
699 clone => { base => 1},
700 template => { current => 1},
701 copy => { base => 1, current => 1},
baafddbd 702 sparseinit => { base => 1, current => 1},
f189504c 703 replicate => { base => 1, current => 1},
2b40ffae
WL
704 };
705
706 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
707 $class->parse_volname($volname);
708
709 my $key = undef;
710
711 if ($snapname) {
712 $key = 'snap';
713 } else {
714 $key = $isBase ? 'base' : 'current';
715 }
716
717 return 1 if $features->{$feature}->{$key};
718
719 return undef;
720}
721
5bb8e010 7221;