From 6817ebab6ab326f00680f02a55221f3a08968b1d Mon Sep 17 00:00:00 2001 From: Manuel Mendez Date: Tue, 18 Aug 2026 11:45:40 -0400 Subject: [PATCH] ansible: Setup swap in stage2 not stage1 Having swap setup in stage1 uses up 10% of the disk while we need as much space as possible (for fetching, building, etc). We are extremely sensitive to cache clean up ordering so that we don't go over, but thats a big pain that can be allievated by moving swap to the end of the run. We run stage2 on beefy machines that don't need swap so lets use the space better. --- ansible/playbook.yml | 5 ++ ansible/tasks/setup-tuned.yml | 37 -------------- ansible/tasks/stage2/setup-swap.yml | 49 +++++++++++++++++++ .../scripts/surrogate-bootstrap-nix.sh | 14 +----- 4 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 ansible/tasks/stage2/setup-swap.yml diff --git a/ansible/playbook.yml b/ansible/playbook.yml index 613c4a99d..5202fe987 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -212,3 +212,8 @@ become: yes shell: | sudo -u ubuntu bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix-collect-garbage -d" + + - name: Setup Swap + when: stage2 and not qemu + become: yes + import_tasks: tasks/stage2/setup-swap.yml diff --git a/ansible/tasks/setup-tuned.yml b/ansible/tasks/setup-tuned.yml index 30f2f611d..1a02becc9 100644 --- a/ansible/tasks/setup-tuned.yml +++ b/ansible/tasks/setup-tuned.yml @@ -185,43 +185,6 @@ value: '10' become: true - - name: 'tuned - Enable zswap if swap is present' # noqa: name[casing] - when: ansible_facts['swaptotal_mb'] > 0 - block: - - name: 'tuned - Decrease the kernel swappiness' # noqa: name[casing] - community.general.ini_file: - create: true - group: 'root' - mode: '0644' - no_extra_spaces: true - option: 'vm.swappiness' - path: '/etc/tuned/profiles/postgresql/tuned.conf' - section: 'sysctl' - state: 'present' - value: '10' - become: true - - - name: 'tuned - Load zstd compressor module' # noqa: name[casing] - community.general.modprobe: - name: 'zstd' - persistent: 'present' - state: 'present' - become: true - - - name: 'tuned - Configure and enable zswap' # noqa: name[casing] - ansible.builtin.shell: - cmd: "echo {{ zswap_item['value'] }} > /sys/module/zswap/parameters/{{ zswap_item['param'] }}" - changed_when: true - loop: - - param: 'compressor' - value: 'zstd' - - param: 'max_pool_percent' - value: '10' - - param: 'enabled' - value: 'Y' - loop_control: - loop_var: 'zswap_item' - - name: 'tuned - Activate the tuned service' # noqa: name[casing] ansible.builtin.systemd_service: daemon_reload: true diff --git a/ansible/tasks/stage2/setup-swap.yml b/ansible/tasks/stage2/setup-swap.yml new file mode 100644 index 000000000..a1823e058 --- /dev/null +++ b/ansible/tasks/stage2/setup-swap.yml @@ -0,0 +1,49 @@ +- name: Allocate and format swapfile + ansible.builtin.shell: | + fallocate -l 1G /swapfile + chmod 600 /swapfile + mkswap /swapfile + args: + creates: /swapfile + +- name: Add swapfile to fstab + ansible.posix.mount: + src: /swapfile + path: none + fstype: swap + opts: sw + dump: "0" + passno: "0" + state: present + +- name: 'tuned - Decrease the kernel swappiness' # noqa: name[casing] + community.general.ini_file: + create: true + group: 'root' + mode: '0644' + no_extra_spaces: true + option: 'vm.swappiness' + path: '/etc/tuned/profiles/postgresql/tuned.conf' + section: 'sysctl' + state: 'present' + value: '10' + +- name: 'tuned - Load zstd compressor module' # noqa: name[casing] + community.general.modprobe: + name: 'zstd' + persistent: 'present' + state: 'present' + +- name: 'tuned - Configure and enable zswap' # noqa: name[casing] + ansible.builtin.shell: + cmd: "echo {{ zswap_item['value'] }} > /sys/module/zswap/parameters/{{ zswap_item['param'] }}" + changed_when: true + loop: + - param: 'compressor' + value: 'zstd' + - param: 'max_pool_percent' + value: '10' + - param: 'enabled' + value: 'Y' + loop_control: + loop_var: 'zswap_item' diff --git a/ebssurrogate/scripts/surrogate-bootstrap-nix.sh b/ebssurrogate/scripts/surrogate-bootstrap-nix.sh index de44160b0..a24072b35 100755 --- a/ebssurrogate/scripts/surrogate-bootstrap-nix.sh +++ b/ebssurrogate/scripts/surrogate-bootstrap-nix.sh @@ -202,24 +202,15 @@ function format_and_mount_rootfs { mount -o defaults,discard /dev/xvdh /mnt/data } -function create_swapfile { - fallocate -l 1G /mnt/swapfile - chmod 600 /mnt/swapfile - mkswap /mnt/swapfile -} - function format_build_partition { mkfs.ext4 -O ^has_journal /dev/xvdc } # Create fstab function create_fstab { - local FMT="%-42s %-11s %-5s %-17s %-5s %s" ROOT_LINE DATA_LINE SWAP_LINE + local FMT="%-42s %-11s %-5s %-17s %-5s %s" ROOT_LINE DATA_LINE ROOT_LINE=$(findmnt -no SOURCE /mnt | xargs blkid -o export | awk -v FMT="$FMT" '/^UUID=/ { printf(FMT, $0, "/", "ext4", "defaults,discard", "0", "1" ) }') DATA_LINE=$(findmnt -no SOURCE /mnt/data | xargs blkid -o export | awk -v FMT="$FMT" '/^UUID=/ { printf(FMT, $0, "/data", "ext4", "defaults,discard", "0", "2" ) }') - # shellcheck disable=SC2059 - SWAP_LINE=$(printf "$FMT" "/swapfile" "none" "swap" "sw" "0" "0") - local EFI_LINE="" if [[ $ARCH == arm64 ]]; then EFI_LINE=$(findmnt -no SOURCE /mnt/boot/efi | xargs blkid -o export | awk -v FMT="$FMT" '/^UUID=/ { printf(FMT, $0, "/boot/efi", "vfat", "umask=0077", "0", "1" ) }') @@ -231,7 +222,6 @@ function create_fstab { echo "$ROOT_LINE" [ -n "$EFI_LINE" ] && echo "$EFI_LINE" echo "$DATA_LINE" - echo "$SWAP_LINE" } >/mnt/etc/fstab } @@ -387,7 +377,6 @@ function clean_system { rm -rf /mnt/root/.vpython* rm -rf /mnt/root/go rm -rf /mnt/usr/share/doc - } # Unmount bind mounts @@ -419,7 +408,6 @@ waitfor_boot_finished install_packages device_partition_mappings format_and_mount_rootfs -create_swapfile format_build_partition setup_chroot_environment execute_playbook