]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPoolPlugin.pm
fix bug #664
[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;
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
107 $name = $pool . '/' . $name;
108
109 $zvol->{pool} = $pool;
110 $zvol->{name} = $name;
111 if ($type eq 'filesystem') {
112 if ($refquota eq 'none') {
113 $zvol->{size} = 0;
114 } else {
115 $zvol->{size} = zfs_parse_size($refquota);
7730694e 116 }
1ccae449
DM
117 $zvol->{format} = 'subvol';
118 } else {
119 $zvol->{size} = zfs_parse_size($size);
120 $zvol->{format} = 'raw';
7730694e 121 }
1ccae449
DM
122 if ($origin !~ /^-$/) {
123 $zvol->{origin} = $origin;
124 }
125 push @$list, $zvol;
7730694e
DM
126 }
127
128 return $list;
129}
130
cc80ed9c
WL
131sub parse_volname {
132 my ($class, $volname) = @_;
133
1ccae449 134 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm|subvol)?-(\d+)-\S+)$/) {
cc80ed9c
WL
135 return ('images', $5, $8, $2, $4, $6);
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
WL
143sub path {
144 my ($class, $scfg, $volname) = @_;
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 }
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-/;
200 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid)
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
DM
209 } elsif ( $fmt eq 'subvol') {
210
211 die "subvolume allocation without name\n" if !$volname;
212 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
213 if $volname !~ m/^subvol-$vmid-/;
76fd7dc7 214
1ccae449
DM
215 $class->zfs_create_subvol($scfg, $volname, $size);
216
217 } else {
218 die "unsupported format '$fmt'";
219 }
b3ba95e4 220
82e08809 221 return $volname;
b3ba95e4
WL
222}
223
e9565df5
WL
224sub free_image {
225 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
226
227 my (undef, $name, undef) = $class->parse_volname($volname);
228
229 $class->zfs_delete_zvol($scfg, $name);
230
231 return undef;
232}
233
ca04180f
WL
234sub list_images {
235 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
236
237 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
238 my $zfspool = $scfg->{pool};
239 my $res = [];
240
241 if (my $dat = $cache->{zfs}->{$zfspool}) {
242
243 foreach my $image (keys %$dat) {
244
245 my $volname = $dat->{$image}->{name};
246 my $parent = $dat->{$image}->{parent};
247
248 my $volid = undef;
249 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
250 my ($basename) = ($1);
251 $volid = "$storeid:$basename/$volname";
252 } else {
253 $volid = "$storeid:$volname";
254 }
255
256 my $owner = $dat->{$volname}->{vmid};
257 if ($vollist) {
258 my $found = grep { $_ eq $volid } @$vollist;
259 next if !$found;
260 } else {
261 next if defined ($vmid) && ($owner ne $vmid);
262 }
263
264 my $info = $dat->{$volname};
265 $info->{volid} = $volid;
266 push @$res, $info;
267 }
268 }
ca04180f
WL
269 return $res;
270}
271
7730694e
DM
272sub zfs_get_pool_stats {
273 my ($class, $scfg) = @_;
274
275 my $available = 0;
276 my $used = 0;
277
278 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
279 'available,used', $scfg->{pool});
280
281 my @lines = split /\n/, $text;
282
283 if($lines[0] =~ /^(\d+)$/) {
284 $available = $1;
285 }
286
287 if($lines[1] =~ /^(\d+)$/) {
288 $used = $1;
289 }
290
291 return ($available, $used);
292}
293
294sub zfs_get_zvol_size {
295 my ($class, $scfg, $zvol) = @_;
296
297 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
298
299 if ($text =~ /volsize\s(\d+)/) {
300 return $1;
301 }
302
303 die "Could not get zvol size";
304}
305
306sub zfs_create_zvol {
307 my ($class, $scfg, $zvol, $size) = @_;
308
309 my $cmd = ['create'];
310
311 push @$cmd, '-s' if $scfg->{sparse};
312
313 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
314
315 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
316
317 $class->zfs_request($scfg, undef, @$cmd);
318}
319
1ccae449
DM
320sub zfs_create_subvol {
321 my ($class, $scfg, $volname, $size) = @_;
322
323 my $dataset = "$scfg->{pool}/$volname";
324
325 my $cmd = ['create', '-o', "refquota=${size}k", $dataset];
326
327 $class->zfs_request($scfg, undef, @$cmd);
328}
329
7730694e
DM
330sub zfs_delete_zvol {
331 my ($class, $scfg, $zvol) = @_;
332
1f390a30
WL
333 my $err;
334
335 for (my $i = 0; $i < 6; $i++) {
336
337 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
338 if ($err = $@) {
339 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
340 sleep(1);
27ff0e99
WL
341 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
342 $err = undef;
343 last;
1f390a30
WL
344 } else {
345 die $err;
346 }
347 } else {
348 last;
349 }
350 }
351
352 die $err if $err;
7730694e
DM
353}
354
355sub zfs_list_zvol {
356 my ($class, $scfg) = @_;
357
1ccae449 358 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
7730694e
DM
359 my $zvols = zfs_parse_zvol_list($text);
360 return undef if !$zvols;
361
362 my $list = ();
363 foreach my $zvol (@$zvols) {
1ccae449
DM
364 my $pool = $zvol->{pool};
365 my $name = $zvol->{name};
7730694e
DM
366 my $parent = $zvol->{origin};
367 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
368 $parent = $1;
369 }
370
1ccae449
DM
371 $list->{$pool}->{$name} = {
372 name => $name,
7730694e
DM
373 size => $zvol->{size},
374 parent => $parent,
1ccae449
DM
375 format => $zvol->{format},
376 vmid => $zvol->{owner},
7730694e
DM
377 };
378 }
379
380 return $list;
381}
382
383sub zfs_find_free_diskname {
384 my ($class, $storeid, $scfg, $vmid) = @_;
385
386 my $name = undef;
387 my $volumes = $class->zfs_list_zvol($scfg);
388
389 my $disk_ids = {};
390 my $dat = $volumes->{$scfg->{pool}};
391
392 foreach my $image (keys %$dat) {
393 my $volname = $dat->{$image}->{name};
394 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
395 $disk_ids->{$2} = 1;
396 }
397 }
398
399 for (my $i = 1; $i < 100; $i++) {
400 if (!$disk_ids->{$i}) {
401 return "vm-$vmid-disk-$i";
402 }
403 }
404
405 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
406}
407
2fc59177
DM
408sub zfs_get_latest_snapshot {
409 my ($class, $scfg, $volname) = @_;
410
411 # abort rollback if snapshot is not the latest
412 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
413 my $text = zfs_request($class, $scfg, undef, 'list', @params);
414 my @snapshots = split(/\n/, $text);
415
416 my $recentsnap;
417 foreach (@snapshots) {
418 if (/$scfg->{pool}\/$volname/) {
419 s/^.*@//;
420 $recentsnap = $_;
421 }
422 }
423
424 return $recentsnap;
425}
426
b5e5f7e3
DM
427sub status {
428 my ($class, $storeid, $scfg, $cache) = @_;
429
430 my $total = 0;
431 my $free = 0;
432 my $used = 0;
433 my $active = 0;
434
435 eval {
436 ($free, $used) = $class->zfs_get_pool_stats($scfg);
437 $active = 1;
438 $total = $free + $used;
439 };
440 warn $@ if $@;
441
442 return ($total, $free, $used, $active);
443}
444
445sub volume_size_info {
446 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
447
448 return $class->zfs_get_zvol_size($scfg, $volname);
449}
450
451sub volume_snapshot {
f5640e7d 452 my ($class, $scfg, $storeid, $volname, $snap) = @_;
b5e5f7e3
DM
453
454 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
455}
456
457sub volume_snapshot_delete {
458 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
459
460 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
461}
462
2b40ffae
WL
463sub volume_snapshot_rollback {
464 my ($class, $scfg, $storeid, $volname, $snap) = @_;
465
1597f1f9
WL
466 zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
467}
468
469sub volume_rollback_is_possible {
470 my ($class, $scfg, $storeid, $volname, $snap) = @_;
471
2fc59177 472 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
2b40ffae 473 if ($snap ne $recentsnap) {
1597f1f9 474 die "can't rollback, more recent snapshots exist\n";
2b40ffae
WL
475 }
476
1597f1f9 477 return 1;
2b40ffae
WL
478}
479
0a3d992f
DM
480sub activate_storage {
481 my ($class, $storeid, $scfg, $cache) = @_;
86d47239
WL
482
483 my @param = ('-o', 'name', '-H');
484
485 my $text = zfs_request($class, $scfg, undef, 'zpool_list', @param);
93124ef4
DM
486
487 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
488 my $pool = $scfg->{pool};
489 $pool =~ s!/.*$!!;
490
491 if ($text !~ $pool) {
86d47239
WL
492 run_command("zpool import -d /dev/disk/by-id/ -a");
493 }
0a3d992f
DM
494 return 1;
495}
496
497sub deactivate_storage {
498 my ($class, $storeid, $scfg, $cache) = @_;
499 return 1;
500}
501
d4c63dc1
WL
502sub activate_volume {
503 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
504 return 1;
505}
506
507sub deactivate_volume {
508 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
509 return 1;
510}
5bb8e010 511
d3a282e8
WL
512sub clone_image {
513 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
514
515 $snap ||= '__base__';
516
517 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
518 $class->parse_volname($volname);
519
520 die "clone_image only works on base images\n" if !$isBase;
521
522 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
523
d3a282e8
WL
524 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
525
526 return $name;
527}
528
529sub create_base {
530 my ($class, $storeid, $scfg, $volname) = @_;
531
532 my $snap = '__base__';
533
534 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
535 $class->parse_volname($volname);
536
537 die "create_base not possible with base image\n" if $isBase;
538
539 my $newname = $name;
540 $newname =~ s/^vm-/base-/;
541
542 my $newvolname = $basename ? "$basename/$newname" : "$newname";
543
544 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
545
546 my $running = undef; #fixme : is create_base always offline ?
547
548 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
549
550 return $newvolname;
551}
552
a4034b9f
WL
553sub volume_resize {
554 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
555
556 my $new_size = ($size/1024);
557
558 $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
559
560 return $new_size;
561}
562
2b40ffae
WL
563sub volume_has_feature {
564 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
565
566 my $features = {
567 snapshot => { current => 1, snap => 1},
568 clone => { base => 1},
569 template => { current => 1},
570 copy => { base => 1, current => 1},
571 };
572
573 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
574 $class->parse_volname($volname);
575
576 my $key = undef;
577
578 if ($snapname) {
579 $key = 'snap';
580 } else {
581 $key = $isBase ? 'base' : 'current';
582 }
583
584 return 1 if $features->{$feature}->{$key};
585
586 return undef;
587}
588
5bb8e010 5891;