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,53 +1,59 @@
|
|||||||
#!/bin/bash
|
#!/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
|
# 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"
|
echo "Hibernation is not set up"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if gum confirm "Remove hibernation setup?"; then
|
if ! gum confirm "Remove hibernation setup?"; then
|
||||||
SWAP_SUBVOLUME="/swap"
|
exit 0
|
||||||
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
|
|
||||||
echo "Disabling swap on $SWAP_FILE"
|
|
||||||
sudo swapoff "$SWAP_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Remove swapfile
|
|
||||||
if [ -f "$SWAP_FILE" ]; then
|
|
||||||
echo "Removing swapfile"
|
|
||||||
sudo rm "$SWAP_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# 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
|
fi
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
# Remove swapfile
|
||||||
|
if [ -f "$SWAP_FILE" ]; then
|
||||||
|
echo "Removing swapfile"
|
||||||
|
sudo rm "$SWAP_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
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
|
||||||
|
echo "Removing resume hook"
|
||||||
|
sudo rm "$MKINITCPIO_CONF"
|
||||||
|
|
||||||
|
echo "Regenerating initramfs..."
|
||||||
|
sudo limine-mkinitcpio
|
||||||
|
|
||||||
|
echo "Hibernation removed"
|
||||||
|
|||||||
@@ -1,49 +1,63 @@
|
|||||||
#!/bin/bash
|
#!/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}')"
|
MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
|
||||||
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"
|
# Check if hibernation is already configured
|
||||||
if ! sudo swaplabel "$SWAP_FILE" &>/dev/null; then
|
if [ -f "$MKINITCPIO_CONF" ] && grep -q "^HOOKS+=(resume)$" "$MKINITCPIO_CONF"; then
|
||||||
echo 'Creating swapfile in Btrfs subvolume'
|
echo "Hibernation is already set up"
|
||||||
MEM_TOTAL_KB="$(grep MemTotal /proc/meminfo | awk '{print $2}')k"
|
exit 0
|
||||||
sudo btrfs filesystem mkswapfile -s "$MEM_TOTAL_KB" "$SWAP_FILE"
|
|
||||||
else
|
|
||||||
echo "File $SWAP_FILE already exists"
|
|
||||||
fi
|
|
||||||
|
|
||||||
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
|
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"
|
||||||
|
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
|
||||||
|
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"
|
||||||
|
|||||||
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