From a55e199399c77df672207a06f3a11e7d0d6a9007 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Dec 18 2023 11:04:56 +0000 Subject: Add a script to set up swap on first boot --- diff --git a/asahi-setup-swap-firstboot.service b/asahi-setup-swap-firstboot.service new file mode 100644 index 0000000..8426439 --- /dev/null +++ b/asahi-setup-swap-firstboot.service @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: MIT + +[Unit] +Description=Set up swap on first boot +ConditionVirtualization=no +# We set up 8G of swap on 8G and 16G machines +ConditionMemory=<=16G +ConditionPathExists=!/var/swap/swapfile + +[Service] +Type=oneshot +TimeoutSec=0 +RemainAfterExit=yes +ExecStart=/usr/libexec/fedora-asahi-remix-scripts/setup-swap.sh 8G + +[Install] +WantedBy=multi-user.target diff --git a/fedora-asahi-remix-scripts.spec b/fedora-asahi-remix-scripts.spec index d0ab702..127816d 100644 --- a/fedora-asahi-remix-scripts.spec +++ b/fedora-asahi-remix-scripts.spec @@ -1,6 +1,6 @@ Name: fedora-asahi-remix-scripts -Version: 20230906 -Release: 2%{?dist} +Version: 20231218 +Release: 1%{?dist} Summary: Fedora Asahi Remix utility scripts License: MIT @@ -12,6 +12,7 @@ BuildArch: noarch BuildRequires: systemd-rpm-macros Requires: dnf Requires: systemd +Requires: btrfs-progs %description Utility scripts for the Fedora Asahi Remix. @@ -24,23 +25,32 @@ Utility scripts for the Fedora Asahi Remix. %install install -Dpm0644 -t %{buildroot}%{_unitdir} asahi-extras-firstboot.service +install -Dpm0644 -t %{buildroot}%{_unitdir} asahi-setup-swap-firstboot.service install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} install-extras.sh +install -Dpm0755 -t %{buildroot}%{_libexecdir}/%{name} setup-swap.sh %post %systemd_post asahi-extras-firstboot.service +%systemd_post asahi-setup-swap-firstboot.service %preun %systemd_preun asahi-extras-firstboot.service +%systemd_preun asahi-setup-swap-firstboot.service %postun %systemd_postun asahi-extras-firstboot.service +%systemd_postun asahi-setup-swap-firstboot.service %files %license LICENSE %{_unitdir}/asahi-extras-firstboot.service +%{_unitdir}/asahi-setup-swap-firstboot.service %{_libexecdir}/%{name}/ %changelog +* Mon Dec 18 2023 Hector Martin - 20231218-1 +- Add swap setup script + * Wed Sep 06 2023 Davide Cavalca - 20230906-2 - Simplify logic in the install script diff --git a/setup-swap.sh b/setup-swap.sh new file mode 100755 index 0000000..1041841 --- /dev/null +++ b/setup-swap.sh @@ -0,0 +1,25 @@ +#!/usr/bin/sh +set -e + +size=8G +[ -n "$1" ] && size="$1" + +free_bytes="$(($(stat -f --format="%a*%S" .)))" +size_bytes="$(numfmt --from=auto "$size")" +need=$((2 * $size_bytes)) + +if [ "$free_bytes" -lt "$need" ]; then + echo "Not enough free disk space to allocate swap" + exit 1 +fi + +btrfs subvolume create /var/swap +chattr +C /var/swap +fallocate -l "$size" /var/swap/swapfile +chmod 0600 /var/swap/swapfile +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 + +# If we got this far, disable it for good measure (even though it should not run again) +systemctl disable asahi-setup-swap-firstboot.service