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