bash-stuff/activate-hibernate-support.sh

36 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
echo "[+] Activating hibernate support!";
echo "[i] Pass the reserved swap size in bytes to reserve some swap space for non-hibernate image usage.";
echo "[!] WARNING polkitd-pkla needs to be installed in newer distributions due to being split from the main package!";
szresv=1073741824;
if [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -gt 0 ]; then
szresv=$1;
fi;
szmem=$(free -b | awk '{print NR==1?$1:$1$2}' | grep 'Mem:' | sed -e "s/Mem://")
szswap=$(free -b | awk '{print NR==1?$1:$1$2}' | grep 'Swap:' | sed -e "s/Swap://");
if [ -z $szswap ]; then
echo "[-] No Swap Partition!";
exit 2;
fi;
szhiber=$(echo "$szswap - $szresv" | bc);
if [ $szmem -gt $szhiber ]; then
echo "[-] Not enough swap space to hibernate!"
exit 1;
fi;
echo "[*] Hibernate image is $szhiber bytes!";
sudo mkdir -p /etc/polkit-1/localauthority/90-mandatory.d;
sudo cp -f hibernate-source-files/enable-hibernate.pkla /etc/polkit-1/localauthority/90-mandatory.d/enable-hibernate.pkla;
sudo rm -f /etc/systemd/system/hibernate_image_size.service;
cat hibernate-source-files/hibernate_image_size.service.p1 | head -c -1 | sudo tee /etc/systemd/system/hibernate_image_size.service > /dev/null;
echo -n "$szhiber" | sudo tee -a /etc/systemd/system/hibernate_image_size.service > /dev/null;
cat hibernate-source-files/hibernate_image_size.service.p2 | head -c -1 | sudo tee -a /etc/systemd/system/hibernate_image_size.service > /dev/null;
sudo rm -f /etc/tmpfiles.d/hibernation_image_size.conf;
cat hibernate-source-files/hibernation_image_size.conf | head -c -1 | sudo tee /etc/tmpfiles.d/hibernation_image_size.conf > /dev/null;
echo "$szhiber" | sudo tee -a /etc/tmpfiles.d/hibernation_image_size.conf > /dev/null;
sudo systemctl daemon-reload;
sudo systemctl enable --now hibernate_image_size.service;
echo -n "$szhiber" | sudo tee /sys/power/image_size > /dev/null;
echo "[+] Finished Hibernate Activation!";
echo "[i] Check the grub file for linux kernel command line info for hibernating with a patched kernel under lockdown.";
echo "[i] This file corresponds to /etc/default/grub";