]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/ZFSPlugin.pm
Code clean up
[pve-storage.git] / PVE / Storage / ZFSPlugin.pm
CommitLineData
4f914e6e
MR
1package PVE::Storage::ZFSPlugin;
2
3use strict;
4use warnings;
5use IO::File;
6use POSIX;
5332e6c9 7use PVE::Tools qw(run_command);
4f914e6e 8use PVE::Storage::Plugin;
4f914e6e
MR
9
10use base qw(PVE::Storage::Plugin);
a7d56be6
MR
11use PVE::Storage::LunCmd::Comstar;
12use PVE::Storage::LunCmd::Istgt;
4f914e6e
MR
13
14my @ssh_opts = ('-o', 'BatchMode=yes');
15my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
16
a7d56be6
MR
17my $lun_cmds = {
18 create_lu => 1,
19 delete_lu => 1,
20 import_lu => 1,
21 modify_lu => 1,
22 add_view => 1,
23 list_view => 1,
24 list_lu => 1,
25};
26
27my $zfs_unknown_scsi_provider = sub {
28 my ($provider) = @_;
29
30 die "$provider: unknown iscsi provider. Available [comstar, istgt]";
31};
32
33my $zfs_get_base = sub {
34 my ($scfg) = @_;
35
36 if ($scfg->{iscsiprovider} eq 'comstar') {
37 return PVE::Storage::LunCmd::Comstar::get_base;
38 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
39 return PVE::Storage::LunCmd::Istgt::get_base;
40 } else {
41 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
42 }
43};
44
4f914e6e
MR
45sub zfs_request {
46 my ($scfg, $timeout, $method, @params) = @_;
47
5332e6c9 48 my $cmdmap;
4f914e6e
MR
49 my $zfscmd;
50 my $target;
a7d56be6 51 my $msg;
4f914e6e 52
5332e6c9 53 $timeout = 5 if !$timeout;
4f914e6e 54
a7d56be6
MR
55 if ($lun_cmds->{$method}) {
56 if ($scfg->{iscsiprovider} eq 'comstar') {
57 $msg = PVE::Storage::LunCmd::Comstar::run_lun_command($scfg, $timeout, $method, @params);
58 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
59 $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params);
60 } else {
61 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
62 }
63 } else {
64 if ($method eq 'zpool_list') {
65 $zfscmd = 'zpool';
66 $method = 'list',
67 } else {
68 $zfscmd = 'zfs';
69 }
70
71 $target = 'root@' . $scfg->{portal};
72
73 my $cmd = [@ssh_cmd, $target, $zfscmd, $method, @params];
74
75 $msg = '';
76
77 my $output = sub {
78 my $line = shift;
79 $msg .= "$line\n";
80 };
81
82 run_command($cmd, outfunc => $output, timeout => $timeout);
83 }
4f914e6e
MR
84
85 return $msg;
86}
87
88sub zfs_parse_size {
89 my ($text) = @_;
90
91 return 0 if !$text;
92
93 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
94 my ($size, $reminder, $unit) = ($1, $2, $3);
95 return $size if !$unit;
96 if ($unit eq 'K') {
97 $size *= 1024;
98 } elsif ($unit eq 'M') {
99 $size *= 1024*1024;
100 } elsif ($unit eq 'G') {
101 $size *= 1024*1024*1024;
102 } elsif ($unit eq 'T') {
103 $size *= 1024*1024*1024*1024;
104 }
105
106 if ($reminder) {
107 $size = ceil($size);
108 }
109 return $size;
110 } else {
111 return 0;
112 }
113}
114
115sub zfs_get_pool_stats {
5332e6c9 116 my ($scfg) = @_;
4f914e6e 117
1fca1464 118 my $available = 0;
5332e6c9 119 my $used = 0;
4f914e6e 120
5332e6c9
DM
121 my $text = zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
122 'available,used', $scfg->{pool});
4f914e6e 123
5332e6c9 124 my @lines = split /\n/, $text;
4f914e6e 125
5332e6c9 126 if($lines[0] =~ /^(\d+)$/) {
1fca1464 127 $available = $1;
5332e6c9 128 }
4f914e6e 129
5332e6c9
DM
130 if($lines[1] =~ /^(\d+)$/) {
131 $used = $1;
132 }
4f914e6e 133
1fca1464 134 return ($available, $used);
4f914e6e
MR
135}
136
137sub zfs_parse_zvol_list {
138 my ($text) = @_;
139
140 my $list = ();
141
142 return $list if !$text;
143
144 my @lines = split /\n/, $text;
145 foreach my $line (@lines) {
146 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
147 my $zvol = {};
148 my $name;
149 my $disk;
150 my @zvols = split /\//, $1;
151 my $pool = $zvols[0];
152
153 if (scalar(@zvols) == 2 && $zvols[0] !~ /^rpool$/) {
154 $disk = $zvols[1];
155 next unless $disk =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
156 $name = $pool . '/' . $disk;
5332e6c9 157 } else {
4f914e6e
MR
158 next;
159 }
160
161 $zvol->{name} = $name;
162 $zvol->{size} = zfs_parse_size($2);
163 if ($3 !~ /^-$/) {
164 $zvol->{origin} = $3;
165 }
166 push @$list, $zvol;
167 }
168 }
169
170 return $list;
171}
172
173sub zfs_get_lu_name {
174 my ($scfg, $zvol) = @_;
175 my $object;
176
a7d56be6 177 my $base = $zfs_get_base->($scfg);
4f914e6e 178 if ($zvol =~ /^.+\/.+/) {
a7d56be6 179 $object = "$base/$zvol";
5332e6c9 180 } else {
a7d56be6 181 $object = "$base/$scfg->{pool}/$zvol";
4f914e6e
MR
182 }
183
a7d56be6
MR
184 my $lu_name = zfs_request($scfg, undef, 'list_lu', $object);
185
186 return $lu_name if $lu_name;
187
4f914e6e
MR
188 die "Could not find lu_name for zvol $zvol";
189}
190
191sub zfs_get_zvol_size {
192 my ($scfg, $zvol) = @_;
193
194 my $text = zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
195
196 if($text =~ /volsize\s(\d+)/){
197 return $1;
198 }
199
200 die "Could not get zvol size";
201}
202
203sub zfs_add_lun_mapping_entry {
204 my ($scfg, $zvol, $guid) = @_;
205
206 if (! defined($guid)) {
207 $guid = zfs_get_lu_name($scfg, $zvol);
208 }
a7d56be6 209
4f914e6e
MR
210 zfs_request($scfg, undef, 'add_view', $guid);
211}
212
213sub zfs_delete_lu {
214 my ($scfg, $zvol) = @_;
215
216 my $guid = zfs_get_lu_name($scfg, $zvol);
217
218 zfs_request($scfg, undef, 'delete_lu', $guid);
219}
220
221sub zfs_create_lu {
222 my ($scfg, $zvol) = @_;
223
a7d56be6
MR
224 my $base = $zfs_get_base->($scfg);
225 my $guid = zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
4f914e6e
MR
226
227 return $guid;
228}
229
230sub zfs_import_lu {
231 my ($scfg, $zvol) = @_;
232
a7d56be6
MR
233 my $base = $zfs_get_base->($scfg);
234 zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
4f914e6e
MR
235}
236
237sub zfs_resize_lu {
238 my ($scfg, $zvol, $size) = @_;
239
240 my $guid = zfs_get_lu_name($scfg, $zvol);
241
a7d56be6 242 zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid);
4f914e6e
MR
243}
244
245sub zfs_create_zvol {
246 my ($scfg, $zvol, $size) = @_;
247
248 zfs_request($scfg, undef, 'create', '-b', $scfg->{blocksize}, '-V', "${size}k", "$scfg->{pool}/$zvol");
249}
250
251sub zfs_delete_zvol {
252 my ($scfg, $zvol) = @_;
253
254 zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
255}
256
257sub zfs_get_lun_number {
258 my ($scfg, $guid) = @_;
4f914e6e
MR
259
260 die "could not find lun_number for guid $guid" if !$guid;
261
a7d56be6 262 return zfs_request($scfg, undef, 'list_view', $guid);
4f914e6e
MR
263}
264
265sub zfs_list_zvol {
266 my ($scfg) = @_;
267
268 my $text = zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-Hr');
269 my $zvols = zfs_parse_zvol_list($text);
270 return undef if !$zvols;
271
272 my $list = ();
273 foreach my $zvol (@$zvols) {
274 my @values = split('/', $zvol->{name});
275
276 my $pool = $values[0];
277 my $image = $values[1];
5332e6c9
DM
278
279 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
280 my $owner = $3;
4f914e6e
MR
281
282 my $parent = $zvol->{origin};
283 if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
284 $parent = $1;
285 }
286
287 $list->{$pool}->{$image} = {
288 name => $image,
289 size => $zvol->{size},
290 parent => $parent,
291 format => 'raw',
292 vmid => $owner
293 };
294 }
295
296 return $list;
297}
298
299# Configuration
300
301sub type {
302 return 'zfs';
303}
304
305sub plugindata {
306 return {
307 content => [ {images => 1}, { images => 1 }],
308 };
309}
310
311sub properties {
312 return {
4f914e6e
MR
313 iscsiprovider => {
314 description => "iscsi provider",
315 type => 'string',
316 },
7ecc43ed
AD
317 blocksize => {
318 description => "block size",
319 type => 'string',
320 }
4f914e6e
MR
321 };
322}
323
324sub options {
325 return {
a7d56be6
MR
326 nodes => { optional => 1 },
327 disable => { optional => 1 },
328 portal => { fixed => 1 },
4f914e6e 329 target => { fixed => 1 },
a7d56be6 330 pool => { fixed => 1 },
4f914e6e
MR
331 blocksize => { fixed => 1 },
332 iscsiprovider => { fixed => 1 },
4f914e6e
MR
333 content => { optional => 1 },
334 };
335}
336
337# Storage implementation
338
339sub parse_volname {
340 my ($class, $volname) = @_;
341
342 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
343 return ('images', $5, $8, $2, $4, $6);
344 }
345
346 die "unable to parse zfs volume name '$volname'\n";
347}
348
349sub path {
350 my ($class, $scfg, $volname) = @_;
351
352 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
353
354 my $target = $scfg->{target};
355 my $portal = $scfg->{portal};
356
357 my $guid = zfs_get_lu_name($scfg, $name);
358 my $lun = zfs_get_lun_number($scfg, $guid);
a7d56be6 359
4f914e6e 360 my $path = "iscsi://$portal/$target/$lun";
a7d56be6 361
4f914e6e
MR
362 return ($path, $vmid, $vtype);
363}
364
365my $find_free_diskname = sub {
366 my ($storeid, $scfg, $vmid) = @_;
367
368 my $name = undef;
369 my $volumes = zfs_list_zvol($scfg);
370
371 my $disk_ids = {};
372 my $dat = $volumes->{$scfg->{pool}};
373
374 foreach my $image (keys %$dat) {
375 my $volname = $dat->{$image}->{name};
376 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
377 $disk_ids->{$2} = 1;
378 }
379 }
380
381 for (my $i = 1; $i < 100; $i++) {
382 if (!$disk_ids->{$i}) {
383 return "vm-$vmid-disk-$i";
384 }
385 }
386
387 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
388};
389
390sub create_base {
391 my ($class, $storeid, $scfg, $volname) = @_;
392
393 my $snap = '__base__';
394
395 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
396 $class->parse_volname($volname);
397
398 die "create_base not possible with base image\n" if $isBase;
399
400 my $newname = $name;
401 $newname =~ s/^vm-/base-/;
402
403 my $newvolname = $basename ? "$basename/$newname" : "$newname";
404
405 zfs_delete_lu($scfg, $name);
406 zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
407
408 my $guid = zfs_create_lu($scfg, $newname);
409 zfs_add_lun_mapping_entry($scfg, $newname, $guid);
410
411 my $running = undef; #fixme : is create_base always offline ?
412
413 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
414
415 return $newvolname;
416}
417
418sub clone_image {
419 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
420
421 my $snap = '__base__';
422
423 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
424 $class->parse_volname($volname);
425
426 die "clone_image only works on base images\n" if !$isBase;
427
428 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
429
430 warn "clone $volname: $basename to $name\n";
431
432 zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
433
434 my $guid = zfs_create_lu($scfg, $name);
435 zfs_add_lun_mapping_entry($scfg, $name, $guid);
436
437 return $name;
438}
439
440sub alloc_image {
441 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
442
443 die "unsupported format '$fmt'" if $fmt ne 'raw';
444
445 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
446 if $name && $name !~ m/^vm-$vmid-/;
447
448 $name = &$find_free_diskname($storeid, $scfg, $vmid);
449
450 zfs_create_zvol($scfg, $name, $size);
451 my $guid = zfs_create_lu($scfg, $name);
452 zfs_add_lun_mapping_entry($scfg, $name, $guid);
453
454 return $name;
455}
456
457sub free_image {
458 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
459
460 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
461
462 zfs_delete_lu($scfg, $name);
463 eval {
464 zfs_delete_zvol($scfg, $name);
465 };
466 do {
467 my $err = $@;
468 my $guid = zfs_create_lu($scfg, $name);
469 zfs_add_lun_mapping_entry($scfg, $name, $guid);
470 die $err;
471 } if $@;
472
473 return undef;
474}
475
476sub list_images {
477 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
478
479 $cache->{zfs} = zfs_list_zvol($scfg) if !$cache->{zfs};
480 my $zfspool = $scfg->{pool};
481 my $res = [];
482
483 if (my $dat = $cache->{zfs}->{$zfspool}) {
484
485 foreach my $image (keys %$dat) {
486
487 my $volname = $dat->{$image}->{name};
488 my $parent = $dat->{$image}->{parent};
489
490 my $volid = undef;
491 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
492 my ($basename) = ($1);
493 $volid = "$storeid:$basename/$volname";
494 } else {
495 $volid = "$storeid:$volname";
496 }
497
498 my $owner = $dat->{$volname}->{vmid};
499 if ($vollist) {
500 my $found = grep { $_ eq $volid } @$vollist;
501 next if !$found;
502 } else {
503 next if defined ($vmid) && ($owner ne $vmid);
504 }
505
506 my $info = $dat->{$volname};
507 $info->{volid} = $volid;
508 push @$res, $info;
509 }
510 }
511
512 return $res;
513}
514
515sub status {
516 my ($class, $storeid, $scfg, $cache) = @_;
517
518 my $total = 0;
519 my $free = 0;
520 my $used = 0;
521 my $active = 0;
522
523 eval {
1fca1464 524 ($free, $used) = zfs_get_pool_stats($scfg);
4f914e6e 525 $active = 1;
1fca1464 526 $total = $free + $used;
4f914e6e
MR
527 };
528 warn $@ if $@;
529
530 return ($total, $free, $used, $active);
531}
532
533sub activate_storage {
534 my ($class, $storeid, $scfg, $cache) = @_;
535 return 1;
536}
537
538sub deactivate_storage {
539 my ($class, $storeid, $scfg, $cache) = @_;
540 return 1;
541}
542
543sub activate_volume {
544 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
545 return 1;
546}
547
548sub deactivate_volume {
549 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
550 return 1;
551}
552
553sub volume_size_info {
554 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
555
556 return zfs_get_zvol_size($scfg, $volname);
557}
558
559sub volume_resize {
560 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
561
562 my $new_size = ($size/1024);
563
564 zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
565 zfs_resize_lu($scfg, $volname, $new_size);
566}
567
568sub volume_snapshot {
569 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
570
571 zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
572}
573
574sub volume_snapshot_rollback {
575 my ($class, $scfg, $storeid, $volname, $snap) = @_;
576
577 zfs_delete_lu($scfg, $volname);
578
579 zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
580
581 zfs_import_lu($scfg, $volname);
582
583 zfs_add_lun_mapping_entry($scfg, $volname);
584}
585
586sub volume_snapshot_delete {
587 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
588
589 zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
590}
591
592sub volume_has_feature {
593 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
594
595 my $features = {
596 snapshot => { current => 1, snap => 1},
597 clone => { base => 1},
598 template => { current => 1},
599 copy => { base => 1, current => 1},
600 };
601
602 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
603 $class->parse_volname($volname);
604
605 my $key = undef;
5332e6c9
DM
606
607 if ($snapname) {
4f914e6e
MR
608 $key = 'snap';
609 } else {
610 $key = $isBase ? 'base' : 'current';
611 }
5332e6c9 612
4f914e6e
MR
613 return 1 if $features->{$feature}->{$key};
614
615 return undef;
616}
617
6181;