Skip to content

Instantly share code, notes, and snippets.

@xiaods
Forked from rfc1459/README.md
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaods/abb39b57a27256180d84 to your computer and use it in GitHub Desktop.
Save xiaods/abb39b57a27256180d84 to your computer and use it in GitHub Desktop.

Fix memory cgroup configuration for lxc-docker on Ubuntu 14.04

Step 1: enable hierarchical memory management

Drop memory-cg.conf into /etc/init. The upstart job will ensure that the memory cgroup is configured with hierarchical stats tracking before systemd-logind and/or docker start creating cgroups.

(Yes, it's mostly a crude hack with an obscure way of syncing with cgroup-lite. That can't be helped, though)

Step 2 (optional): enable swap accounting

Edit /etc/default/grub and append to GRUB_CMDLINE_LINUX_DEFAULT swapaccount=1.

You don't want to add it to GRUB_CMDLINE_LINUX, since it would be applied even while booting recovery mode kernels.

Run update-grub2 afterwards to apply the new configuration.

Step 3: reboot

While it might sound otheros-like, you really have to reboot to pick up the new cgroup configuration, even if you haven't modified the kernel command line. The rationale behind this is that you'd have to stop nearly every service to safely get rid of existing cgroups, then start the new upstart job and restart everything else.

Rebooting is just easier - and it's required if you're enabling swap accounting.

description "Fix memory cgroup configuration for lxc-docker"
author "Matteo Panella <morpheus@level28.org>"
task
# BEWARE: since cgroup handling in Ubuntu is a royal mess, you HAVE to insert
# an explicit dependency on every single job that could create cgroups after
# it's started
start on (starting systemd-logind or starting docker)
script
MEM_USE_HIER="/sys/fs/cgroup/memory/memory.use_hierarchy"
# TL;DR: Explicit sync point with cgroup-lite
status cgroup-lite 2>/dev/null >/dev/null && start wait-for-state WAITER=memory-cg WAIT_FOR=cgroup-lite WAIT_STATE=running 2>/dev/null >/dev/null || :
# Check for the relevant configuration file, abort if it doesn't exist
test -f $MEM_USE_HIER || exit 0
# Check if use_hierarchy is already enabled
test "`cat $MEM_USE_HIER`" = "1" && exit 0 || :
# Enable use_hierarchy - complain loud if we can't
echo 1 > $MEM_USE_HIER || {
logger -is -t "$UPSTART_JOB" "Unable to set memory.use_hierarchy to 1, something has already created a child memory cg"
}
exit 0
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment