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