]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
Add function volume_snapshot_delete_remote.
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
1 package PVE::Storage::ZFSPoolPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use POSIX;
7 use PVE::Tools qw(run_command);
8 use PVE::Storage::Plugin;
9 use PVE::RPCEnvironment;
10 use Net::IP;
11
12 use base qw(PVE::Storage::Plugin);
13
14 sub type {
15 return 'zfspool';
16 }
17
18 sub plugindata {
19 return {
20 content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}],
21 format => [ { raw => 1, subvol => 1 } , 'raw' ],
22 };
23 }
24
25 sub 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
38 sub options {
39 return {
40 pool => { fixed => 1 },
41 blocksize => { optional => 1 },
42 sparse => { optional => 1 },
43 nodes => { optional => 1 },
44 disable => { optional => 1 },
45 content => { optional => 1 },
46 };
47 }
48
49 # static zfs helper methods
50
51 sub 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
87 sub 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) {
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;
101 next if scalar(@parts) < 2; # we need pool/name
102 my $name = pop @parts;
103 my $pool = join('/', @parts);
104
105 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
106 $zvol->{owner} = $2;
107
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);
115 }
116 $zvol->{format} = 'subvol';
117 } else {
118 $zvol->{size} = zfs_parse_size($size);
119 $zvol->{format} = 'raw';
120 }
121 if ($origin !~ /^-$/) {
122 $zvol->{origin} = $origin;
123 }
124 push @$list, $zvol;
125 }
126
127 return $list;
128 }
129
130 sub parse_volname {
131 my ($class, $volname) = @_;
132
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);
137 }
138
139 die "unable to parse zfs volume name '$volname'\n";
140 }
141
142 # virtual zfs methods (subclass can overwrite them)
143
144 sub path {
145 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
146
147 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
148
149 my $path = '';
150
151 if ($vtype eq "images") {
152 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
153 # fixme: we currently assume standard mount point?!
154 $path = "/$scfg->{pool}/$name";
155 } else {
156 $path = "/dev/zvol/$scfg->{pool}/$name";
157 }
158 $path .= "\@$snapname" if defined($snapname);
159 } else {
160 die "$vtype is not allowed in ZFSPool!";
161 }
162
163 return ($path, $vmid, $vtype);
164 }
165
166 sub zfs_request {
167 my ($class, $scfg, $ip, $timeout, $method, @params) = @_;
168
169 my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5;
170
171 my $cmd = [];
172
173 if ($method eq 'zpool_list') {
174 push @$cmd, 'zpool', 'list';
175 } elsif ($method eq 'zpool_import') {
176 push @$cmd, 'zpool', 'import';
177 $default_timeout = 15 if $default_timeout < 15;
178 } else {
179 push @$cmd, 'zfs', $method;
180 }
181
182 push @$cmd, @params;
183 if ($ip) {
184 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
185 unshift @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
186 }
187 my $msg = '';
188
189 my $output = sub {
190 my $line = shift;
191 $msg .= "$line\n";
192 };
193
194 $timeout = $default_timeout if !$timeout;
195
196 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
197
198 return $msg;
199 }
200
201 sub alloc_image {
202 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
203
204 my $volname = $name;
205
206 if ($fmt eq 'raw') {
207
208 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
209 if $volname && $volname !~ m/^vm-$vmid-/;
210 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
211 if !$volname;
212
213 $class->zfs_create_zvol($scfg, $volname, $size);
214 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
215
216 run_command("udevadm trigger --subsystem-match block");
217 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
218
219 } elsif ( $fmt eq 'subvol') {
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
226 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
227 if $volname !~ m/^subvol-$vmid-/;
228
229 $class->zfs_create_subvol($scfg, $volname, $size);
230
231 } else {
232 die "unsupported format '$fmt'";
233 }
234
235 return $volname;
236 }
237
238 sub 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
248 sub 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
259 my $info = $dat->{$image};
260
261 my $volname = $info->{name};
262 my $parent = $info->{parent};
263 my $owner = $info->{vmid};
264
265 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
266 my ($basename) = ($1);
267 $info->{volid} = "$storeid:$basename/$volname";
268 } else {
269 $info->{volid} = "$storeid:$volname";
270 }
271
272 if ($vollist) {
273 my $found = grep { $_ eq $info->{volid} } @$vollist;
274 next if !$found;
275 } else {
276 next if defined ($vmid) && ($owner ne $vmid);
277 }
278
279 push @$res, $info;
280 }
281 }
282 return $res;
283 }
284
285 sub zfs_get_pool_stats {
286 my ($class, $scfg) = @_;
287
288 my $available = 0;
289 my $used = 0;
290
291 my $text = $class->zfs_request($scfg, undef, undef, 'get', '-o', 'value', '-Hp',
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
307 sub 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
318 $class->zfs_request($scfg, undef, undef, @$cmd);
319 }
320
321 sub zfs_create_subvol {
322 my ($class, $scfg, $volname, $size) = @_;
323
324 my $dataset = "$scfg->{pool}/$volname";
325
326 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
327 '-o', "refquota=${size}k", $dataset];
328
329 $class->zfs_request($scfg, undef, undef, @$cmd);
330 }
331
332 sub zfs_delete_zvol {
333 my ($class, $scfg, $zvol) = @_;
334
335 my $err;
336
337 for (my $i = 0; $i < 6; $i++) {
338
339 eval { $class->zfs_request($scfg, undef, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
340 if ($err = $@) {
341 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
342 sleep(1);
343 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
344 $err = undef;
345 last;
346 } else {
347 die $err;
348 }
349 } else {
350 last;
351 }
352 }
353
354 die $err if $err;
355 }
356
357 sub zfs_list_zvol {
358 my ($class, $scfg) = @_;
359
360 my $text = $class->zfs_request($scfg, undef, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
361 my $zvols = zfs_parse_zvol_list($text);
362 return undef if !$zvols;
363
364 my $list = ();
365 foreach my $zvol (@$zvols) {
366 my $pool = $zvol->{pool};
367 my $name = $zvol->{name};
368 my $parent = $zvol->{origin};
369 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
370 $parent = $1;
371 }
372
373 $list->{$pool}->{$name} = {
374 name => $name,
375 size => $zvol->{size},
376 parent => $parent,
377 format => $zvol->{format},
378 vmid => $zvol->{owner},
379 };
380 }
381
382 return $list;
383 }
384
385 sub zfs_find_free_diskname {
386 my ($class, $storeid, $scfg, $vmid, $format) = @_;
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};
396 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
397 $disk_ids->{$2} = 1;
398 }
399 }
400
401 for (my $i = 1; $i < 100; $i++) {
402 if (!$disk_ids->{$i}) {
403 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
404 }
405 }
406
407 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
408 }
409
410 sub zfs_get_latest_snapshot {
411 my ($class, $scfg, $volname) = @_;
412
413 my $vname = ($class->parse_volname($volname))[1];
414
415 # abort rollback if snapshot is not the latest
416 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
417 my $text = $class->zfs_request($scfg, undef, undef, 'list', @params);
418 my @snapshots = split(/\n/, $text);
419
420 my $recentsnap;
421 foreach (@snapshots) {
422 if (/$scfg->{pool}\/$vname/) {
423 s/^.*@//;
424 $recentsnap = $_;
425 }
426 }
427
428 return $recentsnap;
429 }
430
431 sub 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
449 sub volume_size_info {
450 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
451
452 my (undef, $vname, undef, undef, undef, undef, $format) =
453 $class->parse_volname($volname);
454
455 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
456 my $text = $class->zfs_request($scfg, undef, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
457 if ($text =~ /\s$attr\s(\d+)\s/) {
458 return $1;
459 }
460
461 die "Could not get zfs volume size\n";
462 }
463
464 sub volume_snapshot {
465 my ($class, $scfg, $storeid, $volname, $snap) = @_;
466
467 my $vname = ($class->parse_volname($volname))[1];
468
469 $class->zfs_request($scfg, undef, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
470 }
471
472 sub 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
502 if ($ip) {
503 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
504 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
505 }
506
507 push @$cmdrecv, 'zfs', 'recv', '-F', '--';
508
509 $zpath = $target_path if defined($target_path);
510 push @$cmdrecv, $zpath;
511
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
523 sub 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
530 sub volume_snapshot_delete {
531 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
532
533 my $vname = ($class->parse_volname($volname))[1];
534
535 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
536
537 $class->zfs_request($scfg, undef, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
538 }
539
540 sub volume_snapshot_rollback {
541 my ($class, $scfg, $storeid, $volname, $snap) = @_;
542
543 my $vname = ($class->parse_volname($volname))[1];
544
545 $class->zfs_request($scfg, undef, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
546 }
547
548 sub volume_rollback_is_possible {
549 my ($class, $scfg, $storeid, $volname, $snap) = @_;
550
551 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
552 if ($snap ne $recentsnap) {
553 die "can't rollback, more recent snapshots exist\n";
554 }
555
556 return 1;
557 }
558
559 sub 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
591 sub activate_storage {
592 my ($class, $storeid, $scfg, $cache) = @_;
593
594 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
595 my $pool = $scfg->{pool};
596 $pool =~ s!/.*$!!;
597
598 my @param = ('-o', 'name', '-H', "$pool");
599 my $res;
600 eval {
601 $res = $class->zfs_request($scfg, undef, undef, 'zpool_list', @param);
602 };
603
604 if ($@ || !defined($res) || $res !~ $pool) {
605 eval {
606 @param = ('-d', '/dev/disk/by-id/', "$pool");
607 $class->zfs_request($scfg, undef, undef, 'zpool_import', @param);
608 };
609 die "could not activate storage '$storeid', $@\n" if $@;
610 }
611 return 1;
612 }
613
614 sub deactivate_storage {
615 my ($class, $storeid, $scfg, $cache) = @_;
616 return 1;
617 }
618
619 sub activate_volume {
620 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
621 return 1;
622 }
623
624 sub deactivate_volume {
625 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
626 return 1;
627 }
628
629 sub clone_image {
630 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
631
632 $snap ||= '__base__';
633
634 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
635 $class->parse_volname($volname);
636
637 die "clone_image only works on base images\n" if !$isBase;
638
639 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
640
641 if ($format eq 'subvol') {
642 my $size = $class->zfs_request($scfg, undef, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
643 chomp($size);
644 $class->zfs_request($scfg, undef, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
645 } else {
646 $class->zfs_request($scfg, undef, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
647 }
648
649 return "$basename/$name";
650 }
651
652 sub create_base {
653 my ($class, $storeid, $scfg, $volname) = @_;
654
655 my $snap = '__base__';
656
657 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
658 $class->parse_volname($volname);
659
660 die "create_base not possible with base image\n" if $isBase;
661
662 my $newname = $name;
663 if ( $format eq 'subvol' ) {
664 $newname =~ s/^subvol-/basevol-/;
665 } else {
666 $newname =~ s/^vm-/base-/;
667 }
668 my $newvolname = $basename ? "$basename/$newname" : "$newname";
669
670 $class->zfs_request($scfg, undef, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
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
679 sub volume_resize {
680 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
681
682 my $new_size = int($size/1024);
683
684 my (undef, $vname, undef, undef, undef, undef, $format) =
685 $class->parse_volname($volname);
686
687 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
688
689 $class->zfs_request($scfg, undef, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
690
691 return $new_size;
692 }
693
694 sub 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},
702 sparseinit => { base => 1, current => 1},
703 };
704
705 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
706 $class->parse_volname($volname);
707
708 my $key = undef;
709
710 if ($snapname) {
711 $key = 'snap';
712 } else {
713 $key = $isBase ? 'base' : 'current';
714 }
715
716 return 1 if $features->{$feature}->{$key};
717
718 return undef;
719 }
720
721 1;