Skip to content

Commit

Permalink
Merge pull request #20 from hertg/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
hertg committed Nov 9, 2019
2 parents 34f2e31 + 8c5e617 commit 71e5796
Show file tree
Hide file tree
Showing 12 changed files with 514 additions and 323 deletions.
145 changes: 78 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
# egpu-switcher

> **Disclaimer**\
> Works with NVIDIA as well as AMD cards.\
> Tested on Ubuntu, but may work on any Linux (with X-Server) that supports `#!/bin/bash` scripts.
> Works with **NVIDIA** as well as **AMD** cards.\
> Tested on Ubuntu, Arch and many other distros.
>
> For more information and user feedback, take a look at my [Thread](https://egpu.io/forums/thunderbolt-linux-setup/ubuntu-19-04-easy-to-use-setup-script-for-your-egpu/) over on egpu.io.
> For more information and user feedback, take a look at my [Thread](https://egpu.io/forums/thunderbolt-linux-setup/ubuntu-19-04-easy-to-use-setup-script-for-your-egpu/) over on egpu.io or open an issue on Github.
## Description
The goal of this script is to make the initial egpu setup for (new) Linux users less of a pain. With this script your X-Server configs for the different GPUs will be automatically created, you just have to choose which one is the external and which the internal graphics card.

After the setup, your linux installation will at each startup check if your EGPU is connected or not, and then automatically choose the right X-Server configuration in the background.

**This does not provide you with a plug-and-play functionality like you may know from Windows. If you want do connect / disconnect your EGPU, you will have to restart your computer**.

## Screenshot
![Screenshot of setup](https://raw.githubusercontent.com/hertg/egpu-switcher/master/images/screenshot_setup.png)

# TL;DR
## Requirements
1. Your OS is running X-Server
1. You have at least Bash 4.x or higher installed
1. You have already authorized your Thunderbolt EGPU and are able to connect
1. You have already installed the latest (proprietary) drivers for your GPUs

## TL;DR

## Ubuntu (apt)
### Ubuntu (apt)
Installation and setup:
```bash
$ sudo add-apt-repository ppa:hertg/egpu-switcher
Expand All @@ -21,10 +35,10 @@ $ sudo egpu-switcher setup

Uninstall:
```bash
$ apt remove --purge egpu-switcher
$ apt remove egpu-switcher
```

## Other
### Other
Installation and setup:
```bash
$ git clone [email protected]:hertg/egpu-switcher.git
Expand All @@ -41,43 +55,71 @@ $ sudo egpu-switcher cleanup
$ make uninstall
```

# Goal
The goal of this script is to make the initial egpu setup for (new) Linux users less of a pain. With this script your X-Server configs for the different GPUs will be automatically created, you just have to choose which one is the external and which the internal graphics card.
## Commands
<pre>
<b>egpu-switcher setup</b> [--override] [--noprompt]
This will generate your "xorg.conf.egpu" and "xorg.conf.internal" files and
symlink the "xorg.conf" file to one of them.

After the setup, your linux installation will at each startup check if your EGPU is connected or not, and then automatically choose the right X-Server configuration in the background.
It will also create the systemd service, that runs the "switch" command on each startup.

**This does not provide you with a plug-and-play functionality like you may know from Windows. If you want do connect / disconnect your EGPU, you will have to restart your computer**.
The setup will NOT delete any of your files, if you have an existing "xorg.conf"
file it will be backed up to "xorg.conf.backup.{datetime}". You can later revert
it by executing the "cleanup" command (see below).

# Prerequisites
1. You have already authorized your Thunderbolt EGPU and are able to connect
1. You have already installed the latest (proprietary) drivers for your GPUs
<b>--override</b>
If you have an AMD GPU or use the open-source nvidia drivers,
the "switch" command will prevent you from switching to the EGPU
if there are no displays directly attached to it. This flag will
make sure to switch to the EGPU even if there are no displays attached.

> When installing Ubuntu 19.04, please check the box "Install third-party software for graphics and Wi-Fi hardware". After that, all required drivers will be installed automatically.
<b>--noprompt</b>
Prevent the setup from prompting for user interaction if there is
no existing configuration file found.
(Is currently only used by the "postinst" script)
</pre>

> **Hint for people with hybrid graphics**\
> I am using a Lenovo notebook with hybrid graphics (internal graphics **and** a dedicated GPU). I've experienced freezes in the Ubuntu 19.04 installer which could only be resolved by changing the display settings in the BIOS from ~~Hybrid Graphics~~ to **Discrete Graphics**. After the installation was complete, i was able to change this setting back to **Hybrid Graphics**, without any issues.
<pre>
<b>egpu-switcher switch auto|egpu|internal</b> [--override]
Switches to the specified GPU (egpu|internal). If the <b>auto</b> parameter
is used, the script will check if the egpu is attached and switch accordingly.

You need to restart your computer (or display-manager) for this to take effect.

# Install
```bash
$ sudo add-apt-repository ppa:hertg/egpu-switcher
$ sudo apt update
$ sudo apt install egpu-switcher
```
<b>--override</b>
If you have an AMD GPU or use the open-source nvidia drivers,
the "switch" command will prevent you from switching to the EGPU
if there are no displays directly attached to it. This flag will
make sure to switch to the EGPU even if there are no displays attached.
</pre>

Just start the setup with the following command, after the setup is finished, you are good to go.
<pre>
<b>egpu-switcher config</b>
Prompts the user to specify their external/internal GPU and saves their answer
to the configuration file.
</pre>

```bash
$ sudo egpu-switcher setup
```
<pre>
<b>egpu-switcher cleanup</b>
Remove all files egpu-switcher has created previously and restore the backup
of previous "xorg.conf" files.

# Uninstall
Run the following command to uninstall the package. All created files will be removed and your previous `xorg.conf` will be restored if you had one.
```bash
$ apt remove --purge egpu-switcher
```
<b>--hard</b>
Remove configuration files too.
</pre>

---

## Hints

1. **Ubuntu 19.04 or later**\
When installing Ubuntu 19.04 or later, please check the box "Install third-party software for graphics and Wi-Fi hardware". After that, all required drivers will be installed automatically.

# Background information
> A backup of your current `xorg.conf` will be created, nothing gets deleted. If the script doesn't work for you, you can revert the changes by executing `egpu-switcher cleanup` or just completely uninstall the script with `apt remove --purge egpu-switcher`. This will purge all files it has created and also restore your previous `xorg.conf` file.
1. **Notebooks with hybrid graphics**\
I am using a Lenovo notebook with hybrid graphics (internal graphics **and** a dedicated GPU). I've experienced freezes in the Ubuntu 19.04 installer which could only be resolved by changing the display settings in the BIOS from **Hybrid Graphics** to **Discrete Graphics**. After the installation was complete, i was able to change this setting back to **Hybrid Graphics**, without any issues.

## Background information
> A backup of your current `xorg.conf` will be created, nothing gets deleted. If the script doesn't work for you, you can revert the changes by executing `egpu-switcher cleanup` or just completely uninstall the script with `apt remove egpu-switcher`. This will remove all files it has created and also restore your previous `xorg.conf` file.
This script will create two configuration files in your X-Server folder `/etc/X11`.
The file `xorg.conf.egpu` holds the settings for your EGPU and the file `xorg.conf.internal` holds the settings for your internal graphics.
Expand All @@ -92,6 +134,7 @@ Additionally a custom `systemd` service with the following content will be creat
Description=EGPU Service

[Service]
Type=oneshot
ExecStart=egpu-switcher switch auto

[Install]
Expand All @@ -100,39 +143,7 @@ WantedBy=multi-user.target

This will enable the automatic detection wheter your egpu is connected or not on startup.

# Available commands
You usually don't need these commands (apart from the `setup`, of course).

## Setup
`egpu-switcher setup <method>`\
Will start the setup process.
> If no method is passed, the `lspci` will be used by default.\
> The following methods are available:
> - `lspci` (recommended)
> - `nvidia-xconfig`
## Switch
`egpu-switcher switch auto <method>`\
This command will automatically detect if the egpu is connected and update the `xorg.conf` symlink accordingly.
> If no method is passed, the `lspci` will be used by default.\
> The following methods are available:
> - `lspci` (recommended)
> - `nvidia-xconfig`
`egpu-switcher switch egpu`\
This command will point the `xorg.conf` symlink to `xorg.conf.egpu`

`egpu-switcher switch internal`\
This command will point the `xorg.conf` symlink to `xorg.conf.internal`

## Cleanup
`egpu-switcher cleanup`\
This command will revert the whole `setup` process and remove all files it has created.
Additionally the command will restore your previous `xorg.conf` that you had before running the `setup`.

> This command is executed automatically when uninstalling via `apt remove --purge egpu-switcher`.
# Build (notes to myself)
## Build (notes to myself)
1. `sudo apt install devscripts`
1. `sudo apt install debhelper`
1. Update changelog: `dch`
Expand Down
6 changes: 0 additions & 6 deletions debian/README

This file was deleted.

9 changes: 9 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
egpu-switcher (0.11.0) disco; urgency=low

* Remove 'nvidia-xconfig' method of detecting GPUs
* Change egpu.service to Type=oneshot
* Make specifying the internal GPU optional (let X-Server figure it out themselves if EGPU is not connected)
* Save chosen GPUs to config file to prevent re-run of setup after every update

-- Michael Hertig <[email protected]> Sat, 09 Nov 2019 18:40:30 +0100

egpu-switcher (0.10.2) disco; urgency=medium

* Fixed critical typo in Makefile which removed too many files
Expand Down
40 changes: 40 additions & 0 deletions debian/docs/egpu-switcher.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.TH EGPU-SWITCHER 1 2019-11-09 GNU User Manual
.SH NAME
egpu-switcher \- Automatically choose the correct GPU on startup
.SH SYNOPSIS

.B egpu-switcher setup
[\-\-override]
[\-\-noprompt]

.B egpu-switcher switch
[\-\-override]

.B egpu-switcher cleanup
[\-\-hard]

.B egpu-switcher config


.SH DESCRIPTION
.B egpu-switcher
Allows the end-user to define their external GPU and their internal GPU.
After being installed, egpu-switcher runs the command 'egpu-switcher switch auto' on each startup
before the display-manager gets started, preparing the system to use the correct GPU.
The EGPU is only used if it is attached during the startup process. Otherwise the internal configuration will be used.

.SH OPTIONS
.TP
.BR \-\-override
This prevents the 'switch' command from choosing the internal GPU over the external GPU even if the external one is attached.
In some cases, when using open-source drivers, the egpu-switcher script prevents you from switching to the external GPU
if you have no displays directly attached to it.

.TP
.BR \-\-noprompt
This prevents the 'setup' command from prompting user interaction if no existing configuration file was found.
This option is currently only used by the 'postinstall' script.

.TP
.BR \-\-hard
Forces the 'cleanup' command to delete the configuration files too. Otherwise they will only be deleted with a purge.
3 changes: 0 additions & 3 deletions debian/egpu-switcher-docs.docs

This file was deleted.

1 change: 1 addition & 0 deletions debian/egpu-switcher.manpages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs/egpu-switcher.1
2 changes: 1 addition & 1 deletion debian/files
Original file line number Diff line number Diff line change
@@ -1 +1 @@
egpu-switcher_0.10.2_source.buildinfo utils optional
egpu-switcher_0.11.0_source.buildinfo utils optional
7 changes: 7 additions & 0 deletions debian/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

# run the noprompt setup after installation
# the setup will only run if there are existing config files
if [ ${1} = "configure" ]; then
egpu-switcher setup --noprompt
fi
10 changes: 10 additions & 0 deletions debian/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

config_dir=/etc/egpu-switcher
config_file=${config_dir}/egpu-switcher.conf

# remove the config file + dir on purge
if [ ${1} = "purge" ] && [ -e ${config_dir} ]; then
rm -f ${config_file}
rm -fd ${config_dir}
fi
2 changes: 2 additions & 0 deletions debian/prerm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/sh

# clean up files egpu-switcher has created
# and possibly restored backed up files
egpu-switcher cleanup
Loading

0 comments on commit 71e5796

Please sign in to comment.