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