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