]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
rbd : volume_resize
[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 sub rbd_ls {
29 my ($scfg, $storeid) = @_;
30
31 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
32
33 my $list = {};
34
35 my $parser = sub {
36 my $line = shift;
37
38 if ($line =~ m/^(vm-(\d+)-\S+)$/) {
39 my ($image, $owner) = ($1, $2);
40
41 $list->{$scfg->{pool}}->{$image} = {
42 name => $image,
43 size => 0,
44 vmid => $owner
45 };
46 }
47 };
48
49 eval {
50 run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
51 };
52 my $err = $@;
53
54 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
55
56 return $list;
57 }
58
59 sub addslashes {
60 my $text = shift;
61 $text =~ s/;/\\;/g;
62 $text =~ s/:/\\:/g;
63 return $text;
64 }
65
66 # Configuration
67
68 PVE::JSONSchema::register_format('pve-storage-monhost', \&parse_monhost);
69 sub parse_monhost {
70 my ($name, $noerr) = @_;
71
72 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
73 return undef if $noerr;
74 die "lvm name '$name' contains illegal characters\n";
75 }
76
77 return $name;
78 }
79
80 sub type {
81 return 'rbd';
82 }
83
84 sub plugindata {
85 return {
86 content => [ {images => 1}, { images => 1 }],
87 };
88 }
89
90 sub properties {
91 return {
92 monhost => {
93 description => "Monitors daemon ips.",
94 type => 'string',
95 },
96 pool => {
97 description => "Pool.",
98 type => 'string',
99 },
100 username => {
101 description => "RBD Id.",
102 type => 'string',
103 },
104 authsupported => {
105 description => "Authsupported.",
106 type => 'string',
107 },
108 };
109 }
110
111 sub options {
112 return {
113 monhost => { fixed => 1 },
114 pool => { fixed => 1 },
115 username => { fixed => 1 },
116 authsupported => { fixed => 1 },
117 content => { optional => 1 },
118 };
119 }
120
121 # Storage implementation
122
123 sub parse_volname {
124 my ($class, $volname) = @_;
125
126 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
127 return ('images', $1, $2);
128 }
129
130 die "unable to parse rbd volume name '$volname'\n";
131 }
132
133 sub path {
134 my ($class, $scfg, $volname, $storeid) = @_;
135
136 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
137
138 my $monhost = addslashes($scfg->{monhost});
139 my $pool = $scfg->{pool};
140 my $username = $scfg->{username};
141 my $authsupported = addslashes($scfg->{authsupported});
142
143 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
144
145 return ($path, $vmid, $vtype);
146 }
147
148 sub alloc_image {
149 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
150
151
152 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
153 if $name && $name !~ m/^vm-$vmid-/;
154
155 if (!$name) {
156 my $rdb = rbd_ls($scfg, $storeid);
157
158 for (my $i = 1; $i < 100; $i++) {
159 my $tn = "vm-$vmid-disk-$i";
160 if (!defined ($rdb->{$scfg->{pool}}->{$tn})) {
161 $name = $tn;
162 last;
163 }
164 }
165 }
166
167 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
168 if !$name;
169
170 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
171 run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
172
173 return $name;
174 }
175
176 sub free_image {
177 my ($class, $storeid, $scfg, $volname) = @_;
178
179 my $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname);
180 run_command($cmd, errmsg => "rbd rm $volname' error", outfunc => sub {}, errfunc => sub {});
181
182 return undef;
183 }
184
185 sub list_images {
186 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
187
188 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
189
190 my $res = [];
191
192 if (my $dat = $cache->{rbd}->{$scfg->{pool}}) {
193 foreach my $image (keys %$dat) {
194
195 my $volname = $dat->{$image}->{name};
196
197 my $volid = "$storeid:$volname";
198
199 my $owner = $dat->{$volname}->{vmid};
200 if ($vollist) {
201 my $found = grep { $_ eq $volid } @$vollist;
202 next if !$found;
203 } else {
204 next if defined ($vmid) && ($owner ne $vmid);
205 }
206
207 my $info = $dat->{$volname};
208 $info->{volid} = $volid;
209 $info->{format} = 'raw';
210
211 push @$res, $info;
212 }
213 }
214
215 return $res;
216 }
217
218 sub status {
219 my ($class, $storeid, $scfg, $cache) = @_;
220
221 my $total = 0;
222 my $free = 0;
223 my $used = 0;
224 my $active = 1;
225
226 return ($total, $free, $used, $active);
227 }
228
229 sub activate_storage {
230 my ($class, $storeid, $scfg, $cache) = @_;
231 return 1;
232 }
233
234 sub deactivate_storage {
235 my ($class, $storeid, $scfg, $cache) = @_;
236 return 1;
237 }
238
239 sub activate_volume {
240 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
241 return 1;
242 }
243
244 sub deactivate_volume {
245 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
246 return 1;
247 }
248
249 sub volume_size_info {
250 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
251
252 my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
253 my $size = undef;
254 my $parser = sub {
255 my $line = shift;
256
257 if ($line =~ m/size (\d+) MB in (\d+) objects/) {
258 $size = $1;
259 }
260 };
261
262 run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
263
264 $size = $size*1024*1024 if $size;
265
266 return $size;
267 }
268
269 sub volume_resize {
270 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
271
272 return 1 if $running;
273
274 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname);
275 run_command($cmd, errmsg => "rbd resize $volname' error", errfunc => sub {});
276 return undef;
277 }
278
279 1;