]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
Include new storage function volume_snapshot_list.
[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, $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
184 my $msg = '';
185
186 my $output = sub {
187 my $line = shift;
188 $msg .= "$line\n";
189 };
190
191 $timeout = $default_timeout if !$timeout;
192
193 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
194
195 return $msg;
196 }
197
198 sub alloc_image {
199 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
200
201 my $volname = $name;
202
203 if ($fmt eq 'raw') {
204
205 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
206 if $volname && $volname !~ m/^vm-$vmid-/;
207 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
208 if !$volname;
209
210 $class->zfs_create_zvol($scfg, $volname, $size);
211 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
212
213 run_command("udevadm trigger --subsystem-match block");
214 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
215
216 } elsif ( $fmt eq 'subvol') {
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
223 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
224 if $volname !~ m/^subvol-$vmid-/;
225
226 $class->zfs_create_subvol($scfg, $volname, $size);
227
228 } else {
229 die "unsupported format '$fmt'";
230 }
231
232 return $volname;
233 }
234
235 sub 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
245 sub 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
256 my $info = $dat->{$image};
257
258 my $volname = $info->{name};
259 my $parent = $info->{parent};
260 my $owner = $info->{vmid};
261
262 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
263 my ($basename) = ($1);
264 $info->{volid} = "$storeid:$basename/$volname";
265 } else {
266 $info->{volid} = "$storeid:$volname";
267 }
268
269 if ($vollist) {
270 my $found = grep { $_ eq $info->{volid} } @$vollist;
271 next if !$found;
272 } else {
273 next if defined ($vmid) && ($owner ne $vmid);
274 }
275
276 push @$res, $info;
277 }
278 }
279 return $res;
280 }
281
282 sub 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
304 sub 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
318 sub zfs_create_subvol {
319 my ($class, $scfg, $volname, $size) = @_;
320
321 my $dataset = "$scfg->{pool}/$volname";
322
323 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
324 '-o', "refquota=${size}k", $dataset];
325
326 $class->zfs_request($scfg, undef, @$cmd);
327 }
328
329 sub zfs_delete_zvol {
330 my ($class, $scfg, $zvol) = @_;
331
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);
340 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
341 $err = undef;
342 last;
343 } else {
344 die $err;
345 }
346 } else {
347 last;
348 }
349 }
350
351 die $err if $err;
352 }
353
354 sub zfs_list_zvol {
355 my ($class, $scfg) = @_;
356
357 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
358 my $zvols = zfs_parse_zvol_list($text);
359 return undef if !$zvols;
360
361 my $list = ();
362 foreach my $zvol (@$zvols) {
363 my $pool = $zvol->{pool};
364 my $name = $zvol->{name};
365 my $parent = $zvol->{origin};
366 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
367 $parent = $1;
368 }
369
370 $list->{$pool}->{$name} = {
371 name => $name,
372 size => $zvol->{size},
373 parent => $parent,
374 format => $zvol->{format},
375 vmid => $zvol->{owner},
376 };
377 }
378
379 return $list;
380 }
381
382 sub zfs_find_free_diskname {
383 my ($class, $storeid, $scfg, $vmid, $format) = @_;
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};
393 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
394 $disk_ids->{$2} = 1;
395 }
396 }
397
398 for (my $i = 1; $i < 100; $i++) {
399 if (!$disk_ids->{$i}) {
400 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
401 }
402 }
403
404 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
405 }
406
407 sub zfs_get_latest_snapshot {
408 my ($class, $scfg, $volname) = @_;
409
410 my $vname = ($class->parse_volname($volname))[1];
411
412 # abort rollback if snapshot is not the latest
413 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
414 my $text = $class->zfs_request($scfg, undef, 'list', @params);
415 my @snapshots = split(/\n/, $text);
416
417 my $recentsnap;
418 foreach (@snapshots) {
419 if (/$scfg->{pool}\/$vname/) {
420 s/^.*@//;
421 $recentsnap = $_;
422 }
423 }
424
425 return $recentsnap;
426 }
427
428 sub 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
446 sub volume_size_info {
447 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
448
449 my (undef, $vname, undef, undef, undef, undef, $format) =
450 $class->parse_volname($volname);
451
452 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
453 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
454 if ($text =~ /\s$attr\s(\d+)\s/) {
455 return $1;
456 }
457
458 die "Could not get zfs volume size\n";
459 }
460
461 sub volume_snapshot {
462 my ($class, $scfg, $storeid, $volname, $snap) = @_;
463
464 my $vname = ($class->parse_volname($volname))[1];
465
466 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
467 }
468
469 sub 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
499 if ($ip) {
500 $ip = "[$ip]" if Net::IP::ip_is_ipv6($ip);
501 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
502 }
503
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
521 sub volume_snapshot_delete {
522 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
523
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");
528 }
529
530 sub volume_snapshot_rollback {
531 my ($class, $scfg, $storeid, $volname, $snap) = @_;
532
533 my $vname = ($class->parse_volname($volname))[1];
534
535 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
536 }
537
538 sub volume_rollback_is_possible {
539 my ($class, $scfg, $storeid, $volname, $snap) = @_;
540
541 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
542 if ($snap ne $recentsnap) {
543 die "can't rollback, more recent snapshots exist\n";
544 }
545
546 return 1;
547 }
548
549 sub 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
581 sub activate_storage {
582 my ($class, $storeid, $scfg, $cache) = @_;
583
584 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
585 my $pool = $scfg->{pool};
586 $pool =~ s!/.*$!!;
587
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 $@;
600 }
601 return 1;
602 }
603
604 sub deactivate_storage {
605 my ($class, $storeid, $scfg, $cache) = @_;
606 return 1;
607 }
608
609 sub activate_volume {
610 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
611 return 1;
612 }
613
614 sub deactivate_volume {
615 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
616 return 1;
617 }
618
619 sub clone_image {
620 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
621
622 $snap ||= '__base__';
623
624 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
625 $class->parse_volname($volname);
626
627 die "clone_image only works on base images\n" if !$isBase;
628
629 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
630
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 }
638
639 return "$basename/$name";
640 }
641
642 sub create_base {
643 my ($class, $storeid, $scfg, $volname) = @_;
644
645 my $snap = '__base__';
646
647 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
648 $class->parse_volname($volname);
649
650 die "create_base not possible with base image\n" if $isBase;
651
652 my $newname = $name;
653 if ( $format eq 'subvol' ) {
654 $newname =~ s/^subvol-/basevol-/;
655 } else {
656 $newname =~ s/^vm-/base-/;
657 }
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
669 sub volume_resize {
670 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
671
672 my $new_size = int($size/1024);
673
674 my (undef, $vname, undef, undef, undef, undef, $format) =
675 $class->parse_volname($volname);
676
677 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
678
679 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
680
681 return $new_size;
682 }
683
684 sub 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},
692 sparseinit => { base => 1, current => 1},
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
711 1;