egpu-tk/cmd/eject

65 lines
1.4 KiB
Bash

#!/usr/bin/env bash
echo "Terminating eGPU Xorg servers..."
while true; do
EGPU_PROC="$("$APP_CMD" get_egpu_process)"
if [ "$EGPU_PROC" = "" ]; then # No eGPU process
break
fi
echo "* $EGPU_PROC"
kill -n 15 "$EGPU_PROC"
if ! [ "$?" = "0" ]; then
echo "Process couldn't be terminated."
exit 1
fi
while true; do # Wait for process to close
if ! [ -d "/proc/$EGPU_PROC" ]; then
break
fi
sleep 1
done
done
echo "Unbinding eGPU from its drivers..."
for DEV_ID in "${EGPU_IDS[@]}"; do
DEV_PTH="/sys/bus/pci/devices/0000:$DEV_ID"
if ! [ -d "$DEV_PTH" ]; then # Device is already removed
continue
fi
DEV_DRV_SRC="/sys/bus/pci/devices/0000:$DEV_ID/driver"
while true; do
DEV_DRV="$(readlink -f "$DEV_DRV_SRC")"
if ! [ -f "$DEV_DRV/unbind" ]; then # No driver is loaded
break
fi
echo "* ${DEV_ID}: $(basename "$DEV_DRV")"
echo "0000:$DEV_ID" > "$DEV_DRV/unbind"
while true; do # Wait for device to be unbound
if ! [ "$DEV_DRV" = "$(readlink -f "$DEV_DRV_SRC")" ]; then
break
fi
sleep 1
done
done
done
echo "Removing eGPU devices from PCI bus..."
for DEV_ID in "${EGPU_IDS[@]}"; do
DEV_PTH="/sys/bus/pci/devices/0000:$DEV_ID"
if ! [ -d "$DEV_PTH" ]; then # Device is already removed
continue
fi
echo "* ${DEV_ID}"
echo "1" > "$DEV_PTH/remove"
while true; do
if ! [ -d "$DEV_PTH" ]; then
break
fi
sleep 1
done
done
echo "Done."