Last active
December 17, 2024 01:35
-
-
Save ispanos/04346487ff4c892c100fa1e6c8c54047 to your computer and use it in GitHub Desktop.
A script to enable hybernation on Fedora workstation.
This file contains hidden or 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 | |
# Credits: | |
# Hibernation in Fedora Workstation | |
# Fedora Magazine | |
# Posted by Alexander Wellbrock on August 10, 2022 | |
# https://fedoramagazine.org/hibernation-in-fedora-36-workstation/ | |
# Which was based on: https://gist.github.com/eloylp/b0d64d3c947dbfb23d13864e0c051c67 | |
# WARNING: This script has not been properly tested. | |
# Size the the swap has to be on Fedora is zram + ram size | |
# Please make sure you have enough free space on your system | |
swap_size=$( | |
free -h | | |
awk -F':' '{print $2; gsub("Gi","");print $1}' | | |
awk '{total = total + $1}END{print total+1}' | |
) | |
swapfile_path='/swap/swapfile' | |
create_swapfile() { | |
sudo btrfs subvolume create /swap | |
sudo touch "$swapfile_path" | |
sudo chattr +C "$swapfile_path" | |
sudo fallocate --length "${swap_size}"G "$swapfile_path" | |
sudo chmod 600 "$swapfile_path" | |
sudo mkswap "$swapfile_path" | |
} | |
main(){ | |
create_swapfile | |
swap_file_UUID="$(findmnt -no UUID -T $swapfile_path)" | |
cat <<-EOF | sudo tee /etc/dracut.conf.d/resume.conf | |
add_dracutmodules+=" resume " | |
EOF | |
sudo dracut -f | |
curl --output btrfs_map_physical.c \ | |
https://raw.githubusercontent.com/osandov/osandov-linux/master/scripts/btrfs_map_physical.c | |
gcc -O2 -o btrfs_map_physical btrfs_map_physical.c | |
pysical_offset=$(sudo ./btrfs_map_physical "$swapfile_path" | awk 'NR==2 {print $(NF)}') | |
page_size="$(getconf PAGESIZE)" | |
resume_offset="$(awk -v var1=$pysical_offset -v var2=$page_size 'BEGIN { print ( var1 / var2 ) }')" | |
rm btrfs_map_physical.c btrfs_map_physical | |
sudo grubby --args="resume=UUID=${swap_file_UUID} resume_offset=${resume_offset}" --update-kernel=ALL | |
cat <<-EOF | sudo tee /etc/systemd/system/hibernate-preparation.service | |
[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 | |
sudo systemctl enable hibernate-preparation.service | |
cat <<-EOF | sudo tee /etc/systemd/system/hibernate-resume.service | |
[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 | |
sudo systemctl enable hibernate-resume.service | |
sudo mkdir -p /etc/systemd/system/systemd-logind.service.d/ | |
cat <<-EOF | sudo tee /etc/systemd/system/systemd-logind.service.d/override.conf | |
[Service] | |
Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1 | |
EOF | |
sudo mkdir -p /etc/systemd/system/systemd-hibernate.service.d/ | |
cat <<-EOF | sudo tee /etc/systemd/system/systemd-hibernate.service.d/override.conf | |
[Service] | |
Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1 | |
EOF | |
} | |
selinux_set_rules(){ | |
sudo systemctl hibernate | |
sudo audit2allow -b | |
# Expected output | |
# #============= systemd_sleep_t ============== | |
# allow systemd_sleep_t unlabeled_t:dir search; | |
cd /tmp || exit | |
sudo audit2allow -b -M systemd_sleep | |
sudo semodule -i systemd_sleep.pp | |
} | |
main | |
exit | |
## REBOOT, comment 'main' and 'exit' and run the script again, after you reboot. | |
## Then run: | |
selinux_set_rules | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment