Setup & Software Discussions
Connect/Mount eGPU using command line
 

Connect/Mount eGPU using command line  

  RSS

kstl
 kstl
(@kstl)
Active Member
Joined: 1 year ago
 

Hey everyone!

A friend and I are looking into different ways to automate the process of auto-ejecting both the eGPU, to make it quicker and easier to disconnect and go when needed. (For anyone who didn't know, you can disconnect the eGPU via the command line with "/usr/bin/SafeEjectGPU Eject")

However, we're also looking for ways to automate the process of reconnecting the eGPU*. Does anyone know if a command for that exists? I can't see anything else in /usr/bin that refers to eGPU at all, not sure where else to look. 

Thanks!

* in case you're wondering why on earth this would be helpful, the basic idea is to have the system automatically eject the eGPU just before system sleep, (so you can just unplug and go, say, first thing in the morning, without waking the system to do it) and have it automatically re-connect if the system wakes from sleep and detects that the eGPU wasn't actually unplugged.

(edit: word to better clarify)

2018 13" MBP TouchBar + RX580- [email protected] (AKiTiO Node Pro) + 10.14b & Win10
20" Apple Cinema Display


ReplyQuote
mac_editor
(@mac_editor)
Famed Member Moderator
Joined: 3 years ago
 

@kstl See the documentation:

man SafeEjectGPU

Per that, once the eject sequence finalizes, the eGPU must be physically re-connected for use. You could attempt partial ejection:

SafeEjectGPU Initiate
SafeEjectGPU Relaunch

And test, though I doubt you'd be able to finalize the eject (and avoid the disconnect notification), since after wake, if the eGPU is not present, there is nothing to finalize. You can cancel the ejection process with:

SafeEjectGPU Cancel

Finally, another way of doing it is if one could figure out "disabling/enabling the thunderbolt port" temporarily (to simulate physical eject/plug).

Edit: Ok figured it out - not pretty, but does the job.

You will need a script with privileged access, or just use a privileged daemon.

# When system is going to sleep
# --
# Eject GPU
SafeEjectGPU Eject
# --
# Disable Thunderbolt PCI stream
sudo kextunload /System/Library/Extensions/AppleThunderboltPCIAdapters.kext/Contents/PlugIns/AppleThunderboltPCIUpAdapter.kext/
# --
# -- 
# When system is about to wake up
# --
# Re-enable PCI stream - if eGPU is plugged in, it will auto-connect
sudo kextload /System/Library/Extensions/AppleThunderboltPCIAdapters.kext/Contents/PlugIns/AppleThunderboltPCIUpAdapter.kext/

This should do the trick. It shouldn't affect other non-PCI devices connected via Thunderbolt, but I have not tested specific cases.

purge-wrangler.shpurge-nvda.shset-eGPU.shautomate-eGPU EFI Installer
2018 MacBook Pro 15" RP560X + RX 5700 XT (Mantiz Venus)


kstl liked
ReplyQuote
kstl
 kstl
(@kstl)
Active Member
Joined: 1 year ago
 

That's super neat, thanks a lot!

I won't have a lot of time to spend on it this week due to school, but I'm going to try and integrate this into my Hammerspoon config. I can't see any reason why it wouldn't work. I'll report back if/when I have any success.

2018 13" MBP TouchBar + RX580- [email protected] (AKiTiO Node Pro) + 10.14b & Win10
20" Apple Cinema Display


ReplyQuote
(@patrick_b)
New Member
Joined: 1 year ago
 

Hey,

does the proposed solution work? I have the same requirement to have a script that automates the egpu behaviour. Would be really interested in a solution.

Thnaks in advance

This post was modified 1 year ago

Pending: Add my system information and expected eGPU configuration to my signature to give context to my posts


ReplyQuote
nu_ninja
(@nu_ninja)
Estimable Member
Joined: 2 years ago
 

@patrick_birkner
Yes I've used the above commands to good effect in 10.14 Mojave. The eGPU connection after the kextload command can take a few seconds but it definitely works.

Mid-2012 13" Macbook Pro (MacBookPro9,2) TB1 -> RX 460/560 (AKiTiO Node/Thunder2)
+ macOS 10.15+Win10 + Linux Mint 19.1


ReplyQuote
(@patrick_b)
New Member
Joined: 1 year ago
 

@nu_ninja
How do you trigger those command lines for sleep and wake up? Do you use a special program? I couldn't find anything suitable so far that is not 5 years old ;).

Pending: Add my system information and expected eGPU configuration to my signature to give context to my posts


ReplyQuote
nu_ninja
(@nu_ninja)
Estimable Member
Joined: 2 years ago
 

I haven't had sleep/wake issues where I needed the commands triggered by those so I just setup an automator quick action with the following applescript code.

display dialog "" buttons {"Eject", "Reload"}
if the button returned of the result is "Eject" then
do shell script "SafeEjectGPU Eject"
else
do shell script "kextunload /System/Library/Extensions/AppleThunderboltPCIAdapters.kext/Contents/PlugIns/AppleThunderboltPCIUpAdapter.kext/" with administrator privileges
do shell script "kextload /System/Library/Extensions/AppleThunderboltPCIAdapters.kext/Contents/PlugIns/AppleThunderboltPCIUpAdapter.kext/" with administrator privileges 

end if

It's probably not great code, but it works. Maybe switch the if/else statements for safety.

This post was modified 1 year ago

Mid-2012 13" Macbook Pro (MacBookPro9,2) TB1 -> RX 460/560 (AKiTiO Node/Thunder2)
+ macOS 10.15+Win10 + Linux Mint 19.1


ReplyQuote
(@patrick_b)
New Member
Joined: 1 year ago
 

Ok, I will check if it is working for me as well or if I need to extend something. I will keep you updated if someone needs more input as well.
Thanks to you so far for your help. 🙂 

Appreciate it!

This post was modified 1 year ago

Pending: Add my system information and expected eGPU configuration to my signature to give context to my posts


ReplyQuote
nu_ninja
(@nu_ninja)
Estimable Member
Joined: 2 years ago
 

NP, hope it does what you need.

Mid-2012 13" Macbook Pro (MacBookPro9,2) TB1 -> RX 460/560 (AKiTiO Node/Thunder2)
+ macOS 10.15+Win10 + Linux Mint 19.1


ReplyQuote
mac_editor
(@mac_editor)
Famed Member Moderator
Joined: 3 years ago
 

I see I missed a few comments on this thread haha. Looks like the kext loading/unloading does the trick.

@patrick_b Check this out for some ways to trigger scripts on sleep/wake. I suppose the most "native, no-dependency" way of doing it is the LaunchDaemon example there (cannot confirm if it works myself).

purge-wrangler.shpurge-nvda.shset-eGPU.shautomate-eGPU EFI Installer
2018 MacBook Pro 15" RP560X + RX 5700 XT (Mantiz Venus)


ReplyQuote