1 package PVE
::Ceph
::Tools
;
11 use PVE
::Tools
qw(run_command dir_glob_foreach);
12 use PVE
::Cluster
qw(cfs_read_file);
15 my $ccname = 'ceph'; # ceph cluster name
16 my $ceph_cfgdir = "/etc/ceph";
17 my $pve_ceph_cfgpath = "/etc/pve/$ccname.conf";
18 my $ceph_cfgpath = "$ceph_cfgdir/$ccname.conf";
20 my $pve_mon_key_path = "/etc/pve/priv/$ccname.mon.keyring";
21 my $pve_ckeyring_path = "/etc/pve/priv/$ccname.client.admin.keyring";
22 my $ceph_bootstrap_osd_keyring = "/var/lib/ceph/bootstrap-osd/$ccname.keyring";
23 my $ceph_bootstrap_mds_keyring = "/var/lib/ceph/bootstrap-mds/$ccname.keyring";
24 my $ceph_mds_data_dir = '/var/lib/ceph/mds';
27 ceph_bin
=> "/usr/bin/ceph",
28 ceph_mon
=> "/usr/bin/ceph-mon",
29 ceph_mgr
=> "/usr/bin/ceph-mgr",
30 ceph_osd
=> "/usr/bin/ceph-osd",
31 ceph_mds
=> "/usr/bin/ceph-mds",
32 ceph_volume
=> '/usr/sbin/ceph-volume',
37 pve_ceph_cfgpath
=> $pve_ceph_cfgpath,
38 pve_mon_key_path
=> $pve_mon_key_path,
39 pve_ckeyring_path
=> $pve_ckeyring_path,
40 ceph_bootstrap_osd_keyring
=> $ceph_bootstrap_osd_keyring,
41 ceph_bootstrap_mds_keyring
=> $ceph_bootstrap_mds_keyring,
42 ceph_mds_data_dir
=> $ceph_mds_data_dir,
43 long_rados_timeout
=> 60,
46 sub get_local_version
{
49 if (check_ceph_installed
('ceph_bin', $noerr)) {
51 run_command
([$ceph_service->{ceph_bin
}, '--version'],
53 outfunc
=> sub { $ceph_version = shift; });
54 if ($ceph_version && $ceph_version =~ /^ceph.*\s((\d+)\.(\d+)\.(\d+))/) {
55 # return (version, major, minor, patch) : major;
56 return wantarray ?
($1, $2, $3, $4) : $2;
66 my $value = $config_hash->{$key};
68 die "no such ceph config '$key'" if !$value;
73 sub purge_all_ceph_files
{
74 # fixme: this is very dangerous - should we really support this function?
78 unlink $pve_ceph_cfgpath;
79 unlink $pve_ckeyring_path;
80 unlink $pve_mon_key_path;
82 unlink $ceph_bootstrap_osd_keyring;
83 unlink $ceph_bootstrap_mds_keyring;
85 system("rm -rf /var/lib/ceph/mon/ceph-*");
90 sub check_ceph_installed
{
91 my ($service, $noerr) = @_;
93 $service = 'ceph_bin' if !defined($service);
95 if (! -x
$ceph_service->{$service}) {
96 die "binary not installed: $ceph_service->{$service}\n" if !$noerr;
103 sub check_ceph_inited
{
106 return undef if !check_ceph_installed
('ceph_mon', $noerr);
108 if (! -f
$pve_ceph_cfgpath) {
109 die "pveceph configuration not initialized\n" if !$noerr;
116 sub check_ceph_enabled
{
119 return undef if !check_ceph_inited
($noerr);
121 if (! -f
$ceph_cfgpath) {
122 die "pveceph configuration not enabled\n" if !$noerr;
130 my ($pool, $param, $rados) = @_;
132 if (!defined($rados)) {
133 $rados = PVE
::RADOS-
>new();
136 my $pg_num = $param->{pg_num
} || 128;
137 my $size = $param->{size
} || 3;
138 my $min_size = $param->{min_size
} || 2;
139 my $application = $param->{application
} // 'rbd';
141 $rados->mon_command({
142 prefix
=> "osd pool create",
144 pg_num
=> int($pg_num),
148 $rados->mon_command({
149 prefix
=> "osd pool set",
156 $rados->mon_command({
157 prefix
=> "osd pool set",
164 if (defined($param->{crush_rule
})) {
165 $rados->mon_command({
166 prefix
=> "osd pool set",
169 val
=> $param->{crush_rule
},
174 $rados->mon_command({
175 prefix
=> "osd pool application enable",
183 my ($pool, $rados) = @_;
185 if (!defined($rados)) {
186 $rados = PVE
::RADOS-
>new();
189 my $res = $rados->mon_command({ prefix
=> "osd lspools" });
195 my ($pool, $rados) = @_;
197 if (!defined($rados)) {
198 $rados = PVE
::RADOS-
>new();
201 # fixme: '--yes-i-really-really-mean-it'
202 $rados->mon_command({
203 prefix
=> "osd pool delete",
206 sure
=> '--yes-i-really-really-mean-it',
211 sub setup_pve_symlinks
{
212 # fail if we find a real file instead of a link
213 if (-f
$ceph_cfgpath) {
214 my $lnk = readlink($ceph_cfgpath);
215 die "file '$ceph_cfgpath' already exists\n"
216 if !$lnk || $lnk ne $pve_ceph_cfgpath;
218 symlink($pve_ceph_cfgpath, $ceph_cfgpath) ||
219 die "unable to create symlink '$ceph_cfgpath' - $!\n";
223 # wipe the first 200 MB to clear off leftovers from previous use, otherwise a
228 my @wipe_cmd = qw(/bin/dd if=/dev/zero bs=1M conv=fdatasync);
230 foreach my $devpath (@devs) {
231 my $devname = basename
($devpath);
232 my $dev_size = PVE
::Tools
::file_get_contents
("/sys/class/block/$devname/size");
234 ($dev_size) = $dev_size =~ m
|(\d
+)|; # untaint $dev_size
235 die "Coulnd't get the size of the device $devname\n" if (!defined($dev_size));
237 my $size = ($dev_size * 512 / 1024 / 1024);
238 my $count = ($size < 200) ?
$size : 200;
240 print "wipe disk/partition: $devpath\n";
241 eval { run_command
([@wipe_cmd, "count=$count", "of=${devpath}"]) };
246 # get ceph-volume managed osds
247 sub ceph_volume_list
{
251 if (!check_ceph_installed
('ceph_volume', 1)) {
255 my $cmd = [$ceph_service->{ceph_volume
}, 'lvm', 'list', '--format', 'json'];
256 run_command
($cmd, outfunc
=> sub {
260 $result = eval { decode_json
($output) };
265 sub ceph_volume_zap
{
266 my ($osdid, $destroy) = @_;
268 die "no osdid given\n" if !defined($osdid);
270 my $cmd = [$ceph_service->{ceph_volume
}, 'lvm', 'zap', '--osd-id', $osdid];
271 push @$cmd, '--destroy' if $destroy;