]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
zfspool: fix volume_size_info and volume_resize for subvols
[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
10
11 use base qw(PVE::Storage::Plugin);
12
13 sub type {
14 return 'zfspool';
15 }
16
17 sub plugindata {
18 return {
19 content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}],
20 format => [ { raw => 1, subvol => 1 } , 'raw' ],
21 };
22 }
23
24 sub properties {
25 return {
26 blocksize => {
27 description => "block size",
28 type => 'string',
29 },
30 sparse => {
31 description => "use sparse volumes",
32 type => 'boolean',
33 },
34 };
35 }
36
37 sub options {
38 return {
39 pool => { fixed => 1 },
40 blocksize => { optional => 1 },
41 sparse => { optional => 1 },
42 nodes => { optional => 1 },
43 disable => { optional => 1 },
44 maxfiles => { 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)-(\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|vm)-(\d+)-\S+)\/)?((base)?(vm|subvol)?-(\d+)-\S+)$/) {
134 my $format = $7 && $7 eq 'subvol' ? 'subvol' : 'raw';
135 return ('images', $5, $8, $2, $4, $6, $format);
136 }
137
138 die "unable to parse zfs volume name '$volname'\n";
139 }
140
141 # virtual zfs methods (subclass can overwrite them)
142
143 sub path {
144 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
145
146 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
147
148 my $path = '';
149
150 if ($vtype eq "images") {
151 if ($volname =~ m/^subvol-/) {
152 # fixme: we currently assume standard mount point?!
153 $path = "/$scfg->{pool}/$volname";
154 } else {
155 $path = "/dev/zvol/$scfg->{pool}/$volname";
156 }
157 $path .= "\@$snapname" if defined($snapname);
158 } else {
159 die "$vtype is not allowed in ZFSPool!";
160 }
161
162 return ($path, $vmid, $vtype);
163 }
164
165 sub zfs_request {
166 my ($class, $scfg, $timeout, $method, @params) = @_;
167
168 $timeout = 5 if !$timeout;
169
170 my $cmd = [];
171
172 if ($method eq 'zpool_list') {
173 push @$cmd, 'zpool', 'list';
174 } else {
175 push @$cmd, 'zfs', $method;
176 }
177
178 push @$cmd, @params;
179
180 my $msg = '';
181
182 my $output = sub {
183 my $line = shift;
184 $msg .= "$line\n";
185 };
186
187 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
188
189 return $msg;
190 }
191
192 sub alloc_image {
193 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
194
195 my $volname = $name;
196
197 if ($fmt eq 'raw') {
198
199 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
200 if $volname && $volname !~ m/^vm-$vmid-/;
201 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
202 if !$volname;
203
204 $class->zfs_create_zvol($scfg, $volname, $size);
205 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
206
207 run_command("udevadm trigger --subsystem-match block");
208 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
209
210 } elsif ( $fmt eq 'subvol') {
211
212 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
213 if $volname && $volname !~ m/^subvol-$vmid-/;
214 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
215 if !$volname;
216
217 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
218 if $volname !~ m/^subvol-$vmid-/;
219
220 $class->zfs_create_subvol($scfg, $volname, $size);
221
222 } else {
223 die "unsupported format '$fmt'";
224 }
225
226 return $volname;
227 }
228
229 sub free_image {
230 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
231
232 my (undef, $name, undef) = $class->parse_volname($volname);
233
234 $class->zfs_delete_zvol($scfg, $name);
235
236 return undef;
237 }
238
239 sub list_images {
240 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
241
242 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
243 my $zfspool = $scfg->{pool};
244 my $res = [];
245
246 if (my $dat = $cache->{zfs}->{$zfspool}) {
247
248 foreach my $image (keys %$dat) {
249
250 my $volname = $dat->{$image}->{name};
251 my $parent = $dat->{$image}->{parent};
252
253 my $volid = undef;
254 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
255 my ($basename) = ($1);
256 $volid = "$storeid:$basename/$volname";
257 } else {
258 $volid = "$storeid:$volname";
259 }
260
261 my $owner = $dat->{$volname}->{vmid};
262 if ($vollist) {
263 my $found = grep { $_ eq $volid } @$vollist;
264 next if !$found;
265 } else {
266 next if defined ($vmid) && ($owner ne $vmid);
267 }
268
269 my $info = $dat->{$volname};
270 $info->{volid} = $volid;
271 push @$res, $info;
272 }
273 }
274 return $res;
275 }
276
277 sub zfs_get_pool_stats {
278 my ($class, $scfg) = @_;
279
280 my $available = 0;
281 my $used = 0;
282
283 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
284 'available,used', $scfg->{pool});
285
286 my @lines = split /\n/, $text;
287
288 if($lines[0] =~ /^(\d+)$/) {
289 $available = $1;
290 }
291
292 if($lines[1] =~ /^(\d+)$/) {
293 $used = $1;
294 }
295
296 return ($available, $used);
297 }
298
299 sub zfs_create_zvol {
300 my ($class, $scfg, $zvol, $size) = @_;
301
302 my $cmd = ['create'];
303
304 push @$cmd, '-s' if $scfg->{sparse};
305
306 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
307
308 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
309
310 $class->zfs_request($scfg, undef, @$cmd);
311 }
312
313 sub zfs_create_subvol {
314 my ($class, $scfg, $volname, $size) = @_;
315
316 my $dataset = "$scfg->{pool}/$volname";
317
318 my $cmd = ['create', '-o', "refquota=${size}k", $dataset];
319
320 $class->zfs_request($scfg, undef, @$cmd);
321 }
322
323 sub zfs_delete_zvol {
324 my ($class, $scfg, $zvol) = @_;
325
326 my $err;
327
328 for (my $i = 0; $i < 6; $i++) {
329
330 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
331 if ($err = $@) {
332 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
333 sleep(1);
334 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
335 $err = undef;
336 last;
337 } else {
338 die $err;
339 }
340 } else {
341 last;
342 }
343 }
344
345 die $err if $err;
346 }
347
348 sub zfs_list_zvol {
349 my ($class, $scfg) = @_;
350
351 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
352 my $zvols = zfs_parse_zvol_list($text);
353 return undef if !$zvols;
354
355 my $list = ();
356 foreach my $zvol (@$zvols) {
357 my $pool = $zvol->{pool};
358 my $name = $zvol->{name};
359 my $parent = $zvol->{origin};
360 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
361 $parent = $1;
362 }
363
364 $list->{$pool}->{$name} = {
365 name => $name,
366 size => $zvol->{size},
367 parent => $parent,
368 format => $zvol->{format},
369 vmid => $zvol->{owner},
370 };
371 }
372
373 return $list;
374 }
375
376 sub zfs_find_free_diskname {
377 my ($class, $storeid, $scfg, $vmid, $format) = @_;
378
379 my $name = undef;
380 my $volumes = $class->zfs_list_zvol($scfg);
381
382 my $disk_ids = {};
383 my $dat = $volumes->{$scfg->{pool}};
384
385 foreach my $image (keys %$dat) {
386 my $volname = $dat->{$image}->{name};
387 if ($volname =~ m/(vm|base|subvol)-$vmid-disk-(\d+)/){
388 $disk_ids->{$2} = 1;
389 }
390 }
391
392 for (my $i = 1; $i < 100; $i++) {
393 if (!$disk_ids->{$i}) {
394 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
395 }
396 }
397
398 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
399 }
400
401 sub zfs_get_latest_snapshot {
402 my ($class, $scfg, $volname) = @_;
403
404 # abort rollback if snapshot is not the latest
405 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
406 my $text = zfs_request($class, $scfg, undef, 'list', @params);
407 my @snapshots = split(/\n/, $text);
408
409 my $recentsnap;
410 foreach (@snapshots) {
411 if (/$scfg->{pool}\/$volname/) {
412 s/^.*@//;
413 $recentsnap = $_;
414 }
415 }
416
417 return $recentsnap;
418 }
419
420 sub status {
421 my ($class, $storeid, $scfg, $cache) = @_;
422
423 my $total = 0;
424 my $free = 0;
425 my $used = 0;
426 my $active = 0;
427
428 eval {
429 ($free, $used) = $class->zfs_get_pool_stats($scfg);
430 $active = 1;
431 $total = $free + $used;
432 };
433 warn $@ if $@;
434
435 return ($total, $free, $used, $active);
436 }
437
438 sub volume_size_info {
439 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
440
441 my (undef, undef, undef, undef, undef, undef, $format) =
442 $class->parse_volname($volname);
443
444 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
445 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$volname");
446
447 if ($text =~ /\s$attr\s(\d+)\s/) {
448 return $1;
449 }
450
451 die "Could not get zfs volume size\n";
452 }
453
454 sub volume_snapshot {
455 my ($class, $scfg, $storeid, $volname, $snap) = @_;
456
457 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
458 }
459
460 sub volume_snapshot_delete {
461 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
462
463 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
464 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
465 }
466
467 sub volume_snapshot_rollback {
468 my ($class, $scfg, $storeid, $volname, $snap) = @_;
469
470 zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
471 }
472
473 sub volume_rollback_is_possible {
474 my ($class, $scfg, $storeid, $volname, $snap) = @_;
475
476 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
477 if ($snap ne $recentsnap) {
478 die "can't rollback, more recent snapshots exist\n";
479 }
480
481 return 1;
482 }
483
484 sub activate_storage {
485 my ($class, $storeid, $scfg, $cache) = @_;
486
487 my @param = ('-o', 'name', '-H');
488
489 my $text = zfs_request($class, $scfg, undef, 'zpool_list', @param);
490
491 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
492 my $pool = $scfg->{pool};
493 $pool =~ s!/.*$!!;
494
495 if ($text !~ $pool) {
496 run_command("zpool import -d /dev/disk/by-id/ -a");
497 }
498 return 1;
499 }
500
501 sub deactivate_storage {
502 my ($class, $storeid, $scfg, $cache) = @_;
503 return 1;
504 }
505
506 sub activate_volume {
507 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
508 return 1;
509 }
510
511 sub deactivate_volume {
512 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
513 return 1;
514 }
515
516 sub clone_image {
517 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
518
519 $snap ||= '__base__';
520
521 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
522 $class->parse_volname($volname);
523
524 die "clone_image only works on base images\n" if !$isBase;
525
526 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
527
528 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
529
530 return $name;
531 }
532
533 sub create_base {
534 my ($class, $storeid, $scfg, $volname) = @_;
535
536 my $snap = '__base__';
537
538 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
539 $class->parse_volname($volname);
540
541 die "create_base not possible with base image\n" if $isBase;
542
543 my $newname = $name;
544 $newname =~ s/^vm-/base-/;
545
546 my $newvolname = $basename ? "$basename/$newname" : "$newname";
547
548 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
549
550 my $running = undef; #fixme : is create_base always offline ?
551
552 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
553
554 return $newvolname;
555 }
556
557 sub volume_resize {
558 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
559
560 my $new_size = int($size/1024);
561
562 my (undef, undef, undef, undef, undef, undef, $format) =
563 $class->parse_volname($volname);
564
565 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
566
567 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$volname");
568
569 return $new_size;
570 }
571
572 sub volume_has_feature {
573 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
574
575 my $features = {
576 snapshot => { current => 1, snap => 1},
577 clone => { base => 1},
578 template => { current => 1},
579 copy => { base => 1, current => 1},
580 };
581
582 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
583 $class->parse_volname($volname);
584
585 my $key = undef;
586
587 if ($snapname) {
588 $key = 'snap';
589 } else {
590 $key = $isBase ? 'base' : 'current';
591 }
592
593 return 1 if $features->{$feature}->{$key};
594
595 return undef;
596 }
597
598 1;