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