| # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| description "mount cgroups on /sys/fs/cgroup" |
| author "chromium-os-dev@chromium.org" |
| |
| start on starting boot-services |
| task |
| |
| # To be compatible with how Android container mounts cpusets, we mount |
| # without prefix (so "cpuset.cpus" turns into "cpus"). |
| # However, some platforms (like LXC containers on moblab) need the prefix. |
| # So let platforms override as needed. |
| env CPUSET_NOPREFIX=true |
| |
| script |
| log() { |
| logger -t "${UPSTART_JOB}" "failed to mount cgroup susbsystem $1" |
| } |
| |
| default_mount_opts="noexec,nosuid,nodev" |
| |
| mount -t tmpfs -o mode=755,${default_mount_opts} none /sys/fs/cgroup |
| |
| for subsys in cpu freezer devices cpuacct cpuset schedtune memory net_cls; do |
| # Verify that the subsystem is available on our kernel. |
| grep -q ${subsys} /proc/cgroups || continue |
| |
| mkdir -p /sys/fs/cgroup/${subsys} |
| mount_opts="${default_mount_opts}" |
| if [ "${subsys}" = "cpuset" ] && [ "${CPUSET_NOPREFIX}" = true ]; then |
| mount_opts="${mount_opts},noprefix" |
| fi |
| |
| # Mount cpuset cgroup controllers. |
| if [ "${subsys}" = "cpuset" ]; then |
| # First try v2 mode, avoids need for cpuset hotplug on kernel >= 4.14. |
| if ! mount -t cgroup cgroup "/sys/fs/cgroup/${subsys}" \ |
| -o "${subsys},${mount_opts},cpuset_v2_mode"; then |
| # If failed try regular cpuset, kernel < 4.14 needs hotplug fixes. |
| mount -t cgroup cgroup "/sys/fs/cgroup/${subsys}" \ |
| -o "${subsys},${mount_opts}" || log "${subsys}" |
| fi |
| else |
| # Mount non-cpuset cgroup controllers. |
| mount -t cgroup cgroup "/sys/fs/cgroup/${subsys}" \ |
| -o "${subsys},${mount_opts}" || log "${subsys}" |
| fi |
| |
| if [ "${subsys}" != "net_cls" ]; then |
| mkdir -p /sys/fs/cgroup/${subsys}/session_manager_containers |
| if id -u android-root 2>/dev/null; then |
| chown android-root:android-root \ |
| /sys/fs/cgroup/${subsys}/session_manager_containers |
| fi |
| if id -u user-containers 2>/dev/null; then |
| mkdir -p /sys/fs/cgroup/${subsys}/user_containers |
| chown user-containers:user-containers \ |
| /sys/fs/cgroup/${subsys}/user_containers |
| if [ "${subsys}" = "freezer" ]; then |
| chown power:power /sys/fs/cgroup/freezer/user_containers/freezer.state |
| fi |
| fi |
| mkdir -p /sys/fs/cgroup/${subsys}/chronos_containers |
| chown chronos:chronos /sys/fs/cgroup/${subsys}/chronos_containers |
| fi |
| |
| # powerd needs to be able to freeze each child of the root freezer, so give |
| # it ownership of the freezer.state file (and only that file). |
| if [ "${subsys}" = "freezer" ]; then |
| chown power:power \ |
| /sys/fs/cgroup/${subsys}/session_manager_containers/freezer.state |
| chown power:power \ |
| /sys/fs/cgroup/${subsys}/chronos_containers/freezer.state |
| fi |
| done |
| |
| # Create and set up Chrome cpusets with default settings |
| # (all cpus, all mems). |
| cpuset_root_dir="/sys/fs/cgroup/cpuset" |
| if [ -d "${cpuset_root_dir}" ]; then |
| cpuset_root_cpus=$(cat "${cpuset_root_dir}/cpus") |
| cpuset_root_mems=$(cat "${cpuset_root_dir}/mems") |
| for cset in chrome chrome/urgent chrome/non-urgent; do |
| cset_dir="${cpuset_root_dir}/${cset}" |
| mkdir -p "${cset_dir}" |
| chown chronos:chronos "${cset_dir}/tasks" |
| echo "${cpuset_root_cpus}" > "${cset_dir}/cpus" |
| echo "${cpuset_root_mems}" > "${cset_dir}/mems" |
| done |
| |
| # Adjust cpusets of the rest of the created containers to |
| # also have all available cpus and all mems. |
| for cset in session_manager_containers chronos_containers user_containers; |
| do |
| cset_dir="${cpuset_root_dir}/${cset}" |
| if [ -d "${cset_dir}" ]; then |
| echo "${cpuset_root_cpus}" > "${cset_dir}/cpus" |
| echo "${cpuset_root_mems}" > "${cset_dir}/mems" |
| fi |
| done |
| fi |
| |
| # Create a cpu cgroup for each VM type. Default cpu.shares is 1024. Limit the |
| # VMs to 7/8ths of that initially. |
| cgroup_dir="/sys/fs/cgroup/cpu/vms" |
| for vm_type in termina plugin arc ; do |
| mkdir -p "${cgroup_dir}/${vm_type}" |
| echo 896 > "${cgroup_dir}/${vm_type}/cpu.shares" |
| for entry in tasks cpu.shares ; do |
| chown root:crosvm "${cgroup_dir}/${vm_type}/${entry}" |
| chmod g+w "${cgroup_dir}/${vm_type}/${entry}" |
| done |
| done |
| |
| schedtune_root_dir="/sys/fs/cgroup/schedtune" |
| if [ -d "${schedtune_root_dir}" ]; then |
| for cset in chrome chrome/urgent; do |
| cset_dir="${schedtune_root_dir}/${cset}" |
| mkdir -p "${cset_dir}" |
| chown chronos:chronos "${cset_dir}/tasks" |
| done |
| fi |
| |
| end script |