1 package PVE
::CephTools
;
7 use POSIX qw
(LONG_MAX
);
11 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach);
13 my $ccname = 'ceph'; # ceph cluster name
14 my $ceph_cfgdir = "/etc/ceph";
15 my $pve_ceph_cfgpath = "/etc/pve/$ccname.conf";
16 my $ceph_cfgpath = "$ceph_cfgdir/$ccname.conf";
18 my $pve_mon_key_path = "/etc/pve/priv/$ccname.mon.keyring";
19 my $pve_ckeyring_path = "/etc/pve/priv/$ccname.client.admin.keyring";
20 my $ceph_bootstrap_osd_keyring = "/var/lib/ceph/bootstrap-osd/$ccname.keyring";
21 my $ceph_bootstrap_mds_keyring = "/var/lib/ceph/bootstrap-mds/$ccname.keyring";
23 my $ceph_bin = "/usr/bin/ceph";
27 pve_ceph_cfgpath
=> $pve_ceph_cfgpath,
28 pve_mon_key_path
=> $pve_mon_key_path,
29 pve_ckeyring_path
=> $pve_ckeyring_path,
30 ceph_bootstrap_osd_keyring
=> $ceph_bootstrap_osd_keyring,
31 ceph_bootstrap_mds_keyring
=> $ceph_bootstrap_mds_keyring,
32 long_rados_timeout
=> 60,
38 my $value = $config_hash->{$key};
40 die "no such ceph config '$key'" if !$value;
45 sub verify_blockdev_path
{
48 $path = abs_path
($path);
50 die "got unusual device path '$path'\n" if $path !~ m
|^/dev/(.*)$|;
52 $path = "/dev/$1"; # untaint
54 die "no such block device '$path'\n"
60 sub purge_all_ceph_files
{
61 # fixme: this is very dangerous - should we really support this function?
65 unlink $pve_ceph_cfgpath;
66 unlink $pve_ckeyring_path;
67 unlink $pve_mon_key_path;
69 unlink $ceph_bootstrap_osd_keyring;
70 unlink $ceph_bootstrap_mds_keyring;
72 system("rm -rf /var/lib/ceph/mon/ceph-*");
77 sub check_ceph_installed
{
81 die "ceph binaries not installed\n" if !$noerr;
88 sub check_ceph_inited
{
91 return undef if !check_ceph_installed
($noerr);
93 if (! -f
$pve_ceph_cfgpath) {
94 die "pveceph configuration not initialized\n" if !$noerr;
101 sub check_ceph_enabled
{
104 return undef if !check_ceph_inited
($noerr);
106 if (! -f
$ceph_cfgpath) {
107 die "pveceph configuration not enabled\n" if !$noerr;
114 sub parse_ceph_config
{
117 $filename = $pve_ceph_cfgpath if !$filename;
121 return $cfg if ! -f
$filename;
123 my $fh = IO
::File-
>new($filename, "r") ||
124 die "unable to open '$filename' - $!\n";
128 while (defined(my $line = <$fh>)) {
129 $line =~ s/[;#].*$//;
134 $section = $1 if $line =~ m/^\[(\S+)\]$/;
136 warn "no section - skip: $line\n";
140 if ($line =~ m/^(.*\S)\s*=\s*(\S.*)$/) {
141 $cfg->{$section}->{$1} = $2;
149 sub write_ceph_config
{
154 my $cond_write_sec = sub {
157 foreach my $section (keys %$cfg) {
158 next if $section !~ m/^$re$/;
159 $out .= "[$section]\n";
160 foreach my $key (sort keys %{$cfg->{$section}}) {
161 $out .= "\t $key = $cfg->{$section}->{$key}\n";
167 &$cond_write_sec('global');
168 &$cond_write_sec('mon');
169 &$cond_write_sec('osd');
170 &$cond_write_sec('mon\..*');
171 &$cond_write_sec('osd\..*');
173 PVE
::Tools
::file_set_contents
($pve_ceph_cfgpath, $out);
176 sub setup_pve_symlinks
{
177 # fail if we find a real file instead of a link
178 if (-f
$ceph_cfgpath) {
179 my $lnk = readlink($ceph_cfgpath);
180 die "file '$ceph_cfgpath' already exists\n"
181 if !$lnk || $lnk ne $pve_ceph_cfgpath;
183 symlink($pve_ceph_cfgpath, $ceph_cfgpath) ||
184 die "unable to create symlink '$ceph_cfgpath' - $!\n";
188 sub ceph_service_cmd
{
189 # ceph daemons does not call 'setsid', so we do that ourself
190 # (fork_worker send KILL to whole process group)
191 PVE
::Tools
::run_command
(['setsid', 'service', 'ceph', '-c', $pve_ceph_cfgpath, @_]);
197 my $fd = IO
::File-
>new("/proc/mounts", "r") ||
198 die "unable to open /proc/mounts - $!\n";
202 while (defined(my $line = <$fd>)) {
203 my ($dev, $path, $fstype) = split(/\s+/, $line);
204 next if !($dev && $path && $fstype);
205 next if $dev !~ m
|^/dev/|;
206 my $real_dev = abs_path
($dev);
207 $mounted->{$real_dev} = $path;
211 my $dev_is_mounted = sub {
213 return $mounted->{$dev};
216 my $dir_is_epmty = sub {
219 my $dh = IO
::Dir-
>new ($dir);
222 while (defined(my $tmp = $dh->read)) {
223 next if $tmp eq '.' || $tmp eq '..';
231 my $journal_uuid = '45b0969e-9b03-4f30-b4c6-b4b80ceff106';
233 my $journalhash = {};
234 dir_glob_foreach
('/dev/disk/by-parttypeuuid', "$journal_uuid\..+", sub {
236 my $real_dev = abs_path
("/dev/disk/by-parttypeuuid/$entry");
237 $journalhash->{$real_dev} = 1;
240 dir_glob_foreach
('/sys/block', '.*', sub {
243 return if $dev eq '.';
244 return if $dev eq '..';
246 return if $dev =~ m
|^ram\d
+$|; # skip ram devices
247 return if $dev =~ m
|^loop\d
+$|; # skip loop devices
248 return if $dev =~ m
|^md\d
+$|; # skip md devices
249 return if $dev =~ m
|^dm-
.*$|; # skip dm related things
250 return if $dev =~ m
|^fd\d
+$|; # skip Floppy
251 return if $dev =~ m
|^sr\d
+$|; # skip CDs
253 my $devdir = "/sys/block/$dev/device";
254 return if ! -d
$devdir;
256 my $size = file_read_firstline
("/sys/block/$dev/size");
261 my $info = `udevadm info --path /sys/block/$dev --query all`;
264 return if $info !~ m/^E: DEVTYPE=disk$/m;
265 return if $info =~ m/^E: ID_CDROM/m;
267 my $serial = 'unknown';
268 if ($info =~ m/^E: ID_SERIAL_SHORT=(\S+)$/m) {
273 if ($info =~ m/^E: ID_PART_TABLE_TYPE=gpt$/m) {
277 # detect SSD (fixme - currently only works for ATA disks)
278 my $rpm = 7200; # default guess
279 if ($info =~ m/^E: ID_ATA_ROTATION_RATE_RPM=(\d+)$/m) {
283 my $vendor = file_read_firstline
("$devdir/vendor") || 'unknown';
284 my $model = file_read_firstline
("$devdir/model") || 'unknown';
288 $used = 'LVM' if !&$dir_is_epmty("/sys/block/$dev/holders");
290 $used = 'mounted' if &$dev_is_mounted("/dev/$dev");
292 $disklist->{$dev} = {
303 my $journal_count = 0;
305 my $found_partitions;
307 my $found_mountpoints;
308 dir_glob_foreach
("/sys/block/$dev", "$dev.+", sub {
311 $found_partitions = 1;
313 if (my $mp = &$dev_is_mounted("/dev/$part")) {
314 $found_mountpoints = 1;
315 if ($mp =~ m
|^/var/lib
/ceph/osd/ceph-
(\d
+)$|) {
319 if (!&$dir_is_epmty("/sys/block/$dev/$part/holders")) {
322 $journal_count++ if $journalhash->{"/dev/$part"};
325 $used = 'mounted' if $found_mountpoints && !$used;
326 $used = 'LVM' if $found_lvm && !$used;
327 $used = 'partitions' if $found_partitions && !$used;
329 $disklist->{$dev}->{used
} = $used if $used;
330 $disklist->{$dev}->{osdid
} = $osdid;
331 $disklist->{$dev}->{journals
} = $journal_count;