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