]>
Commit | Line | Data |
---|---|---|
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'); | |
48 | ||
49 | my $list = {}; | |
50 | ||
51 | my $parser = sub { | |
52 | my $line = shift; | |
53 | ||
54 | if ($line =~ m/^(vm-(\d+)-\S+)$/) { | |
55 | my ($image, $owner) = ($1, $2); | |
56 | ||
57 | my ($size, $parent) = rbd_volume_info($scfg, $storeid, $image); | |
58 | $list->{$scfg->{pool}}->{$image} = { | |
59 | name => $image, | |
60 | size => $size, | |
61 | parent => $parent, | |
62 | vmid => $owner | |
63 | }; | |
64 | } | |
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/ ; | |
73 | ||
74 | return $list; | |
75 | } | |
76 | ||
77 | sub rbd_volume_info { | |
78 | my ($scfg, $storeid, $volname) = @_; | |
79 | ||
80 | my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname); | |
81 | my $size = undef; | |
82 | my $parent = undef; | |
83 | ||
84 | my $parser = sub { | |
85 | my $line = shift; | |
86 | ||
87 | if ($line =~ m/size (\d+) MB in (\d+) objects/) { | |
88 | $size = $1; | |
89 | } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) { | |
90 | $parent = $2; | |
91 | } | |
92 | }; | |
93 | ||
94 | run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); | |
95 | ||
96 | $size = $size*1024*1024 if $size; | |
97 | ||
98 | return ($size, $parent); | |
99 | } | |
100 | ||
101 | sub addslashes { | |
102 | my $text = shift; | |
103 | $text =~ s/;/\\;/g; | |
104 | $text =~ s/:/\\:/g; | |
105 | return $text; | |
106 | } | |
107 | ||
108 | # Configuration | |
109 | ||
110 | PVE::JSONSchema::register_format('pve-storage-monhost', \&parse_monhost); | |
111 | sub parse_monhost { | |
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 | ||
122 | sub type { | |
123 | return 'rbd'; | |
124 | } | |
125 | ||
126 | sub plugindata { | |
127 | return { | |
128 | content => [ {images => 1}, { images => 1 }], | |
129 | }; | |
130 | } | |
131 | ||
132 | sub properties { | |
133 | return { | |
134 | monhost => { | |
135 | description => "Monitors daemon ips.", | |
136 | type => 'string', | |
137 | }, | |
138 | pool => { | |
139 | description => "Pool.", | |
140 | type => 'string', | |
141 | }, | |
142 | username => { | |
143 | description => "RBD Id.", | |
144 | type => 'string', | |
145 | }, | |
146 | authsupported => { | |
147 | description => "Authsupported.", | |
148 | type => 'string', | |
149 | }, | |
150 | }; | |
151 | } | |
152 | ||
153 | sub options { | |
154 | return { | |
155 | nodes => { optional => 1 }, | |
156 | disable => { optional => 1 }, | |
157 | monhost => { fixed => 1 }, | |
158 | pool => { fixed => 1 }, | |
159 | username => { fixed => 1 }, | |
160 | authsupported => { fixed => 1 }, | |
161 | content => { optional => 1 }, | |
162 | }; | |
163 | } | |
164 | ||
165 | # Storage implementation | |
166 | ||
167 | sub 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 | ||
177 | sub path { | |
178 | my ($class, $scfg, $volname, $storeid) = @_; | |
179 | ||
180 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
181 | ||
182 | my $monhost = addslashes($scfg->{monhost}); | |
183 | my $pool = $scfg->{pool}; | |
184 | my $username = $scfg->{username}; | |
185 | my $authsupported = addslashes($scfg->{authsupported}); | |
186 | ||
187 | my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost"; | |
188 | ||
189 | return ($path, $vmid, $vtype); | |
190 | } | |
191 | ||
192 | sub alloc_image { | |
193 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
194 | ||
195 | ||
196 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" | |
197 | if $name && $name !~ m/^vm-$vmid-/; | |
198 | ||
199 | if (!$name) { | |
200 | my $rdb = rbd_ls($scfg, $storeid); | |
201 | ||
202 | for (my $i = 1; $i < 100; $i++) { | |
203 | my $tn = "vm-$vmid-disk-$i"; | |
204 | if (!defined ($rdb->{$scfg->{pool}}->{$tn})) { | |
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 | ||
214 | my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name); | |
215 | run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {}); | |
216 | ||
217 | return $name; | |
218 | } | |
219 | ||
220 | sub free_image { | |
221 | my ($class, $storeid, $scfg, $volname) = @_; | |
222 | ||
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); | |
227 | run_command($cmd, errmsg => "rbd rm $volname' error", outfunc => sub {}, errfunc => sub {}); | |
228 | ||
229 | return undef; | |
230 | } | |
231 | ||
232 | sub list_images { | |
233 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
234 | ||
235 | $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd}; | |
236 | ||
237 | my $res = []; | |
238 | ||
239 | if (my $dat = $cache->{rbd}->{$scfg->{pool}}) { | |
240 | foreach my $image (keys %$dat) { | |
241 | ||
242 | my $volname = $dat->{$image}->{name}; | |
243 | ||
244 | my $volid = "$storeid:$volname"; | |
245 | ||
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; | |
256 | $info->{format} = 'raw'; | |
257 | ||
258 | push @$res, $info; | |
259 | } | |
260 | } | |
261 | ||
262 | return $res; | |
263 | } | |
264 | ||
265 | sub status { | |
266 | my ($class, $storeid, $scfg, $cache) = @_; | |
267 | ||
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; | |
286 | my $active = 1; | |
287 | ||
288 | return ($total, $free, $used, $active); | |
289 | } | |
290 | ||
291 | sub activate_storage { | |
292 | my ($class, $storeid, $scfg, $cache) = @_; | |
293 | return 1; | |
294 | } | |
295 | ||
296 | sub deactivate_storage { | |
297 | my ($class, $storeid, $scfg, $cache) = @_; | |
298 | return 1; | |
299 | } | |
300 | ||
301 | sub activate_volume { | |
302 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
303 | return 1; | |
304 | } | |
305 | ||
306 | sub deactivate_volume { | |
307 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
308 | return 1; | |
309 | } | |
310 | ||
311 | sub volume_size_info { | |
312 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
313 | ||
314 | my ($size, undef) = rbd_volume_info($scfg, $storeid, $volname); | |
315 | return $size; | |
316 | } | |
317 | ||
318 | sub 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 | ||
328 | sub 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 | ||
338 | sub 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 | ||
345 | sub 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 | ||
355 | 1; |