diff --git a/bin/omarchy-hibernation-available b/bin/omarchy-hibernation-available new file mode 100755 index 00000000..575a8391 --- /dev/null +++ b/bin/omarchy-hibernation-available @@ -0,0 +1,18 @@ +#!/bin/bash + +# Check if hibernation is supported +if [[ ! -f /sys/power/image_size ]]; then + exit 1 +fi + +# Sum all swap sizes (excluding zram) +SWAPSIZE_KB=$(awk '!/Filename|zram/ {sum += $3} END {print sum+0}' /proc/swaps) +SWAPSIZE=$(( 1024 * ${SWAPSIZE_KB:-0} )) + +HIBERNATION_IMAGE_SIZE=$(cat /sys/power/image_size) + +if [[ "$SWAPSIZE" -gt "$HIBERNATION_IMAGE_SIZE" ]] && [[ -f /etc/mkinitcpio.conf.d/omarchy_resume.conf ]]; then + exit 0 +else + exit 1 +fi diff --git a/bin/omarchy-hibernation-remove b/bin/omarchy-hibernation-remove new file mode 100755 index 00000000..cadb4283 --- /dev/null +++ b/bin/omarchy-hibernation-remove @@ -0,0 +1,53 @@ +#!/bin/bash + +# Removes hibernation setup: disables swap, removes swapfile, removes fstab entry, and removes resume hook. + +# 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 + 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" +fi diff --git a/bin/omarchy-hibernation-setup b/bin/omarchy-hibernation-setup new file mode 100755 index 00000000..68f405d5 --- /dev/null +++ b/bin/omarchy-hibernation-setup @@ -0,0 +1,49 @@ +#!/bin/bash + +# Creates a swap file in the btrfs subvolume, adds the swap file to /etc/fstab, and adds a resume hook to mkinitcpio. + +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" + 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 diff --git a/bin/omarchy-menu b/bin/omarchy-menu index 2d689c30..459b0f73 100755 --- a/bin/omarchy-menu +++ b/bin/omarchy-menu @@ -172,7 +172,7 @@ show_setup_menu() { local options=" Audio\n Wifi\n󰂯 Bluetooth\n󱐋 Power Profile\n󰍹 Monitors" [ -f ~/.config/hypr/bindings.conf ] && options="$options\n Keybindings" [ -f ~/.config/hypr/input.conf ] && options="$options\n Input" - options="$options\n Defaults\n󰱔 DNS\n Security\n Config\n Tweaks" + options="$options\n󰱔 DNS\n Security\n Config\n System" case $(menu "Setup" "$options") in *Audio*) omarchy-launch-audio ;; @@ -182,11 +182,10 @@ show_setup_menu() { *Monitors*) open_in_editor ~/.config/hypr/monitors.conf ;; *Keybindings*) open_in_editor ~/.config/hypr/bindings.conf ;; *Input*) open_in_editor ~/.config/hypr/input.conf ;; - *Defaults*) open_in_editor ~/.config/uwsm/default ;; *DNS*) present_terminal omarchy-setup-dns ;; *Security*) show_setup_security_menu ;; *Config*) show_setup_config_menu ;; - *Tweaks*) show_setup_tweaks_menu ;; + *System*) show_setup_system_menu ;; *) show_main_menu ;; esac } @@ -210,7 +209,8 @@ show_setup_security_menu() { } show_setup_config_menu() { - case $(menu "Setup" " Hyprland\n Hypridle\n Hyprlock\n Hyprsunset\n Swayosd\n󰌧 Walker\n󰍜 Waybar\n󰞅 XCompose") in + case $(menu "Setup" " Defaults\n Hyprland\n Hypridle\n Hyprlock\n Hyprsunset\n Swayosd\n󰌧 Walker\n󰍜 Waybar\n󰞅 XCompose") in + *Defaults*) open_in_editor ~/.config/uwsm/default ;; *Hyprland*) open_in_editor ~/.config/hypr/hyprland.conf ;; *Hypridle*) open_in_editor ~/.config/hypr/hypridle.conf && omarchy-restart-hypridle ;; *Hyprlock*) open_in_editor ~/.config/hypr/hyprlock.conf ;; @@ -219,21 +219,30 @@ show_setup_config_menu() { *Walker*) open_in_editor ~/.config/walker/config.toml && omarchy-restart-walker ;; *Waybar*) open_in_editor ~/.config/waybar/config.jsonc && omarchy-restart-waybar ;; *XCompose*) open_in_editor ~/.XCompose && omarchy-restart-xcompose ;; - *) show_main_menu ;; + *) show_setup_menu ;; esac } -show_setup_tweaks_menu() { +show_setup_system_menu() { local options="" + if [ -f ~/.local/state/omarchy/toggles/suspend-on ]; then - options="$options󰒲 Hide System Menu Suspend" + options="$options󰒲 Disable Suspend" else - options="$options󰒲 Reveal System Menu Suspend" + options="$options󰒲 Enable Suspend" fi - case $(menu "Tweaks" "$options") in + if omarchy-hibernation-available; then + options="$options\n󰤁 Disable Hibernate" + else + options="$options\n󰤁 Enable Hibernate" + fi + + case $(menu "System" "$options") in *Suspend*) omarchy-toggle-suspend ;; - *) show_main_menu ;; + *"Enable Hibernate"*) present_terminal omarchy-hibernation-setup ;; + *"Disable Hibernate"*) present_terminal omarchy-hibernation-remove ;; + *) show_setup_menu ;; esac } @@ -510,12 +519,14 @@ show_update_password_menu() { show_system_menu() { local options=" Lock\n󱄄 Screensaver" [ -f ~/.local/state/omarchy/toggles/suspend-on ] && options="$options\n󰒲 Suspend" + omarchy-hibernation-available && options="$options\n󰤁 Hibernate" options="$options\n󰜉 Restart\n󰐥 Shutdown" case $(menu "System" "$options") in *Lock*) omarchy-lock-screen ;; *Screensaver*) omarchy-launch-screensaver force ;; *Suspend*) systemctl suspend ;; + *Hibernate*) systemctl hibernate ;; *Restart*) omarchy-cmd-reboot ;; *Shutdown*) omarchy-cmd-shutdown ;; *) back_to show_main_menu ;;