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