]>
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 my ($size, $parent) = rbd_volume_info
($scfg, $storeid, $image);
58 $list->{$scfg->{pool
}}->{$image} = {
68 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
72 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
78 my ($scfg, $storeid, $volname) = @_;
80 my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
87 if ($line =~ m/size (\d+) MB in (\d+) objects/) {
89 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
94 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
96 $size = $size*1024*1024 if $size;
98 return ($size, $parent);
110 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
112 my ($name, $noerr) = @_;
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";
128 content
=> [ {images
=> 1}, { images
=> 1 }],
135 description
=> "Monitors daemon ips.",
139 description
=> "Pool.",
143 description
=> "RBD Id.",
147 description
=> "Authsupported.",
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 },
165 # Storage implementation
168 my ($class, $volname) = @_;
170 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
171 return ('images', $1, $2);
174 die "unable to parse rbd volume name '$volname'\n";
178 my ($class, $scfg, $volname, $storeid) = @_;
180 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
182 my $monhost = addslashes
($scfg->{monhost
});
183 my $pool = $scfg->{pool
};
184 my $username = $scfg->{username
};
185 my $authsupported = addslashes
($scfg->{authsupported
});
187 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
189 return ($path, $vmid, $vtype);
193 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
196 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
197 if $name && $name !~ m/^vm-$vmid-/;
200 my $rdb = rbd_ls
($scfg, $storeid);
202 for (my $i = 1; $i < 100; $i++) {
203 my $tn = "vm-$vmid-disk-$i";
204 if (!defined ($rdb->{$scfg->{pool
}}->{$tn})) {
211 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
214 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
215 run_command
($cmd, errmsg
=> "rbd create $name' error", errfunc
=> sub {});
221 my ($class, $storeid, $scfg, $volname) = @_;
223 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $volname);
224 run_command
($cmd, errmsg
=> "rbd snap purge $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
226 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname);
227 run_command
($cmd, errmsg
=> "rbd rm $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
233 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
235 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
239 if (my $dat = $cache->{rbd
}->{$scfg->{pool
}}) {
240 foreach my $image (keys %$dat) {
242 my $volname = $dat->{$image}->{name
};
244 my $volid = "$storeid:$volname";
246 my $owner = $dat->{$volname}->{vmid
};
248 my $found = grep { $_ eq $volid } @$vollist;
251 next if defined ($vmid) && ($owner ne $vmid);
254 my $info = $dat->{$volname};
255 $info->{volid
} = $volid;
256 $info->{format
} = 'raw';
266 my ($class, $storeid, $scfg, $cache) = @_;
268 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
274 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
280 run_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
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;
288 return ($total, $free, $used, $active);
291 sub activate_storage
{
292 my ($class, $storeid, $scfg, $cache) = @_;
296 sub deactivate_storage
{
297 my ($class, $storeid, $scfg, $cache) = @_;
301 sub activate_volume
{
302 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
306 sub deactivate_volume
{
307 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
311 sub volume_size_info
{
312 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
314 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $volname);
319 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
321 return 1 if $running;
323 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname);
324 run_command
($cmd, errmsg
=> "rbd resize $volname' error", errfunc
=> sub {});
328 sub volume_snapshot
{
329 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
331 return 1 if $running;
333 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $volname);
334 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
338 sub volume_snapshot_rollback
{
339 my ($class, $scfg, $storeid, $volname, $snap) = @_;
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 {});
345 sub volume_snapshot_delete
{
346 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
348 return 1 if $running;
350 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $volname);
351 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
355 sub volume_has_feature
{
356 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
359 snapshot
=> { current
=> 1, snap
=> 1},
360 clone
=> { snap
=> 1},
363 my $snap = $snapname ?
'snap' : 'current';
364 return 1 if $features->{$feature}->{$snap};