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