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