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