Advertisement
Guest User

Fedora 36 btrfs+zram hibernation setup (WIP)

a guest
Jul 4th, 2022
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.01 KB | None | 0 0
  1. #!/usr/bin/bash
  2.  
  3. if [[ ! $UID -eq 0 ]]; then
  4.   echo "Script should be runned under root!" 1>&2
  5.   exit 1
  6. fi
  7.  
  8. SWAP_DIR=/swap
  9. HIBEFILE=hibernation
  10.  
  11. HIBERFILE_PATH=${SWAP_DIR}/${HIBEFILE}
  12.  
  13. FS_TYPE=$(mount | grep " on / " | awk '{print $5}')
  14.  
  15. if [[ "${FS_TYPE}" != 'btrfs' ]]; then
  16.   echo "Only 'btrfs' filesystem supported" 1>&2
  17.   exit 1
  18. fi
  19.  
  20. ZRAM_SIZE=$(zramctl -no DISKSIZE | sed -e 's%\s%%g')
  21.  
  22. ZRAM_SIZE_INT=$(echo ${ZRAM_SIZE} | sed -e 's%[a-zA-Z\s]%%')
  23. ZRAM_SIZE_SUFF=$(echo ${ZRAM_SIZE} | sed -e 's%[0-9]%%g' -e 's%$%ib%')
  24.  
  25. RAM_SIZE=$(free -gt | grep ^Mem | awk '{print $2}')
  26.  
  27. HIBERFILE_SIZE=$(( ZRAM_SIZE_INT*2 ))
  28. HIBERFILE_SIZE=$(( HIBERFILE_SIZE+RAM_SIZE ))
  29.  
  30. btrfs subvolume show ${SWAP_DIR} > /dev/null 2>&1
  31. if [[ ! $? -eq 0 ]]; then
  32.   btrfs subvolume create "${SWAP_DIR}"
  33. fi
  34.  
  35. if [[ ! -e ${HIBERFILE_PATH} ]]; then
  36.   touch "${HIBERFILE_PATH}"
  37.   chattr +C "${HIBERFILE_PATH}"
  38.   fallocate --length ${HIBERFILE_SIZE}${ZRAM_SIZE_SUFF} ${HIBERFILE_PATH}
  39.   chmod 600 ${HIBERFILE_PATH}
  40.   mkswap ${HIBERFILE_PATH}
  41. fi
  42.  
  43. if [[ ! -e /etc/dracut.conf.d/resume.conf ]]; then
  44.   echo 'add_dracutmodules+=" resume "' > /etc/dracut.conf.d/resume.conf
  45.   dracut -f
  46. fi
  47.  
  48. RESUME_UUID=$(findmnt -no UUID -T ${SWAP_DIR})
  49.  
  50. grep /etc/default/grub -e "resume=UUID=${RESUME_UUID}" -q > /dev/null 2>&1
  51.  
  52. if [[ ! $? -eq 0 ]]; then
  53.   curl "https://raw.githubusercontent.com/osandov/osandov-linux/master/scripts/btrfs_map_physical.c" > /tmp/btrfs_map_physical.c
  54.   gcc -O2 -o /tmp/btrfs_map_physical /tmp/btrfs_map_physical.c
  55.   chmod +x /tmp/btrfs_map_physical
  56.  
  57.   PAGESIZE=$(getconf PAGESIZE)
  58.   RESUME_OFFSET=$(/tmp/btrfs_map_physical ${HIBERFILE_PATH} | sed -n 2p | awk '{print $9}')
  59.   RESUME_OFFSET=$(( RESUME_OFFSET / PAGESIZE ))
  60.  
  61.   rm -f /tmp/btrfs_map_physical*
  62.  
  63.   cp /etc/default/grub /etc/default/grub.bak
  64.  
  65.   GRUB_ARGS="resume=UUID=${RESUME_UUID} resume_offset=${RESUME_OFFSET}"
  66.  
  67.   grubby --args="${GRUB_ARGS}" --update-kernel=ALL
  68. fi
  69.  
  70. SYSTEMD_ENVFILE=/etc/sysconfig/hibernation
  71.  
  72. if [[ ! -e ${SYSTEMD_ENVFILE} ]]; then
  73. cat > ${SYSTEMD_ENVFILE} <<EOF
  74. HIBERFILE=${HIBERFILE_PATH}
  75. EOF
  76. fi
  77.  
  78. mkdir -p /etc/systemd/system/systemd-{logind,hibernate}.service.d/
  79.  
  80. SYSTEMD_OVERRIDES="
  81. /etc/systemd/system/systemd-logind.service.d/override.conf
  82. /etc/systemd/system/systemd-hibernate.service.d/override.conf
  83. "
  84.  
  85. for override in ${SYSTEMD_OVERRIDES}; do
  86.   [[ -e ${override} ]] && continue
  87.   cat > ${override} <<EOF
  88. [Service]
  89. Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1
  90. EOF
  91. done
  92.  
  93. cat > /etc/systemd/system/hibernate-pre.service <<EOF
  94. [Unit]
  95. Description=Enable swap file and disable zram before hibernate
  96. Before=systemd-hibernate.service
  97.  
  98. [Service]
  99. User=root
  100. Type=oneshot
  101. EnvironmentFile=${SYSTEMD_ENVFILE}
  102. ExecStart=/bin/bash -c "/usr/sbin/swapon \$HIBERFILE && /usr/sbin/swapoff /dev/zram0"
  103.  
  104. [Install]
  105. WantedBy=systemd-hibernate.service
  106. EOF
  107.  
  108. cat > /etc/systemd/system/hibernate-resume.service <<EOF
  109. [Unit]
  110. Description=Disable swap after resuming from hibernation
  111. After=hibernate.target
  112.  
  113. [Service]
  114. User=root
  115. Type=oneshot
  116. EnvironmentFile=${SYSTEMD_ENVFILE}
  117. ExecStart=/usr/sbin/swapoff \$HIBERFILE
  118.  
  119. [Install]
  120. WantedBy=hibernate.target
  121. EOF
  122.  
  123. #mkdir -p /usr/lib/system-sleep/sleep/
  124. #cat > /usr/lib/systemd/system-sleep/suspend-then-hibernate-post-suspend.sh <<EOF
  125. ##!/bin/bash
  126. #
  127. #if [ "\$1" = "post" ] && [ "\$2" = "suspend-then-hibernate" ] && [ "\$SYSTEMD_SLEEP_ACTION" = "suspend" ]
  128. #then
  129. #    . ${SYSTEMD_ENVFILE}
  130. #    echo "suspend-then-hibernate (post suspend): enabling swapfile and disabling zram"
  131. #    /usr/sbin/swapon \$HIBERFILE && /usr/sbin/swapoff /dev/zram0
  132. #fi
  133. #EOF
  134. #
  135. #chmod +x /usr/lib/systemd/system-sleep/suspend-then-hibernate-post-suspend.sh
  136. #
  137. #cat > /etc/systemd/system/suspend-then-hibernate-resume.service <<EOF
  138. #[Unit]
  139. #Description=Disable swapfile after resuming from hibernation during suspend-then-hibernate
  140. #After=suspend-then-hibernate.target
  141. #
  142. #[Service]
  143. #User=root
  144. #Type=oneshot
  145. #EnvironmentFile=${SYSTEMD_ENVFILE}
  146. #ExecStart=/usr/sbin/swapoff \$HIBERFILE
  147. #
  148. #[Install]
  149. #WantedBy=suspend-then-hibernate.target
  150. #EOF
  151.  
  152. systemctl daemon-reload
  153. #systemctl enable suspend-then-hibernate-resume.service
  154. #systemctl enable hibernate-pre.service hibernate-resume.service
  155. systemctl enable hibernate-pre.service
  156.  
  157.  
  158. rpm -q setools-console > /dev/null 2>&1
  159. if [[ ! $? -eq 0 ]]; then
  160.   dnf install setools-console -y
  161. fi
  162.  
  163. #semodule -l | grep "^systemd_sleep$" -q > /dev/null 2>&1
  164.  
  165. #if [[ ! $? -eq 0 ]]; then
  166.  
  167. if [[ -z "$(sesearch -A -s systemd_sleep_t -t unlabeled_t -c dir -p search)" ]]; then
  168.   SELINUX_TEMP=$(mktemp)
  169.   cat > ${SELINUX_TEMP} <<EOL
  170.  
  171. module systemd_sleep 1.0;
  172.  
  173. require {
  174.     type unlabeled_t;
  175.     type systemd_sleep_t;
  176.     class dir search;
  177. }
  178.  
  179. #============= systemd_sleep_t ==============
  180. allow systemd_sleep_t unlabeled_t:dir search;
  181. EOL
  182.  
  183.   checkmodule -M -m -o ${SELINUX_TEMP}.mod ${SELINUX_TEMP}
  184.   semodule_package -o ${SELINUX_TEMP}.pp -m ${SELINUX_TEMP}.mod
  185.   semodule -i ${SELINUX_TEMP}.pp
  186.   semodule -e systemd_sleep
  187.  
  188.   rm -f ${SELINUX_TEMP}*
  189. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement