From 6b327be31124310a417cb1a318d96f9238711466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 1 Mar 2025 14:08:59 +0100 Subject: [PATCH] Detect and enable swap partitions on live The laptops owned by SOI have 8 GB of RAM, and live systems running on them sometimes run out of memory. To mitigate this, find and enable the swap partition of the installed OS on the internal SSD. --- .../etc/systemd/system/detect-swap.service | 14 ++++++++++++++ .../live/includes.chroot/usr/local/bin/detect-swap | 13 +++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 os/layers/live/includes.chroot/etc/systemd/system/detect-swap.service create mode 100644 os/layers/live/includes.chroot/usr/local/bin/detect-swap diff --git a/os/layers/live/includes.chroot/etc/systemd/system/detect-swap.service b/os/layers/live/includes.chroot/etc/systemd/system/detect-swap.service new file mode 100644 index 0000000..912c92e --- /dev/null +++ b/os/layers/live/includes.chroot/etc/systemd/system/detect-swap.service @@ -0,0 +1,14 @@ +[Unit] +Description=detect and enable swap partitions. +Before=basic.target +After=local-fs.target systemd-tmpfiles-setup.service +DefaultDependencies=no +ConditionKernelCommandLine=boot=live + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/local/bin/detect-swap + +[Install] +WantedBy=basic.target diff --git a/os/layers/live/includes.chroot/usr/local/bin/detect-swap b/os/layers/live/includes.chroot/usr/local/bin/detect-swap new file mode 100644 index 0000000..19a5042 --- /dev/null +++ b/os/layers/live/includes.chroot/usr/local/bin/detect-swap @@ -0,0 +1,13 @@ +#!/usr/bin/python3 + +import subprocess +import json + +SD_GPT_SWAP = '0657fd6d-a4ab-43c4-84e5-0933c84b4f4f' + +lsblk_result = subprocess.run(['lsblk', '--json', '--output=PATH,PARTTYPE'], check=True, stdout=subprocess.PIPE) +lablk_out = json.loads(lsblk_result.stdout) +for block in lablk_out['blockdevices']: + if block['parttype'] == SD_GPT_SWAP: + print('Enabling swap on', block['path']) + subprocess.run(['swapon', block['path']])