]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Ceph.pm
1 package PVE
::API2
::CephOSD
;
9 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach);
10 use PVE
::Exception
qw(raise raise_param_exc);
12 use PVE
::Cluster
qw(cfs_lock_file cfs_read_file cfs_write_file);
13 use PVE
::AccessControl
;
16 use PVE
::RPCEnvironment
;
17 use PVE
::JSONSchema
qw(get_standard_option);
22 use base
qw(PVE::RESTHandler);
24 use Data
::Dumper
; # fixme: remove
26 my $get_osd_status = sub {
27 my ($rados, $osdid) = @_;
29 my $stat = $rados->mon_command({ prefix
=> 'osd dump' });
31 my $osdlist = $stat->{osds
} || [];
33 my $flags = $stat->{flags
} || undef;
36 foreach my $d (@$osdlist) {
37 $osdstat->{$d->{osd
}} = $d if defined($d->{osd
});
39 if (defined($osdid)) {
40 die "no such OSD '$osdid'\n" if !$osdstat->{$osdid};
41 return $osdstat->{$osdid};
44 return wantarray?
($osdstat, $flags):$osdstat;
47 my $get_osd_usage = sub {
50 my $osdlist = $rados->mon_command({ prefix
=> 'pg dump',
51 dumpcontents
=> [ 'osds' ]}) || [];
54 foreach my $d (@$osdlist) {
55 $osdstat->{$d->{osd
}} = $d if defined($d->{osd
});
61 __PACKAGE__-
>register_method ({
65 description
=> "Get Ceph osd list/tree.",
69 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
72 additionalProperties
=> 0,
74 node
=> get_standard_option
('pve-node'),
77 # fixme: return a list instead of extjs tree format ?
84 PVE
::CephTools
::check_ceph_inited
();
86 my $rados = PVE
::RADOS-
>new();
87 my $res = $rados->mon_command({ prefix
=> 'osd tree' });
89 die "no tree nodes found\n" if !($res && $res->{nodes
});
91 my ($osdhash, $flags) = &$get_osd_status($rados);
93 my $usagehash = &$get_osd_usage($rados);
97 foreach my $e (@{$res->{nodes
}}) {
98 $nodes->{$e->{id
}} = $e;
106 foreach my $opt (qw(status crush_weight reweight)) {
107 $new->{$opt} = $e->{$opt} if defined($e->{$opt});
110 if (my $stat = $osdhash->{$e->{id
}}) {
111 $new->{in} = $stat->{in} if defined($stat->{in});
114 if (my $stat = $usagehash->{$e->{id
}}) {
115 $new->{total_space
} = ($stat->{kb
} || 1) * 1024;
116 $new->{bytes_used
} = ($stat->{kb_used
} || 0) * 1024;
117 $new->{percent_used
} = ($new->{bytes_used
}*100)/$new->{total_space
};
118 if (my $d = $stat->{fs_perf_stat
}) {
119 $new->{commit_latency_ms
} = $d->{commit_latency_ms
};
120 $new->{apply_latency_ms
} = $d->{apply_latency_ms
};
124 $newnodes->{$e->{id
}} = $new;
127 foreach my $e (@{$res->{nodes
}}) {
128 my $new = $newnodes->{$e->{id
}};
129 if ($e->{children
} && scalar(@{$e->{children
}})) {
130 $new->{children
} = [];
132 foreach my $cid (@{$e->{children
}}) {
133 $nodes->{$cid}->{parent
} = $e->{id
};
134 if ($nodes->{$cid}->{type
} eq 'osd' &&
135 $e->{type
} eq 'host') {
136 $newnodes->{$cid}->{host
} = $e->{name
};
138 push @{$new->{children
}}, $newnodes->{$cid};
141 $new->{leaf
} = ($e->{id
} >= 0) ?
1 : 0;
146 foreach my $e (@{$res->{nodes
}}) {
147 if (!$nodes->{$e->{id
}}->{parent
}) {
148 push @$roots, $newnodes->{$e->{id
}};
152 die "no root node\n" if !@$roots;
154 my $data = { root
=> { leaf
=> 0, children
=> $roots } };
156 # we want this for the noout flag
157 $data->{flags
} = $flags if $flags;
162 __PACKAGE__-
>register_method ({
166 description
=> "Create OSD",
170 additionalProperties
=> 0,
172 node
=> get_standard_option
('pve-node'),
174 description
=> "Block device name.",
178 description
=> "Block device name for journal.",
183 description
=> "File system type.",
185 enum
=> ['xfs', 'ext4', 'btrfs'],
191 returns
=> { type
=> 'string' },
195 my $rpcenv = PVE
::RPCEnvironment
::get
();
197 my $authuser = $rpcenv->get_user();
199 PVE
::CephTools
::check_ceph_inited
();
201 PVE
::CephTools
::setup_pve_symlinks
();
205 if ($param->{journal_dev
} && ($param->{journal_dev
} ne $param->{dev
})) {
206 $journal_dev = PVE
::Diskmanage
::verify_blockdev_path
($param->{journal_dev
});
209 $param->{dev
} = PVE
::Diskmanage
::verify_blockdev_path
($param->{dev
});
211 my $devname = $param->{dev
};
212 $devname =~ s
|/dev/||;
214 my $disklist = PVE
::Diskmanage
::get_disks
($devname, 1);
216 my $diskinfo = $disklist->{$devname};
217 die "unable to get device info for '$devname'\n"
220 die "device '$param->{dev}' is in use\n"
221 if $diskinfo->{used
};
223 my $devpath = $diskinfo->{devpath
};
224 my $rados = PVE
::RADOS-
>new();
225 my $monstat = $rados->mon_command({ prefix
=> 'mon_status' });
226 die "unable to get fsid\n" if !$monstat->{monmap
} || !$monstat->{monmap
}->{fsid
};
228 my $fsid = $monstat->{monmap
}->{fsid
};
229 $fsid = $1 if $fsid =~ m/^([0-9a-f\-]+)$/;
231 my $ceph_bootstrap_osd_keyring = PVE
::CephTools
::get_config
('ceph_bootstrap_osd_keyring');
233 if (! -f
$ceph_bootstrap_osd_keyring) {
234 my $bindata = $rados->mon_command({ prefix
=> 'auth get', entity
=> 'client.bootstrap-osd', format
=> 'plain' });
235 PVE
::Tools
::file_set_contents
($ceph_bootstrap_osd_keyring, $bindata);
241 my $fstype = $param->{fstype
} || 'xfs';
243 print "create OSD on $devpath ($fstype)\n";
245 my $ccname = PVE
::CephTools
::get_config
('ccname');
247 my $cmd = ['ceph-disk', 'prepare', '--zap-disk', '--fs-type', $fstype,
248 '--cluster', $ccname, '--cluster-uuid', $fsid ];
251 print "using device '$journal_dev' for journal\n";
252 push @$cmd, '--journal-dev', $devpath, $journal_dev;
254 push @$cmd, $devpath;
260 return $rpcenv->fork_worker('cephcreateosd', $devname, $authuser, $worker);
263 __PACKAGE__-
>register_method ({
264 name
=> 'destroyosd',
267 description
=> "Destroy OSD",
271 additionalProperties
=> 0,
273 node
=> get_standard_option
('pve-node'),
275 description
=> 'OSD ID',
279 description
=> "If set, we remove partition table entries.",
286 returns
=> { type
=> 'string' },
290 my $rpcenv = PVE
::RPCEnvironment
::get
();
292 my $authuser = $rpcenv->get_user();
294 PVE
::CephTools
::check_ceph_inited
();
296 my $osdid = $param->{osdid
};
298 my $rados = PVE
::RADOS-
>new();
299 my $osdstat = &$get_osd_status($rados, $osdid);
301 die "osd is in use (in == 1)\n" if $osdstat->{in};
302 #&$run_ceph_cmd(['osd', 'out', $osdid]);
304 die "osd is still runnung (up == 1)\n" if $osdstat->{up
};
306 my $osdsection = "osd.$osdid";
311 # reopen with longer timeout
312 $rados = PVE
::RADOS-
>new(timeout
=> PVE
::CephTools
::get_config
('long_rados_timeout'));
314 print "destroy OSD $osdsection\n";
316 eval { PVE
::CephTools
::ceph_service_cmd
('stop', $osdsection); };
319 print "Remove $osdsection from the CRUSH map\n";
320 $rados->mon_command({ prefix
=> "osd crush remove", name
=> $osdsection, format
=> 'plain' });
322 print "Remove the $osdsection authentication key.\n";
323 $rados->mon_command({ prefix
=> "auth del", entity
=> $osdsection, format
=> 'plain' });
325 print "Remove OSD $osdsection\n";
326 $rados->mon_command({ prefix
=> "osd rm", ids
=> [ $osdsection ], format
=> 'plain' });
328 # try to unmount from standard mount point
329 my $mountpoint = "/var/lib/ceph/osd/ceph-$osdid";
331 my $remove_partition = sub {
334 return if !$part || (! -b
$part );
335 my $partnum = PVE
::Diskmanage
::get_partnum
($part);
336 my $devpath = PVE
::Diskmanage
::get_blockdev
($part);
338 print "remove partition $part (disk '${devpath}', partnum $partnum)\n";
339 eval { run_command
(['/sbin/sgdisk', '-d', $partnum, "${devpath}"]); };
346 if ($param->{cleanup
}) {
347 my $jpath = "$mountpoint/journal";
348 $journal_part = abs_path
($jpath);
350 if (my $fd = IO
::File-
>new("/proc/mounts", "r")) {
351 while (defined(my $line = <$fd>)) {
352 my ($dev, $path, $fstype) = split(/\s+/, $line);
353 next if !($dev && $path && $fstype);
354 next if $dev !~ m
|^/dev/|;
355 if ($path eq $mountpoint) {
356 $data_part = abs_path
($dev);
364 print "Unmount OSD $osdsection from $mountpoint\n";
365 eval { run_command
(['/bin/umount', $mountpoint]); };
368 } elsif ($param->{cleanup
}) {
369 #be aware of the ceph udev rules which can remount.
370 &$remove_partition($data_part);
371 &$remove_partition($journal_part);
375 return $rpcenv->fork_worker('cephdestroyosd', $osdsection, $authuser, $worker);
378 __PACKAGE__-
>register_method ({
380 path
=> '{osdid}/in',
382 description
=> "ceph osd in",
386 check
=> ['perm', '/', [ 'Sys.Modify' ]],
389 additionalProperties
=> 0,
391 node
=> get_standard_option
('pve-node'),
393 description
=> 'OSD ID',
398 returns
=> { type
=> "null" },
402 PVE
::CephTools
::check_ceph_inited
();
404 my $osdid = $param->{osdid
};
406 my $rados = PVE
::RADOS-
>new();
408 my $osdstat = &$get_osd_status($rados, $osdid); # osd exists?
410 my $osdsection = "osd.$osdid";
412 $rados->mon_command({ prefix
=> "osd in", ids
=> [ $osdsection ], format
=> 'plain' });
417 __PACKAGE__-
>register_method ({
419 path
=> '{osdid}/out',
421 description
=> "ceph osd out",
425 check
=> ['perm', '/', [ 'Sys.Modify' ]],
428 additionalProperties
=> 0,
430 node
=> get_standard_option
('pve-node'),
432 description
=> 'OSD ID',
437 returns
=> { type
=> "null" },
441 PVE
::CephTools
::check_ceph_inited
();
443 my $osdid = $param->{osdid
};
445 my $rados = PVE
::RADOS-
>new();
447 my $osdstat = &$get_osd_status($rados, $osdid); # osd exists?
449 my $osdsection = "osd.$osdid";
451 $rados->mon_command({ prefix
=> "osd out", ids
=> [ $osdsection ], format
=> 'plain' });
456 package PVE
::API2
::Ceph
;
462 use POSIX qw
(LONG_MAX
);
463 use Cwd
qw(abs_path);
469 use PVE
::Tools
qw(extract_param run_command file_get_contents file_read_firstline dir_glob_regex dir_glob_foreach);
470 use PVE
::Exception
qw(raise raise_param_exc);
472 use PVE
::Cluster
qw(cfs_lock_file cfs_read_file cfs_write_file);
473 use PVE
::AccessControl
;
475 use PVE
::RESTHandler
;
476 use PVE
::RPCEnvironment
;
477 use PVE
::JSONSchema
qw(get_standard_option);
482 use base
qw(PVE::RESTHandler);
484 use Data
::Dumper
; # fixme: remove
486 my $pve_osd_default_journal_size = 1024*5;
488 __PACKAGE__-
>register_method ({
489 subclass
=> "PVE::API2::CephOSD",
493 __PACKAGE__-
>register_method ({
497 description
=> "Directory index.",
498 permissions
=> { user
=> 'all' },
500 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
503 additionalProperties
=> 0,
505 node
=> get_standard_option
('pve-node'),
514 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
526 { name
=> 'status' },
528 { name
=> 'config' },
537 __PACKAGE__-
>register_method ({
541 description
=> "List local disks.",
545 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
548 additionalProperties
=> 0,
550 node
=> get_standard_option
('pve-node'),
552 description
=> "Only list specific types of disks.",
554 enum
=> ['unused', 'journal_disks'],
564 dev
=> { type
=> 'string' },
565 used
=> { type
=> 'string', optional
=> 1 },
566 gpt
=> { type
=> 'boolean' },
567 size
=> { type
=> 'integer' },
568 osdid
=> { type
=> 'integer' },
569 vendor
=> { type
=> 'string', optional
=> 1 },
570 model
=> { type
=> 'string', optional
=> 1 },
571 serial
=> { type
=> 'string', optional
=> 1 },
574 # links => [ { rel => 'child', href => "{}" } ],
579 PVE
::CephTools
::check_ceph_inited
();
581 my $disks = PVE
::Diskmanage
::get_disks
(undef, 1);
584 foreach my $dev (keys %$disks) {
585 my $d = $disks->{$dev};
586 if ($param->{type
}) {
587 if ($param->{type
} eq 'journal_disks') {
588 next if $d->{osdid
} >= 0;
590 } elsif ($param->{type
} eq 'unused') {
593 die "internal error"; # should not happen
597 $d->{dev
} = "/dev/$dev";
604 __PACKAGE__-
>register_method ({
609 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
611 description
=> "Get Ceph configuration.",
613 additionalProperties
=> 0,
615 node
=> get_standard_option
('pve-node'),
618 returns
=> { type
=> 'string' },
622 PVE
::CephTools
::check_ceph_inited
();
624 my $path = PVE
::CephTools
::get_config
('pve_ceph_cfgpath');
625 return PVE
::Tools
::file_get_contents
($path);
629 __PACKAGE__-
>register_method ({
633 description
=> "Get Ceph monitor list.",
637 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
640 additionalProperties
=> 0,
642 node
=> get_standard_option
('pve-node'),
650 name
=> { type
=> 'string' },
651 addr
=> { type
=> 'string' },
654 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
659 PVE
::CephTools
::check_ceph_inited
();
663 my $cfg = PVE
::CephTools
::parse_ceph_config
();
666 foreach my $section (keys %$cfg) {
667 my $d = $cfg->{$section};
668 if ($section =~ m/^mon\.(\S+)$/) {
670 if ($d->{'mon addr'} && $d->{'host'}) {
671 $monhash->{$monid} = {
672 addr
=> $d->{'mon addr'},
673 host
=> $d->{'host'},
681 my $rados = PVE
::RADOS-
>new();
682 my $monstat = $rados->mon_command({ prefix
=> 'mon_status' });
683 my $mons = $monstat->{monmap
}->{mons
};
684 foreach my $d (@$mons) {
685 next if !defined($d->{name
});
686 $monhash->{$d->{name
}}->{rank
} = $d->{rank
};
687 $monhash->{$d->{name
}}->{addr
} = $d->{addr
};
688 if (grep { $_ eq $d->{rank
} } @{$monstat->{quorum
}}) {
689 $monhash->{$d->{name
}}->{quorum
} = 1;
695 return PVE
::RESTHandler
::hash_to_array
($monhash, 'name');
698 __PACKAGE__-
>register_method ({
702 description
=> "Create initial ceph default configuration and setup symlinks.",
706 check
=> ['perm', '/', [ 'Sys.Modify' ]],
709 additionalProperties
=> 0,
711 node
=> get_standard_option
('pve-node'),
713 description
=> "Use specific network for all ceph related traffic",
714 type
=> 'string', format
=> 'CIDR',
719 description
=> 'Number of replicas per object',
727 description
=> "Placement group bits, used to specify the " .
728 "default number of placement groups.\n\nNOTE: 'osd pool " .
729 "default pg num' does not work for default pools.",
737 description
=> "Disable cephx authentification.\n\n" .
738 "WARNING: cephx is a security feature protecting against " .
739 "man-in-the-middle attacks. Only consider disabling cephx ".
740 "if your network is private!",
747 returns
=> { type
=> 'null' },
751 PVE
::CephTools
::check_ceph_installed
();
753 # simply load old config if it already exists
754 my $cfg = PVE
::CephTools
::parse_ceph_config
();
756 if (!$cfg->{global
}) {
761 UUID
::generate
($uuid);
762 UUID
::unparse
($uuid, $fsid);
764 my $auth = $param->{disable_cephx
} ?
'none' : 'cephx';
768 'auth cluster required' => $auth,
769 'auth service required' => $auth,
770 'auth client required' => $auth,
771 'osd journal size' => $pve_osd_default_journal_size,
772 'osd pool default min size' => 1,
775 # this does not work for default pools
776 #'osd pool default pg num' => $pg_num,
777 #'osd pool default pgp num' => $pg_num,
780 $cfg->{global
}->{keyring
} = '/etc/pve/priv/$cluster.$name.keyring';
781 $cfg->{osd
}->{keyring
} = '/var/lib/ceph/osd/ceph-$id/keyring';
783 $cfg->{global
}->{'osd pool default size'} = $param->{size
} if $param->{size
};
785 if ($param->{pg_bits
}) {
786 $cfg->{global
}->{'osd pg bits'} = $param->{pg_bits
};
787 $cfg->{global
}->{'osd pgp bits'} = $param->{pg_bits
};
790 if ($param->{network
}) {
791 $cfg->{global
}->{'public network'} = $param->{network
};
792 $cfg->{global
}->{'cluster network'} = $param->{network
};
795 PVE
::CephTools
::write_ceph_config
($cfg);
797 PVE
::CephTools
::setup_pve_symlinks
();
802 my $find_node_ip = sub {
805 my $net = Net
::IP-
>new($cidr) || die Net
::IP
::Error
() . "\n";
806 my $id = $net->version == 6 ?
'address6' : 'address';
808 my $config = PVE
::INotify
::read_file
('interfaces');
809 my $ifaces = $config->{ifaces
};
811 foreach my $iface (keys %$ifaces) {
812 my $d = $ifaces->{$iface};
814 my $a = Net
::IP-
>new($d->{$id});
816 return $d->{$id} if $net->overlaps($a);
819 die "unable to find local address within network '$cidr'\n";
822 __PACKAGE__-
>register_method ({
826 description
=> "Create Ceph Monitor",
830 check
=> ['perm', '/', [ 'Sys.Modify' ]],
833 additionalProperties
=> 0,
835 node
=> get_standard_option
('pve-node'),
838 returns
=> { type
=> 'string' },
842 PVE
::CephTools
::check_ceph_inited
();
844 PVE
::CephTools
::setup_pve_symlinks
();
846 my $rpcenv = PVE
::RPCEnvironment
::get
();
848 my $authuser = $rpcenv->get_user();
850 my $cfg = PVE
::CephTools
::parse_ceph_config
();
854 my $monaddrhash = {};
856 my $systemd_managed = PVE
::CephTools
::systemd_managed
();
858 foreach my $section (keys %$cfg) {
859 next if $section eq 'global';
860 my $d = $cfg->{$section};
861 if ($section =~ m/^mon\./) {
863 if ($d->{'mon addr'}) {
864 $monaddrhash->{$d->{'mon addr'}} = $section;
870 for (my $i = 0; $i < 7; $i++) {
871 if (!$cfg->{"mon.$i"}) {
876 die "unable to find usable monitor id\n" if !defined($monid);
878 my $monsection = "mon.$monid";
880 if (my $pubnet = $cfg->{global
}->{'public network'}) {
881 $ip = &$find_node_ip($pubnet);
883 $ip = PVE
::Cluster
::remote_node_ip
($param->{node
});
886 my $monaddr = Net
::IP
::ip_is_ipv6
($ip) ?
"[$ip]:6789" : "$ip:6789";
887 my $monname = $param->{node
};
889 die "monitor '$monsection' already exists\n" if $cfg->{$monsection};
890 die "monitor address '$monaddr' already in use by '$monaddrhash->{$monaddr}'\n"
891 if $monaddrhash->{$monaddr};
896 my $pve_ckeyring_path = PVE
::CephTools
::get_config
('pve_ckeyring_path');
898 if (! -f
$pve_ckeyring_path) {
899 run_command
("ceph-authtool $pve_ckeyring_path --create-keyring " .
900 "--gen-key -n client.admin");
903 my $pve_mon_key_path = PVE
::CephTools
::get_config
('pve_mon_key_path');
904 if (! -f
$pve_mon_key_path) {
905 run_command
("cp $pve_ckeyring_path $pve_mon_key_path.tmp");
906 run_command
("ceph-authtool $pve_mon_key_path.tmp -n client.admin --set-uid=0 " .
907 "--cap mds 'allow' " .
908 "--cap osd 'allow *' " .
909 "--cap mon 'allow *'");
910 run_command
("cp $pve_mon_key_path.tmp /etc/ceph/ceph.client.admin.keyring") if $systemd_managed;
911 run_command
("chown ceph:ceph /etc/ceph/ceph.client.admin.keyring") if $systemd_managed;
912 run_command
("ceph-authtool $pve_mon_key_path.tmp --gen-key -n mon. --cap mon 'allow *'");
913 run_command
("mv $pve_mon_key_path.tmp $pve_mon_key_path");
916 my $ccname = PVE
::CephTools
::get_config
('ccname');
918 my $mondir = "/var/lib/ceph/mon/$ccname-$monid";
919 -d
$mondir && die "monitor filesystem '$mondir' already exist\n";
921 my $monmap = "/tmp/monmap";
926 run_command
("chown ceph:ceph $mondir") if $systemd_managed;
929 my $rados = PVE
::RADOS-
>new(timeout
=> PVE
::CephTools
::get_config
('long_rados_timeout'));
930 my $mapdata = $rados->mon_command({ prefix
=> 'mon getmap', format
=> 'plain' });
931 PVE
::Tools
::file_set_contents
($monmap, $mapdata);
933 run_command
("monmaptool --create --clobber --add $monid $monaddr --print $monmap");
936 run_command
("ceph-mon --mkfs -i $monid --monmap $monmap --keyring $pve_mon_key_path");
937 run_command
("chown ceph:ceph -R $mondir") if $systemd_managed;
942 File
::Path
::remove_tree
($mondir);
946 $cfg->{$monsection} = {
948 'mon addr' => $monaddr,
951 PVE
::CephTools
::write_ceph_config
($cfg);
953 my $create_keys_pid = fork();
954 if (!defined($create_keys_pid)) {
955 die "Could not spawn ceph-create-keys to create bootstrap keys\n";
956 } elsif ($create_keys_pid == 0) {
957 exit PVE
::Tools
::run_command
(['ceph-create-keys', '-i', $monid]);
959 PVE
::CephTools
::ceph_service_cmd
('start', $monsection);
961 if ($systemd_managed) {
962 #to ensure we have the correct startup order.
963 eval { PVE
::Tools
::run_command
(['/bin/systemctl', 'enable', "ceph-mon\@${monid}.service"]); };
964 warn "Enable ceph-mon\@${monid}.service manually"if $@;
966 waitpid($create_keys_pid, 0);
970 return $rpcenv->fork_worker('cephcreatemon', $monsection, $authuser, $worker);
973 __PACKAGE__-
>register_method ({
974 name
=> 'destroymon',
975 path
=> 'mon/{monid}',
977 description
=> "Destroy Ceph monitor.",
981 check
=> ['perm', '/', [ 'Sys.Modify' ]],
984 additionalProperties
=> 0,
986 node
=> get_standard_option
('pve-node'),
988 description
=> 'Monitor ID',
993 returns
=> { type
=> 'string' },
997 my $rpcenv = PVE
::RPCEnvironment
::get
();
999 my $authuser = $rpcenv->get_user();
1001 PVE
::CephTools
::check_ceph_inited
();
1003 my $cfg = PVE
::CephTools
::parse_ceph_config
();
1005 my $monid = $param->{monid
};
1006 my $monsection = "mon.$monid";
1008 my $rados = PVE
::RADOS-
>new();
1009 my $monstat = $rados->mon_command({ prefix
=> 'mon_status' });
1010 my $monlist = $monstat->{monmap
}->{mons
};
1012 die "no such monitor id '$monid'\n"
1013 if !defined($cfg->{$monsection});
1015 my $ccname = PVE
::CephTools
::get_config
('ccname');
1017 my $mondir = "/var/lib/ceph/mon/$ccname-$monid";
1018 -d
$mondir || die "monitor filesystem '$mondir' does not exist on this node\n";
1020 die "can't remove last monitor\n" if scalar(@$monlist) <= 1;
1025 # reopen with longer timeout
1026 $rados = PVE
::RADOS-
>new(timeout
=> PVE
::CephTools
::get_config
('long_rados_timeout'));
1028 $rados->mon_command({ prefix
=> "mon remove", name
=> $monid, format
=> 'plain' });
1030 eval { PVE
::CephTools
::ceph_service_cmd
('stop', $monsection); };
1033 delete $cfg->{$monsection};
1034 PVE
::CephTools
::write_ceph_config
($cfg);
1035 File
::Path
::remove_tree
($mondir);
1038 return $rpcenv->fork_worker('cephdestroymon', $monsection, $authuser, $worker);
1041 __PACKAGE__-
>register_method ({
1045 description
=> "Stop ceph services.",
1049 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1052 additionalProperties
=> 0,
1054 node
=> get_standard_option
('pve-node'),
1056 description
=> 'Ceph service name.',
1059 pattern
=> '(mon|mds|osd)\.[A-Za-z0-9]{1,32}',
1063 returns
=> { type
=> 'string' },
1067 my $rpcenv = PVE
::RPCEnvironment
::get
();
1069 my $authuser = $rpcenv->get_user();
1071 PVE
::CephTools
::check_ceph_inited
();
1073 my $cfg = PVE
::CephTools
::parse_ceph_config
();
1074 scalar(keys %$cfg) || die "no configuration\n";
1080 if ($param->{service
}) {
1081 push @$cmd, $param->{service
};
1084 PVE
::CephTools
::ceph_service_cmd
(@$cmd);
1087 return $rpcenv->fork_worker('srvstop', $param->{service
} || 'ceph',
1088 $authuser, $worker);
1091 __PACKAGE__-
>register_method ({
1095 description
=> "Start ceph services.",
1099 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1102 additionalProperties
=> 0,
1104 node
=> get_standard_option
('pve-node'),
1106 description
=> 'Ceph service name.',
1109 pattern
=> '(mon|mds|osd)\.[A-Za-z0-9]{1,32}',
1113 returns
=> { type
=> 'string' },
1117 my $rpcenv = PVE
::RPCEnvironment
::get
();
1119 my $authuser = $rpcenv->get_user();
1121 PVE
::CephTools
::check_ceph_inited
();
1123 my $cfg = PVE
::CephTools
::parse_ceph_config
();
1124 scalar(keys %$cfg) || die "no configuration\n";
1129 my $cmd = ['start'];
1130 if ($param->{service
}) {
1131 push @$cmd, $param->{service
};
1134 PVE
::CephTools
::ceph_service_cmd
(@$cmd);
1137 return $rpcenv->fork_worker('srvstart', $param->{service
} || 'ceph',
1138 $authuser, $worker);
1141 __PACKAGE__-
>register_method ({
1145 description
=> "Get ceph status.",
1149 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
1152 additionalProperties
=> 0,
1154 node
=> get_standard_option
('pve-node'),
1157 returns
=> { type
=> 'object' },
1161 PVE
::CephTools
::check_ceph_enabled
();
1163 my $rados = PVE
::RADOS-
>new();
1164 return $rados->mon_command({ prefix
=> 'status' });
1167 __PACKAGE__-
>register_method ({
1171 description
=> "List all pools.",
1175 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
1178 additionalProperties
=> 0,
1180 node
=> get_standard_option
('pve-node'),
1188 pool
=> { type
=> 'integer' },
1189 pool_name
=> { type
=> 'string' },
1190 size
=> { type
=> 'integer' },
1193 links
=> [ { rel
=> 'child', href
=> "{pool_name}" } ],
1198 PVE
::CephTools
::check_ceph_inited
();
1200 my $rados = PVE
::RADOS-
>new();
1203 my $res = $rados->mon_command({ prefix
=> 'df' });
1204 my $total = $res->{stats
}->{total_avail_bytes
} || 0;
1206 foreach my $d (@{$res->{pools
}}) {
1207 next if !$d->{stats
};
1208 next if !defined($d->{id
});
1209 $stats->{$d->{id
}} = $d->{stats
};
1212 $res = $rados->mon_command({ prefix
=> 'osd dump' });
1215 foreach my $e (@{$res->{pools
}}) {
1217 foreach my $attr (qw(pool pool_name size min_size pg_num crush_ruleset)) {
1218 $d->{$attr} = $e->{$attr} if defined($e->{$attr});
1220 if (my $s = $stats->{$d->{pool
}}) {
1221 $d->{bytes_used
} = $s->{bytes_used
};
1222 $d->{percent_used
} = ($s->{bytes_used
} / $total)*100
1223 if $s->{max_avail
} && $total;
1232 __PACKAGE__-
>register_method ({
1233 name
=> 'createpool',
1236 description
=> "Create POOL",
1240 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1243 additionalProperties
=> 0,
1245 node
=> get_standard_option
('pve-node'),
1247 description
=> "The name of the pool. It must be unique.",
1251 description
=> 'Number of replicas per object',
1259 description
=> 'Minimum number of replicas per object',
1267 description
=> "Number of placement groups.",
1275 description
=> "The ruleset to use for mapping object placement in the cluster.",
1284 returns
=> { type
=> 'null' },
1288 PVE
::CephTools
::check_ceph_inited
();
1290 my $pve_ckeyring_path = PVE
::CephTools
::get_config
('pve_ckeyring_path');
1292 die "not fully configured - missing '$pve_ckeyring_path'\n"
1293 if ! -f
$pve_ckeyring_path;
1295 my $pg_num = $param->{pg_num
} || 64;
1296 my $size = $param->{size
} || 2;
1297 my $min_size = $param->{min_size
} || 1;
1298 my $ruleset = $param->{crush_ruleset
} || 0;
1299 my $rados = PVE
::RADOS-
>new();
1301 $rados->mon_command({
1302 prefix
=> "osd pool create",
1303 pool
=> $param->{name
},
1304 pg_num
=> int($pg_num),
1305 # this does not work for unknown reason
1306 # properties => ["size=$size", "min_size=$min_size", "crush_ruleset=$ruleset"],
1310 $rados->mon_command({
1311 prefix
=> "osd pool set",
1312 pool
=> $param->{name
},
1318 $rados->mon_command({
1319 prefix
=> "osd pool set",
1320 pool
=> $param->{name
},
1326 if (defined($param->{crush_ruleset
})) {
1327 $rados->mon_command({
1328 prefix
=> "osd pool set",
1329 pool
=> $param->{name
},
1330 var
=> 'crush_ruleset',
1331 val
=> $param->{crush_ruleset
},
1339 __PACKAGE__-
>register_method ({
1340 name
=> 'get_flags',
1343 description
=> "get all set ceph flags",
1347 check
=> ['perm', '/', [ 'Sys.Audit' ]],
1350 additionalProperties
=> 0,
1352 node
=> get_standard_option
('pve-node'),
1355 returns
=> { type
=> 'string' },
1359 PVE
::CephTools
::check_ceph_inited
();
1361 my $pve_ckeyring_path = PVE
::CephTools
::get_config
('pve_ckeyring_path');
1363 die "not fully configured - missing '$pve_ckeyring_path'\n"
1364 if ! -f
$pve_ckeyring_path;
1366 my $rados = PVE
::RADOS-
>new();
1368 my $stat = $rados->mon_command({ prefix
=> 'osd dump' });
1370 return $stat->{flags
} // '';
1373 __PACKAGE__-
>register_method ({
1375 path
=> 'flags/{flag}',
1377 description
=> "Set a ceph flag",
1381 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1384 additionalProperties
=> 0,
1386 node
=> get_standard_option
('pve-node'),
1388 description
=> 'The ceph flag to set/unset',
1390 enum
=> [ 'full', 'pause', 'noup', 'nodown', 'noout', 'noin', 'nobackfill', 'norebalance', 'norecover', 'noscrub', 'nodeep-scrub', 'notieragent'],
1394 returns
=> { type
=> 'null' },
1398 PVE
::CephTools
::check_ceph_inited
();
1400 my $pve_ckeyring_path = PVE
::CephTools
::get_config
('pve_ckeyring_path');
1402 die "not fully configured - missing '$pve_ckeyring_path'\n"
1403 if ! -f
$pve_ckeyring_path;
1405 my $set = $param->{set
} // !$param->{unset
};
1406 my $rados = PVE
::RADOS-
>new();
1408 $rados->mon_command({
1409 prefix
=> "osd set",
1410 key
=> $param->{flag
},
1416 __PACKAGE__-
>register_method ({
1417 name
=> 'unset_flag',
1418 path
=> 'flags/{flag}',
1420 description
=> "Unset a ceph flag",
1424 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1427 additionalProperties
=> 0,
1429 node
=> get_standard_option
('pve-node'),
1431 description
=> 'The ceph flag to set/unset',
1433 enum
=> [ 'full', 'pause', 'noup', 'nodown', 'noout', 'noin', 'nobackfill', 'norebalance', 'norecover', 'noscrub', 'nodeep-scrub', 'notieragent'],
1437 returns
=> { type
=> 'null' },
1441 PVE
::CephTools
::check_ceph_inited
();
1443 my $pve_ckeyring_path = PVE
::CephTools
::get_config
('pve_ckeyring_path');
1445 die "not fully configured - missing '$pve_ckeyring_path'\n"
1446 if ! -f
$pve_ckeyring_path;
1448 my $set = $param->{set
} // !$param->{unset
};
1449 my $rados = PVE
::RADOS-
>new();
1451 $rados->mon_command({
1452 prefix
=> "osd unset",
1453 key
=> $param->{flag
},
1459 __PACKAGE__-
>register_method ({
1460 name
=> 'destroypool',
1461 path
=> 'pools/{name}',
1463 description
=> "Destroy pool",
1467 check
=> ['perm', '/', [ 'Sys.Modify' ]],
1470 additionalProperties
=> 0,
1472 node
=> get_standard_option
('pve-node'),
1474 description
=> "The name of the pool. It must be unique.",
1478 description
=> "If true, destroys pool even if in use",
1485 returns
=> { type
=> 'null' },
1489 PVE
::CephTools
::check_ceph_inited
();
1491 # if not forced, destroy ceph pool only when no
1492 # vm disks are on it anymore
1493 if (!$param->{force
}) {
1494 my $storagecfg = PVE
::Storage
::config
();
1495 foreach my $storageid (keys %{$storagecfg->{ids
}}) {
1496 my $storage = $storagecfg->{ids
}->{$storageid};
1497 next if $storage->{type
} ne 'rbd';
1498 next if $storage->{pool
} ne $param->{name
};
1500 # check if any vm disks are on the pool
1501 my $res = PVE
::Storage
::vdisk_list
($storagecfg, $storageid);
1502 die "ceph pool '$param->{name}' still in use by storage '$storageid'\n"
1503 if @{$res->{$storageid}} != 0;
1507 my $rados = PVE
::RADOS-
>new();
1508 # fixme: '--yes-i-really-really-mean-it'
1509 $rados->mon_command({
1510 prefix
=> "osd pool delete",
1511 pool
=> $param->{name
},
1512 pool2
=> $param->{name
},
1513 sure
=> '--yes-i-really-really-mean-it',
1521 __PACKAGE__-
>register_method ({
1525 description
=> "Get OSD crush map",
1529 check
=> ['perm', '/', [ 'Sys.Audit', 'Datastore.Audit' ], any
=> 1],
1532 additionalProperties
=> 0,
1534 node
=> get_standard_option
('pve-node'),
1537 returns
=> { type
=> 'string' },
1541 PVE
::CephTools
::check_ceph_inited
();
1543 # this produces JSON (difficult to read for the user)
1544 # my $txt = &$run_ceph_cmd_text(['osd', 'crush', 'dump'], quiet => 1);
1548 my $mapfile = "/var/tmp/ceph-crush.map.$$";
1549 my $mapdata = "/var/tmp/ceph-crush.txt.$$";
1551 my $rados = PVE
::RADOS-
>new();
1554 my $bindata = $rados->mon_command({ prefix
=> 'osd getcrushmap', format
=> 'plain' });
1555 PVE
::Tools
::file_set_contents
($mapfile, $bindata);
1556 run_command
(['crushtool', '-d', $mapfile, '-o', $mapdata]);
1557 $txt = PVE
::Tools
::file_get_contents
($mapdata);
1569 __PACKAGE__-
>register_method({
1573 description
=> "Read ceph log",
1576 check
=> ['perm', '/nodes/{node}', [ 'Sys.Syslog' ]],
1580 additionalProperties
=> 0,
1582 node
=> get_standard_option
('pve-node'),
1601 description
=> "Line number",
1605 description
=> "Line text",
1614 my $rpcenv = PVE
::RPCEnvironment
::get
();
1615 my $user = $rpcenv->get_user();
1616 my $node = $param->{node
};
1618 my $logfile = "/var/log/ceph/ceph.log";
1619 my ($count, $lines) = PVE
::Tools
::dump_logfile
($logfile, $param->{start
}, $param->{limit
});
1621 $rpcenv->set_result_attrib('total', $count);