]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
rbd : add owner attribute when rbs ls
[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
13 sub rbd_ls{
14 my ($scfg, $storeid) = @_;
15
16 my $rbdpool = $scfg->{pool};
17 my $monhost = $scfg->{monhost};
18 $monhost =~ s/;/,/g;
19
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' ];
21 my $list = {};
22
23 my $errfunc = sub {
24 my $line = shift;
25 die $line if $line;
26 };
27
28 eval {
29 run_command($cmd, errmsg => "rbd error", errfunc => $errfunc,outfunc => sub {
30 my $line = shift;
31
32 $line = trim($line);
33 my ($image) = $line;
34
35 my $owner;
36 if ($image =~ m/^(vm-(\d+)-\S+)$/) {
37 $owner = $2;
38 }
39
40 $list->{$rbdpool}->{$image} = {
41 name => $image,
42 size => "",
43 vmid => $owner
44 };
45
46 });
47 };
48
49 my $err = $@;
50 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
51
52 return $list;
53
54 }
55
56 sub addslashes {
57 my $text = shift;
58 $text =~ s/;/\\;/g;
59 $text =~ s/:/\\:/g;
60 return $text;
61 }
62
63 # Configuration
64
65 PVE::JSONSchema::register_format('pve-storage-monhost', \&parse_monhost);
66 sub parse_monhost {
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
78 sub type {
79 return 'rbd';
80 }
81
82 sub plugindata {
83 return {
84 content => [ {images => 1}, { images => 1 }],
85 };
86 }
87
88 sub properties {
89 return {
90 monhost => {
91 description => "Monitors daemon ips.",
92 type => 'string',
93 },
94 pool => {
95 description => "Pool.",
96 type => 'string',
97 },
98 username => {
99 description => "RBD Id.",
100 type => 'string',
101 },
102 authsupported => {
103 description => "Authsupported.",
104 type => 'string',
105 },
106 };
107 }
108
109 sub options {
110 return {
111 monhost => { fixed => 1 },
112 pool => { fixed => 1 },
113 username => { fixed => 1 },
114 authsupported => { fixed => 1 },
115 content => { optional => 1 },
116 };
117 }
118
119 # Storage implementation
120
121 sub 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
131 sub path {
132 my ($class, $scfg, $volname, $storeid) = @_;
133
134 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
135
136 my $monhost = addslashes($scfg->{monhost});
137 my $pool = $scfg->{pool};
138 my $username = $scfg->{username};
139 my $authsupported = addslashes($scfg->{authsupported});
140
141 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
142
143 return ($path, $vmid, $vtype);
144 }
145
146 sub alloc_image {
147 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
148
149
150 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
151 if $name && $name !~ m/^vm-$vmid-/;
152 my $rbdpool = $scfg->{pool};
153 my $monhost = $scfg->{monhost};
154 $monhost =~ s/;/,/g;
155
156 if (!$name) {
157 my $rdb = rbd_ls($scfg, $storeid);
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
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 ];
172 run_command($cmd, errmsg => "rbd create $name' error");
173
174 return $name;
175 }
176
177 sub free_image {
178 my ($class, $storeid, $scfg, $volname) = @_;
179
180 my $rbdpool = $scfg->{pool};
181 my $monhost = $scfg->{monhost};
182 $monhost =~ s/;/,/g;
183
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 ];
185 run_command($cmd, errmsg => "rbd rm $volname' error");
186
187 return undef;
188 }
189
190 sub list_images {
191 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
192
193 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
194 my $rbdpool = $scfg->{pool};
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
224 sub status {
225 my ($class, $storeid, $scfg, $cache) = @_;
226
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
236 sub activate_storage {
237 my ($class, $storeid, $scfg, $cache) = @_;
238 return 1;
239 }
240
241 sub deactivate_storage {
242 my ($class, $storeid, $scfg, $cache) = @_;
243 return 1;
244 }
245
246 sub activate_volume {
247 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
248 return 1;
249 }
250
251 sub deactivate_volume {
252 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
253 return 1;
254 }
255
256 1;