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