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