]>
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);
15 "G" => 1024*1024*1024,
16 "T" => 1024*1024*1024*1024,
21 my ($scfg, $storeid, $op, @options) = @_;
23 my $monhost = $scfg->{monhost
};
26 my $cmd = ['/usr/bin/rbd', '-p', $scfg->{pool
}, '-m', $monhost, '-n',
27 "client.$scfg->{username}",
28 '--keyring', "/etc/pve/priv/ceph/${storeid}.keyring",
29 '--auth_supported', $scfg->{authsupported
}, $op];
31 push @$cmd, @options if scalar(@options);
37 my ($scfg, $storeid, $op, @options) = @_;
39 my $monhost = $scfg->{monhost
};
42 my $cmd = ['/usr/bin/rados', '-p', $scfg->{pool
}, '-m', $monhost, '-n',
43 "client.$scfg->{username}",
44 '--keyring', "/etc/pve/priv/ceph/${storeid}.keyring",
45 '--auth_supported', $scfg->{authsupported
}, $op];
47 push @$cmd, @options if scalar(@options);
53 my ($scfg, $storeid) = @_;
55 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
62 if ($line =~ m/^(vm-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/(vm-\d
+-\S
+@\S+))?
/) {
63 my ($image, $owner, $size, $unit, $parent) = ($1, $2, $3, $4, $7);
65 $list->{$scfg->{pool
}}->{$image} = {
67 size
=> $size*rbd_unittobytes()->{$unit},
75 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
79 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
85 my ($scfg, $storeid, $volname) = @_;
87 my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
94 if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
95 $size = $1 * rbd_unittobytes
()->{$2} if ($1);
96 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
101 run_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
103 return ($size, $parent);
115 PVE
::JSONSchema
::register_format
('pve-storage-monhost', \
&parse_monhost
);
117 my ($name, $noerr) = @_;
119 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
120 return undef if $noerr;
121 die "lvm name '$name' contains illegal characters\n";
133 content
=> [ {images
=> 1}, { images
=> 1 }],
140 description
=> "Monitors daemon ips.",
144 description
=> "Pool.",
148 description
=> "RBD Id.",
152 description
=> "Authsupported.",
160 nodes
=> { optional
=> 1 },
161 disable
=> { optional
=> 1 },
162 monhost
=> { fixed
=> 1 },
163 pool
=> { fixed
=> 1 },
164 username
=> { fixed
=> 1 },
165 authsupported
=> { fixed
=> 1 },
166 content
=> { optional
=> 1 },
170 # Storage implementation
173 my ($class, $volname) = @_;
175 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
176 return ('images', $1, $2);
179 die "unable to parse rbd volume name '$volname'\n";
183 my ($class, $scfg, $volname, $storeid) = @_;
185 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
187 my $monhost = addslashes
($scfg->{monhost
});
188 my $pool = $scfg->{pool
};
189 my $username = $scfg->{username
};
190 my $authsupported = addslashes
($scfg->{authsupported
});
192 my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost";
194 return ($path, $vmid, $vtype);
198 my ($class, $storeid, $scfg, $volname) = @_;
200 die "not implemented";
204 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
206 die "not implemented";
210 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
213 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
214 if $name && $name !~ m/^vm-$vmid-/;
217 my $rdb = rbd_ls
($scfg, $storeid);
219 for (my $i = 1; $i < 100; $i++) {
220 my $tn = "vm-$vmid-disk-$i";
221 if (!defined ($rdb->{$scfg->{pool
}}->{$tn})) {
228 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
231 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--format' , 2, '--size', ($size/1024), $name);
232 run_command
($cmd, errmsg
=> "rbd create $name' error", errfunc
=> sub {});
238 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
240 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $volname);
241 run_command
($cmd, errmsg
=> "rbd snap purge $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
243 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname);
244 run_command
($cmd, errmsg
=> "rbd rm $volname' error", outfunc
=> sub {}, errfunc
=> sub {});
250 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
252 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
256 if (my $dat = $cache->{rbd
}->{$scfg->{pool
}}) {
257 foreach my $image (keys %$dat) {
259 my $volname = $dat->{$image}->{name
};
261 my $volid = "$storeid:$volname";
263 my $owner = $dat->{$volname}->{vmid
};
265 my $found = grep { $_ eq $volid } @$vollist;
268 next if defined ($vmid) && ($owner ne $vmid);
271 my $info = $dat->{$volname};
272 $info->{volid
} = $volid;
273 $info->{format
} = 'raw';
283 my ($class, $storeid, $scfg, $cache) = @_;
285 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
291 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
297 run_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
300 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
301 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
302 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
305 return ($total, $free, $used, $active);
308 sub activate_storage
{
309 my ($class, $storeid, $scfg, $cache) = @_;
313 sub deactivate_storage
{
314 my ($class, $storeid, $scfg, $cache) = @_;
318 sub activate_volume
{
319 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
323 sub deactivate_volume
{
324 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
328 sub volume_size_info
{
329 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
331 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $volname);
336 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
338 return 1 if $running;
340 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname);
341 run_command
($cmd, errmsg
=> "rbd resize $volname' error", errfunc
=> sub {});
345 sub volume_snapshot
{
346 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
348 return 1 if $running;
350 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $volname);
351 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
355 sub volume_snapshot_rollback
{
356 my ($class, $scfg, $storeid, $volname, $snap) = @_;
358 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $volname);
359 run_command
($cmd, errmsg
=> "rbd snapshot $volname to $snap' error", errfunc
=> sub {});
362 sub volume_snapshot_delete
{
363 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
365 return 1 if $running;
367 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $volname);
368 run_command
($cmd, errmsg
=> "rbd snapshot $volname' error", errfunc
=> sub {});
372 sub volume_has_feature
{
373 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
376 snapshot
=> { current
=> 1, snap
=> 1},
377 clone
=> { snap
=> 1},
380 my $snap = $snapname ?
'snap' : 'current';
381 return 1 if $features->{$feature}->{$snap};