3 # Copyright (c) 2019 Wind River Systems, Inc.
5 # SPDX-License-Identifier: Apache-2.0
7 # This script does minimal cgroup setup for kubelet. This creates k8s-infra
8 # cgroup for a minimal set of resource controllers, and configures cpuset
9 # attributes to span all online cpus and nodes. This will do nothing if
10 # the k8s-infra cgroup already exists (i.e., assume already configured).
11 # NOTE: The creation of directories under /sys/fs/cgroup is volatile, and
12 # does not persist reboots. The cpuset.mems and cpuset.cpus is later updated
13 # by puppet kubernetes.pp manifest.
17 PATH=/bin:/usr/bin:/usr/local/bin
19 # Log info message to /var/log/daemon.log
21 logger -p daemon.info "$0($$): $@"
24 # Log error message to /var/log/daemon.log
26 logger -s -p daemon.error "$0($$): ERROR: $@"
29 # Create minimal cgroup directories and configure cpuset attributes
30 # pids should be first in the list, since it appears to get auto deleted
31 function create_cgroup {
36 local CGROUP=/sys/fs/cgroup
37 local CONTROLLERS=("pids" "cpuset" "memory" "cpu,cpuacct" "systemd")
42 # Create the cgroup for required controllers
43 for cnt in ${CONTROLLERS[@]}; do
44 CGDIR=${CGROUP}/${cnt}/${cg_name}
45 if [ -d ${CGDIR} ]; then
46 LOG "Nothing to do, already configured: ${CGDIR}."
49 LOG "Creating: ${CGDIR}"
52 if [ ${RC} -ne 0 ]; then
53 ERROR "Creating: ${CGDIR}, rc=${RC}"
58 # Customize cpuset attributes
59 LOG "Configuring cgroup: ${cg_name}, nodeset: ${cg_nodeset}, cpuset: ${cg_cpuset}"
60 CGDIR=${CGROUP}/cpuset/${cg_name}
61 local CGMEMS=${CGDIR}/cpuset.mems
62 local CGCPUS=${CGDIR}/cpuset.cpus
63 local CGTASKS=${CGDIR}/tasks
65 # Assign cgroup memory nodeset
66 LOG "Assign nodeset ${cg_nodeset} to ${CGMEMS}"
67 /bin/echo ${cg_nodeset} > ${CGMEMS}
69 if [ ${RC} -ne 0 ]; then
70 ERROR "Unable to write to: ${CGMEMS}, rc=${RC}"
75 LOG "Assign cpuset ${cg_cpuset} to ${CGCPUS}"
76 /bin/echo ${cg_cpuset} > ${CGCPUS}
78 if [ ${RC} -ne 0 ]; then
79 ERROR "Assigning: ${cg_cpuset} to ${CGCPUS}, rc=${RC}"
84 chown root:root ${CGMEMS} ${CGCPUS} ${CGTASKS}
86 if [ ${RC} -ne 0 ]; then
87 ERROR "Setting owner for: ${CGMEMS}, ${CGCPUS}, ${CGTASKS}, rc=${RC}"
91 # Set file mode permissions
92 chmod 644 ${CGMEMS} ${CGCPUS} ${CGTASKS}
94 if [ ${RC} -ne 0 ]; then
95 ERROR "Setting mode for: ${CGMEMS}, ${CGCPUS}, ${CGTASKS}, rc=${RC}"
102 if [ $UID -ne 0 ]; then
103 ERROR "Require sudo/root."
107 # Configure default kubepods cpuset to span all online cpus and nodes.
108 ONLINE_NODESET=$(/bin/cat /sys/devices/system/node/online)
109 ONLINE_CPUSET=$(/bin/cat /sys/devices/system/cpu/online)
111 # Configure kubelet cgroup to match cgroupRoot.
112 create_cgroup 'k8s-infra' ${ONLINE_NODESET} ${ONLINE_CPUSET}