Skip to content

Instantly share code, notes, and snippets.

@grahamperrin
Last active October 10, 2022 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamperrin/f741050b2dac26ed79e8a3f1748302e5 to your computer and use it in GitHub Desktop.
Save grahamperrin/f741050b2dac26ed79e8a3f1748302e5 to your computer and use it in GitHub Desktop.
FreeBSD sleep/wake (suspend/resume): extending rc.suspend to allow detachment of storage devices on USB, and to avoid audio-related issues
#!/bin/sh
# Save this script at a suitable path, for example
# /usr/local/sbin/suspend.sh
# then edit /etc/rc.suspend to include the path.
# Allow detachment of a USB device that is used for a ZFS pool named
# Transcend.
#
while mount | grep Transcend 2>&1; do
zpool export Transcend
sleep 5
done
#
# – since <https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=253553>
# is fixed, I wonder whether combined use of sysutils/lsof lsof(8)
# and devel/libnotify notify-send(1) can notify the user of the path
# to an open file, if one is open within the pool.
# <https://www.freebsd.org/cgi/man.cgi?query=lsof&sektion=8&manpath=FreeBSD-Ports>
# <https://www.freebsd.org/cgi/man.cgi?query=notify-send&sektion=1&manpath=FreeBSD-Ports>
# Allow detachment of USB devices that are used for OpenZFS L2ARC for
# the boot pool on internal storage.
#
zpool offline august gpt/cache-august
zpool offline august gpt/cache2-august
sync
# I imagined that this would prevent PulseAudio-related
# failures to suspend:
#
# killall pulseaudio
#
# – unfortunately not. Instead, avoid use of PulseAudio:
#
# autospawn = no
#
# – in /usr/local/etc/pulse/client.conf
# Credit to David Schlachter for 'USB audio devices on FreeBSD'
# <https://www.davidschlachter.com/misc/freebsd-usb-audio>
#
# Attempt to kill all dsp- and mixer-related processes.
#
while fstat | grep -e dsp -e mixer 2>&1; do
fstat | grep -e dsp -e mixer | cut -w -f 3 | while read pid;
do kill -15 "$pid"
done
done
# Make <IDT 92HD81B1X (Analog 2.0+HP/2.0)> (play/rec) the default
# on an HP EliteBook 8570p.
#
sysctl hw.snd.default_unit=1
# Retrospective
# =============
#
# I found it impossible to kill PulseAudio. Instead, it was set to
# no longer automatically spawn:
# <https://forums.FreeBSD.org/threads/80412/post-519408>.
#
# Killing of KMix by the script required a manual run of
# /usr/local/bin/kded5 &
# as grahamperrin after each wake of the computer.
#
# For a while, I forced deletion of KMix:
# <https://forums.FreeBSD.org/threads/80412/post-524012>
# – and used a fake manifest to prevent automated reinstallation:
# <https://forums.FreeBSD.org/threads/80412/post-528187>.
#
# Eventually, the issue involving KMix was resolved:
# <https://cgit.freebsd.org/ports/commit/?id=7dc8045a63930a3a520e17723139003867b58c39>
@grahamperrin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment