mirror of
https://github.com/basecamp/omarchy.git
synced 2026-02-17 15:25:37 +00:00
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:
@@ -1,17 +1,22 @@
|
||||
#!/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
|
||||
if ! gum confirm "Remove hibernation setup?"; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
SWAP_SUBVOLUME="/swap"
|
||||
SWAP_FILE="$SWAP_SUBVOLUME/swapfile"
|
||||
MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
|
||||
|
||||
# Disable swap if active
|
||||
if swapon --show | grep -q "$SWAP_FILE"; then
|
||||
@@ -36,18 +41,19 @@ if gum confirm "Remove hibernation setup?"; 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 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
|
||||
if [ -f "$MKINITCPIO_CONF" ]; then
|
||||
echo "Removing resume hook from $MKINITCPIO_CONF"
|
||||
echo "Removing resume hook"
|
||||
sudo rm "$MKINITCPIO_CONF"
|
||||
|
||||
echo "Regenerating initramfs..."
|
||||
sudo limine-mkinitcpio
|
||||
fi
|
||||
|
||||
echo "Hibernation removed"
|
||||
fi
|
||||
|
||||
@@ -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.
|
||||
|
||||
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
|
||||
|
||||
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'
|
||||
sudo btrfs subvolume create "$SWAP_SUBVOLUME"
|
||||
else
|
||||
echo "Btrfs subvolume $SWAP_SUBVOLUME already exists"
|
||||
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"
|
||||
sudo btrfs filesystem mkswapfile -s "$MEM_TOTAL_KB" "$SWAP_FILE"
|
||||
else
|
||||
echo "File $SWAP_FILE already exists"
|
||||
|
||||
# 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"
|
||||
fi
|
||||
|
||||
# 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"
|
||||
fi
|
||||
|
||||
# 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"
|
||||
# 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
|
||||
|
||||
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
|
||||
# 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"
|
||||
else
|
||||
echo "Hibernation hooks already enabled"
|
||||
fi
|
||||
fi
|
||||
|
||||
3
defaults/systemd/hibernate.conf
Normal file
3
defaults/systemd/hibernate.conf
Normal file
@@ -0,0 +1,3 @@
|
||||
[Sleep]
|
||||
HibernateDelaySec=30min
|
||||
HibernateOnACPower=no
|
||||
2
defaults/systemd/lid.conf
Normal file
2
defaults/systemd/lid.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
[Login]
|
||||
HandleLidSwitch=suspend-then-hibernate
|
||||
Reference in New Issue
Block a user