]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/RBDPlugin.pm
plugin : has_feature
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
CommitLineData
0509010d
AD
1package PVE::Storage::RBDPlugin;
2
3use strict;
4use warnings;
5use IO::File;
6use PVE::Tools qw(run_command trim);
7use PVE::Storage::Plugin;
8use PVE::JSONSchema qw(get_standard_option);
9
10use base qw(PVE::Storage::Plugin);
11
411476cd
DM
12my $rbd_cmd = sub {
13 my ($scfg, $storeid, $op, @options) = @_;
0509010d 14
e5427b00 15 my $monhost = $scfg->{monhost};
0509010d
AD
16 $monhost =~ s/;/,/g;
17
411476cd
DM
18 my $cmd = ['/usr/bin/rbd', '-p', $scfg->{pool}, '-m', $monhost, '-n',
19 "client.$scfg->{username}",
20 '--keyring', "/etc/pve/priv/ceph/${storeid}.keyring",
21 '--auth_supported', $scfg->{authsupported}, $op];
3e195ccc 22
411476cd 23 push @$cmd, @options if scalar(@options);
3e195ccc 24
411476cd
DM
25 return $cmd;
26};
0509010d 27
69589444
AD
28my $rados_cmd = sub {
29 my ($scfg, $storeid, $op, @options) = @_;
30
31 my $monhost = $scfg->{monhost};
32 $monhost =~ s/;/,/g;
33
34 my $cmd = ['/usr/bin/rados', '-p', $scfg->{pool}, '-m', $monhost, '-n',
35 "client.$scfg->{username}",
36 '--keyring', "/etc/pve/priv/ceph/${storeid}.keyring",
37 '--auth_supported', $scfg->{authsupported}, $op];
38
39 push @$cmd, @options if scalar(@options);
40
41 return $cmd;
42};
43
411476cd
DM
44sub rbd_ls {
45 my ($scfg, $storeid) = @_;
d70e7f6c 46
411476cd 47 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
d70e7f6c 48
411476cd 49 my $list = {};
0509010d 50
8c3abf12 51 my $parser = sub {
411476cd 52 my $line = shift;
0509010d 53
411476cd
DM
54 if ($line =~ m/^(vm-(\d+)-\S+)$/) {
55 my ($image, $owner) = ($1, $2);
0509010d 56
62b98a65 57 my ($size, $parent) = rbd_volume_info($scfg, $storeid, $image);
411476cd
DM
58 $list->{$scfg->{pool}}->{$image} = {
59 name => $image,
62b98a65
AD
60 size => $size,
61 parent => $parent,
411476cd
DM
62 vmid => $owner
63 };
64 }
8c3abf12
DM
65 };
66
67 eval {
68 run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
69 };
70 my $err = $@;
71
72 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
411476cd
DM
73
74 return $list;
0509010d
AD
75}
76
62b98a65 77sub rbd_volume_info {
e110213e
AD
78 my ($scfg, $storeid, $volname) = @_;
79
80 my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
81 my $size = undef;
62b98a65
AD
82 my $parent = undef;
83
e110213e
AD
84 my $parser = sub {
85 my $line = shift;
86
87 if ($line =~ m/size (\d+) MB in (\d+) objects/) {
88 $size = $1;
62b98a65
AD
89 } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
90 $parent = $2;
e110213e
AD
91 }
92 };
93
94 run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
95
96 $size = $size*1024*1024 if $size;
97
62b98a65 98 return ($size, $parent);
e110213e
AD
99}
100
0509010d
AD
101sub addslashes {
102 my $text = shift;
103 $text =~ s/;/\\;/g;
104 $text =~ s/:/\\:/g;
105 return $text;
106}
107
e5427b00 108# Configuration
0509010d 109
e5427b00
AD
110PVE::JSONSchema::register_format('pve-storage-monhost', \&parse_monhost);
111sub parse_monhost {
0509010d
AD
112 my ($name, $noerr) = @_;
113
114 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
115 return undef if $noerr;
116 die "lvm name '$name' contains illegal characters\n";
117 }
118
119 return $name;
120}
121
0509010d
AD
122sub type {
123 return 'rbd';
124}
125
126sub plugindata {
127 return {
128 content => [ {images => 1}, { images => 1 }],
129 };
130}
131
132sub properties {
133 return {
e5427b00 134 monhost => {
0509010d 135 description => "Monitors daemon ips.",
e5427b00 136 type => 'string',
0509010d 137 },
e5427b00
AD
138 pool => {
139 description => "Pool.",
0509010d
AD
140 type => 'string',
141 },
e5427b00
AD
142 username => {
143 description => "RBD Id.",
0509010d
AD
144 type => 'string',
145 },
e5427b00 146 authsupported => {
0509010d
AD
147 description => "Authsupported.",
148 type => 'string',
149 },
150 };
151}
152
153sub options {
154 return {
35d6dfaf
AD
155 nodes => { optional => 1 },
156 disable => { optional => 1 },
e5427b00 157 monhost => { fixed => 1 },
35d6dfaf 158 pool => { fixed => 1 },
e5427b00 159 username => { fixed => 1 },
35d6dfaf 160 authsupported => { fixed => 1 },
0509010d
AD
161 content => { optional => 1 },
162 };
163}
164
165# Storage implementation
166
167sub parse_volname {
168 my ($class, $volname) = @_;
169
170 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
171 return ('images', $1, $2);
172 }
173
174 die "unable to parse rbd volume name '$volname'\n";
175}
176
177sub path {
e5427b00 178 my ($class, $scfg, $volname, $storeid) = @_;
0509010d
AD
179
180 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
181
e5427b00
AD
182 my $monhost = addslashes($scfg->{monhost});
183 my $pool = $scfg->{pool};
184 my $username = $scfg->{username};
185 my $authsupported = addslashes($scfg->{authsupported});
186
4e2d3bc8 187 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
0509010d
AD
188
189 return ($path, $vmid, $vtype);
190}
191
192sub alloc_image {
193 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
194
195
e5427b00 196 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
0509010d 197 if $name && $name !~ m/^vm-$vmid-/;
0509010d
AD
198
199 if (!$name) {
e5427b00 200 my $rdb = rbd_ls($scfg, $storeid);
0509010d
AD
201
202 for (my $i = 1; $i < 100; $i++) {
203 my $tn = "vm-$vmid-disk-$i";
411476cd 204 if (!defined ($rdb->{$scfg->{pool}}->{$tn})) {
0509010d
AD
205 $name = $tn;
206 last;
207 }
208 }
209 }
210
211 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
212 if !$name;
213
411476cd
DM
214 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
215 run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
0509010d
AD
216
217 return $name;
218}
219
220sub free_image {
221 my ($class, $storeid, $scfg, $volname) = @_;
222
c30470a3
AD
223 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $volname);
224 run_command($cmd, errmsg => "rbd snap purge $volname' error", outfunc => sub {}, errfunc => sub {});
225
226 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname);
411476cd 227 run_command($cmd, errmsg => "rbd rm $volname' error", outfunc => sub {}, errfunc => sub {});
0509010d
AD
228
229 return undef;
230}
231
232sub list_images {
233 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
234
e5427b00 235 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
411476cd 236
0509010d
AD
237 my $res = [];
238
411476cd 239 if (my $dat = $cache->{rbd}->{$scfg->{pool}}) {
0509010d
AD
240 foreach my $image (keys %$dat) {
241
242 my $volname = $dat->{$image}->{name};
243
244 my $volid = "$storeid:$volname";
245
0509010d
AD
246 my $owner = $dat->{$volname}->{vmid};
247 if ($vollist) {
248 my $found = grep { $_ eq $volid } @$vollist;
249 next if !$found;
250 } else {
251 next if defined ($vmid) && ($owner ne $vmid);
252 }
253
254 my $info = $dat->{$volname};
255 $info->{volid} = $volid;
411476cd 256 $info->{format} = 'raw';
0509010d
AD
257
258 push @$res, $info;
259 }
260 }
261
411476cd 262 return $res;
0509010d
AD
263}
264
0509010d
AD
265sub status {
266 my ($class, $storeid, $scfg, $cache) = @_;
267
69589444
AD
268 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
269
270 my $stats = {};
271
272 my $parser = sub {
273 my $line = shift;
274 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
275 $stats->{$1} = $2;
276 }
277 };
278
279 eval {
280 run_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser);
281 };
282
283 my $total = $stats->{space} ? $stats->{space}*1024 : 0;
284 my $free = $stats->{avail} ? $stats->{avail}*1024 : 0;
285 my $used = $stats->{used} ? $stats->{used}*1024: 0;
0509010d 286 my $active = 1;
0509010d 287
411476cd 288 return ($total, $free, $used, $active);
0509010d
AD
289}
290
291sub activate_storage {
292 my ($class, $storeid, $scfg, $cache) = @_;
293 return 1;
294}
295
296sub deactivate_storage {
297 my ($class, $storeid, $scfg, $cache) = @_;
298 return 1;
299}
300
301sub activate_volume {
302 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
303 return 1;
304}
305
306sub deactivate_volume {
307 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
308 return 1;
309}
310
0002d9cc
AD
311sub volume_size_info {
312 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
313
62b98a65
AD
314 my ($size, undef) = rbd_volume_info($scfg, $storeid, $volname);
315 return $size;
0002d9cc
AD
316}
317
e7a42a76
AD
318sub volume_resize {
319 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
320
321 return 1 if $running;
322
323 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname);
324 run_command($cmd, errmsg => "rbd resize $volname' error", errfunc => sub {});
325 return undef;
326}
327
788dd8e1
AD
328sub volume_snapshot {
329 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
330
331 return 1 if $running;
332
333 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $volname);
334 run_command($cmd, errmsg => "rbd snapshot $volname' error", errfunc => sub {});
335 return undef;
336}
337
5a2b2e2f
AD
338sub volume_snapshot_rollback {
339 my ($class, $scfg, $storeid, $volname, $snap) = @_;
340
341 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $volname);
342 run_command($cmd, errmsg => "rbd snapshot $volname to $snap' error", errfunc => sub {});
343}
344
cce29bcd
AD
345sub volume_snapshot_delete {
346 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
347
348 return 1 if $running;
349
350 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $volname);
351 run_command($cmd, errmsg => "rbd snapshot $volname' error", errfunc => sub {});
352 return undef;
353}
354
0509010d 3551;