]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Storage::ZFSPlugin; | |
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 | use base qw(PVE::Storage::Plugin); | |
11 | use PVE::Storage::LunCmd::Comstar; | |
12 | use PVE::Storage::LunCmd::Istgt; | |
13 | ||
14 | my @ssh_opts = ('-o', 'BatchMode=yes'); | |
15 | my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts); | |
16 | ||
17 | my $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 | ||
27 | my $zfs_unknown_scsi_provider = sub { | |
28 | my ($provider) = @_; | |
29 | ||
30 | die "$provider: unknown iscsi provider. Available [comstar, istgt]"; | |
31 | }; | |
32 | ||
33 | my $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 | ||
45 | sub zfs_request { | |
46 | my ($scfg, $timeout, $method, @params) = @_; | |
47 | ||
48 | my $cmdmap; | |
49 | my $zfscmd; | |
50 | my $target; | |
51 | my $msg; | |
52 | ||
53 | $timeout = 5 if !$timeout; | |
54 | ||
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 | } | |
84 | ||
85 | return $msg; | |
86 | } | |
87 | ||
88 | sub 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 | ||
115 | sub zfs_get_pool_stats { | |
116 | my ($scfg) = @_; | |
117 | ||
118 | my $available = 0; | |
119 | my $used = 0; | |
120 | ||
121 | my $text = zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp', | |
122 | 'available,used', $scfg->{pool}); | |
123 | ||
124 | my @lines = split /\n/, $text; | |
125 | ||
126 | if($lines[0] =~ /^(\d+)$/) { | |
127 | $available = $1; | |
128 | } | |
129 | ||
130 | if($lines[1] =~ /^(\d+)$/) { | |
131 | $used = $1; | |
132 | } | |
133 | ||
134 | return ($available, $used); | |
135 | } | |
136 | ||
137 | sub 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; | |
157 | } else { | |
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 | ||
173 | sub zfs_get_lu_name { | |
174 | my ($scfg, $zvol) = @_; | |
175 | my $object; | |
176 | ||
177 | my $base = $zfs_get_base->($scfg); | |
178 | if ($zvol =~ /^.+\/.+/) { | |
179 | $object = "$base/$zvol"; | |
180 | } else { | |
181 | $object = "$base/$scfg->{pool}/$zvol"; | |
182 | } | |
183 | ||
184 | my $lu_name = zfs_request($scfg, undef, 'list_lu', $object); | |
185 | ||
186 | return $lu_name if $lu_name; | |
187 | ||
188 | die "Could not find lu_name for zvol $zvol"; | |
189 | } | |
190 | ||
191 | sub 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 | ||
203 | sub zfs_add_lun_mapping_entry { | |
204 | my ($scfg, $zvol, $guid) = @_; | |
205 | ||
206 | if (! defined($guid)) { | |
207 | $guid = zfs_get_lu_name($scfg, $zvol); | |
208 | } | |
209 | ||
210 | zfs_request($scfg, undef, 'add_view', $guid); | |
211 | } | |
212 | ||
213 | sub 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 | ||
221 | sub zfs_create_lu { | |
222 | my ($scfg, $zvol) = @_; | |
223 | ||
224 | my $base = $zfs_get_base->($scfg); | |
225 | my $guid = zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol"); | |
226 | ||
227 | return $guid; | |
228 | } | |
229 | ||
230 | sub zfs_import_lu { | |
231 | my ($scfg, $zvol) = @_; | |
232 | ||
233 | my $base = $zfs_get_base->($scfg); | |
234 | zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol"); | |
235 | } | |
236 | ||
237 | sub zfs_resize_lu { | |
238 | my ($scfg, $zvol, $size) = @_; | |
239 | ||
240 | my $guid = zfs_get_lu_name($scfg, $zvol); | |
241 | ||
242 | zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid); | |
243 | } | |
244 | ||
245 | sub 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 | ||
251 | sub zfs_delete_zvol { | |
252 | my ($scfg, $zvol) = @_; | |
253 | ||
254 | zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); | |
255 | } | |
256 | ||
257 | sub zfs_get_lun_number { | |
258 | my ($scfg, $guid) = @_; | |
259 | ||
260 | die "could not find lun_number for guid $guid" if !$guid; | |
261 | ||
262 | return zfs_request($scfg, undef, 'list_view', $guid); | |
263 | } | |
264 | ||
265 | sub 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]; | |
278 | ||
279 | next if $image !~ m/^((vm|base)-(\d+)-\S+)$/; | |
280 | my $owner = $3; | |
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 | ||
301 | sub type { | |
302 | return 'zfs'; | |
303 | } | |
304 | ||
305 | sub plugindata { | |
306 | return { | |
307 | content => [ {images => 1}, { images => 1 }], | |
308 | }; | |
309 | } | |
310 | ||
311 | sub properties { | |
312 | return { | |
313 | iscsiprovider => { | |
314 | description => "iscsi provider", | |
315 | type => 'string', | |
316 | }, | |
317 | blocksize => { | |
318 | description => "block size", | |
319 | type => 'string', | |
320 | } | |
321 | }; | |
322 | } | |
323 | ||
324 | sub options { | |
325 | return { | |
326 | nodes => { optional => 1 }, | |
327 | disable => { optional => 1 }, | |
328 | portal => { fixed => 1 }, | |
329 | target => { fixed => 1 }, | |
330 | pool => { fixed => 1 }, | |
331 | blocksize => { fixed => 1 }, | |
332 | iscsiprovider => { fixed => 1 }, | |
333 | content => { optional => 1 }, | |
334 | }; | |
335 | } | |
336 | ||
337 | # Storage implementation | |
338 | ||
339 | sub 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 | ||
349 | sub 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); | |
359 | ||
360 | my $path = "iscsi://$portal/$target/$lun"; | |
361 | ||
362 | return ($path, $vmid, $vtype); | |
363 | } | |
364 | ||
365 | my $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 | ||
390 | sub 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 | ||
418 | sub 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 | ||
440 | sub 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 | ||
457 | sub 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 | ||
476 | sub 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 | ||
515 | sub 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 { | |
524 | ($free, $used) = zfs_get_pool_stats($scfg); | |
525 | $active = 1; | |
526 | $total = $free + $used; | |
527 | }; | |
528 | warn $@ if $@; | |
529 | ||
530 | return ($total, $free, $used, $active); | |
531 | } | |
532 | ||
533 | sub activate_storage { | |
534 | my ($class, $storeid, $scfg, $cache) = @_; | |
535 | return 1; | |
536 | } | |
537 | ||
538 | sub deactivate_storage { | |
539 | my ($class, $storeid, $scfg, $cache) = @_; | |
540 | return 1; | |
541 | } | |
542 | ||
543 | sub activate_volume { | |
544 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
545 | return 1; | |
546 | } | |
547 | ||
548 | sub deactivate_volume { | |
549 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
550 | return 1; | |
551 | } | |
552 | ||
553 | sub volume_size_info { | |
554 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
555 | ||
556 | return zfs_get_zvol_size($scfg, $volname); | |
557 | } | |
558 | ||
559 | sub 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 | ||
568 | sub volume_snapshot { | |
569 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
570 | ||
571 | zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap"); | |
572 | } | |
573 | ||
574 | sub 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 | ||
586 | sub volume_snapshot_delete { | |
587 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
588 | ||
589 | zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap"); | |
590 | } | |
591 | ||
592 | sub 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; | |
606 | ||
607 | if ($snapname) { | |
608 | $key = 'snap'; | |
609 | } else { | |
610 | $key = $isBase ? 'base' : 'current'; | |
611 | } | |
612 | ||
613 | return 1 if $features->{$feature}->{$key}; | |
614 | ||
615 | return undef; | |
616 | } | |
617 | ||
618 | 1; |