]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
plugin : add volume_snapshot_rollback
[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 nodes => { optional => 1 },
114 disable => { optional => 1 },
115 monhost => { fixed => 1 },
116 pool => { fixed => 1 },
117 username => { fixed => 1 },
118 authsupported => { fixed => 1 },
119 content => { optional => 1 },
120 };
121 }
122
123 # Storage implementation
124
125 sub parse_volname {
126 my ($class, $volname) = @_;
127
128 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
129 return ('images', $1, $2);
130 }
131
132 die "unable to parse rbd volume name '$volname'\n";
133 }
134
135 sub path {
136 my ($class, $scfg, $volname, $storeid) = @_;
137
138 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
139
140 my $monhost = addslashes($scfg->{monhost});
141 my $pool = $scfg->{pool};
142 my $username = $scfg->{username};
143 my $authsupported = addslashes($scfg->{authsupported});
144
145 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
146
147 return ($path, $vmid, $vtype);
148 }
149
150 sub alloc_image {
151 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
152
153
154 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
155 if $name && $name !~ m/^vm-$vmid-/;
156
157 if (!$name) {
158 my $rdb = rbd_ls($scfg, $storeid);
159
160 for (my $i = 1; $i < 100; $i++) {
161 my $tn = "vm-$vmid-disk-$i";
162 if (!defined ($rdb->{$scfg->{pool}}->{$tn})) {
163 $name = $tn;
164 last;
165 }
166 }
167 }
168
169 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
170 if !$name;
171
172 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
173 run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
174
175 return $name;
176 }
177
178 sub free_image {
179 my ($class, $storeid, $scfg, $volname) = @_;
180
181 my $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname);
182 run_command($cmd, errmsg => "rbd rm $volname' error", outfunc => sub {}, errfunc => sub {});
183
184 return undef;
185 }
186
187 sub list_images {
188 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
189
190 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
191
192 my $res = [];
193
194 if (my $dat = $cache->{rbd}->{$scfg->{pool}}) {
195 foreach my $image (keys %$dat) {
196
197 my $volname = $dat->{$image}->{name};
198
199 my $volid = "$storeid:$volname";
200
201 my $owner = $dat->{$volname}->{vmid};
202 if ($vollist) {
203 my $found = grep { $_ eq $volid } @$vollist;
204 next if !$found;
205 } else {
206 next if defined ($vmid) && ($owner ne $vmid);
207 }
208
209 my $info = $dat->{$volname};
210 $info->{volid} = $volid;
211 $info->{format} = 'raw';
212
213 push @$res, $info;
214 }
215 }
216
217 return $res;
218 }
219
220 sub status {
221 my ($class, $storeid, $scfg, $cache) = @_;
222
223 my $total = 0;
224 my $free = 0;
225 my $used = 0;
226 my $active = 1;
227
228 return ($total, $free, $used, $active);
229 }
230
231 sub activate_storage {
232 my ($class, $storeid, $scfg, $cache) = @_;
233 return 1;
234 }
235
236 sub deactivate_storage {
237 my ($class, $storeid, $scfg, $cache) = @_;
238 return 1;
239 }
240
241 sub activate_volume {
242 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
243 return 1;
244 }
245
246 sub deactivate_volume {
247 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
248 return 1;
249 }
250
251 sub volume_size_info {
252 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
253
254 my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
255 my $size = undef;
256 my $parser = sub {
257 my $line = shift;
258
259 if ($line =~ m/size (\d+) MB in (\d+) objects/) {
260 $size = $1;
261 }
262 };
263
264 run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
265
266 $size = $size*1024*1024 if $size;
267
268 return $size;
269 }
270
271 sub volume_resize {
272 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
273
274 return 1 if $running;
275
276 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname);
277 run_command($cmd, errmsg => "rbd resize $volname' error", errfunc => sub {});
278 return undef;
279 }
280
281 sub volume_snapshot {
282 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
283
284 return 1 if $running;
285
286 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $volname);
287 run_command($cmd, errmsg => "rbd snapshot $volname' error", errfunc => sub {});
288 return undef;
289 }
290
291 1;