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