From 942ef6affe7e028b0230ca0b1465cd0651a295aa Mon Sep 17 00:00:00 2001
From: Hector Martin <marcan@fedoraproject.org>
Date: Nov 22 2024 20:45:56 +0000
Subject: [PATCH 1/2] Switch out zram for zswap on swapful systems


---

diff --git a/asahi-enable-zswap.conf b/asahi-enable-zswap.conf
new file mode 100644
index 0000000..8b284e7
--- /dev/null
+++ b/asahi-enable-zswap.conf
@@ -0,0 +1,4 @@
+#Type Path                                            Mode UID  GID  Age  Content
+w     /sys/module/zswap/parameters/max_pool_percent   -    -    -    -    50
+w     /sys/module/zswap/parameters/zpool              -    -    -    -    zsmalloc
+w     /sys/module/zswap/parameters/enabled            -    -    -    -    Y
diff --git a/fedora-asahi-remix-scripts.spec b/fedora-asahi-remix-scripts.spec
index 4415439..2ff586a 100644
--- a/fedora-asahi-remix-scripts.spec
+++ b/fedora-asahi-remix-scripts.spec
@@ -1,5 +1,5 @@
 Name:           fedora-asahi-remix-scripts
-Version:        20240722
+Version:        20241123
 Release:        1%{?dist}
 Summary:        Fedora Asahi Remix utility scripts
 
@@ -34,6 +34,7 @@ install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} install-extras.sh
 install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} setup-swap.sh
 install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} fixup-branding-packages.sh
 install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} check-network-online.sh
+install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} asahi-enable-zswap.conf
 
 %post
 %systemd_post asahi-extras-firstboot.service
@@ -44,6 +45,14 @@ install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} check-network-online.sh
 systemctl enable asahi-branding-fixup.timer
 %endif
 
+# Switch zram to zswap for systems installed prior to the updated script
+if [ $1 -gt 1 ] && grep -q '^/var/swap/swapfile' /etc/fstab &&
+    [ ! -e /etc/systemd/zram-generator.conf ] &&
+    [ ! -e /etc/tmpfiles.d/asahi-enable-zswap.conf ]; then
+    echo "# disable zram-generator-defaults config in favor of zswap on Apple systems with swap" >/etc/systemd/zram-generator.conf
+    cp %{_libexecdir}/%{name}/asahi-enable-zswap.conf /etc/tmpfiles.d/
+fi
+
 %preun
 %systemd_preun asahi-extras-firstboot.service
 %systemd_preun asahi-setup-swap-firstboot.service
@@ -64,6 +73,9 @@ systemctl enable asahi-branding-fixup.timer
 %{_libexecdir}/%{name}/
 
 %changelog
+* Fri Nov 22 2024 Hector Martin <marcan@fedoraproject.org> - 20241123-1
+- Switch out zram for zswap on swapful systems
+
 * Mon Jul 22 2024 Janne Grunau <janne-fdr@jannau.net> - 20240722-1
 - Install hwdb file to override auto suspend on GL9755 SD card reader
 
diff --git a/setup-swap.sh b/setup-swap.sh
index f99d6c0..d3c9a0e 100755
--- a/setup-swap.sh
+++ b/setup-swap.sh
@@ -1,7 +1,9 @@
 #!/usr/bin/sh
 set -e
 
-size=8G
+cd "$(dirname "$0")"
+
+size=12G
 [ -n "$1" ] && size="$1"
 
 free_bytes="$(($(stat -f --format="%a*%S" .)))"
@@ -26,5 +28,13 @@ mkswap /var/swap/swapfile
 swapon /var/swap/swapfile
 grep -q /var/swap/swapfile /etc/fstab || echo '/var/swap/swapfile swap swap sw 0 0' >> /etc/fstab
 
+# Disable zram (allow this to fail for the running system)
+echo "# disable zram-generator-defaults config in favor of zswap on Apple systems with swap" >/etc/systemd/zram-generator.conf
+systemctl stop systemd-zram-setup@zram0.service || true
+
+# Enable zswap
+cp asahi-enable-zswap.conf /etc/tmpfiles.d/
+systemd-tmpfiles --create /etc/tmpfiles.d/asahi-enable-zswap.conf
+
 # If we got this far, disable it for good measure (even though it should not run again)
 systemctl disable asahi-setup-swap-firstboot.service

From bf4785b2a59a2197ecbd20d254415fcafdf71376 Mon Sep 17 00:00:00 2001
From: Hector Martin <marcan@fedoraproject.org>
Date: Nov 22 2024 20:45:59 +0000
Subject: [PATCH 2/2] Support re-creating swapfile in setup-swap.sh


To make it easier for users to change the swap size.

---

diff --git a/setup-swap.sh b/setup-swap.sh
index d3c9a0e..ec5d289 100755
--- a/setup-swap.sh
+++ b/setup-swap.sh
@@ -4,6 +4,21 @@ set -e
 cd "$(dirname "$0")"
 
 size=12G
+
+if [ "$(whoami)" != "root" ]; then
+    echo "Script must be run as root" >&2
+    exit 1
+fi
+
+if [ "$1" == "--recreate" ]; then
+    shift
+    swapoff /var/swap/swapfile || true
+    rm /var/swap/swapfile
+elif [ -e /var/swap/swapfile ]; then
+    echo "/var/swap/swapfile already exists, use --recreate to recreate" >&2
+    exit 1
+fi
+
 [ -n "$1" ] && size="$1"
 
 free_bytes="$(($(stat -f --format="%a*%S" .)))"
@@ -15,7 +30,7 @@ if [ "$free_bytes" -lt "$need" ]; then
     exit 1
 fi
 
-btrfs subvolume create /var/swap
+[ ! -e /var/swap ] && btrfs subvolume create /var/swap
 chattr +C /var/swap
 fallocate -l "$size" /var/swap/swapfile
 chmod 0600 /var/swap/swapfile

