]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Storage::RBDPlugin; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use Net::IP; | |
7 | use PVE::Tools qw(run_command trim); | |
8 | use PVE::Storage::Plugin; | |
9 | use PVE::JSONSchema qw(get_standard_option); | |
10 | ||
11 | use base qw(PVE::Storage::Plugin); | |
12 | ||
13 | sub rbd_unittobytes { | |
14 | { | |
15 | "M" => 1024*1024, | |
16 | "G" => 1024*1024*1024, | |
17 | "T" => 1024*1024*1024*1024, | |
18 | } | |
19 | } | |
20 | ||
21 | my $add_pool_to_disk = sub { | |
22 | my ($scfg, $disk) = @_; | |
23 | ||
24 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
25 | ||
26 | return "$pool/$disk"; | |
27 | }; | |
28 | ||
29 | my $hostlist = sub { | |
30 | my ($list_text, $separator) = @_; | |
31 | my @monhostlist = PVE::Tools::split_list($list_text); | |
32 | return join($separator, map { | |
33 | my ($host, $port) = PVE::Tools::parse_host_and_port($_); | |
34 | $port = defined($port) ? ":$port" : ''; | |
35 | $host = "[$host]" if Net::IP::ip_is_ipv6($host); | |
36 | "${host}${port}" | |
37 | } @monhostlist); | |
38 | }; | |
39 | ||
40 | my $rbd_cmd = sub { | |
41 | my ($scfg, $storeid, $op, @options) = @_; | |
42 | ||
43 | my $monhost = &$hostlist($scfg->{monhost}, ','); | |
44 | ||
45 | my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; | |
46 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
47 | my $username = $scfg->{username} ? $scfg->{username} : 'admin'; | |
48 | ||
49 | my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost]; | |
50 | ||
51 | if(-e $keyring){ | |
52 | push @$cmd, '-n', "client.$username"; | |
53 | push @$cmd, '--keyring', $keyring; | |
54 | push @$cmd, '--auth_supported', 'cephx'; | |
55 | }else{ | |
56 | push @$cmd, '--auth_supported', 'none'; | |
57 | } | |
58 | ||
59 | my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf"; | |
60 | ||
61 | if(-e $cephconfig){ | |
62 | push @$cmd, '-c', $cephconfig; | |
63 | } | |
64 | ||
65 | push @$cmd, $op; | |
66 | ||
67 | push @$cmd, @options if scalar(@options); | |
68 | ||
69 | return $cmd; | |
70 | }; | |
71 | ||
72 | my $rados_cmd = sub { | |
73 | my ($scfg, $storeid, $op, @options) = @_; | |
74 | ||
75 | my $monhost = &$hostlist($scfg->{monhost}, ','); | |
76 | ||
77 | my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; | |
78 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
79 | my $username = $scfg->{username} ? $scfg->{username} : 'admin'; | |
80 | ||
81 | my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost]; | |
82 | ||
83 | if(-e $keyring){ | |
84 | push @$cmd, '-n', "client.$username"; | |
85 | push @$cmd, '--keyring', $keyring; | |
86 | push @$cmd, '--auth_supported', 'cephx'; | |
87 | }else{ | |
88 | push @$cmd, '--auth_supported', 'none'; | |
89 | } | |
90 | ||
91 | my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf"; | |
92 | ||
93 | if(-e $cephconfig){ | |
94 | push @$cmd, '-c', $cephconfig; | |
95 | } | |
96 | ||
97 | push @$cmd, $op; | |
98 | ||
99 | push @$cmd, @options if scalar(@options); | |
100 | ||
101 | return $cmd; | |
102 | }; | |
103 | ||
104 | # needed for volumes created using ceph jewel (or higher) | |
105 | my $krdb_feature_disable = sub { | |
106 | my ($scfg, $storeid, $name) = @_; | |
107 | ||
108 | return 1 if !$scfg->{krbd}; | |
109 | ||
110 | my ($major, undef, undef, undef) = ceph_version(); | |
111 | return 1 if $major < 10; | |
112 | ||
113 | my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock'); | |
114 | run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name"); | |
115 | }; | |
116 | ||
117 | my $ceph_version_parser = sub { | |
118 | my $line = shift; | |
119 | if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))?$/) { | |
120 | return ($2, $3, $4, $1); | |
121 | } else { | |
122 | warn "Could not parse Ceph version: '$line'\n"; | |
123 | } | |
124 | }; | |
125 | ||
126 | sub ceph_version { | |
127 | my ($cache) = @_; | |
128 | ||
129 | my $version_string = $cache; | |
130 | ||
131 | my $major; | |
132 | my $minor; | |
133 | my $bugfix; | |
134 | ||
135 | if (defined($version_string)) { | |
136 | ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string); | |
137 | } else { | |
138 | run_command('ceph --version', outfunc => sub { | |
139 | my $line = shift; | |
140 | ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line); | |
141 | }); | |
142 | } | |
143 | return undef if !defined($version_string); | |
144 | return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string; | |
145 | } | |
146 | ||
147 | sub run_rbd_command { | |
148 | my ($cmd, %args) = @_; | |
149 | ||
150 | my $lasterr; | |
151 | my $errmsg = $args{errmsg} . ": " || ""; | |
152 | if (!exists($args{errfunc})) { | |
153 | # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect; | |
154 | # at least 1 child(ren) in pool cephstor1 | |
155 | $args{errfunc} = sub { | |
156 | my $line = shift; | |
157 | if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) { | |
158 | $lasterr = "$1\n"; | |
159 | } else { | |
160 | $lasterr = $line; | |
161 | } | |
162 | print STDERR $lasterr; | |
163 | *STDERR->flush(); | |
164 | }; | |
165 | } | |
166 | ||
167 | eval { run_command($cmd, %args); }; | |
168 | if (my $err = $@) { | |
169 | die $errmsg . $lasterr if length($lasterr); | |
170 | die $err; | |
171 | } | |
172 | ||
173 | return undef; | |
174 | } | |
175 | ||
176 | sub rbd_ls { | |
177 | my ($scfg, $storeid) = @_; | |
178 | ||
179 | my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l'); | |
180 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
181 | ||
182 | my $list = {}; | |
183 | ||
184 | my $parser = sub { | |
185 | my $line = shift; | |
186 | ||
187 | if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) { | |
188 | my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8); | |
189 | ||
190 | $list->{$pool}->{$image} = { | |
191 | name => $image, | |
192 | size => $size*rbd_unittobytes()->{$unit}, | |
193 | parent => $parent, | |
194 | vmid => $owner | |
195 | }; | |
196 | } | |
197 | }; | |
198 | ||
199 | eval { | |
200 | run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); | |
201 | }; | |
202 | my $err = $@; | |
203 | ||
204 | die $err if $err && $err !~ m/doesn't contain rbd images/ ; | |
205 | ||
206 | return $list; | |
207 | } | |
208 | ||
209 | sub rbd_volume_info { | |
210 | my ($scfg, $storeid, $volname, $snap) = @_; | |
211 | ||
212 | my $cmd = undef; | |
213 | ||
214 | if($snap){ | |
215 | $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap); | |
216 | }else{ | |
217 | $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname); | |
218 | } | |
219 | ||
220 | my $size = undef; | |
221 | my $parent = undef; | |
222 | my $format = undef; | |
223 | my $protected = undef; | |
224 | ||
225 | my $parser = sub { | |
226 | my $line = shift; | |
227 | ||
228 | if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) { | |
229 | $size = $1 * rbd_unittobytes()->{$2} if ($1); | |
230 | } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) { | |
231 | $parent = $2; | |
232 | } elsif ($line =~ m/format:\s(\d+)/) { | |
233 | $format = $1; | |
234 | } elsif ($line =~ m/protected:\s(\S+)/) { | |
235 | $protected = 1 if $1 eq "True"; | |
236 | } | |
237 | ||
238 | }; | |
239 | ||
240 | run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); | |
241 | ||
242 | return ($size, $parent, $format, $protected); | |
243 | } | |
244 | ||
245 | # Configuration | |
246 | ||
247 | sub type { | |
248 | return 'rbd'; | |
249 | } | |
250 | ||
251 | sub plugindata { | |
252 | return { | |
253 | content => [ {images => 1, rootdir => 1}, { images => 1 }], | |
254 | }; | |
255 | } | |
256 | ||
257 | sub properties { | |
258 | return { | |
259 | monhost => { | |
260 | description => "Monitors daemon ips.", | |
261 | type => 'string', format => 'pve-storage-portal-dns-list', | |
262 | }, | |
263 | pool => { | |
264 | description => "Pool.", | |
265 | type => 'string', | |
266 | }, | |
267 | username => { | |
268 | description => "RBD Id.", | |
269 | type => 'string', | |
270 | }, | |
271 | authsupported => { | |
272 | description => "Authsupported.", | |
273 | type => 'string', | |
274 | }, | |
275 | krbd => { | |
276 | description => "Access rbd through krbd kernel module.", | |
277 | type => 'boolean', | |
278 | }, | |
279 | }; | |
280 | } | |
281 | ||
282 | sub options { | |
283 | return { | |
284 | nodes => { optional => 1 }, | |
285 | disable => { optional => 1 }, | |
286 | monhost => { fixed => 1 }, | |
287 | pool => { optional => 1 }, | |
288 | username => { optional => 1 }, | |
289 | content => { optional => 1 }, | |
290 | krbd => { optional => 1 }, | |
291 | }; | |
292 | } | |
293 | ||
294 | # Storage implementation | |
295 | ||
296 | sub parse_volname { | |
297 | my ($class, $volname) = @_; | |
298 | ||
299 | if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) { | |
300 | return ('images', $4, $7, $2, $3, $5, 'raw'); | |
301 | } | |
302 | ||
303 | die "unable to parse rbd volume name '$volname'\n"; | |
304 | } | |
305 | ||
306 | sub path { | |
307 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; | |
308 | ||
309 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
310 | $name .= '@'.$snapname if $snapname; | |
311 | ||
312 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
313 | return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd}; | |
314 | ||
315 | my $monhost = &$hostlist($scfg->{monhost}, ';'); | |
316 | $monhost =~ s/:/\\:/g; | |
317 | ||
318 | my $username = $scfg->{username} ? $scfg->{username} : 'admin'; | |
319 | ||
320 | my $path = "rbd:$pool/$name:mon_host=$monhost"; | |
321 | my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; | |
322 | ||
323 | if(-e $keyring ){ | |
324 | $path .= ":id=$username:auth_supported=cephx:keyring=$keyring"; | |
325 | }else{ | |
326 | $path .= ":auth_supported=none"; | |
327 | } | |
328 | ||
329 | my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf"; | |
330 | ||
331 | if(-e $cephconfig){ | |
332 | $path .= ":conf=$cephconfig"; | |
333 | } | |
334 | ||
335 | return ($path, $vmid, $vtype); | |
336 | } | |
337 | ||
338 | my $find_free_diskname = sub { | |
339 | my ($storeid, $scfg, $vmid) = @_; | |
340 | ||
341 | my $rbd = rbd_ls($scfg, $storeid); | |
342 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
343 | my $disk_ids = {}; | |
344 | my $dat = $rbd->{$pool}; | |
345 | ||
346 | foreach my $image (keys %$dat) { | |
347 | my $volname = $dat->{$image}->{name}; | |
348 | if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){ | |
349 | $disk_ids->{$2} = 1; | |
350 | } | |
351 | } | |
352 | #fix: can we search in $rbd hash key with a regex to find (vm|base) ? | |
353 | for (my $i = 1; $i < 100; $i++) { | |
354 | if (!$disk_ids->{$i}) { | |
355 | return "vm-$vmid-disk-$i"; | |
356 | } | |
357 | } | |
358 | ||
359 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"; | |
360 | }; | |
361 | ||
362 | sub create_base { | |
363 | my ($class, $storeid, $scfg, $volname) = @_; | |
364 | ||
365 | my $snap = '__base__'; | |
366 | ||
367 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
368 | $class->parse_volname($volname); | |
369 | ||
370 | die "create_base not possible with base image\n" if $isBase; | |
371 | ||
372 | my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name); | |
373 | die "rbd volume info on '$name' failed\n" if !($size); | |
374 | ||
375 | die "rbd image must be at format V2" if $format ne "2"; | |
376 | ||
377 | die "volname '$volname' contains wrong information about parent $parent $basename\n" | |
378 | if $basename && (!$parent || $parent ne $basename."@".$snap); | |
379 | ||
380 | my $newname = $name; | |
381 | $newname =~ s/^vm-/base-/; | |
382 | ||
383 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; | |
384 | ||
385 | my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname)); | |
386 | run_rbd_command($cmd, errmsg => "rbd rename '$name' error"); | |
387 | ||
388 | my $running = undef; #fixme : is create_base always offline ? | |
389 | ||
390 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
391 | ||
392 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap); | |
393 | ||
394 | if (!$protected){ | |
395 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap); | |
396 | run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error"); | |
397 | } | |
398 | ||
399 | return $newvolname; | |
400 | ||
401 | } | |
402 | ||
403 | sub clone_image { | |
404 | my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_; | |
405 | ||
406 | my $snap = '__base__'; | |
407 | $snap = $snapname if length $snapname; | |
408 | ||
409 | my ($vtype, $basename, $basevmid, undef, undef, $isBase) = | |
410 | $class->parse_volname($volname); | |
411 | ||
412 | die "$volname is not a base image and snapname is not provided\n" | |
413 | if !$isBase && !length($snapname); | |
414 | ||
415 | my $name = &$find_free_diskname($storeid, $scfg, $vmid); | |
416 | ||
417 | warn "clone $volname: $basename snapname $snap to $name\n"; | |
418 | ||
419 | if (length($snapname)) { | |
420 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname); | |
421 | ||
422 | if (!$protected) { | |
423 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname); | |
424 | run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error"); | |
425 | } | |
426 | } | |
427 | ||
428 | my $newvol = "$basename/$name"; | |
429 | $newvol = $name if length($snapname); | |
430 | ||
431 | my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename), | |
432 | '--snap', $snap, &$add_pool_to_disk($scfg, $name)); | |
433 | ||
434 | run_rbd_command($cmd, errmsg => "rbd clone '$basename' error"); | |
435 | ||
436 | &$krdb_feature_disable($scfg, $storeid, $name); | |
437 | ||
438 | return $newvol; | |
439 | } | |
440 | ||
441 | sub alloc_image { | |
442 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
443 | ||
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) if !$name; | |
449 | ||
450 | my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name); | |
451 | run_rbd_command($cmd, errmsg => "rbd create $name' error"); | |
452 | ||
453 | &$krdb_feature_disable($scfg, $storeid, $name); | |
454 | ||
455 | return $name; | |
456 | } | |
457 | ||
458 | sub free_image { | |
459 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; | |
460 | ||
461 | my ($vtype, $name, $vmid, undef, undef, undef) = | |
462 | $class->parse_volname($volname); | |
463 | ||
464 | if ($isBase) { | |
465 | my $snap = '__base__'; | |
466 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap); | |
467 | if ($protected){ | |
468 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap); | |
469 | run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error"); | |
470 | } | |
471 | } | |
472 | ||
473 | $class->deactivate_volume($storeid, $scfg, $volname); | |
474 | ||
475 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name); | |
476 | run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error"); | |
477 | ||
478 | $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name); | |
479 | run_rbd_command($cmd, errmsg => "rbd rm '$volname' error"); | |
480 | ||
481 | return undef; | |
482 | } | |
483 | ||
484 | sub list_images { | |
485 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
486 | ||
487 | $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd}; | |
488 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
489 | ||
490 | my $res = []; | |
491 | ||
492 | if (my $dat = $cache->{rbd}->{$pool}) { | |
493 | foreach my $image (keys %$dat) { | |
494 | ||
495 | my $volname = $dat->{$image}->{name}; | |
496 | ||
497 | my $volid = "$storeid:$volname"; | |
498 | ||
499 | my $owner = $dat->{$volname}->{vmid}; | |
500 | if ($vollist) { | |
501 | my $found = grep { $_ eq $volid } @$vollist; | |
502 | next if !$found; | |
503 | } else { | |
504 | next if defined ($vmid) && ($owner ne $vmid); | |
505 | } | |
506 | ||
507 | my $info = $dat->{$volname}; | |
508 | $info->{volid} = $volid; | |
509 | $info->{format} = 'raw'; | |
510 | ||
511 | push @$res, $info; | |
512 | } | |
513 | } | |
514 | ||
515 | return $res; | |
516 | } | |
517 | ||
518 | sub status { | |
519 | my ($class, $storeid, $scfg, $cache) = @_; | |
520 | ||
521 | my $cmd = &$rados_cmd($scfg, $storeid, 'df'); | |
522 | ||
523 | my $stats = {}; | |
524 | ||
525 | my $parser = sub { | |
526 | my $line = shift; | |
527 | if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) { | |
528 | $stats->{$1} = $2; | |
529 | } | |
530 | }; | |
531 | ||
532 | eval { | |
533 | run_rbd_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser); | |
534 | }; | |
535 | ||
536 | my $total = $stats->{space} ? $stats->{space}*1024 : 0; | |
537 | my $free = $stats->{avail} ? $stats->{avail}*1024 : 0; | |
538 | my $used = $stats->{used} ? $stats->{used}*1024: 0; | |
539 | my $active = 1; | |
540 | ||
541 | return ($total, $free, $used, $active); | |
542 | } | |
543 | ||
544 | sub activate_storage { | |
545 | my ($class, $storeid, $scfg, $cache) = @_; | |
546 | return 1; | |
547 | } | |
548 | ||
549 | sub deactivate_storage { | |
550 | my ($class, $storeid, $scfg, $cache) = @_; | |
551 | return 1; | |
552 | } | |
553 | ||
554 | sub activate_volume { | |
555 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
556 | ||
557 | return 1 if !$scfg->{krbd}; | |
558 | ||
559 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
560 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
561 | ||
562 | my $path = "/dev/rbd/$pool/$name"; | |
563 | $path .= '@'.$snapname if $snapname; | |
564 | return if -b $path; | |
565 | ||
566 | $name .= '@'.$snapname if $snapname; | |
567 | my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name); | |
568 | run_rbd_command($cmd, errmsg => "can't mount rbd volume $name"); | |
569 | ||
570 | return 1; | |
571 | } | |
572 | ||
573 | sub deactivate_volume { | |
574 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
575 | ||
576 | return 1 if !$scfg->{krbd}; | |
577 | ||
578 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
579 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
580 | ||
581 | my $path = "/dev/rbd/$pool/$name"; | |
582 | $path .= '@'.$snapname if $snapname; | |
583 | return if ! -b $path; | |
584 | ||
585 | my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path); | |
586 | run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name"); | |
587 | ||
588 | return 1; | |
589 | } | |
590 | ||
591 | sub volume_size_info { | |
592 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
593 | ||
594 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
595 | my ($size, undef) = rbd_volume_info($scfg, $storeid, $name); | |
596 | return $size; | |
597 | } | |
598 | ||
599 | sub volume_resize { | |
600 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
601 | ||
602 | return 1 if $running; | |
603 | ||
604 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
605 | ||
606 | my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name); | |
607 | run_rbd_command($cmd, errmsg => "rbd resize '$volname' error"); | |
608 | return undef; | |
609 | } | |
610 | ||
611 | sub volume_snapshot { | |
612 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
613 | ||
614 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
615 | ||
616 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name); | |
617 | run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error"); | |
618 | return undef; | |
619 | } | |
620 | ||
621 | sub volume_snapshot_rollback { | |
622 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
623 | ||
624 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
625 | ||
626 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name); | |
627 | run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error"); | |
628 | } | |
629 | ||
630 | sub volume_snapshot_delete { | |
631 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
632 | ||
633 | return 1 if $running; | |
634 | ||
635 | $class->deactivate_volume($storeid, $scfg, $volname, $snap, {}); | |
636 | ||
637 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
638 | ||
639 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap); | |
640 | if ($protected){ | |
641 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap); | |
642 | run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error"); | |
643 | } | |
644 | ||
645 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name); | |
646 | ||
647 | run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error"); | |
648 | ||
649 | return undef; | |
650 | } | |
651 | ||
652 | sub volume_has_feature { | |
653 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
654 | ||
655 | my $features = { | |
656 | snapshot => { current => 1, snap => 1}, | |
657 | clone => { base => 1, snap => 1}, | |
658 | template => { current => 1}, | |
659 | copy => { base => 1, current => 1, snap => 1}, | |
660 | sparseinit => { base => 1, current => 1}, | |
661 | }; | |
662 | ||
663 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
664 | $class->parse_volname($volname); | |
665 | ||
666 | my $key = undef; | |
667 | if($snapname){ | |
668 | $key = 'snap'; | |
669 | }else{ | |
670 | $key = $isBase ? 'base' : 'current'; | |
671 | } | |
672 | return 1 if $features->{$feature}->{$key}; | |
673 | ||
674 | return undef; | |
675 | } | |
676 | ||
677 | 1; |