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