]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
remove running from Storage and check it in QemuServer
[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, rootdir => 1}, {images => 1 , rootdir => 1}],
20 format => [ { raw => 1, subvol => 1 } , 'raw' ],
21 };
22 }
23
24 sub 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
37 sub options {
38 return {
39 pool => { fixed => 1 },
40 blocksize => { optional => 1 },
41 sparse => { optional => 1 },
42 nodes => { optional => 1 },
43 disable => { optional => 1 },
44 maxfiles => { optional => 1 },
45 content => { optional => 1 },
46 };
47 }
48
49 # static zfs helper methods
50
51 sub 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
87 sub 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) {
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);
116 }
117 $zvol->{format} = 'subvol';
118 } else {
119 $zvol->{size} = zfs_parse_size($size);
120 $zvol->{format} = 'raw';
121 }
122 if ($origin !~ /^-$/) {
123 $zvol->{origin} = $origin;
124 }
125 push @$list, $zvol;
126 }
127
128 return $list;
129 }
130
131 sub parse_volname {
132 my ($class, $volname) = @_;
133
134 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm|subvol)?-(\d+)-\S+)$/) {
135 return ('images', $5, $8, $2, $4, $6);
136 }
137
138 die "unable to parse zfs volume name '$volname'\n";
139 }
140
141 # virtual zfs methods (subclass can overwrite them)
142
143 sub path {
144 my ($class, $scfg, $volname) = @_;
145
146 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
147
148 my $path = '';
149
150 if ($vtype eq "images") {
151 if ($volname =~ m/^subvol-/) {
152 # fixme: we currently assume standard mount point?!
153 $path = "$scfg->{pool}/$volname";
154 } else {
155 $path = "/dev/zvol/$scfg->{pool}/$volname";
156 }
157 } else {
158 die "$vtype is not allowed in ZFSPool!";
159 }
160
161 return ($path, $vmid, $vtype);
162 }
163
164 sub 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') {
172 push @$cmd, 'zpool', 'list';
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
186 run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
187
188 return $msg;
189 }
190
191 sub alloc_image {
192 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
193
194 my $volname = $name;
195
196 if ($fmt eq 'raw') {
197
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;
202
203 $class->zfs_create_zvol($scfg, $volname, $size);
204 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
205
206 run_command("udevadm trigger --subsystem-match block");
207 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
208
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-/;
214
215 $class->zfs_create_subvol($scfg, $volname, $size);
216
217 } else {
218 die "unsupported format '$fmt'";
219 }
220
221 return $volname;
222 }
223
224 sub 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
234 sub 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 }
269 return $res;
270 }
271
272 sub 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
294 sub 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
306 sub 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
320 sub 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
330 sub zfs_delete_zvol {
331 my ($class, $scfg, $zvol) = @_;
332
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);
341 } else {
342 die $err;
343 }
344 } else {
345 last;
346 }
347 }
348
349 die $err if $err;
350 }
351
352 sub zfs_list_zvol {
353 my ($class, $scfg) = @_;
354
355 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
356 my $zvols = zfs_parse_zvol_list($text);
357 return undef if !$zvols;
358
359 my $list = ();
360 foreach my $zvol (@$zvols) {
361 my $pool = $zvol->{pool};
362 my $name = $zvol->{name};
363 my $parent = $zvol->{origin};
364 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
365 $parent = $1;
366 }
367
368 $list->{$pool}->{$name} = {
369 name => $name,
370 size => $zvol->{size},
371 parent => $parent,
372 format => $zvol->{format},
373 vmid => $zvol->{owner},
374 };
375 }
376
377 return $list;
378 }
379
380 sub zfs_find_free_diskname {
381 my ($class, $storeid, $scfg, $vmid) = @_;
382
383 my $name = undef;
384 my $volumes = $class->zfs_list_zvol($scfg);
385
386 my $disk_ids = {};
387 my $dat = $volumes->{$scfg->{pool}};
388
389 foreach my $image (keys %$dat) {
390 my $volname = $dat->{$image}->{name};
391 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
392 $disk_ids->{$2} = 1;
393 }
394 }
395
396 for (my $i = 1; $i < 100; $i++) {
397 if (!$disk_ids->{$i}) {
398 return "vm-$vmid-disk-$i";
399 }
400 }
401
402 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
403 }
404
405 sub zfs_get_latest_snapshot {
406 my ($class, $scfg, $volname) = @_;
407
408 # abort rollback if snapshot is not the latest
409 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
410 my $text = zfs_request($class, $scfg, undef, 'list', @params);
411 my @snapshots = split(/\n/, $text);
412
413 my $recentsnap;
414 foreach (@snapshots) {
415 if (/$scfg->{pool}\/$volname/) {
416 s/^.*@//;
417 $recentsnap = $_;
418 }
419 }
420
421 return $recentsnap;
422 }
423
424 sub status {
425 my ($class, $storeid, $scfg, $cache) = @_;
426
427 my $total = 0;
428 my $free = 0;
429 my $used = 0;
430 my $active = 0;
431
432 eval {
433 ($free, $used) = $class->zfs_get_pool_stats($scfg);
434 $active = 1;
435 $total = $free + $used;
436 };
437 warn $@ if $@;
438
439 return ($total, $free, $used, $active);
440 }
441
442 sub volume_size_info {
443 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
444
445 return $class->zfs_get_zvol_size($scfg, $volname);
446 }
447
448 sub volume_snapshot {
449 my ($class, $scfg, $storeid, $volname, $snap) = @_;
450
451 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
452 }
453
454 sub volume_snapshot_delete {
455 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
456
457 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
458 }
459
460 sub volume_snapshot_rollback {
461 my ($class, $scfg, $storeid, $volname, $snap) = @_;
462
463 zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
464 }
465
466 sub volume_rollback_is_possible {
467 my ($class, $scfg, $storeid, $volname, $snap) = @_;
468
469 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
470 if ($snap ne $recentsnap) {
471 die "can't rollback, more recent snapshots exist\n";
472 }
473
474 return 1;
475 }
476
477 sub activate_storage {
478 my ($class, $storeid, $scfg, $cache) = @_;
479
480 my @param = ('-o', 'name', '-H');
481
482 my $text = zfs_request($class, $scfg, undef, 'zpool_list', @param);
483
484 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
485 my $pool = $scfg->{pool};
486 $pool =~ s!/.*$!!;
487
488 if ($text !~ $pool) {
489 run_command("zpool import -d /dev/disk/by-id/ -a");
490 }
491 return 1;
492 }
493
494 sub deactivate_storage {
495 my ($class, $storeid, $scfg, $cache) = @_;
496 return 1;
497 }
498
499 sub activate_volume {
500 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
501 return 1;
502 }
503
504 sub deactivate_volume {
505 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
506 return 1;
507 }
508
509 sub clone_image {
510 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
511
512 $snap ||= '__base__';
513
514 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
515 $class->parse_volname($volname);
516
517 die "clone_image only works on base images\n" if !$isBase;
518
519 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
520
521 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
522
523 return $name;
524 }
525
526 sub create_base {
527 my ($class, $storeid, $scfg, $volname) = @_;
528
529 my $snap = '__base__';
530
531 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
532 $class->parse_volname($volname);
533
534 die "create_base not possible with base image\n" if $isBase;
535
536 my $newname = $name;
537 $newname =~ s/^vm-/base-/;
538
539 my $newvolname = $basename ? "$basename/$newname" : "$newname";
540
541 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
542
543 my $running = undef; #fixme : is create_base always offline ?
544
545 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
546
547 return $newvolname;
548 }
549
550 sub volume_resize {
551 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
552
553 my $new_size = ($size/1024);
554
555 $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
556
557 return $new_size;
558 }
559
560 sub volume_has_feature {
561 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
562
563 my $features = {
564 snapshot => { current => 1, snap => 1},
565 clone => { base => 1},
566 template => { current => 1},
567 copy => { base => 1, current => 1},
568 };
569
570 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
571 $class->parse_volname($volname);
572
573 my $key = undef;
574
575 if ($snapname) {
576 $key = 'snap';
577 } else {
578 $key = $isBase ? 'base' : 'current';
579 }
580
581 return 1 if $features->{$feature}->{$key};
582
583 return undef;
584 }
585
586 1;