]>
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 | |
0509010d AD |
235 | sub type { |
236 | return 'rbd'; | |
237 | } | |
238 | ||
239 | sub plugindata { | |
240 | return { | |
1f79bb07 | 241 | content => [ {images => 1, rootdir => 1}, { images => 1 }], |
0509010d AD |
242 | }; |
243 | } | |
244 | ||
245 | sub properties { | |
246 | return { | |
e5427b00 | 247 | monhost => { |
0509010d | 248 | description => "Monitors daemon ips.", |
e858048f | 249 | type => 'string', format => 'pve-storage-portal-dns-list', |
0509010d | 250 | }, |
e5427b00 AD |
251 | pool => { |
252 | description => "Pool.", | |
0509010d AD |
253 | type => 'string', |
254 | }, | |
e5427b00 AD |
255 | username => { |
256 | description => "RBD Id.", | |
0509010d AD |
257 | type => 'string', |
258 | }, | |
e5427b00 | 259 | authsupported => { |
0509010d AD |
260 | description => "Authsupported.", |
261 | type => 'string', | |
262 | }, | |
9f20a8a6 AD |
263 | krbd => { |
264 | description => "Access rbd through krbd kernel module.", | |
265 | type => 'boolean', | |
266 | }, | |
0509010d AD |
267 | }; |
268 | } | |
269 | ||
270 | sub options { | |
271 | return { | |
35d6dfaf AD |
272 | nodes => { optional => 1 }, |
273 | disable => { optional => 1 }, | |
e5427b00 | 274 | monhost => { fixed => 1 }, |
1440604a AD |
275 | pool => { optional => 1 }, |
276 | username => { optional => 1 }, | |
0509010d | 277 | content => { optional => 1 }, |
9f20a8a6 | 278 | krbd => { optional => 1 }, |
0509010d AD |
279 | }; |
280 | } | |
281 | ||
282 | # Storage implementation | |
283 | ||
284 | sub parse_volname { | |
285 | my ($class, $volname) = @_; | |
286 | ||
d04c7e55 | 287 | if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) { |
7800e84d | 288 | return ('images', $4, $7, $2, $3, $5, 'raw'); |
0509010d AD |
289 | } |
290 | ||
291 | die "unable to parse rbd volume name '$volname'\n"; | |
292 | } | |
293 | ||
294 | sub path { | |
38e6ec3f | 295 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
0509010d AD |
296 | |
297 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
38e6ec3f | 298 | $name .= '@'.$snapname if $snapname; |
0509010d | 299 | |
33cef4c8 WB |
300 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; |
301 | return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd}; | |
302 | ||
e858048f | 303 | my $monhost = &$hostlist($scfg->{monhost}, ';'); |
a8176f54 AD |
304 | $monhost =~ s/:/\\:/g; |
305 | ||
1440604a AD |
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 AD |
316 | |
317 | return ($path, $vmid, $vtype); | |
318 | } | |
319 | ||
5b9b9b14 AD |
320 | my $find_free_diskname = sub { |
321 | my ($storeid, $scfg, $vmid) = @_; | |
322 | ||
323 | my $rbd = rbd_ls($scfg, $storeid); | |
1440604a | 324 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; |
5b9b9b14 | 325 | my $disk_ids = {}; |
1440604a | 326 | my $dat = $rbd->{$pool}; |
5b9b9b14 AD |
327 | |
328 | foreach my $image (keys %$dat) { | |
329 | my $volname = $dat->{$image}->{name}; | |
330 | if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){ | |
331 | $disk_ids->{$2} = 1; | |
332 | } | |
333 | } | |
334 | #fix: can we search in $rbd hash key with a regex to find (vm|base) ? | |
335 | for (my $i = 1; $i < 100; $i++) { | |
336 | if (!$disk_ids->{$i}) { | |
337 | return "vm-$vmid-disk-$i"; | |
338 | } | |
339 | } | |
340 | ||
341 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"; | |
342 | }; | |
343 | ||
5eab0272 DM |
344 | sub create_base { |
345 | my ($class, $storeid, $scfg, $volname) = @_; | |
346 | ||
992e6835 AD |
347 | my $snap = '__base__'; |
348 | ||
349 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
350 | $class->parse_volname($volname); | |
351 | ||
352 | die "create_base not possible with base image\n" if $isBase; | |
353 | ||
354 | my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name); | |
355 | die "rbd volume info on '$name' failed\n" if !($size); | |
356 | ||
357 | die "rbd image must be at format V2" if $format ne "2"; | |
358 | ||
359 | die "volname '$volname' contains wrong information about parent $parent $basename\n" | |
360 | if $basename && (!$parent || $parent ne $basename."@".$snap); | |
361 | ||
362 | my $newname = $name; | |
363 | $newname =~ s/^vm-/base-/; | |
364 | ||
365 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; | |
366 | ||
8897f5dc | 367 | my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname)); |
c693f749 | 368 | run_rbd_command($cmd, errmsg => "rbd rename '$name' error"); |
992e6835 AD |
369 | |
370 | my $running = undef; #fixme : is create_base always offline ? | |
371 | ||
372 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
373 | ||
374 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap); | |
375 | ||
376 | if (!$protected){ | |
377 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap); | |
2362bc87 | 378 | run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error"); |
992e6835 AD |
379 | } |
380 | ||
381 | return $newvolname; | |
382 | ||
5eab0272 DM |
383 | } |
384 | ||
385 | sub clone_image { | |
f236eaf8 | 386 | my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_; |
5eab0272 | 387 | |
f2708285 | 388 | my $snap = '__base__'; |
f236eaf8 | 389 | $snap = $snapname if length $snapname; |
f2708285 AD |
390 | |
391 | my ($vtype, $basename, $basevmid, undef, undef, $isBase) = | |
392 | $class->parse_volname($volname); | |
393 | ||
63da6d79 DM |
394 | die "$volname is not a base image and snapname is not provided\n" |
395 | if !$isBase && !length($snapname); | |
f2708285 AD |
396 | |
397 | my $name = &$find_free_diskname($storeid, $scfg, $vmid); | |
398 | ||
f236eaf8 SP |
399 | warn "clone $volname: $basename snapname $snap to $name\n"; |
400 | ||
63da6d79 | 401 | if (length($snapname)) { |
f236eaf8 SP |
402 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname); |
403 | ||
63da6d79 | 404 | if (!$protected) { |
f236eaf8 SP |
405 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname); |
406 | run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error"); | |
407 | } | |
408 | } | |
f2708285 AD |
409 | |
410 | my $newvol = "$basename/$name"; | |
63da6d79 DM |
411 | $newvol = $name if length($snapname); |
412 | ||
413 | my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename), | |
414 | '--snap', $snap, &$add_pool_to_disk($scfg, $name)); | |
f2708285 | 415 | |
c693f749 | 416 | run_rbd_command($cmd, errmsg => "rbd clone '$basename' error"); |
f2708285 | 417 | |
d86fd0a4 FG |
418 | &$krdb_feature_disable($scfg, $storeid, $name); |
419 | ||
f2708285 | 420 | return $newvol; |
5eab0272 DM |
421 | } |
422 | ||
0509010d AD |
423 | sub alloc_image { |
424 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
425 | ||
426 | ||
e5427b00 | 427 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" |
0509010d | 428 | if $name && $name !~ m/^vm-$vmid-/; |
0509010d | 429 | |
3fad2603 | 430 | $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name; |
0509010d | 431 | |
a8c3f8f6 | 432 | my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name); |
c693f749 | 433 | run_rbd_command($cmd, errmsg => "rbd create $name' error"); |
0509010d | 434 | |
d86fd0a4 FG |
435 | &$krdb_feature_disable($scfg, $storeid, $name); |
436 | ||
0509010d AD |
437 | return $name; |
438 | } | |
439 | ||
440 | sub free_image { | |
32437ed2 | 441 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
0509010d | 442 | |
42d07b9a AD |
443 | my ($vtype, $name, $vmid, undef, undef, undef) = |
444 | $class->parse_volname($volname); | |
445 | ||
446 | if ($isBase) { | |
447 | my $snap = '__base__'; | |
448 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap); | |
449 | if ($protected){ | |
450 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap); | |
2362bc87 | 451 | run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error"); |
42d07b9a AD |
452 | } |
453 | } | |
454 | ||
515ef80b WL |
455 | $class->deactivate_volume($storeid, $scfg, $volname); |
456 | ||
42d07b9a | 457 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name); |
c693f749 | 458 | run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error"); |
c30470a3 | 459 | |
42d07b9a | 460 | $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name); |
c693f749 | 461 | run_rbd_command($cmd, errmsg => "rbd rm '$volname' error"); |
0509010d AD |
462 | |
463 | return undef; | |
464 | } | |
465 | ||
466 | sub list_images { | |
467 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
468 | ||
e5427b00 | 469 | $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd}; |
1440604a | 470 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; |
411476cd | 471 | |
0509010d AD |
472 | my $res = []; |
473 | ||
1440604a | 474 | if (my $dat = $cache->{rbd}->{$pool}) { |
0509010d AD |
475 | foreach my $image (keys %$dat) { |
476 | ||
477 | my $volname = $dat->{$image}->{name}; | |
478 | ||
479 | my $volid = "$storeid:$volname"; | |
480 | ||
0509010d AD |
481 | my $owner = $dat->{$volname}->{vmid}; |
482 | if ($vollist) { | |
483 | my $found = grep { $_ eq $volid } @$vollist; | |
484 | next if !$found; | |
485 | } else { | |
486 | next if defined ($vmid) && ($owner ne $vmid); | |
487 | } | |
488 | ||
489 | my $info = $dat->{$volname}; | |
490 | $info->{volid} = $volid; | |
411476cd | 491 | $info->{format} = 'raw'; |
0509010d AD |
492 | |
493 | push @$res, $info; | |
494 | } | |
495 | } | |
496 | ||
411476cd | 497 | return $res; |
0509010d AD |
498 | } |
499 | ||
0509010d AD |
500 | sub status { |
501 | my ($class, $storeid, $scfg, $cache) = @_; | |
502 | ||
69589444 AD |
503 | my $cmd = &$rados_cmd($scfg, $storeid, 'df'); |
504 | ||
505 | my $stats = {}; | |
506 | ||
507 | my $parser = sub { | |
508 | my $line = shift; | |
509 | if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) { | |
510 | $stats->{$1} = $2; | |
511 | } | |
512 | }; | |
513 | ||
514 | eval { | |
c693f749 | 515 | run_rbd_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser); |
69589444 AD |
516 | }; |
517 | ||
518 | my $total = $stats->{space} ? $stats->{space}*1024 : 0; | |
519 | my $free = $stats->{avail} ? $stats->{avail}*1024 : 0; | |
520 | my $used = $stats->{used} ? $stats->{used}*1024: 0; | |
0509010d | 521 | my $active = 1; |
0509010d | 522 | |
411476cd | 523 | return ($total, $free, $used, $active); |
0509010d AD |
524 | } |
525 | ||
526 | sub activate_storage { | |
527 | my ($class, $storeid, $scfg, $cache) = @_; | |
528 | return 1; | |
529 | } | |
530 | ||
531 | sub deactivate_storage { | |
532 | my ($class, $storeid, $scfg, $cache) = @_; | |
533 | return 1; | |
534 | } | |
535 | ||
536 | sub activate_volume { | |
02e797b8 | 537 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
9f20a8a6 AD |
538 | |
539 | return 1 if !$scfg->{krbd}; | |
540 | ||
541 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
4f6a99d8 DM |
542 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; |
543 | ||
544 | my $path = "/dev/rbd/$pool/$name"; | |
02e797b8 | 545 | $path .= '@'.$snapname if $snapname; |
4f6a99d8 | 546 | return if -b $path; |
9f20a8a6 | 547 | |
02e797b8 | 548 | $name .= '@'.$snapname if $snapname; |
9f20a8a6 AD |
549 | my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name); |
550 | run_rbd_command($cmd, errmsg => "can't mount rbd volume $name"); | |
551 | ||
0509010d AD |
552 | return 1; |
553 | } | |
554 | ||
555 | sub deactivate_volume { | |
02e797b8 | 556 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
9f20a8a6 AD |
557 | |
558 | return 1 if !$scfg->{krbd}; | |
559 | ||
560 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
561 | my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; | |
562 | ||
563 | my $path = "/dev/rbd/$pool/$name"; | |
02e797b8 | 564 | $path .= '@'.$snapname if $snapname; |
b50812f9 WL |
565 | return if ! -b $path; |
566 | ||
9f20a8a6 | 567 | my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path); |
72e743bd | 568 | run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name"); |
9f20a8a6 | 569 | |
0509010d AD |
570 | return 1; |
571 | } | |
572 | ||
0002d9cc AD |
573 | sub volume_size_info { |
574 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
575 | ||
81d1d017 AD |
576 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); |
577 | my ($size, undef) = rbd_volume_info($scfg, $storeid, $name); | |
62b98a65 | 578 | return $size; |
0002d9cc AD |
579 | } |
580 | ||
e7a42a76 AD |
581 | sub volume_resize { |
582 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
583 | ||
584 | return 1 if $running; | |
585 | ||
478fc06c AD |
586 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); |
587 | ||
588 | my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name); | |
c693f749 | 589 | run_rbd_command($cmd, errmsg => "rbd resize '$volname' error"); |
e7a42a76 AD |
590 | return undef; |
591 | } | |
592 | ||
788dd8e1 | 593 | sub volume_snapshot { |
f5640e7d | 594 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
788dd8e1 | 595 | |
9af33ed0 AD |
596 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); |
597 | ||
598 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name); | |
c693f749 | 599 | run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error"); |
788dd8e1 AD |
600 | return undef; |
601 | } | |
602 | ||
5a2b2e2f AD |
603 | sub volume_snapshot_rollback { |
604 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
605 | ||
c6ce2cc8 AD |
606 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); |
607 | ||
608 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name); | |
2362bc87 | 609 | run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error"); |
5a2b2e2f AD |
610 | } |
611 | ||
cce29bcd AD |
612 | sub volume_snapshot_delete { |
613 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
614 | ||
615 | return 1 if $running; | |
616 | ||
399581a2 WB |
617 | $class->deactivate_volume($storeid, $scfg, $volname, $snap, {}); |
618 | ||
c78cb110 AD |
619 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); |
620 | ||
f90a0a20 SP |
621 | my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap); |
622 | if ($protected){ | |
623 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap); | |
624 | run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error"); | |
625 | } | |
626 | ||
c78cb110 | 627 | my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name); |
c693f749 SP |
628 | |
629 | run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error"); | |
630 | ||
cce29bcd AD |
631 | return undef; |
632 | } | |
633 | ||
774f21b9 AD |
634 | sub volume_has_feature { |
635 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
636 | ||
637 | my $features = { | |
5649ccfe | 638 | snapshot => { current => 1, snap => 1}, |
44c3689a | 639 | clone => { base => 1, snap => 1}, |
5649ccfe AD |
640 | template => { current => 1}, |
641 | copy => { base => 1, current => 1, snap => 1}, | |
baafddbd | 642 | sparseinit => { base => 1, current => 1}, |
774f21b9 AD |
643 | }; |
644 | ||
1e7ae581 AD |
645 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = |
646 | $class->parse_volname($volname); | |
647 | ||
648 | my $key = undef; | |
649 | if($snapname){ | |
2c5a7097 | 650 | $key = 'snap'; |
1e7ae581 AD |
651 | }else{ |
652 | $key = $isBase ? 'base' : 'current'; | |
653 | } | |
654 | return 1 if $features->{$feature}->{$key}; | |
774f21b9 AD |
655 | |
656 | return undef; | |
657 | } | |
658 | ||
0509010d | 659 | 1; |