]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
1 package PVE
::Storage
::RBDPlugin
;
6 use PVE
::Tools
qw(run_command trim);
7 use PVE
::Storage
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
qw(PVE::Storage::Plugin);
13 my ($scfg, $storeid, $op, @options) = @_;
15 my $monhost = $scfg->{monhost
};
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];
23 push @$cmd, @options if scalar(@options);
29 my ($scfg, $storeid, $op, @options) = @_;
31 my $monhost = $scfg->{monhost
};
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];
39 push @$cmd, @options if scalar(@options);
45 my ($scfg, $storeid) = @_;
47 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
54 if ($line =~ m/^(vm-(\d+)-\S+)$/) {
55 my ($image, $owner) = ($1, $2);
57 $list->{$scfg->{pool
}}->{$image} = {
66 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
70 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
84 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
86 my ($name, $noerr) = @_;
88 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
89 return undef if $noerr;
90 die "lvm name '$name' contains illegal characters\n";
102 content
=> [ {images
=> 1}, { images
=> 1 }],
109 description
=> "Monitors daemon ips.",
113 description
=> "Pool.",
117 description
=> "RBD Id.",
121 description
=> "Authsupported.",
129 nodes
=> { optional
=> 1 },
130 disable
=> { optional
=> 1 },
131 monhost
=> { fixed
=> 1 },
132 pool
=> { fixed
=> 1 },
133 username
=> { fixed
=> 1 },
134 authsupported
=> { fixed
=> 1 },
135 content
=> { optional
=> 1 },
139 # Storage implementation
142 my ($class, $volname) = @_;
144 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
145 return ('images', $1, $2);
148 die "unable to parse rbd volume name '$volname'\n";
152 my ($class, $scfg, $volname, $storeid) = @_;
154 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
156 my $monhost = addslashes
($scfg->{monhost
});
157 my $pool = $scfg->{pool
};
158 my $username = $scfg->{username
};
159 my $authsupported = addslashes
($scfg->{authsupported
});
161 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
163 return ($path, $vmid, $vtype);
167 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
170 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
171 if $name && $name !~ m/^vm-$vmid-/;
174 my $rdb = rbd_ls
($scfg, $storeid);
176 for (my $i = 1; $i < 100; $i++) {
177 my $tn = "vm-$vmid-disk-$i";
178 if (!defined ($rdb->{$scfg->{pool
}}->{$tn})) {
185 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
188 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
189 run_command
($cmd, errmsg
=> "rbd create $name' error", errfunc
=> sub {});
195 my ($class, $storeid, $scfg, $volname) = @_;
197 my $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname);
198 run_command
($cmd, errmsg
=> "rbd rm $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
204 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
206 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
210 if (my $dat = $cache->{rbd
}->{$scfg->{pool
}}) {
211 foreach my $image (keys %$dat) {
213 my $volname = $dat->{$image}->{name
};
215 my $volid = "$storeid:$volname";
217 my $owner = $dat->{$volname}->{vmid
};
219 my $found = grep { $_ eq $volid } @$vollist;
222 next if defined ($vmid) && ($owner ne $vmid);
225 my $info = $dat->{$volname};
226 $info->{volid
} = $volid;
227 $info->{format
} = 'raw';
237 my ($class, $storeid, $scfg, $cache) = @_;
239 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
245 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
251 run_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
254 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
255 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
256 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
259 return ($total, $free, $used, $active);
262 sub activate_storage
{
263 my ($class, $storeid, $scfg, $cache) = @_;
267 sub deactivate_storage
{
268 my ($class, $storeid, $scfg, $cache) = @_;
272 sub activate_volume
{
273 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
277 sub deactivate_volume
{
278 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
282 sub volume_size_info
{
283 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
285 my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
290 if ($line =~ m/size (\d+) MB in (\d+) objects/) {
295 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
297 $size = $size*1024*1024 if $size;
303 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
305 return 1 if $running;
307 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname);
308 run_command
($cmd, errmsg
=> "rbd resize $volname' error", errfunc
=> sub {});
312 sub volume_snapshot
{
313 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
315 return 1 if $running;
317 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $volname);
318 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
322 sub volume_snapshot_rollback
{
323 my ($class, $scfg, $storeid, $volname, $snap) = @_;
325 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $volname);
326 run_command
($cmd, errmsg
=> "rbd snapshot $volname to $snap' error", errfunc
=> sub {});
329 sub volume_snapshot_delete
{
330 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
332 return 1 if $running;
334 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $volname);
335 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});