From 74c2b197c7d656ef1863272153867e3ce715f13c Mon Sep 17 00:00:00 2001 From: Peter Wilmott Date: Mon, 3 Aug 2015 18:22:07 +0000 Subject: [PATCH] Allow the setting of cgroup limits using env variables; Allocate CPU resources via shares not cores --- README.md | 28 ++++++++++++++++++++++++++++ bocker | 6 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 99c3e63..157eaaf 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,34 @@ Created: img_42150 $ bocker run img_42150 which wget /usr/bin/wget + +$ bocker run img_42150 cat /proc/1/cgroup +... +4:memory:/ps_42152 +3:cpuacct,cpu:/ps_42152 +... + +$ cat /sys/fs/cgroup/cpu/ps_42152/cpu.shares +512 + +$ cat cat /sys/fs/cgroup/memory/ps_42152/memory.limit_in_bytes +512000000 + + +$ BOCKER_CPU_SHARE=1024 \ + BOCKER_MEM_LIMIT=1024 \ + bocker run img_42150 cat /proc/1/cgroup +... +4:memory:/ps_42188 +3:cpuacct,cpu:/ps_42188 + +$ cat /sys/fs/cgroup/cpu/ps_42188/cpu.shares +1024 + +$ cat cat /sys/fs/cgroup/memory/ps_42188/memory.limit_in_bytes +1024000000 +... + ``` ## Functionality: Currently Implemented diff --git a/bocker b/bocker index 523376a..eaadf45 100755 --- a/bocker +++ b/bocker @@ -1,6 +1,6 @@ #!/usr/bin/env bash set -o errexit -o nounset -o pipefail; shopt -s nullglob -btrfs_path='/var/bocker'; cgroups='cpu,cpuacct,cpuset,memory'; +btrfs_path='/var/bocker' && cgroups='cpu,cpuacct,memory'; function bocker_check() { btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1 @@ -76,8 +76,8 @@ function bocker_run() { #HELP Create a container:\nBOCKER run "$btrfs_path/$uuid"/etc/resolv.conf echo "$cmd" > "$btrfs_path/$uuid/$uuid.cmd" cgcreate -g "$cgroups:/$uuid" - cgset -r cpuset.cpus=0-1 "$uuid" && cgset -r cpuset.mems=0 "$uuid" - cgset -r memory.limit_in_bytes=512000000 "$uuid" + : ${BOCKER_CPU_SHARE:=512} && cgset -r cpu.shares="$BOCKER_CPU_SHARE" "$uuid" + : ${BOCKER_MEM_LIMIT:=512} && cgset -r memory.limit_in_bytes="$((BOCKER_MEM_LIMIT * 1000000))" "$uuid" cgexec -g "$cgroups:$uuid" \ ip netns exec netns_"$uuid" \ unshare -fmuip --mount-proc \