Initial commit

This commit is contained in:
Fierelier 2023-12-12 06:44:09 +01:00
commit d6ee660f9d
9 changed files with 152 additions and 0 deletions

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

64
cmd/eject Normal file
View File

@ -0,0 +1,64 @@
#!/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."

27
cmd/get_egpu_process Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env bash
for proc in "/proc"/*; do
first_arg=1
xfconf=0
while IFS= read -r -d '' proc_arg; do
if [ "$first_arg" = "1" ]; then
if ! [[ "$proc_arg" == *"Xorg" ]]; then
break
fi
first_arg=0
continue
fi
if [ "$xfconf" = "1" ]; then
if ! [ "$proc_arg" == "$IGPU_XORG_CONFIG" ]; then
echo "$(basename "$proc")"
exit 0
fi
break
fi
if [ "$proc_arg" = "-config" ]; then
xfconf=1
fi
done < <(cat "$proc/cmdline" 2>/dev/null)
done
exit 0

6
cmd/get_process_tty Normal file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
while IFS= read -r -d '' proc_arg; do
if [[ "$proc_arg" == "vt"* ]]; then
echo "${proc_arg:2}"
fi
done < <(cat "/proc/$1/cmdline" 2>/dev/null)

7
egpu-tk Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
APP_CMD="$(realpath "$BASH_SOURCE")"
APP_DIR="$(dirname "$APP_CMD")"
APP_ARGS=("${@}")
unset "APP_ARGS[0]"
source "$APP_DIR/env"
source "$APP_DIR/cmd/$1" "${APP_ARGS[@]}"

9
env Normal file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
# IGPU info
IGPU_ID="00:02.0" # The PCI ID of the device
IGPU_XORG_CONFIG="xorg-igpu-only.conf" # The Xorg conf you use to boot a desktop on the device
# EGPU info
EGPU_ID="05:00.0" # The PCI ID of the device
EGPU_IDS=("$EGPU_ID" "05:00.1") # Which devices to eject
EGPU_XORG_CONFIG="xorg-egpu-only.conf" # The Xorg conf you use to boot a desktop on the device

10
readme.txt Normal file
View File

@ -0,0 +1,10 @@
egpu-tk (External GPU Toolkit)
A helper for managing eGPUs. This is meant to be used with 2 instances of X, both restricted to one of the GPUs.
* Use lspci to see your devices
* Edit env appropriately
* Edit xorg-egpu-only.conf and xorg-igpu-only.conf appropriately. Put them into /etc/X11
* Run "Xorg :1 -config xorg-igpu-only.conf vt7" for the iGPU
* Run "Xorg :2 -config xorg-igpu-only.conf vt8" for the dGPU
* To eject the dGPU, use "sudo ./egpu-tk eject"

10
xorg-egpu-only.conf Normal file
View File

@ -0,0 +1,10 @@
Section "ServerFlags"
Option "AutoAddGPU" "off"
EndSection
# GPU you want to use for display
Section "Device"
Identifier "AMD"
Driver "radeon"
BusID "PCI:5:0:0"
EndSection

10
xorg-igpu-only.conf Normal file
View File

@ -0,0 +1,10 @@
Section "ServerFlags"
Option "AutoAddGPU" "off"
EndSection
# GPU you want to use for display
Section "Device"
Identifier "Intel"
Driver "intel"
BusID "PCI:00:02:0"
EndSection