Skip to content

Commit df64a31

Browse files
authored
Merge pull request sap-linuxlab#1141 from marcelmamula/storage-16
sap_storage_setup/SUSE: Add btrfs support for SLES 16
2 parents de748dc + 4d6e616 commit df64a31

1 file changed

Lines changed: 123 additions & 15 deletions

File tree

roles/sap_storage_setup/tasks/generic_tasks/configure_swap.yml

Lines changed: 123 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# SPDX-License-Identifier: Apache-2.0
22
---
3+
4+
# Starting with SLES 16, the default filesystem is 'btrfs' which requires special handling when creating a swap file.
5+
# Swapon on btrfs filesystems will fail with "swapon: /swapfile: swapon failed: Invalid argument"
6+
# This is solved by setting the NOCOW attribute on the swap file with 'chattr +C /swapfile'.
7+
# Fallocate command does not always create empty file, resulting in 'chattr +C' silently failing.
8+
# Therefore, we first create an empty file with 'touch'.
9+
310
- name: SAP Storage Setup - Block handling the swap file
411
# Block parameters
512
vars:
@@ -28,9 +35,63 @@
2835
path: "{{ swap_file.swap_path }}"
2936
register: __sap_storage_setup_register_check_swapfile
3037

38+
- name: SAP Storage Setup - (swap file) Get all active swap files and partitions
39+
ansible.builtin.command:
40+
cmd: "swapon --show=NAME --noheadings"
41+
register: __sap_storage_setup_register_swapfile_active
42+
changed_when: false
43+
44+
45+
- name: SAP Storage Setup - (swap file) Create empty file for swap with touch
46+
ansible.builtin.file:
47+
path: "{{ swap_file.swap_path }}"
48+
state: touch
49+
mode: '0600'
50+
when:
51+
- not __sap_storage_setup_register_check_swapfile.stat.exists
52+
53+
- name: SAP Storage Setup - (swap file) Get the actual mount point and filesystem type
54+
# This command returns just the fstype of the mount containing the path
55+
ansible.builtin.shell:
56+
cmd: "set -o pipefail && df --output=fstype {{ swap_file.swap_path | dirname }} | tail -1"
57+
register: __sap_storage_setup_register_swapfile_fstype
58+
changed_when: false
59+
60+
61+
- name: Block for btrfs handling
62+
when: __sap_storage_setup_register_swapfile_fstype.stdout | trim == 'btrfs'
63+
block:
64+
- name: SAP Storage Setup - (swap file) - btrfs - Check file attributes
65+
ansible.builtin.command:
66+
cmd: "lsattr {{ swap_file.swap_path }}"
67+
register: __sap_storage_setup_register_swapfile_attributes
68+
failed_when: false
69+
changed_when: false
70+
71+
- name: SAP Storage Setup - (swap file) - btrfs - Fail when swap file exists without NOCOW attribute
72+
ansible.builtin.fail:
73+
msg: |
74+
FAIL: The swap file {{ swap_file.swap_path }} exists on a btrfs filesystem but does not have the NOCOW attribute set.
75+
This will lead to failure when activating the swap file.
76+
Please remove the existing swap file and rerun this Ansible Role to re-create swap file with correct attributes.
77+
when:
78+
- __sap_storage_setup_register_check_swapfile.stat.exists
79+
- __sap_storage_setup_register_swapfile_attributes.rc is defined
80+
- __sap_storage_setup_register_swapfile_attributes.rc == 0
81+
- "'C' not in __sap_storage_setup_register_swapfile_attributes.stdout"
82+
83+
- name: SAP Storage Setup - (swap file) - btrfs - Create 0-byte file and set NOCOW
84+
ansible.builtin.file:
85+
path: "{{ swap_file.swap_path }}"
86+
attributes: '+C'
87+
when:
88+
- not __sap_storage_setup_register_check_swapfile.stat.exists
89+
- "'C' not in __sap_storage_setup_register_swapfile_attributes.stdout"
90+
91+
3192
- name: SAP Storage Setup - (swap file) Allocate space
32-
ansible.builtin.shell: |
33-
fallocate -l {{ swap_file.disk_size | int * 1024 }}MB {{ swap_file.swap_path }}
93+
ansible.builtin.command:
94+
cmd: "fallocate -l {{ swap_file.disk_size | int * 1024 }}MB {{ swap_file.swap_path }}"
3495
changed_when: true
3596
when:
3697
- not __sap_storage_setup_register_check_swapfile.stat.exists
@@ -40,22 +101,50 @@
40101
path: "{{ swap_file.swap_path }}"
41102
mode: "0600"
42103

43-
- name: SAP Storage Setup - (swap file) Create and activate swap
44-
ansible.builtin.shell: |
45-
mkswap {{ swap_file.swap_path }}
46-
swapon {{ swap_file.swap_path }}
104+
# We will format swap file only when newly created to avoid touching existing.
105+
- name: SAP Storage Setup - (swap file) Format the new swap file
106+
ansible.builtin.command:
107+
cmd: "mkswap {{ swap_file.swap_path }}"
47108
changed_when: true
48109
when:
49110
- not __sap_storage_setup_register_check_swapfile.stat.exists
50111

51-
- name: SAP Storage Setup - (swap file) Add fstab entry
112+
# Fstab will be always updated even if already existing.
113+
# 'path' must be 'none' or 'swap' for swap devices.
114+
# 'opts' should contain 'sw' for swap devices.
115+
- name: SAP Storage Setup - (swap file) Maintain entry in /etc/fstab
52116
ansible.posix.mount:
53-
path: swap
117+
path: none
54118
src: "{{ swap_file.swap_path }}"
55119
fstype: swap
56-
opts: defaults
120+
opts: sw
57121
state: present
58122

123+
# Activate swap file if it is not already active.
124+
- name: SAP Storage Setup - (swap file) Activate swap file
125+
ansible.builtin.command:
126+
cmd: "swapon {{ swap_file.swap_path }}"
127+
register: __sap_storage_setup_register_swapfile_activate
128+
changed_when: true
129+
failed_when: false
130+
when:
131+
- swap_file.swap_path not in __sap_storage_setup_register_swapfile_active.stdout_lines
132+
133+
- name: SAP Storage Setup - (swap file) Fail if swap file could not be activated
134+
ansible.builtin.fail:
135+
msg: |
136+
FAIL: The swap file {{ swap_file.swap_path }} could not be activated.
137+
{% if __sap_storage_setup_register_check_swapfile.stat.exists %}
138+
The existing swap file might be corrupted or was not created with correct attributes.
139+
{% else %}
140+
The newly created swap file could not be activated. Please check the system logs for details.
141+
{% endif %}
142+
143+
Command output: {{ __sap_storage_setup_register_swapfile_activate.stdout }}
144+
when:
145+
- __sap_storage_setup_register_swapfile_activate.rc is defined
146+
- __sap_storage_setup_register_swapfile_activate.rc != 0
147+
59148

60149
### End of swapfile block
61150

@@ -73,6 +162,10 @@
73162
| first
74163
-}}
75164
165+
# Define the full path to the swap partition for easier reference in subsequent tasks.
166+
swap_partition_path:
167+
"/dev/{{ swap_volume.lvm_vg_name | default('vg_swap') }}/{{ swap_volume.lvm_lv_name | default('lv_swap') }}"
168+
76169
# Block conditional
77170
when: |
78171
sap_storage_setup_definition
@@ -83,23 +176,38 @@
83176
84177
block:
85178

86-
- name: SAP Storage Setup - Check if swap partition exists
179+
# TODO: This can be replaced with "swapon --show=NAME --noheadings" if applicable.
180+
- name: SAP Storage Setup - (swap partition) Get all existing swap partitions
87181
ansible.builtin.shell: |
88182
set -o pipefail && lsblk | grep SWAP || echo "no active swap"
89183
register: __sap_storage_setup_register_check_swap_partition
90184
changed_when: false
91185

92-
- name: SAP Storage Setup - Add fstab entry for swap
186+
- name: SAP Storage Setup - (swap partition) Check if LV device exists
187+
ansible.builtin.stat:
188+
path: "{{ swap_partition_path }}"
189+
register: __sap_storage_setup_register_check_swap_partition_stat
190+
191+
- name: SAP Storage Setup - (swap partition) Fail if LV device does not exist
192+
ansible.builtin.fail:
193+
msg: |
194+
FAIL: The logical volume device '{{ swap_partition_path }}' for the swap partition was not found.
195+
This indicates that the LVM creation tasks may have failed. Please check the logs for earlier errors.
196+
when:
197+
- not __sap_storage_setup_register_check_swap_partition_stat.stat.exists
198+
- not ansible_check_mode
199+
200+
- name: SAP Storage Setup - (swap partition) Maintain entry in /etc/fstab
93201
ansible.posix.mount:
94202
path: swap
95-
src: "/dev/{{ swap_volume.lvm_vg_name | default('vg_swap') }}/{{ swap_volume.lvm_lv_name | default('lv_swap') }}"
203+
src: "{{ swap_partition_path }}"
96204
fstype: swap
97205
opts: defaults
98206
state: present
99207

100-
- name: SAP Storage Setup - Enable swap
101-
ansible.builtin.shell: |
102-
swapon /dev/{{ swap_volume.lvm_vg_name | default('vg_swap') }}/{{ swap_volume.lvm_lv_name | default('lv_swap') }}
208+
- name: SAP Storage Setup - (swap partition) Enable swap
209+
ansible.builtin.command: |
210+
swapon {{ swap_partition_path }}
103211
changed_when: true
104212
when:
105213
- not ansible_check_mode

0 commit comments

Comments
 (0)