I'm using Ubuntu 14.04.5 and the latest version of docker-ce in my project. I want to enable the cgroup swap limit capabilities so I'm looking at the section Your kernel does not support cgroup swap limit capabilities.

Instead of doing that manually, I want to modify the /etc/default/grub file in an Ansible playbook because I already have many playbooks to set up the IT infrastructures.

The simplest solution is to find the GRUB_CMDLINE_LINUX_DEFAULT line and append cgroup_enable=memory swapaccount=1 without checking if they already exist. The playbook task would look like this:

- name: Enable the memory limit.
    path: /etc/default/grub
    backrefs: yes
    regexp: '^(GRUB_CMDLINE_LINUX_DEFAULT=")(.*)(".*)$'
    line: '\1\2 cgroup_enable=memory swapaccount=1\3'
  when: <String `cgroup_enable=memory swapaccount=1` is not present>

(Note the last when line is not valid Ansible syntax. I put it there to show the idea.)

I set a when condition so if these two flags with the desired values are already there, they won't be appended again. However, they are still appended in the cases like the following:

  • Only one is present, e.g.: GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=memory"
  • Both are present but with different values, e.g.: GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=something swapaccount=999" (just some stupid values)
  • Both are present but separated by something else, e.g.: GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=memory somekey=somevalue swapaccount=1"
  • etc..

In summary, the playbook task is not perfect, and I will improve it soon.

My questions are:

  1. If I end up with a line like GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=something swapaccount=999 cgroup_enable=memory swapaccount=1", would the values that come later ("memory" and "1" in this case) override the earlier values ("something" and "999") of the same keys?? I tested and it seems to be working this way.
  2. If it does or does not override the earlier values, where is the official documentation that specifies this behavior? I've tried to find it in the links below but wasn't lucky:

it's not Grub what parses these command lines so you will unlikely find any word about this in Grub manuals. It's the linux kernel, drivers and userspace programs what reads those. So there is no definite answer to your question - generally speaking yes, they do override the earlier values if they are parsed by linux kernel or drivers. But any program in linux can read those command lines via /proc/cmdline file and parse them in any way. Therefore I generally recommend to avoid this to be on the safe side.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.