Add sleep-then-hibernate after 30 minutes

Taken from
https://hacktheplanet.be/spells/Linux/4-suspend-then-hibernate-in-omarchy/
This commit is contained in:
David Heinemeier Hansson
2026-01-06 22:49:32 +01:00
parent 68ca8d8083
commit 9c4a0c2abd
4 changed files with 113 additions and 88 deletions

View File

@@ -1,53 +1,59 @@
#!/bin/bash
# Removes hibernation setup: disables swap, removes swapfile, removes fstab entry, and removes resume hook.
# Removes hibernation setup: disables swap, removes swapfile, removes fstab entry,
# removes resume hook, and removes suspend-then-hibernate configuration.
MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
# Check if hibernation is configured
if [ ! -f /etc/mkinitcpio.conf.d/omarchy_resume.conf ] || ! grep -q "^HOOKS+=(resume)$" /etc/mkinitcpio.conf.d/omarchy_resume.conf; then
if [ ! -f "$MKINITCPIO_CONF" ] || ! grep -q "^HOOKS+=(resume)$" "$MKINITCPIO_CONF"; then
echo "Hibernation is not set up"
exit 0
fi
if gum confirm "Remove hibernation setup?"; then
SWAP_SUBVOLUME="/swap"
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"
MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
if ! gum confirm "Remove hibernation setup?"; then
exit 0
fi
# Disable swap if active
if swapon --show | grep -q "$SWAP_FILE"; then
SWAP_SUBVOLUME="/swap"
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"
# Disable swap if active
if swapon --show | grep -q "$SWAP_FILE"; then
echo "Disabling swap on $SWAP_FILE"
sudo swapoff "$SWAP_FILE"
fi
fi
# Remove swapfile
if [ -f "$SWAP_FILE" ]; then
# Remove swapfile
if [ -f "$SWAP_FILE" ]; then
echo "Removing swapfile"
sudo rm "$SWAP_FILE"
fi
fi
# Remove swap subvolume
if sudo btrfs subvolume show "$SWAP_SUBVOLUME" &>/dev/null; then
# Remove swap subvolume
if sudo btrfs subvolume show "$SWAP_SUBVOLUME" &>/dev/null; then
echo "Removing Btrfs subvolume $SWAP_SUBVOLUME"
sudo btrfs subvolume delete "$SWAP_SUBVOLUME"
fi
fi
# Remove fstab entry
if grep -Fq "$SWAP_FILE" /etc/fstab; then
# Remove fstab entry
if grep -Fq "$SWAP_FILE" /etc/fstab; then
echo "Removing swapfile from /etc/fstab"
sudo cp -a /etc/fstab "/etc/fstab.$(date +%Y%m%d%H%M%S).back"
sudo sed -i "\|$SWAP_FILE|d" /etc/fstab
# Also remove the comment line if present
sudo sed -i '/^# Btrfs swapfile for system hibernation$/d' /etc/fstab
fi
# Remove mkinitcpio resume hook
if [ -f "$MKINITCPIO_CONF" ]; then
echo "Removing resume hook from $MKINITCPIO_CONF"
sudo rm "$MKINITCPIO_CONF"
echo "Regenerating initramfs..."
sudo limine-mkinitcpio
fi
echo "Hibernation removed"
fi
# Remove suspend-then-hibernate configuration
echo "Removing suspend-then-hibernate configuration"
sudo rm -f /etc/systemd/logind.conf.d/lid.conf
sudo rm -f /etc/systemd/sleep.conf.d/hibernate.conf
# Remove mkinitcpio resume hook
echo "Removing resume hook"
sudo rm "$MKINITCPIO_CONF"
echo "Regenerating initramfs..."
sudo limine-mkinitcpio
echo "Hibernation removed"

View File

@@ -1,49 +1,63 @@
#!/bin/bash
# Creates a swap file in the btrfs subvolume, adds the swap file to /etc/fstab, and adds a resume hook to mkinitcpio.
# Creates a swap file in the btrfs subvolume, adds the swap file to /etc/fstab,
# adds a resume hook to mkinitcpio, and configures suspend-then-hibernate.
MEM_TOTAL_HUMAN="$(free --human |grep "Mem" |awk '{print $2}')"
if gum confirm "Use $MEM_TOTAL_HUMAN on boot drive to make hibernation available?"; then
SWAP_SUBVOLUME="/swap"
if ! sudo btrfs subvolume show $SWAP_SUBVOLUME &>/dev/null ; then
echo 'Creating Btrfs subvolume'
MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
# Check if hibernation is already configured
if [ -f "$MKINITCPIO_CONF" ] && grep -q "^HOOKS+=(resume)$" "$MKINITCPIO_CONF"; then
echo "Hibernation is already set up"
exit 0
fi
MEM_TOTAL_HUMAN=$(free --human | awk '/Mem/ {print $2}')
if ! gum confirm "Use $MEM_TOTAL_HUMAN on boot drive to make hibernation available?"; then
exit 0
fi
SWAP_SUBVOLUME="/swap"
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"
# Create btrfs subvolume for swap
if ! sudo btrfs subvolume show "$SWAP_SUBVOLUME" &>/dev/null; then
echo "Creating Btrfs subvolume"
sudo btrfs subvolume create "$SWAP_SUBVOLUME"
else
echo "Btrfs subvolume $SWAP_SUBVOLUME already exists"
fi
fi
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"
if ! sudo swaplabel "$SWAP_FILE" &>/dev/null; then
echo 'Creating swapfile in Btrfs subvolume'
MEM_TOTAL_KB="$(grep MemTotal /proc/meminfo | awk '{print $2}')k"
# Create swapfile
if ! sudo swaplabel "$SWAP_FILE" &>/dev/null; then
echo "Creating swapfile in Btrfs subvolume"
MEM_TOTAL_KB="$(awk '/MemTotal/ {print $2}' /proc/meminfo)k"
sudo btrfs filesystem mkswapfile -s "$MEM_TOTAL_KB" "$SWAP_FILE"
else
echo "File $SWAP_FILE already exists"
fi
fi
if ! grep -Fq "$SWAP_FILE" /etc/fstab; then
# Add swapfile to fstab
if ! grep -Fq "$SWAP_FILE" /etc/fstab; then
echo "Adding swapfile to /etc/fstab"
sudo cp -a /etc/fstab "/etc/fstab.$(date +%Y%m%d%H%M%S).back"
printf "\n# Btrfs swapfile for system hibernation\n%s none swap defaults,pri=0 0 0\n" "$SWAP_FILE" | sudo tee -a /etc/fstab >/dev/null
else
echo "Swapfile $SWAP_FILE already exists in /etc/fstab"
fi
echo "Enabling swap on $SWAP_FILE with priority 0"
sudo swapon -p 0 "$SWAP_FILE"
sudo mkdir -p /etc/mkinitcpio.conf.d
MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
HOOKS_LINE='HOOKS+=(resume)'
if [ ! -f "$MKINITCPIO_CONF" ] || ! grep -q "^${HOOKS_LINE}$" "$MKINITCPIO_CONF"; then
echo "Adding resume hooks to $MKINITCPIO_CONF"
echo "$HOOKS_LINE" | sudo tee -a "$MKINITCPIO_CONF" >/dev/null
echo "Regenerating initramfs..."
sudo limine-mkinitcpio
echo "Hibernation enabled"
else
echo "Hibernation hooks already enabled"
fi
fi
# Enable swap
if ! swapon --show | grep -q "$SWAP_FILE"; then
echo "Enabling swap on $SWAP_FILE"
sudo swapon -p 0 "$SWAP_FILE"
fi
# Add resume hook to mkinitcpio
sudo mkdir -p /etc/mkinitcpio.conf.d
echo "Adding resume hook to $MKINITCPIO_CONF"
echo "HOOKS+=(resume)" | sudo tee "$MKINITCPIO_CONF" >/dev/null
# Configure suspend-then-hibernate
echo "Configuring suspend-then-hibernate"
sudo mkdir -p /etc/systemd/logind.conf.d /etc/systemd/sleep.conf.d
sudo cp "$OMARCHY_DIR/defaults/systemd/lid.conf" /etc/systemd/logind.conf.d/
sudo cp "$OMARCHY_DIR/defaults/systemd/hibernate.conf" /etc/systemd/sleep.conf.d/
# Regenerate initramfs
echo "Regenerating initramfs..."
sudo limine-mkinitcpio
echo "Hibernation enabled"

View File

@@ -0,0 +1,3 @@
[Sleep]
HibernateDelaySec=30min
HibernateOnACPower=no

View File

@@ -0,0 +1,2 @@
[Login]
HandleLidSwitch=suspend-then-hibernate