-
-
Save neutronth/52997d1c3912599ed6418cfeca767f68 to your computer and use it in GitHub Desktop.
systemd [/lib/systemd/system-sleep/usb] USB hub unbind/bind properly on suspend/resume
This file contains 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/sh | |
mount_path=/media/neutron | |
mount_match="(DataBackup|DataLife)" | |
usb_drivers_path=/sys/bus/usb/drivers | |
usb_hub_list=/tmp/usb-hub.list | |
unbind_usb_hub () { | |
> $usb_hub_list | |
for i in $(find $usb_drivers_path/hub 2>/dev/null | egrep '[0-9]+\-[0-9]+\:.*$'); do | |
device=$(basename $i) | |
path=$(dirname $i)/unbind | |
echo "Unbind device $device in $path ..." | |
echo -n $device > $path | |
echo $i >> $usb_hub_list | |
done | |
} | |
bind_usb_hub () { | |
for i in $(cat $usb_hub_list); do | |
device=$(basename $i) | |
path=$(dirname $i)/bind | |
echo "Bind device $device in $path ..." | |
echo -n $device > $path | |
done | |
} | |
close_all_usb_disks_programs () { | |
pids=$(lsof -n | egrep "$mount_path/$mount_match" | awk '{print $2}' | sort | uniq) | |
retry=3 | |
alive= | |
test -n "$pids" && kill $pids | |
while [ -n "$pids" ] && [ $retry -gt 0 ]; do | |
test -n "$alive" && kill $alive | |
alive= | |
for p in $pids; do | |
if ps aux | awk '{print $2}' | grep -w $p >/dev/null ; then | |
alive="$p $alive" | |
fi | |
done | |
if [ -z "$alive" ]; then | |
retry=0 | |
else | |
retry=$((retry - 1)) | |
sleep 2 | |
fi | |
done | |
test -n "$alive" && kill -9 $alive | |
} | |
unmount_all () { | |
sync | |
mounts=$(mount | egrep "$mount_path/$mount_match" | awk '{print $1}') | |
for m in $mounts; do | |
echo "Unmount $m ..." | |
umount $m | |
done | |
} | |
case $1 in | |
pre) | |
close_all_usb_disks_programs | |
unmount_all | |
unbind_usb_hub | |
modprobe -r uas | |
;; | |
post) | |
modprobe uas | |
bind_usb_hub | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment