Skip to content

Instantly share code, notes, and snippets.

@wvdschel
Created October 20, 2022 08:02
Show Gist options
  • Save wvdschel/16622faffa2063e861d89fa21137b00b to your computer and use it in GitHub Desktop.
Save wvdschel/16622faffa2063e861d89fa21137b00b to your computer and use it in GitHub Desktop.
Hibernation enablement script for Fedora
#!/bin/bash
# Based on https://fedoramagazine.org/hibernation-in-fedora-36-workstation/
set -eo pipefail
ZRAM_KBYTES=$(cat /proc/swaps | grep zram0 | sed -E 's/\s+/\t/g' | cut -f 3)
TOTAL_RAM=$(free -k | egrep ^Mem: | sed -E "s/ +/\t/g" | cut -f2)
TOTAL_ZRAM=0
UNCOMPRESSED_RAM=${TOTAL_RAM}
for ZRAM in ${ZRAM_KBYTES}; do
TOTAL_ZRAM=$(expr ${ZRAM} \* 2 + ${TOTAL_ZRAM})
UNCOMPRESSED_RAM=$(expr ${UNCOMPRESSED_RAM} - ${ZRAM})
done
TOTAL_MEM_SPACE=$(expr ${TOTAL_ZRAM} + ${UNCOMPRESSED_RAM})
echo "MEMORY: ${TOTAL_MEM_SPACE} / ZRAM: ${TOTAL_ZRAM} / UNCOMPRESSED: ${UNCOMPRESSED_RAM}"
# Create swap file
btrfs subvolume create /swap
touch /swap/swapfile
chattr +C /swap/swapfile
fallocate --length ${TOTAL_MEM_SPACE}K /swap/swapfile
chmod 600 /swap/swapfile
mkswap /swap/swapfile
SWAP_UUID=$(findmnt -no UUID -T /swap/swapfile)
# Enable resume support in initramfs
cat <<-EOF | sudo tee /etc/dracut.conf.d/resume.conf
add_dracutmodules+=" resume "
EOF
dracut -f
curl -s https://raw.githubusercontent.com/osandov/osandov-linux/master/scripts/btrfs_map_physical.c -o btrfs_map_physical.c
gcc -Os -o /tmp/btrfs_map_physical btrfs_map_physical.c
SWAPFILE_OFFSET=$(/tmp/btrfs_map_physical /swap/swapfile | egrep ^0 | sed -E 's/\s+/\t/g' | cut -f9)
PAGESIZE=$(getconf PAGESIZE)
SWAPFILE_OFFSET_PAGES=$(expr ${SWAPFILE_OFFSET} / ${PAGESIZE})
echo "SWAP: UUID=${SWAP_UUID} OFFSET=${SWAPFILE_OFFSET_PAGES}"
grubby --args="resume=UUID=${SWAP_UUID} resume_offset=${SWAPFILE_OFFSET_PAGES}" --update-kernel=ALL
cat >/etc/systemd/system/hibernate-preparation.service <<-EOF
[Unit]
Description=Enable swap file and disable zram before hibernate
Before=systemd-hibernate.service
[Service]
User=root
Type=oneshot
ExecStart=/bin/bash -c "/usr/sbin/swapon /swap/swapfile && /usr/sbin/swapoff /dev/zram0"
[Install]
WantedBy=systemd-hibernate.service
EOF
cat >/etc/systemd/system/hibernate-resume.service <<-EOF
[Unit]
Description=Disable swap after resuming from hibernation
After=hibernate.target
[Service]
User=root
Type=oneshot
ExecStart=/usr/sbin/swapoff /swap/swapfile
[Install]
WantedBy=hibernate.target
EOF
systemctl enable hibernate-preparation.service
systemctl enable hibernate-resume.service
mkdir -p /etc/systemd/system/systemd-logind.service.d/ /etc/systemd/system/systemd-hibernate.service.d/
cat >/etc/systemd/system/systemd-logind.service.d/override.conf <<-EOF
[Service]
Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1
EOF
cat >/etc/systemd/system/systemd-hibernate.service.d/override.conf <<-EOF
[Service]
Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment