]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
490b61c7c101c3c2db708dff50406ace2c306e08
[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}, { images => 1 }],
20 };
21 }
22
23 sub properties {
24 return {
25 blocksize => {
26 description => "block size",
27 type => 'string',
28 },
29 sparse => {
30 description => "use sparse volumes",
31 type => 'boolean',
32 },
33 };
34 }
35
36 sub options {
37 return {
38 pool => { fixed => 1 },
39 blocksize => { optional => 1 },
40 sparse => { optional => 1 },
41 nodes => { optional => 1 },
42 disable => { optional => 1 },
43 maxfiles => { optional => 1 },
44 content => { optional => 1 },
45 };
46 }
47
48 # static zfs helper methods
49
50 sub 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
86 sub 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) {
95 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
96 my $zvol = {};
97 my @parts = split /\//, $1;
98 my $name = pop @parts;
99 my $pool = join('/', @parts);
100
101 if ($pool !~ /^rpool$/) {
102 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
103 $name = $pool . '/' . $name;
104 } else {
105 next;
106 }
107
108 $zvol->{pool} = $pool;
109 $zvol->{name} = $name;
110 $zvol->{size} = zfs_parse_size($2);
111 if ($3 !~ /^-$/) {
112 $zvol->{origin} = $3;
113 }
114 push @$list, $zvol;
115 }
116 }
117
118 return $list;
119 }
120
121 sub parse_volname {
122 my ($class, $volname) = @_;
123
124 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
125 return ('images', $5, $8, $2, $4, $6);
126 }
127
128 die "unable to parse zfs volume name '$volname'\n";
129 }
130
131 # virtual zfs methods (subclass can overwrite them)
132
133 sub path {
134 my ($class, $scfg, $volname) = @_;
135
136 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
137
138 my $path = '';
139
140 if($vtype eq "images"){
141 $path = "/dev/zvol/$scfg->{pool}/$volname";
142 } else {
143 die "$vtype is not allowed in ZFSPool!";
144 }
145
146 return ($path, $vmid, $vtype);
147 }
148
149 sub zfs_request {
150 my ($class, $scfg, $timeout, $method, @params) = @_;
151
152 $timeout = 5 if !$timeout;
153
154 my $cmd = [];
155
156 if ($method eq 'zpool_list') {
157 push @$cmd = 'zpool', 'list';
158 } else {
159 push @$cmd, 'zfs', $method;
160 }
161
162 push @$cmd, @params;
163
164 my $msg = '';
165
166 my $output = sub {
167 my $line = shift;
168 $msg .= "$line\n";
169 };
170
171 run_command($cmd, outfunc => $output, timeout => $timeout);
172
173 return $msg;
174 }
175
176 sub alloc_image {
177 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
178
179 die "unsupported format '$fmt'" if $fmt ne 'raw';
180
181 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
182 if $name && $name !~ m/^vm-$vmid-/;
183
184 $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
185
186 $class->zfs_create_zvol($scfg, $name, $size);
187 run_command ("udevadm trigger --subsystem-match block");
188 run_command ("udevadm settle --timeout 5");
189
190 for (1..10) {
191 last if -e "/dev/zvol/$scfg->{pool}/$name" ;
192 Time::HiRes::usleep(100);
193 }
194
195 return $name;
196 }
197
198 sub free_image {
199 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
200
201 my (undef, $name, undef) = $class->parse_volname($volname);
202
203 $class->zfs_delete_zvol($scfg, $name);
204
205 return undef;
206 }
207
208 sub list_images {
209 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
210
211 $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
212 my $zfspool = $scfg->{pool};
213 my $res = [];
214
215 if (my $dat = $cache->{zfs}->{$zfspool}) {
216
217 foreach my $image (keys %$dat) {
218
219 my $volname = $dat->{$image}->{name};
220 my $parent = $dat->{$image}->{parent};
221
222 my $volid = undef;
223 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
224 my ($basename) = ($1);
225 $volid = "$storeid:$basename/$volname";
226 } else {
227 $volid = "$storeid:$volname";
228 }
229
230 my $owner = $dat->{$volname}->{vmid};
231 if ($vollist) {
232 my $found = grep { $_ eq $volid } @$vollist;
233 next if !$found;
234 } else {
235 next if defined ($vmid) && ($owner ne $vmid);
236 }
237
238 my $info = $dat->{$volname};
239 $info->{volid} = $volid;
240 push @$res, $info;
241 }
242 }
243
244 return $res;
245 }
246
247 sub zfs_get_pool_stats {
248 my ($class, $scfg) = @_;
249
250 my $available = 0;
251 my $used = 0;
252
253 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
254 'available,used', $scfg->{pool});
255
256 my @lines = split /\n/, $text;
257
258 if($lines[0] =~ /^(\d+)$/) {
259 $available = $1;
260 }
261
262 if($lines[1] =~ /^(\d+)$/) {
263 $used = $1;
264 }
265
266 return ($available, $used);
267 }
268
269 sub zfs_get_zvol_size {
270 my ($class, $scfg, $zvol) = @_;
271
272 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
273
274 if ($text =~ /volsize\s(\d+)/) {
275 return $1;
276 }
277
278 die "Could not get zvol size";
279 }
280
281 sub zfs_create_zvol {
282 my ($class, $scfg, $zvol, $size) = @_;
283
284 my $cmd = ['create'];
285
286 push @$cmd, '-s' if $scfg->{sparse};
287
288 push @$cmd, '-b', $scfg->{blocksize} if $scfg->{blocksize};
289
290 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
291
292 $class->zfs_request($scfg, undef, @$cmd);
293 }
294
295 sub zfs_delete_zvol {
296 my ($class, $scfg, $zvol) = @_;
297
298 $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
299 }
300
301 sub zfs_list_zvol {
302 my ($class, $scfg) = @_;
303
304 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
305 my $zvols = zfs_parse_zvol_list($text);
306 return undef if !$zvols;
307
308 my $list = ();
309 foreach my $zvol (@$zvols) {
310 my @values = split('/', $zvol->{name});
311
312 my $image = pop @values;
313 my $pool = join('/', @values);
314
315 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
316 my $owner = $3;
317
318 my $parent = $zvol->{origin};
319 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
320 $parent = $1;
321 }
322
323 $list->{$pool}->{$image} = {
324 name => $image,
325 size => $zvol->{size},
326 parent => $parent,
327 format => 'raw',
328 vmid => $owner
329 };
330 }
331
332 return $list;
333 }
334
335 sub zfs_find_free_diskname {
336 my ($class, $storeid, $scfg, $vmid) = @_;
337
338 my $name = undef;
339 my $volumes = $class->zfs_list_zvol($scfg);
340
341 my $disk_ids = {};
342 my $dat = $volumes->{$scfg->{pool}};
343
344 foreach my $image (keys %$dat) {
345 my $volname = $dat->{$image}->{name};
346 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
347 $disk_ids->{$2} = 1;
348 }
349 }
350
351 for (my $i = 1; $i < 100; $i++) {
352 if (!$disk_ids->{$i}) {
353 return "vm-$vmid-disk-$i";
354 }
355 }
356
357 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
358 }
359
360 sub zfs_get_latest_snapshot {
361 my ($class, $scfg, $volname) = @_;
362
363 # abort rollback if snapshot is not the latest
364 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
365 my $text = zfs_request($class, $scfg, undef, 'list', @params);
366 my @snapshots = split(/\n/, $text);
367
368 my $recentsnap;
369 foreach (@snapshots) {
370 if (/$scfg->{pool}\/$volname/) {
371 s/^.*@//;
372 $recentsnap = $_;
373 }
374 }
375
376 return $recentsnap;
377 }
378
379 sub status {
380 my ($class, $storeid, $scfg, $cache) = @_;
381
382 my $total = 0;
383 my $free = 0;
384 my $used = 0;
385 my $active = 0;
386
387 eval {
388 ($free, $used) = $class->zfs_get_pool_stats($scfg);
389 $active = 1;
390 $total = $free + $used;
391 };
392 warn $@ if $@;
393
394 return ($total, $free, $used, $active);
395 }
396
397 sub volume_size_info {
398 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
399
400 return $class->zfs_get_zvol_size($scfg, $volname);
401 }
402
403 sub volume_snapshot {
404 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
405
406 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
407 }
408
409 sub volume_snapshot_delete {
410 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
411
412 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
413 }
414
415 sub volume_snapshot_rollback {
416 my ($class, $scfg, $storeid, $volname, $snap) = @_;
417
418 # abort rollback if snapshot is not the latest
419 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
420 if ($snap ne $recentsnap) {
421 die "cannot rollback, more recent snapshots exist\n";
422 }
423
424 zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
425 }
426
427 sub activate_storage {
428 my ($class, $storeid, $scfg, $cache) = @_;
429 return 1;
430 }
431
432 sub deactivate_storage {
433 my ($class, $storeid, $scfg, $cache) = @_;
434 return 1;
435 }
436
437 sub activate_volume {
438 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
439 return 1;
440 }
441
442 sub deactivate_volume {
443 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
444 return 1;
445 }
446
447 sub clone_image {
448 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
449
450 $snap ||= '__base__';
451
452 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
453 $class->parse_volname($volname);
454
455 die "clone_image only works on base images\n" if !$isBase;
456
457 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
458
459 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
460
461 return $name;
462 }
463
464 sub create_base {
465 my ($class, $storeid, $scfg, $volname) = @_;
466
467 my $snap = '__base__';
468
469 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
470 $class->parse_volname($volname);
471
472 die "create_base not possible with base image\n" if $isBase;
473
474 my $newname = $name;
475 $newname =~ s/^vm-/base-/;
476
477 my $newvolname = $basename ? "$basename/$newname" : "$newname";
478
479 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
480
481 my $running = undef; #fixme : is create_base always offline ?
482
483 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
484
485 return $newvolname;
486 }
487
488 sub volume_has_feature {
489 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
490
491 my $features = {
492 snapshot => { current => 1, snap => 1},
493 clone => { base => 1},
494 template => { current => 1},
495 copy => { base => 1, current => 1},
496 };
497
498 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
499 $class->parse_volname($volname);
500
501 my $key = undef;
502
503 if ($snapname) {
504 $key = 'snap';
505 } else {
506 $key = $isBase ? 'base' : 'current';
507 }
508
509 return 1 if $features->{$feature}->{$key};
510
511 return undef;
512 }
513
514 1;