Disable/Enable modules pre-post hibernation in linux

Script for disabling kernel modules pre-hibernation and enabling them post-hibernation

I noticed my WiFi module (mt7921e) was not functioning every time my Framework 16 woke from hibernation and I had no internet as a consequence. I found out you can make pre-post sleep scripts, so I wrote a little script. You can replace the kernel-module (here mt7921e) with what suits you best. You should place it in /usr/lib/systemd/system-sleep (systemd) or /etc/elogind/system-sleep (non-systemd) and you’re good to go. Don’t forget to make the script executable (chmod +x hibernate-pre-post.sh).

It’s also on my GitHub. An earlier version of the script was posted in the framework community.

#!/bin/bash
#
# You should place this script in:
# - '/usr/lib/systemd/system-sleep' (systemd)
# - '/etc/elogind/system-sleep' (non-systemd)
# and make it executable.
# See 'man 1 loginctl', section 'Hook directories' for more information.
#
# Some devices do not function after hibernate, this script can solve that problem.
# It disables kernel module(s) pre-hibernate and enables kernel module(s) post-hibernate.
# You can add "hybrid-sleep" in the script too, if you use that.
#
#
# Module name(s): see output of 'lspci -knn'. Change below value to match your system.
# Add more modules if needed (and in the script below too).
WiFiModule=mt7921e

case "$1 $2" in
  "pre hibernate" | "pre suspend-then-hibernate")
    modprobe -r $WiFiModule
    ;;
  "post hibernate" | "post suspend-then-hibernate")
    modprobe $WiFiModule
    ;;
  *)
    :
    ;;
esac