Created
August 2, 2024 20:26
-
-
Save SmugZombie/acb833fb11f35421d9db65dc13af39b8 to your computer and use it in GitHub Desktop.
Automatically enables hibernation on Ubuntu 24.04 laptops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script configures suspend mode and prepares for hibernation on Ubuntu 24.04 | |
# It ensures swap space is sufficient and configures GRUB for proper resume settings | |
# Check if the script is run as root or with sudo | |
if [ "$(id -u)" -ne 0 ]; then | |
echo "This script must be run as root or with sudo." | |
exit 1 | |
fi | |
# Function to check and adjust swap size | |
adjust_swap() { | |
local RAM_SIZE=$(free -m | awk '/^Mem:/{print $2}') | |
local SWAP_SIZE=$(free -m | awk '/^Swap:/{print $2}') | |
local SWAP_FILE="/swapfile" | |
echo "RAM size: ${RAM_SIZE}MB" | |
echo "Current swap size: ${SWAP_SIZE}MB" | |
if [ "$SWAP_SIZE" -lt "$RAM_SIZE" ]; then | |
echo "Current swap space is less than RAM size. Adjusting swap space..." | |
# Disable and remove current swap file if it exists | |
swapoff -a | |
[ -f $SWAP_FILE ] && rm -f $SWAP_FILE | |
# Create a new swap file with size equal to RAM size | |
fallocate -l ${RAM_SIZE}M $SWAP_FILE | |
chmod 600 $SWAP_FILE | |
mkswap $SWAP_FILE | |
swapon $SWAP_FILE | |
# Add swap file entry to /etc/fstab if not already present | |
if ! grep -q "$SWAP_FILE" /etc/fstab; then | |
echo "$SWAP_FILE none swap sw 0 0" | tee -a /etc/fstab | |
fi | |
echo "Swap space adjusted to ${RAM_SIZE}MB." | |
else | |
echo "Current swap space is sufficient." | |
fi | |
} | |
# Function to configure GRUB for hibernation | |
configure_grub_for_hibernation() { | |
local SWAP_UUID=$(blkid -o value -s UUID $(findmnt -no SOURCE --target /swapfile)) | |
local RESUME_FILE=$(readlink -f /swapfile) | |
local RESUME_OFFSET=$(filefrag -v $RESUME_FILE | awk '/^ *0:/{print $4}' | sed 's/\..*//') | |
echo "Configuring GRUB for hibernation with resume UUID $SWAP_UUID and offset $RESUME_OFFSET" | |
# Add or update GRUB configuration | |
sed -i.bak -r "s|^(GRUB_CMDLINE_LINUX_DEFAULT=).*|\1\"quiet splash resume=UUID=$SWAP_UUID resume_offset=$RESUME_OFFSET\"|" /etc/default/grub | |
# Update GRUB | |
update-grub | |
echo "GRUB configuration updated. Please verify the settings and reboot if necessary." | |
} | |
# Update and upgrade the system | |
apt update && apt upgrade -y | |
# Install necessary packages (if not already installed) | |
apt install -y pm-utils | |
# Enable suspend on lid close (for laptops) | |
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action 'suspend' | |
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action 'suspend' | |
# Set suspend timeouts (adjust as necessary) | |
# Suspend after 15 minutes on battery | |
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'suspend' | |
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout 900 | |
# Suspend after 30 minutes on AC power | |
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'suspend' | |
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 1800 | |
# Ensure sufficient swap space | |
adjust_swap | |
# Configure GRUB for hibernation | |
configure_grub_for_hibernation | |
# Optional: Install and configure TLP for additional power management (for laptops) | |
apt install -y tlp tlp-rdw | |
systemctl enable tlp | |
systemctl start tlp | |
# Confirm changes | |
echo "Suspend mode and hibernation configuration are complete. Please verify the settings." | |
# Optional: Reboot to apply changes | |
# Uncomment the next line if you want to reboot automatically | |
# sudo reboot | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment