|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 |
2 | 2 | --- |
| 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 | + |
3 | 10 | - name: SAP Storage Setup - Block handling the swap file |
4 | 11 | # Block parameters |
5 | 12 | vars: |
|
28 | 35 | path: "{{ swap_file.swap_path }}" |
29 | 36 | register: __sap_storage_setup_register_check_swapfile |
30 | 37 |
|
| 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 | + |
31 | 92 | - 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 }}" |
34 | 95 | changed_when: true |
35 | 96 | when: |
36 | 97 | - not __sap_storage_setup_register_check_swapfile.stat.exists |
|
40 | 101 | path: "{{ swap_file.swap_path }}" |
41 | 102 | mode: "0600" |
42 | 103 |
|
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 }}" |
47 | 108 | changed_when: true |
48 | 109 | when: |
49 | 110 | - not __sap_storage_setup_register_check_swapfile.stat.exists |
50 | 111 |
|
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 |
52 | 116 | ansible.posix.mount: |
53 | | - path: swap |
| 117 | + path: none |
54 | 118 | src: "{{ swap_file.swap_path }}" |
55 | 119 | fstype: swap |
56 | | - opts: defaults |
| 120 | + opts: sw |
57 | 121 | state: present |
58 | 122 |
|
| 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 | + |
59 | 148 |
|
60 | 149 | ### End of swapfile block |
61 | 150 |
|
|
73 | 162 | | first |
74 | 163 | -}} |
75 | 164 |
|
| 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 | + |
76 | 169 | # Block conditional |
77 | 170 | when: | |
78 | 171 | sap_storage_setup_definition |
|
83 | 176 |
|
84 | 177 | block: |
85 | 178 |
|
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 |
87 | 181 | ansible.builtin.shell: | |
88 | 182 | set -o pipefail && lsblk | grep SWAP || echo "no active swap" |
89 | 183 | register: __sap_storage_setup_register_check_swap_partition |
90 | 184 | changed_when: false |
91 | 185 |
|
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 |
93 | 201 | ansible.posix.mount: |
94 | 202 | 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 }}" |
96 | 204 | fstype: swap |
97 | 205 | opts: defaults |
98 | 206 | state: present |
99 | 207 |
|
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 }} |
103 | 211 | changed_when: true |
104 | 212 | when: |
105 | 213 | - not ansible_check_mode |
|
0 commit comments