From b198c57cfb616594f1040a6f31293f0585ef17a6 Mon Sep 17 00:00:00 2001 From: Chris Evich Date: Wed, 20 Sep 2017 17:34:01 -0400 Subject: [PATCH] integration-playbook: Idempotent Swapping If the playbook is run multiple times or a host already has swap configured, re-adding swap over the existing file will cause untold problems. Also, it will not persist across reboots unless added to fstab. Avoid this by checking if any swap is active. If not create a unique swapfile and format it. Then enable it to persist across reboots. Signed-off-by: Chris Evich --- contrib/test/integration/swap.yml | 42 +++++++++++++++++++++++++++++ contrib/test/integration/system.yml | 9 ++----- contrib/test/integration/vars.yml | 3 +++ 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 contrib/test/integration/swap.yml diff --git a/contrib/test/integration/swap.yml b/contrib/test/integration/swap.yml new file mode 100644 index 00000000..6777699c --- /dev/null +++ b/contrib/test/integration/swap.yml @@ -0,0 +1,42 @@ +--- + +- name: Obtain current state of swap + command: swapon --noheadings --show=NAME + register: swapon + +- name: Setup swap if none already, to prevent kernel firing off the OOM killer + block: + + - name: A unique swapfile path is generated + command: mktemp --tmpdir=/root swapfile_XXX + register: swapfilepath + + - name: Swap file path is buffered + set_fact: + swapfilepath: '{{ swapfilepath.stdout | trim }}' + + - name: Set swap file permissions + file: + path: "{{ swapfilepath }}" + owner: root + group: root + mode: 0600 + + - name: Swapfile padded to swapfile_size & timed to help debug any performance problems + shell: 'time dd if=/dev/zero of={{ swapfilepath }} bs={{ swapfileGB }}M count=1024' + + - name: Swap file is formatted + command: 'mkswap {{ swapfilepath }}' + + - name: Write swap entry in fstab + mount: + path: none + src: "{{ swapfilepath }}" + fstype: swap + opts: sw + state: present + + - name: Mount swap + command: "swapon -a" + + when: not (swapon.stdout_lines | length) diff --git a/contrib/test/integration/system.yml b/contrib/test/integration/system.yml index da1e8a93..c5a9299f 100644 --- a/contrib/test/integration/system.yml +++ b/contrib/test/integration/system.yml @@ -72,13 +72,8 @@ async: 600 poll: 10 -- name: Setup swap to prevent kernel firing off the OOM killer - shell: | - truncate -s 8G /root/swap && \ - export SWAPDEV=$(losetup --show -f /root/swap | head -1) && \ - mkswap $SWAPDEV && \ - swapon $SWAPDEV && \ - swapon --show +- name: Check / setup swap + include: "swap.yml" - name: ensure directories exist as needed file: diff --git a/contrib/test/integration/vars.yml b/contrib/test/integration/vars.yml index f1e5e2f7..33ecf20c 100644 --- a/contrib/test/integration/vars.yml +++ b/contrib/test/integration/vars.yml @@ -1,5 +1,8 @@ --- +# When swap setup is necessary, make it this size +swapfileGB: 8 + # For results.yml Paths use rsync 'source' conventions artifacts: "/tmp/artifacts" # Base-directory for collection crio_integration_filepath: "{{ artifacts }}/testout.txt"