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