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