diff --git a/roles/memory/handlers/main.yml b/roles/memory/handlers/main.yml new file mode 100644 index 0000000..97d440b --- /dev/null +++ b/roles/memory/handlers/main.yml @@ -0,0 +1,4 @@ +- name: Rebuild initramfs and limine entries + become: true + ansible.builtin.command: limine-mkinitcpio + changed_when: true diff --git a/roles/memory/tasks/main.yml b/roles/memory/tasks/main.yml new file mode 100644 index 0000000..4ed1d19 --- /dev/null +++ b/roles/memory/tasks/main.yml @@ -0,0 +1,92 @@ +# Everything that lands on the kernel cmdline goes in one marked block, leaving +# the existing KERNEL_CMDLINE[default]+= line untouched. +# +# systemd.zram=0 - stop zram-generator creating the zram device +# zswap.* - module params, must be set before first swap-out +# ttm.pages_limit - hard cap on GTT allocation, lifted from the ~50% of RAM +# default. 3959290 pages of 4K = ~15.1 GiB +# ttm.page_pool_size - must match pages_limit or allocations can fail +- name: Add BC-250 kernel parameters + become: true + ansible.builtin.blockinfile: + path: /etc/default/limine + marker: '# {mark} ANSIBLE bc250 memory' + block: | + KERNEL_CMDLINE[default]+=" systemd.zram=0 zswap.enabled=1 zswap.shrinker_enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=20 ttm.pages_limit=3959290 ttm.page_pool_size=3959290" + notify: Rebuild initramfs and limine entries + +# lz4 must be in the initramfs for zswap to use it from early boot. +- name: Add lz4 to the initramfs modules + become: true + ansible.builtin.lineinfile: + path: /etc/mkinitcpio.conf + regexp: '^MODULES=' + line: 'MODULES=(lz4)' + notify: Rebuild initramfs and limine entries + +# CachyOS ships a udev rule that disables zswap when zram comes up. +# An empty file with the same name overrides it. +- name: Neutralise the CachyOS zram udev rule + become: true + ansible.builtin.copy: + content: '' + dest: /etc/udev/rules.d/30-zram.rules + mode: '0644' + +- name: Lower swappiness + become: true + ansible.builtin.copy: + content: vm.swappiness=10 + dest: /etc/sysctl.d/99-bc250.conf + mode: '0644' + register: sysctl_conf + +- name: Apply swappiness now + become: true + ansible.builtin.command: + argv: + - sysctl + - --system + when: sysctl_conf.changed + changed_when: true + +- name: Create the swap subvolume + become: true + ansible.builtin.command: + argv: + - btrfs + - subvolume + - create + - /swap + args: + creates: /swap + +- name: Create the swapfile + become: true + ansible.builtin.command: + argv: + - btrfs + - filesystem + - mkswapfile + - --size + - 8g + - --uuid + - clear + - /swap/swapfile + args: + creates: /swap/swapfile + +- name: Add the swapfile to fstab + become: true + ansible.builtin.lineinfile: + path: /etc/fstab + regexp: '^/swap/swapfile' + line: '/swap/swapfile none swap defaults 0 0' + +- name: Apply pending initramfs changes + ansible.builtin.meta: flush_handlers + +- name: Reboot to apply the new kernel parameters + become: true + ansible.builtin.reboot: + reboot_timeout: 600 diff --git a/site.yml b/site.yml index 6b88807..f512ad8 100644 --- a/site.yml +++ b/site.yml @@ -16,3 +16,5 @@ - name: Install CPU governor role: cpu_governor + - name: Setup zram, zswap and GPU memory allocation + role: memory