]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
add lvmthinscan to CLI
[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;
9
10
11use base qw(PVE::Storage::Plugin);
12
5bb8e010 13sub type {
85fda4dd 14 return 'zfspool';
5bb8e010
DM
15}
16
17sub plugindata {
18 return {
1ccae449
DM
19 content => [ {images => 1, rootdir => 1}, {images => 1 , rootdir => 1}],
20 format => [ { raw => 1, subvol => 1 } , 'raw' ],
5bb8e010 21 };
85fda4dd 22}
5bb8e010 23
7730694e
DM
24sub 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
5bb8e010
DM
37sub options {
38 return {
7730694e
DM
39 pool => { fixed => 1 },
40 blocksize => { optional => 1 },
41 sparse => { optional => 1 },
42 nodes => { optional => 1 },
5bb8e010 43 disable => { optional => 1 },
5bb8e010
DM
44 content => { optional => 1 },
45 };
46}
47
7730694e
DM
48# static zfs helper methods
49
060ef890
DM
50sub zfs_parse_size {
51 my ($text) = @_;
52
53 return 0 if !$text;
54
55 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
56
57 my ($size, $reminder, $unit) = ($1, $2, $3);
58
59 if ($unit) {
60 if ($unit eq 'K') {
61 $size *= 1024;
62 } elsif ($unit eq 'M') {
63 $size *= 1024*1024;
64 } elsif ($unit eq 'G') {
65 $size *= 1024*1024*1024;
66 } elsif ($unit eq 'T') {
67 $size *= 1024*1024*1024*1024;
68 } else {
69 die "got unknown zfs size unit '$unit'\n";
70 }
71 }
72
73 if ($reminder) {
74 $size = ceil($size);
75 }
76
77 return $size;
78
79 }
80
81 warn "unable to parse zfs size '$text'\n";
82
83 return 0;
84}
85
7730694e
DM
86sub zfs_parse_zvol_list {
87 my ($text) = @_;
88
89 my $list = ();
90
91 return $list if !$text;
92
93 my @lines = split /\n/, $text;
94 foreach my $line (@lines) {
1ccae449
DM
95 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
96 next if !($type eq 'volume' || $type eq 'filesystem');
97
98 my $zvol = {};
99 my @parts = split /\//, $dataset;
dec97937 100 next if scalar(@parts) < 2; # we need pool/name
1ccae449
DM
101 my $name = pop @parts;
102 my $pool = join('/', @parts);
103
104 next unless $name =~ m!^(vm|base|subvol)-(\d+)-(\S+)$!;
105 $zvol->{owner} = $2;
106
1ccae449
DM
107 $zvol->{pool} = $pool;
108 $zvol->{name} = $name;
109 if ($type eq 'filesystem') {
110 if ($refquota eq 'none') {
111 $zvol->{size} = 0;
112 } else {
113 $zvol->{size} = zfs_parse_size($refquota);
7730694e 114 }
1ccae449
DM
115 $zvol->{format} = 'subvol';
116 } else {
117 $zvol->{size} = zfs_parse_size($size);
118 $zvol->{format} = 'raw';
7730694e 119 }
1ccae449
DM
120 if ($origin !~ /^-$/) {
121 $zvol->{origin} = $origin;
122 }
123 push @$list, $zvol;
7730694e
DM
124 }
125
126 return $list;
127}
128
cc80ed9c
WL
129sub parse_volname {
130 my ($class, $volname) = @_;
131
1ccae449 132 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm|subvol)?-(\d+)-\S+)$/) {
55525ad2
DM
133 my $format = $7 && $7 eq 'subvol' ? 'subvol' : 'raw';
134 return ('images', $5, $8, $2, $4, $6, $format);
cc80ed9c
WL
135 }
136
137 die "unable to parse zfs volume name '$volname'\n";
138}
139
7730694e
DM
140# virtual zfs methods (subclass can overwrite them)
141
f3e632d0 142sub path {
e67069eb 143 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f3e632d0
WL
144
145 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
146
147 my $path = '';
148
1ccae449
DM
149 if ($vtype eq "images") {
150 if ($volname =~ m/^subvol-/) {
f482231e 151 # fixme: we currently assume standard mount point?!
d6d924d0 152 $path = "/$scfg->{pool}/$volname";
1ccae449
DM
153 } else {
154 $path = "/dev/zvol/$scfg->{pool}/$volname";
155 }
f482231e 156 $path .= "\@$snapname" if defined($snapname);
f3e632d0 157 } else {
85fda4dd 158 die "$vtype is not allowed in ZFSPool!";
f3e632d0
WL
159 }
160
161 return ($path, $vmid, $vtype);
162}
163
7730694e
DM
164sub zfs_request {
165 my ($class, $scfg, $timeout, $method, @params) = @_;
166
167 $timeout = 5 if !$timeout;
168
169 my $cmd = [];
170
171 if ($method eq 'zpool_list') {
86d47239 172 push @$cmd, 'zpool', 'list';
7730694e
DM
173 } else {
174 push @$cmd, 'zfs', $method;
175 }
176
177 push @$cmd, @params;
178
179 my $msg = '';
180
181 my $output = sub {
182 my $line = shift;
183 $msg .= "$line\n";
184 };
185
1f390a30 186 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
7730694e
DM
187
188 return $msg;
189}
190
b3ba95e4
WL
191sub alloc_image {
192 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
193
1ccae449
DM
194 my $volname = $name;
195
196 if ($fmt eq 'raw') {
b3ba95e4 197
1ccae449
DM
198 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
199 if $volname && $volname !~ m/^vm-$vmid-/;
55525ad2 200 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
1ccae449 201 if !$volname;
b3ba95e4 202
1ccae449
DM
203 $class->zfs_create_zvol($scfg, $volname, $size);
204 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
82e08809 205
1ccae449
DM
206 run_command("udevadm trigger --subsystem-match block");
207 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
76fd7dc7 208
1ccae449 209 } elsif ( $fmt eq 'subvol') {
55525ad2
DM
210
211 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
212 if $volname && $volname !~ m/^subvol-$vmid-/;
213 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
214 if !$volname;
215
1ccae449
DM
216 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
217 if $volname !~ m/^subvol-$vmid-/;
76fd7dc7 218
1ccae449
DM
219 $class->zfs_create_subvol($scfg, $volname, $size);
220
221 } else {
222 die "unsupported format '$fmt'";
223 }
b3ba95e4 224
82e08809 225 return $volname;
b3ba95e4
WL
226}
227
e9565df5
WL
228sub free_image {
229 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
230
231 my (undef, $name, undef) = $class->parse_volname($volname);
232
233 $class->zfs_delete_zvol($scfg, $name);
234
235 return undef;
236}
237
ca04180f
WL
238sub list_images {
239 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
240
241 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
242 my $zfspool = $scfg->{pool};
243 my $res = [];
244
245 if (my $dat = $cache->{zfs}->{$zfspool}) {
246
247 foreach my $image (keys %$dat) {
248
249 my $volname = $dat->{$image}->{name};
250 my $parent = $dat->{$image}->{parent};
251
252 my $volid = undef;
253 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
254 my ($basename) = ($1);
255 $volid = "$storeid:$basename/$volname";
256 } else {
257 $volid = "$storeid:$volname";
258 }
259
260 my $owner = $dat->{$volname}->{vmid};
261 if ($vollist) {
262 my $found = grep { $_ eq $volid } @$vollist;
263 next if !$found;
264 } else {
265 next if defined ($vmid) && ($owner ne $vmid);
266 }
267
268 my $info = $dat->{$volname};
269 $info->{volid} = $volid;
270 push @$res, $info;
271 }
272 }
ca04180f
WL
273 return $res;
274}
275
7730694e
DM
276sub zfs_get_pool_stats {
277 my ($class, $scfg) = @_;
278
279 my $available = 0;
280 my $used = 0;
281
282 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
283 'available,used', $scfg->{pool});
284
285 my @lines = split /\n/, $text;
286
287 if($lines[0] =~ /^(\d+)$/) {
288 $available = $1;
289 }
290
291 if($lines[1] =~ /^(\d+)$/) {
292 $used = $1;
293 }
294
295 return ($available, $used);
296}
297
7730694e
DM
298sub zfs_create_zvol {
299 my ($class, $scfg, $zvol, $size) = @_;
300
301 my $cmd = ['create'];
302
303 push @$cmd, '-s' if $scfg->{sparse};
304
305 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
306
307 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
308
309 $class->zfs_request($scfg, undef, @$cmd);
310}
311
1ccae449
DM
312sub zfs_create_subvol {
313 my ($class, $scfg, $volname, $size) = @_;
314
315 my $dataset = "$scfg->{pool}/$volname";
316
efaf4017
DM
317 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
318 '-o', "refquota=${size}k", $dataset];
1ccae449
DM
319
320 $class->zfs_request($scfg, undef, @$cmd);
321}
322
7730694e
DM
323sub zfs_delete_zvol {
324 my ($class, $scfg, $zvol) = @_;
325
1f390a30
WL
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);
27ff0e99
WL
334 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
335 $err = undef;
336 last;
1f390a30
WL
337 } else {
338 die $err;
339 }
340 } else {
341 last;
342 }
343 }
344
345 die $err if $err;
7730694e
DM
346}
347
348sub zfs_list_zvol {
349 my ($class, $scfg) = @_;
350
1ccae449 351 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
7730694e
DM
352 my $zvols = zfs_parse_zvol_list($text);
353 return undef if !$zvols;
354
355 my $list = ();
356 foreach my $zvol (@$zvols) {
1ccae449
DM
357 my $pool = $zvol->{pool};
358 my $name = $zvol->{name};
7730694e
DM
359 my $parent = $zvol->{origin};
360 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
361 $parent = $1;
362 }
363
1ccae449
DM
364 $list->{$pool}->{$name} = {
365 name => $name,
7730694e
DM
366 size => $zvol->{size},
367 parent => $parent,
1ccae449
DM
368 format => $zvol->{format},
369 vmid => $zvol->{owner},
7730694e
DM
370 };
371 }
372
373 return $list;
374}
375
376sub zfs_find_free_diskname {
55525ad2 377 my ($class, $storeid, $scfg, $vmid, $format) = @_;
7730694e
DM
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};
55525ad2 387 if ($volname =~ m/(vm|base|subvol)-$vmid-disk-(\d+)/){
7730694e
DM
388 $disk_ids->{$2} = 1;
389 }
390 }
391
392 for (my $i = 1; $i < 100; $i++) {
393 if (!$disk_ids->{$i}) {
55525ad2 394 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
7730694e
DM
395 }
396 }
397
398 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
399}
400
2fc59177
DM
401sub 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');
281f9587 406 my $text = $class->zfs_request($scfg, undef, 'list', @params);
2fc59177
DM
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
b5e5f7e3
DM
420sub 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
438sub volume_size_info {
439 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
440
79f2b938
DM
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";
b5e5f7e3
DM
452}
453
454sub volume_snapshot {
f5640e7d 455 my ($class, $scfg, $storeid, $volname, $snap) = @_;
b5e5f7e3
DM
456
457 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
458}
459
460sub volume_snapshot_delete {
461 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
462
399581a2 463 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
b5e5f7e3
DM
464 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
465}
466
2b40ffae
WL
467sub volume_snapshot_rollback {
468 my ($class, $scfg, $storeid, $volname, $snap) = @_;
469
281f9587 470 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
1597f1f9
WL
471}
472
473sub volume_rollback_is_possible {
474 my ($class, $scfg, $storeid, $volname, $snap) = @_;
475
2fc59177 476 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
2b40ffae 477 if ($snap ne $recentsnap) {
1597f1f9 478 die "can't rollback, more recent snapshots exist\n";
2b40ffae
WL
479 }
480
1597f1f9 481 return 1;
2b40ffae
WL
482}
483
0a3d992f
DM
484sub activate_storage {
485 my ($class, $storeid, $scfg, $cache) = @_;
86d47239
WL
486
487 my @param = ('-o', 'name', '-H');
488
281f9587 489 my $text = $class->zfs_request($scfg, undef, 'zpool_list', @param);
93124ef4
DM
490
491 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
492 my $pool = $scfg->{pool};
493 $pool =~ s!/.*$!!;
494
495 if ($text !~ $pool) {
86d47239
WL
496 run_command("zpool import -d /dev/disk/by-id/ -a");
497 }
0a3d992f
DM
498 return 1;
499}
500
501sub deactivate_storage {
502 my ($class, $storeid, $scfg, $cache) = @_;
503 return 1;
504}
505
d4c63dc1 506sub activate_volume {
02e797b8 507 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
508 return 1;
509}
510
511sub deactivate_volume {
02e797b8 512 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
513 return 1;
514}
5bb8e010 515
d3a282e8
WL
516sub clone_image {
517 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
518
519 $snap ||= '__base__';
520
55525ad2 521 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
d3a282e8
WL
522 $class->parse_volname($volname);
523
524 die "clone_image only works on base images\n" if !$isBase;
525
55525ad2 526 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
d3a282e8 527
d3a282e8
WL
528 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
529
530 return $name;
531}
532
533sub 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
a4034b9f
WL
557sub volume_resize {
558 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
559
79f2b938
DM
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';
a4034b9f 566
79f2b938 567 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$volname");
a4034b9f
WL
568
569 return $new_size;
570}
571
2b40ffae
WL
572sub 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
5bb8e010 5981;