]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
bump version to 4.0-32
[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
DM
43 disable => { optional => 1 },
44 maxfiles => { optional => 1 },
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
105 next unless $name =~ m!^(vm|base|subvol)-(\d+)-(\S+)$!;
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
1ccae449 133 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm|subvol)?-(\d+)-\S+)$/) {
55525ad2
DM
134 my $format = $7 && $7 eq 'subvol' ? 'subvol' : 'raw';
135 return ('images', $5, $8, $2, $4, $6, $format);
cc80ed9c
WL
136 }
137
138 die "unable to parse zfs volume name '$volname'\n";
139}
140
7730694e
DM
141# virtual zfs methods (subclass can overwrite them)
142
f3e632d0 143sub path {
e67069eb 144 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
f3e632d0
WL
145
146 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
147
148 my $path = '';
149
1ccae449
DM
150 if ($vtype eq "images") {
151 if ($volname =~ m/^subvol-/) {
152 # fixme: we currently assume standard mount point?!
d6d924d0 153 $path = "/$scfg->{pool}/$volname";
1ccae449
DM
154 } else {
155 $path = "/dev/zvol/$scfg->{pool}/$volname";
156 }
e67069eb 157 $path .= "\@$snapname" if defined($snapname);
f3e632d0 158 } else {
85fda4dd 159 die "$vtype is not allowed in ZFSPool!";
f3e632d0
WL
160 }
161
162 return ($path, $vmid, $vtype);
163}
164
7730694e
DM
165sub 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') {
86d47239 173 push @$cmd, 'zpool', 'list';
7730694e
DM
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
1f390a30 187 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
7730694e
DM
188
189 return $msg;
190}
191
b3ba95e4
WL
192sub alloc_image {
193 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
194
1ccae449
DM
195 my $volname = $name;
196
197 if ($fmt eq 'raw') {
b3ba95e4 198
1ccae449
DM
199 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
200 if $volname && $volname !~ m/^vm-$vmid-/;
55525ad2 201 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
1ccae449 202 if !$volname;
b3ba95e4 203
1ccae449
DM
204 $class->zfs_create_zvol($scfg, $volname, $size);
205 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
82e08809 206
1ccae449
DM
207 run_command("udevadm trigger --subsystem-match block");
208 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
76fd7dc7 209
1ccae449 210 } elsif ( $fmt eq 'subvol') {
55525ad2
DM
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
1ccae449
DM
217 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
218 if $volname !~ m/^subvol-$vmid-/;
76fd7dc7 219
1ccae449
DM
220 $class->zfs_create_subvol($scfg, $volname, $size);
221
222 } else {
223 die "unsupported format '$fmt'";
224 }
b3ba95e4 225
82e08809 226 return $volname;
b3ba95e4
WL
227}
228
e9565df5
WL
229sub 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
ca04180f
WL
239sub 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 }
ca04180f
WL
274 return $res;
275}
276
7730694e
DM
277sub 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
7730694e
DM
299sub 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
1ccae449
DM
313sub zfs_create_subvol {
314 my ($class, $scfg, $volname, $size) = @_;
315
316 my $dataset = "$scfg->{pool}/$volname";
317
efaf4017
DM
318 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
319 '-o', "refquota=${size}k", $dataset];
1ccae449
DM
320
321 $class->zfs_request($scfg, undef, @$cmd);
322}
323
7730694e
DM
324sub zfs_delete_zvol {
325 my ($class, $scfg, $zvol) = @_;
326
1f390a30
WL
327 my $err;
328
329 for (my $i = 0; $i < 6; $i++) {
330
331 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
332 if ($err = $@) {
333 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
334 sleep(1);
27ff0e99
WL
335 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
336 $err = undef;
337 last;
1f390a30
WL
338 } else {
339 die $err;
340 }
341 } else {
342 last;
343 }
344 }
345
346 die $err if $err;
7730694e
DM
347}
348
349sub zfs_list_zvol {
350 my ($class, $scfg) = @_;
351
1ccae449 352 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
7730694e
DM
353 my $zvols = zfs_parse_zvol_list($text);
354 return undef if !$zvols;
355
356 my $list = ();
357 foreach my $zvol (@$zvols) {
1ccae449
DM
358 my $pool = $zvol->{pool};
359 my $name = $zvol->{name};
7730694e
DM
360 my $parent = $zvol->{origin};
361 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
362 $parent = $1;
363 }
364
1ccae449
DM
365 $list->{$pool}->{$name} = {
366 name => $name,
7730694e
DM
367 size => $zvol->{size},
368 parent => $parent,
1ccae449
DM
369 format => $zvol->{format},
370 vmid => $zvol->{owner},
7730694e
DM
371 };
372 }
373
374 return $list;
375}
376
377sub zfs_find_free_diskname {
55525ad2 378 my ($class, $storeid, $scfg, $vmid, $format) = @_;
7730694e
DM
379
380 my $name = undef;
381 my $volumes = $class->zfs_list_zvol($scfg);
382
383 my $disk_ids = {};
384 my $dat = $volumes->{$scfg->{pool}};
385
386 foreach my $image (keys %$dat) {
387 my $volname = $dat->{$image}->{name};
55525ad2 388 if ($volname =~ m/(vm|base|subvol)-$vmid-disk-(\d+)/){
7730694e
DM
389 $disk_ids->{$2} = 1;
390 }
391 }
392
393 for (my $i = 1; $i < 100; $i++) {
394 if (!$disk_ids->{$i}) {
55525ad2 395 return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
7730694e
DM
396 }
397 }
398
399 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
400}
401
2fc59177
DM
402sub zfs_get_latest_snapshot {
403 my ($class, $scfg, $volname) = @_;
404
405 # abort rollback if snapshot is not the latest
406 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
407 my $text = zfs_request($class, $scfg, undef, 'list', @params);
408 my @snapshots = split(/\n/, $text);
409
410 my $recentsnap;
411 foreach (@snapshots) {
412 if (/$scfg->{pool}\/$volname/) {
413 s/^.*@//;
414 $recentsnap = $_;
415 }
416 }
417
418 return $recentsnap;
419}
420
b5e5f7e3
DM
421sub status {
422 my ($class, $storeid, $scfg, $cache) = @_;
423
424 my $total = 0;
425 my $free = 0;
426 my $used = 0;
427 my $active = 0;
428
429 eval {
430 ($free, $used) = $class->zfs_get_pool_stats($scfg);
431 $active = 1;
432 $total = $free + $used;
433 };
434 warn $@ if $@;
435
436 return ($total, $free, $used, $active);
437}
438
439sub volume_size_info {
440 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
441
79f2b938
DM
442 my (undef, undef, undef, undef, undef, undef, $format) =
443 $class->parse_volname($volname);
444
445 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
446 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$volname");
447
448 if ($text =~ /\s$attr\s(\d+)\s/) {
449 return $1;
450 }
451
452 die "Could not get zfs volume size\n";
b5e5f7e3
DM
453}
454
455sub volume_snapshot {
f5640e7d 456 my ($class, $scfg, $storeid, $volname, $snap) = @_;
b5e5f7e3
DM
457
458 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
459}
460
461sub volume_snapshot_delete {
462 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
463
399581a2 464 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
b5e5f7e3
DM
465 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
466}
467
2b40ffae
WL
468sub volume_snapshot_rollback {
469 my ($class, $scfg, $storeid, $volname, $snap) = @_;
470
1597f1f9
WL
471 zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
472}
473
474sub volume_rollback_is_possible {
475 my ($class, $scfg, $storeid, $volname, $snap) = @_;
476
2fc59177 477 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
2b40ffae 478 if ($snap ne $recentsnap) {
1597f1f9 479 die "can't rollback, more recent snapshots exist\n";
2b40ffae
WL
480 }
481
1597f1f9 482 return 1;
2b40ffae
WL
483}
484
0a3d992f
DM
485sub activate_storage {
486 my ($class, $storeid, $scfg, $cache) = @_;
86d47239
WL
487
488 my @param = ('-o', 'name', '-H');
489
490 my $text = zfs_request($class, $scfg, undef, 'zpool_list', @param);
93124ef4
DM
491
492 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
493 my $pool = $scfg->{pool};
494 $pool =~ s!/.*$!!;
495
496 if ($text !~ $pool) {
86d47239
WL
497 run_command("zpool import -d /dev/disk/by-id/ -a");
498 }
0a3d992f
DM
499 return 1;
500}
501
502sub deactivate_storage {
503 my ($class, $storeid, $scfg, $cache) = @_;
504 return 1;
505}
506
d4c63dc1 507sub activate_volume {
02e797b8 508 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
509 return 1;
510}
511
512sub deactivate_volume {
02e797b8 513 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
d4c63dc1
WL
514 return 1;
515}
5bb8e010 516
d3a282e8
WL
517sub clone_image {
518 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
519
520 $snap ||= '__base__';
521
55525ad2 522 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
d3a282e8
WL
523 $class->parse_volname($volname);
524
525 die "clone_image only works on base images\n" if !$isBase;
526
55525ad2 527 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
d3a282e8 528
d3a282e8
WL
529 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
530
531 return $name;
532}
533
534sub create_base {
535 my ($class, $storeid, $scfg, $volname) = @_;
536
537 my $snap = '__base__';
538
539 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
540 $class->parse_volname($volname);
541
542 die "create_base not possible with base image\n" if $isBase;
543
544 my $newname = $name;
545 $newname =~ s/^vm-/base-/;
546
547 my $newvolname = $basename ? "$basename/$newname" : "$newname";
548
549 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
550
551 my $running = undef; #fixme : is create_base always offline ?
552
553 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
554
555 return $newvolname;
556}
557
a4034b9f
WL
558sub volume_resize {
559 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
560
79f2b938
DM
561 my $new_size = int($size/1024);
562
563 my (undef, undef, undef, undef, undef, undef, $format) =
564 $class->parse_volname($volname);
565
566 my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
a4034b9f 567
79f2b938 568 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$volname");
a4034b9f
WL
569
570 return $new_size;
571}
572
2b40ffae
WL
573sub volume_has_feature {
574 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
575
576 my $features = {
577 snapshot => { current => 1, snap => 1},
578 clone => { base => 1},
579 template => { current => 1},
580 copy => { base => 1, current => 1},
581 };
582
583 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
584 $class->parse_volname($volname);
585
586 my $key = undef;
587
588 if ($snapname) {
589 $key = 'snap';
590 } else {
591 $key = $isBase ? 'base' : 'current';
592 }
593
594 return 1 if $features->{$feature}->{$key};
595
596 return undef;
597}
598
5bb8e010 5991;