diff --git a/bin/omarchy-hibernation-remove b/bin/omarchy-hibernation-remove index cadb4283..aad216f9 100755 --- a/bin/omarchy-hibernation-remove +++ b/bin/omarchy-hibernation-remove @@ -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" - - # 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" +if ! gum confirm "Remove hibernation setup?"; then + exit 0 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" diff --git a/bin/omarchy-hibernation-setup b/bin/omarchy-hibernation-setup index 68f405d5..0e5afb9c 100755 --- a/bin/omarchy-hibernation-setup +++ b/bin/omarchy-hibernation-setup @@ -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' - sudo btrfs subvolume create "$SWAP_SUBVOLUME" - else - echo "Btrfs subvolume $SWAP_SUBVOLUME already exists" - fi +MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf" - 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" - 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 +# 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" +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" diff --git a/defaults/systemd/hibernate.conf b/defaults/systemd/hibernate.conf new file mode 100644 index 00000000..b146ae9b --- /dev/null +++ b/defaults/systemd/hibernate.conf @@ -0,0 +1,3 @@ +[Sleep] +HibernateDelaySec=30min +HibernateOnACPower=no diff --git a/defaults/systemd/lid.conf b/defaults/systemd/lid.conf new file mode 100644 index 00000000..c6ecbcb7 --- /dev/null +++ b/defaults/systemd/lid.conf @@ -0,0 +1,2 @@ +[Login] +HandleLidSwitch=suspend-then-hibernate