]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
Include new storage function volume_snapshot_list.
[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
DM
166sub zfs_request {
167 my ($class, $scfg, $timeout, $method, @params) = @_;
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;
183
184 my $msg = '';
185
186 my $output = sub {
187 my $line = shift;
188 $msg .= "$line\n";
189 };
190
72bdeea1
FG
191 $timeout = $default_timeout if !$timeout;
192
1f390a30 193 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
7730694e
DM
194
195 return $msg;
196}
197
b3ba95e4
WL
198sub alloc_image {
199 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
200
1ccae449
DM
201 my $volname = $name;
202
203 if ($fmt eq 'raw') {
b3ba95e4 204
1ccae449
DM
205 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
206 if $volname && $volname !~ m/^vm-$vmid-/;
55525ad2 207 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
1ccae449 208 if !$volname;
b3ba95e4 209
1ccae449
DM
210 $class->zfs_create_zvol($scfg, $volname, $size);
211 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
82e08809 212
1ccae449 213 run_command("udevadm trigger --subsystem-match block");
602eacfe 214 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
76fd7dc7 215
1ccae449 216 } elsif ( $fmt eq 'subvol') {
55525ad2
DM
217
218 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
219 if $volname && $volname !~ m/^subvol-$vmid-/;
220 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
221 if !$volname;
222
1ccae449
DM
223 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
224 if $volname !~ m/^subvol-$vmid-/;
76fd7dc7 225
1ccae449
DM
226 $class->zfs_create_subvol($scfg, $volname, $size);
227
228 } else {
229 die "unsupported format '$fmt'";
230 }
b3ba95e4 231
82e08809 232 return $volname;
b3ba95e4
WL
233}
234
e9565df5
WL
235sub free_image {
236 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
237
238 my (undef, $name, undef) = $class->parse_volname($volname);
239
240 $class->zfs_delete_zvol($scfg, $name);
241
242 return undef;
243}
244
ca04180f
WL
245sub list_images {
246 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
247
248 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
249 my $zfspool = $scfg->{pool};
250 my $res = [];
251
252 if (my $dat = $cache->{zfs}->{$zfspool}) {
253
254 foreach my $image (keys %$dat) {
255
1b83c3d9 256 my $info = $dat->{$image};
ca04180f 257
1b83c3d9
FG
258 my $volname = $info->{name};
259 my $parent = $info->{parent};
260 my $owner = $info->{vmid};
261
262 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
ca04180f 263 my ($basename) = ($1);
1b83c3d9 264 $info->{volid} = "$storeid:$basename/$volname";
ca04180f 265 } else {
1b83c3d9 266 $info->{volid} = "$storeid:$volname";
ca04180f
WL
267 }
268
ca04180f 269 if ($vollist) {
1b83c3d9 270 my $found = grep { $_ eq $info->{volid} } @$vollist;
ca04180f
WL
271 next if !$found;
272 } else {
273 next if defined ($vmid) && ($owner ne $vmid);
274 }
275
ca04180f
WL
276 push @$res, $info;
277 }
278 }
ca04180f
WL
279 return $res;
280}
281
7730694e
DM
282sub zfs_get_pool_stats {
283 my ($class, $scfg) = @_;
284
285 my $available = 0;
286 my $used = 0;
287
288 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
289 'available,used', $scfg->{pool});
290
291 my @lines = split /\n/, $text;
292
293 if($lines[0] =~ /^(\d+)$/) {
294 $available = $1;
295 }
296
297 if($lines[1] =~ /^(\d+)$/) {
298 $used = $1;
299 }
300
301 return ($available, $used);
302}
303
7730694e
DM
304sub zfs_create_zvol {
305 my ($class, $scfg, $zvol, $size) = @_;
306
307 my $cmd = ['create'];
308
309 push @$cmd, '-s' if $scfg->{sparse};
310
311 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
312
313 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
314
315 $class->zfs_request($scfg, undef, @$cmd);
316}
317
1ccae449
DM
318sub zfs_create_subvol {
319 my ($class, $scfg, $volname, $size) = @_;
320
321 my $dataset = "$scfg->{pool}/$volname";
322
efaf4017
DM
323 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
324 '-o', "refquota=${size}k", $dataset];
1ccae449
DM
325
326 $class->zfs_request($scfg, undef, @$cmd);
327}
328
7730694e
DM
329sub zfs_delete_zvol {
330 my ($class, $scfg, $zvol) = @_;
331
1f390a30
WL
332 my $err;
333
334 for (my $i = 0; $i < 6; $i++) {
335
336 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
337 if ($err = $@) {
338 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
339 sleep(1);
27ff0e99
WL
340 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
341 $err = undef;
342 last;
1f390a30
WL
343 } else {
344 die $err;
345 }
346 } else {
347 last;
348 }
349 }
350
351 die $err if $err;
7730694e
DM
352}
353
354sub zfs_list_zvol {
355 my ($class, $scfg) = @_;
356
1ccae449 357 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
7730694e
DM
358 my $zvols = zfs_parse_zvol_list($text);
359 return undef if !$zvols;
360
361 my $list = ();
362 foreach my $zvol (@$zvols) {
1ccae449
DM
363 my $pool = $zvol->{pool};
364 my $name = $zvol->{name};
7730694e
DM
365 my $parent = $zvol->{origin};
366 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
367 $parent = $1;
368 }
369
1ccae449
DM
370 $list->{$pool}->{$name} = {
371 name => $name,
7730694e
DM
372 size => $zvol->{size},
373 parent => $parent,
1ccae449
DM
374 format => $zvol->{format},
375 vmid => $zvol->{owner},
7730694e
DM
376 };
377 }
378
379 return $list;
380}
381
382sub zfs_find_free_diskname {
55525ad2 383 my ($class, $storeid, $scfg, $vmid, $format) = @_;
7730694e
DM
384
385 my $name = undef;
386 my $volumes = $class->zfs_list_zvol($scfg);
387
388 my $disk_ids = {};
389 my $dat = $volumes->{$scfg->{pool}};
390
391 foreach my $image (keys %$dat) {
392 my $volname = $dat->{$image}->{name};
851658c3 393 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
7730694e
DM
394 $disk_ids->{$2} = 1;
395 }
396 }
397
398 for (my $i = 1; $i < 100; $i++) {
399 if (!$disk_ids->{$i}) {
55525ad2 400 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
7730694e
DM
401 }
402 }
403
404 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
405}
406
2fc59177
DM
407sub zfs_get_latest_snapshot {
408 my ($class, $scfg, $volname) = @_;
409
851658c3
WL
410 my $vname = ($class->parse_volname($volname))[1];
411
2fc59177
DM
412 # abort rollback if snapshot is not the latest
413 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
281f9587 414 my $text = $class->zfs_request($scfg, undef, 'list', @params);
2fc59177
DM
415 my @snapshots = split(/\n/, $text);
416
417 my $recentsnap;
418 foreach (@snapshots) {
851658c3 419 if (/$scfg->{pool}\/$vname/) {
2fc59177
DM
420 s/^.*@//;
421 $recentsnap = $_;
422 }
423 }
424
425 return $recentsnap;
426}
427
b5e5f7e3
DM
428sub status {
429 my ($class, $storeid, $scfg, $cache) = @_;
430
431 my $total = 0;
432 my $free = 0;
433 my $used = 0;
434 my $active = 0;
435
436 eval {
437 ($free, $used) = $class->zfs_get_pool_stats($scfg);
438 $active = 1;
439 $total = $free + $used;
440 };
441 warn $@ if $@;
442
443 return ($total, $free, $used, $active);
444}
445
446sub volume_size_info {
447 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
448
851658c3 449 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
450 $class->parse_volname($volname);
451
452 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
851658c3 453 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
79f2b938
DM
454 if ($text =~ /\s$attr\s(\d+)\s/) {
455 return $1;
456 }
457
458 die "Could not get zfs volume size\n";
b5e5f7e3
DM
459}
460
461sub volume_snapshot {
f5640e7d 462 my ($class, $scfg, $storeid, $volname, $snap) = @_;
b5e5f7e3 463
851658c3
WL
464 my $vname = ($class->parse_volname($volname))[1];
465
466 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
b5e5f7e3
DM
467}
468
b76774e5
WL
469sub volume_send {
470 my ($class, $scfg, $storeid, $volname, $ip, $snap,
471 $incremental_snap, $verbose, $limit, $target_path) = @_;
472
473 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
474
475 my $zpath = "$scfg->{pool}/$name";
476
477 die "$vtype is not allowed in ZFSPool!" if ($vtype ne "images");
478
479 my $cmdsend = [];
480 my $cmdlimit = [];
481
482 push @$cmdsend, 'zfs', 'send', '-R';
483 push @$cmdsend, '-v' if defined($verbose);
484
485 if( defined($incremental_snap)) {
486 push @$cmdsend, '-I', "$zpath\@${incremental_snap}";
487 }
488
489 push @$cmdsend, '--', "$zpath\@${snap}";
490
491 # limit in kByte/s
492 if ($limit){
493 my $bwl = $limit * 1024;
494 push @$cmdlimit, 'cstream', '-t', $bwl;
495 }
496
497 my $cmdrecv = [];
498
aefe82ea
WL
499 if ($ip) {
500 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
501 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
502 }
503
b76774e5
WL
504 push @$cmdrecv, 'zfs', 'recv', '-F', '--';
505
506 $zpath = $target_path if defined($target_path);
507 push @$cmdrecv, $zpath;
508
509
510 if ($limit) {
511 eval { run_command([$cmdsend, $cmdlimit, $cmdrecv]) };
512 } else {
513 eval { run_command([$cmdsend, $cmdrecv]) };
514 }
515
516 if (my $err = $@) {
517 die $err;
518 }
519}
520
b5e5f7e3
DM
521sub volume_snapshot_delete {
522 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
523
851658c3
WL
524 my $vname = ($class->parse_volname($volname))[1];
525
526 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
527 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
b5e5f7e3
DM
528}
529
2b40ffae
WL
530sub volume_snapshot_rollback {
531 my ($class, $scfg, $storeid, $volname, $snap) = @_;
532
851658c3
WL
533 my $vname = ($class->parse_volname($volname))[1];
534
535 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
1597f1f9
WL
536}
537
538sub volume_rollback_is_possible {
539 my ($class, $scfg, $storeid, $volname, $snap) = @_;
540
2fc59177 541 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
2b40ffae 542 if ($snap ne $recentsnap) {
1597f1f9 543 die "can't rollback, more recent snapshots exist\n";
2b40ffae
WL
544 }
545
1597f1f9 546 return 1;
2b40ffae
WL
547}
548
aefe82ea
WL
549sub volume_snapshot_list {
550 my ($class, $scfg, $storeid, $volname, $prefix, $ip) = @_;
551
552 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
553
554 my $zpath = "$scfg->{pool}/$name";
555
556 $prefix = '' if !defined($prefix);
557 my $snaps = [];
558
559 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
560 'name', $zpath];
561
562 if ($ip) {
563 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
564 unshift @$cmd, 'ssh', '-o', ' BatchMode=yes', "root\@${ip}", '--';
565 }
566
567 my $outfunc = sub {
568 my $line = shift;
569
570 if ($line =~ m/^\Q$zpath\E@(\Q$prefix\E.*)$/) {
571 push @$snaps, $1;
572 }
573 };
574
575 eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
576
577 # return an empty array if dataset does not exist.
578 return $snaps;
579}
580
0a3d992f
DM
581sub activate_storage {
582 my ($class, $storeid, $scfg, $cache) = @_;
86d47239 583
93124ef4
DM
584 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
585 my $pool = $scfg->{pool};
586 $pool =~ s!/.*$!!;
587
e2e63801
FG
588 my @param = ('-o', 'name', '-H', "$pool");
589 my $res;
590 eval {
591 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
592 };
593
594 if ($@ || !defined($res) || $res !~ $pool) {
595 eval {
596 @param = ('-d', '/dev/disk/by-id/', "$pool");
597 $class->zfs_request($scfg, undef, 'zpool_import', @param);
598 };
599 die "could not activate storage '$storeid', $@\n" if $@;
86d47239 600 }
0a3d992f
DM
601 return 1;
602}
603
604sub deactivate_storage {
605 my ($class, $storeid, $scfg, $cache) = @_;
606 return 1;
607}
608
d4c63dc1 609sub activate_volume {
02e797b8 610 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
611 return 1;
612}
613
614sub deactivate_volume {
02e797b8 615 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
616 return 1;
617}
5bb8e010 618
d3a282e8
WL
619sub clone_image {
620 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
621
622 $snap ||= '__base__';
623
55525ad2 624 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
d3a282e8
WL
625 $class->parse_volname($volname);
626
627 die "clone_image only works on base images\n" if !$isBase;
628
55525ad2 629 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
d3a282e8 630
851658c3
WL
631 if ($format eq 'subvol') {
632 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
633 chomp($size);
634 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
635 } else {
636 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
637 }
d3a282e8 638
851658c3 639 return "$basename/$name";
d3a282e8
WL
640}
641
642sub create_base {
643 my ($class, $storeid, $scfg, $volname) = @_;
644
645 my $snap = '__base__';
646
851658c3 647 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
d3a282e8
WL
648 $class->parse_volname($volname);
649
650 die "create_base not possible with base image\n" if $isBase;
651
652 my $newname = $name;
851658c3
WL
653 if ( $format eq 'subvol' ) {
654 $newname =~ s/^subvol-/basevol-/;
655 } else {
656 $newname =~ s/^vm-/base-/;
657 }
d3a282e8
WL
658 my $newvolname = $basename ? "$basename/$newname" : "$newname";
659
660 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
661
662 my $running = undef; #fixme : is create_base always offline ?
663
664 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
665
666 return $newvolname;
667}
668
a4034b9f
WL
669sub volume_resize {
670 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
671
79f2b938
DM
672 my $new_size = int($size/1024);
673
851658c3 674 my (undef, $vname, undef, undef, undef, undef, $format) =
79f2b938
DM
675 $class->parse_volname($volname);
676
677 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
a4034b9f 678
851658c3 679 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
a4034b9f
WL
680
681 return $new_size;
682}
683
2b40ffae
WL
684sub volume_has_feature {
685 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
686
687 my $features = {
688 snapshot => { current => 1, snap => 1},
689 clone => { base => 1},
690 template => { current => 1},
691 copy => { base => 1, current => 1},
baafddbd 692 sparseinit => { base => 1, current => 1},
2b40ffae
WL
693 };
694
695 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
696 $class->parse_volname($volname);
697
698 my $key = undef;
699
700 if ($snapname) {
701 $key = 'snap';
702 } else {
703 $key = $isBase ? 'base' : 'current';
704 }
705
706 return 1 if $features->{$feature}->{$key};
707
708 return undef;
709}
710
5bb8e010 7111;