[SOLVED] checking gpu manufacturer

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Rootbeer
Level 3
Level 3
Posts: 115
Joined: Sun Nov 24, 2019 3:22 am

[SOLVED] checking gpu manufacturer

Post by Rootbeer »

I'm trying to write a install script, and checking if the gpu manufacturer is amd or nvidia.

Anyone with a nvidia card that can tell me the results of this command?

gpudriver=`sudo lshw -c video |grep 'driver=' |awk '{ print $2}'`

( if the gpu manufacturer is amd the results are "driver=amdgpu" )
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: checking gpu manufacturer

Post by 1000 »

Code: Select all

$ sudo lshw -c video |grep 'driver=' | awk '{ print $2}'
driver=nvidia   
1. Probably you want only "nvidia" word.

2. You don't need root for Linux Mint, because there are also other commands.
Example for driver name:

Code: Select all

inxi -G | grep "Graphics:" | awk -F ".:" '{ print $4 }' | awk '{ print $1 }'
Example code

Code: Select all

#!/bin/bash


GRAPHICS_CARD_MANUFACTURER=$(inxi -G | grep "Device-1"  | sed -e 's/.*Device-1: //g' -e 's/driver.* //g')
echo "DEVICE=$GRAPHICS_CARD_MANUFACTURER"


if grep -iq "nvidia" <<< "$GRAPHICS_CARD_MANUFACTURER"; then
    echo "Nvidia device detected."
elif grep -iq "intel" <<< "$GRAPHICS_CARD_MANUFACTURER"; then
    echo "Intel device detected."
elif grep -iq "amd" <<< "$GRAPHICS_CARD_MANUFACTURER"; then
    echo "AMD device detected."
else
    printf "Device not recognized from the list."
fi
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: checking gpu manufacturer

Post by Termy »

I get `nouveau`, but I'm on the open-source driver.

I would not (in-fact, I never) script with `sudo` inside.

Also, to optimize the line with the command substitution in the variable assignment:

Code: Select all

while IFS=':' read -a Line; do
    if [ "${Line[0]// /}" == 'configuration' ]; then
        for Field in ${Line[1]}; {
            IFS='=' read -a KeyVal <<< "$Field"
            if [ "${KeyVal[0]}" == 'driver' ]; then
                gpudriver=${KeyVal[1]}
                break
            fi
        }

        break
    fi
done <<< "$(lshw -c video 2> /dev/null)"
I am assuming you're referring to BASH and not the Bourne Shell approach, because the above won't work in the latter.

You could probably use sysfs to do that without needing lshw(1) (I had to install it, for example).

Are you on GitHub? I doubt it, but thought I'd ask. If you are, I'm happy to help with the script.

Did a little digging. There are two things I found interesting. The '/proc/fb' file can be used to get the frame buffer name(?), but I don't know how reliable that is. The '/proc/modules' seems to show 'Live' modules, so if you're using 'nouveau', as I am, then that's easy and cheap to get. I'm guessing it shows 'nvidia' if you're using the proprietary driver; someone might have the module loaded (Live?) despite not using it, though.

Not found anything in sysfs yet ('/sys') other than the exact value you want, but it's related to thermal stuff, for some reason.

Code: Select all

while IFS='=' read -a Line; do
    if [ "${Line[0]}" == 'DRIVER' ]; then
        gpudriver=${Line[1]}
        break
    fi
done < /sys/class/graphics/fb0/device/uevent
Nailed it. :) That's very likely something like what lshw(1) does. It checks your first frame buffer (first display, I believe) for the uevent value 'DRIVER', which in my case is also 'neouveau'. Should be quite portable, certainly in Ubuntu- and Debian-based distributions of Linux.

If I were writing that script, I'd make it more robust by incorporating both approaches, in-case one fails, with lshw(1) as the fallback, since it's not a guaranteed install. The whole thing then becomes:

Code: Select all

UEventFile='/sys/class/graphics/fb0/device/uevent'

if [ -f "$UEventFile" -a -r "$UEventFile" ]; then
    while IFS='=' read -a Line; do
        if [ "${Line[0]}" == 'DRIVER' ]; then
            gpudriver=${Line[1]}
            break
        fi
    done < /sys/class/graphics/fb0/device/uevent
elif type -P lshw &> /dev/null; then
    # Fallback approach. Check for lshw(1) first.
    while IFS=':' read -a Line; do
        if [ "${Line[0]// /}" == 'configuration' ]; then
            for Field in ${Line[1]}; {
                IFS='=' read -a KeyVal <<< "$Field"
                if [ "${KeyVal[0]}" == 'driver' ]; then
                    gpudriver=${KeyVal[1]}
                    break
                fi
            }

            break
        fi
    done <<< "$(lshw -c video 2> /dev/null)"
else
    : # Error out here, or otherwise handle it.
fi
I'm also Terminalforlife on GitHub.
Rootbeer
Level 3
Level 3
Posts: 115
Joined: Sun Nov 24, 2019 3:22 am

Re: checking gpu manufacturer

Post by Rootbeer »

1000 wrote: Thu Dec 10, 2020 9:45 am ...
this worked like a charm:

Code: Select all

inxi -G | grep "Graphics:" | awk -F ".:" '{ print $4 }' | awk '{ print $1 }'
and it was also much much faster then my approch, which took about 5seconds for the command to finnish.

if grep -iq "nvidia" <<< "$GRAPHICS_CARD_MANUFACTURER"; then
echo "Nvidia device detected."


<<< can you explain what the tripple-tag does? I've never seen that before :)
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: checking gpu manufacturer

Post by Termy »

If you want speed and efficiency, refer to my post, as it's instant, and by default requires nothing be installed.

The use of '<<<' means that what follows will be provided via STDIN (standard input) of the given command, akin to piping. There's also '<<' for heredocs, and '<' for use with files. '<<<' typically gets used with command substitution ('$()' or '``') or process substitution ('<()'). BTW, '<<<' is for BASH and BASH-like shells; it won't work with Bourne Shell.
I'm also Terminalforlife on GitHub.
Rootbeer
Level 3
Level 3
Posts: 115
Joined: Sun Nov 24, 2019 3:22 am

[SOLVED] Re: checking gpu manufacturer

Post by Rootbeer »

Termy wrote: Thu Dec 10, 2020 10:00 am ....
Excellent! many thanks! yeah it's even better if the approach does not require special software installed. Although, I'm having some difficulty to understand everything in the script, but I'll take some time and thinking it over :)

btw, sorry to say that I'm not on github
Rootbeer
Level 3
Level 3
Posts: 115
Joined: Sun Nov 24, 2019 3:22 am

Re: checking gpu manufacturer

Post by Rootbeer »

Termy wrote: Thu Dec 10, 2020 10:42 am If you want speed and efficiency, refer to my post, as it's instant, and by default requires nothing be installed.

The use of '<<<' means that what follows will be provided via STDIN (standard input) of the given command, akin to piping. There's also '<<' for heredocs, and '<' for use with files. '<<<' typically gets used with command substitution ('$()' or '``') or process substitution ('<()'). BTW, '<<<' is for BASH and BASH-like shells; it won't work with Bourne Shell.
I'm some familiar with < and << but never seen triple tags.

Do you mean that <<< is kind of "reverse-piping" ?

like: ls -l |grep myfile

but in reverse in some way..?

And, could you give some more examples of <<<
:)
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [SOLVED] checking gpu manufacturer

Post by Termy »

https://github.com/terminalforlife/Extr ... e/fbdriver

You're welcome. I've just uploaded that to GitHub, in-case you need it again in future, or in-case someone else might find it useful; might even use it myself. Will tweak it if needed.

I talk a LOT about pure-shell approaches to things and stuff like while read loops on my channel, which you're free to discover. :lol:
I'm also Terminalforlife on GitHub.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] checking gpu manufacturer

Post by 1000 »

Code: Select all

 grep -iq "nvidia" <<< "$GRAPHICS_CARD_MANUFACTURER"
More humanly. Normally "grep" command working with files.

Code: Select all

grep -iq "nvidia" $variable
It will not work.

First writing a variable to a file and then use "grep" with file is weird.
This "<<<" allows you redirect a variable to the "grep" command immediately.
It's faster this way.

_______________________

Code: Select all

if grep something ; then
    next commands
fi
Probably you know.
The "grep" command returns true "0" if variable / word is found
or false "1" if not exist.

For example only grep :

Code: Select all

$ grep nvidia /sys/class/drm/card0/device/uevent
DRIVER=nvidia

Code: Select all

$ grep nvidia /sys/class/drm/card0/device/uevent ; echo $?
DRIVER=nvidia
0
For not printing the result and search for a variable with different size I use "-q" and "-i" from "grep" command.

Code: Select all

$ grep -qi nvidia /sys/class/drm/card0/device/uevent ; echo $?
0
_______________

Something else.

Code: Select all

$ cat /sys/class/graphics/fb0/device/uevent
DRIVER=vesa-framebuffer
MODALIAS=platform:vesa-framebuffer
I use Nvidia driver. If you want use "/sys/class/" I don't know if it's not better

Code: Select all

$ cat /sys/class/drm/card0/device/uevent
DRIVER=nvidia
PCI_CLASS=30000
...
Edit
Probably this is more complicated
https://developpaper.com/linux-hardware ... quisition/

Edit
Maybe ?

Code: Select all

$ lspci -k | grep -A2 VGA | grep "Kernel driver in use" | awk '{print $5}'
nvidia
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] checking gpu manufacturer

Post by 1000 »

Something else about "grep"
You can also

Code: Select all

echo $variable | grep something
But here is used "echo" command and pipe "|" , so I believe it's a longer way than just "<<<" .
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [SOLVED] checking gpu manufacturer

Post by Termy »

Just put up a video explaining the code, in-depth, OP. Hope it helps alleviate any confusion. Much easier to show and tell rather than write out an essay. :lol: You'll have to find it yourself, if you'd like to check it out, due to board rules.
1000 wrote: Thu Dec 10, 2020 12:51 pm But here is used "echo" command and pipe "|" , so I believe it's a longer way than just "<<<" .
Yeah, `echo` is a built-in (enabled by default, but there is a GNU alternative '/bin/echo'), so that is fairly efficient. However, the use of a pipe in BASH does actually fork a sub-shell, but AFAIK `<<<` doesn't, or at least isn't mentioned in the BASH manual. What this means, is that the `<<<` (called a Here String, big brother to the Here Doc '<<') is a little bit more efficient, technically.
1000 wrote: Thu Dec 10, 2020 11:55 amSomething else.

Code: Select all

$ cat /sys/class/graphics/fb0/device/uevent
DRIVER=vesa-framebuffer
MODALIAS=platform:vesa-framebuffer
I use Nvidia driver. If you want use "/sys/class/" I don't know if it's not better
What do you mean? If you use 'nvidia' why is this example showing 'vesa-framebuffer' but the 2nd one shows 'nvidia'?

What you could do, and again, what I'd probably do, is have another check at the end of my combined suggestion, to see if the resulting variable is actually an expected value, like 'amd', 'nvidia', 'nouveau', or whatever else, where anything else would result in an error.

If it turns out the first method isn't so reliable, you could switch around the conditions in the main if statement so that the lshw(1) method works first, using the sysfs method as a fallback.
I'm also Terminalforlife on GitHub.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] checking gpu manufacturer

Post by 1000 »

What do you mean? If you use 'nvidia' why is this example showing 'vesa-framebuffer'
By reading the link above,
Video card information in/sys/class/drm/It also contains the output interface supported by the graphics card, but only card+integer
Probably I have second Intel graphic card inside CPU.
Maybe it's the reason.
So, it means that some conditions should be added.
If not device Directory or device /uevent Nothing in pci No information usb The information is filtered and obtained as follows:
cat /sys/class/hwmon/hwmon2/device/uevent
So, for script maybe it is not faster but it is easier use command than check all files.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] checking gpu manufacturer

Post by 1000 »

From https://www.kernel.org/doc/html/latest/ ... uffer.html
I suspect that fb from /sys/class/graphics/fb0/device/uevent
this is "The frame buffer".

On a different linux system with nouveau driver
I found in

Code: Select all

$ dmesg | grep -i VGA
...
[    0.000000] fb0: VESA VGA frame buffer device
[    0.000000] fb: switching to nouveaufb from VESA VGA
...
On Linux Mint with nvidia driver

Code: Select all

$ dmesg | grep -i vga
...
[    0.466041] fb0: VESA VGA frame buffer device
...
https://en.wikipedia.org/wiki/Linux_framebuffer
The Linux framebuffer (fbdev) is a graphic hardware-independent abstraction layer to show graphics on a computer monitor, typically on the system console
I remember long time ago Ubuntu had problem with GRUB resolution ( maybe in Ubuntu 10.04 )
the problem was evident after installing the proprietary drivers,
because old VESA driver was used for GRUB.
Vesa driver has a resolution limitation and it is 2D driver.
Maybe it's the same.


In https://www.kernel.org/doc/html/latest/gpu/index.html
the word "drm" appears very often, but still I do not know / I am not sure
if it's the same as "/sys/class/drm" .

Edit
https://en.wikipedia.org/wiki/Direct_Rendering_Manager
Rootbeer
Level 3
Level 3
Posts: 115
Joined: Sun Nov 24, 2019 3:22 am

Re: [SOLVED] checking gpu manufacturer

Post by Rootbeer »

thanks to both of you!

Termy: many thanks for explaining in videoclip!
mpez0

Re: [SOLVED] checking gpu manufacturer

Post by mpez0 »

> gpudriver=`sudo lshw -c video |grep 'driver=' |awk '{ print $2}'`
Don't use grep | awk. Awk is perfectly capable of doing its own filtering of input, viz:

Code: Select all

... | awk '/driver=/ {print $2}'
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: [SOLVED] checking gpu manufacturer

Post by Termy »

mpez0 wrote: Sun Dec 13, 2020 3:59 pm > gpudriver=`sudo lshw -c video |grep 'driver=' |awk '{ print $2}'`
Don't use grep | awk. Awk is perfectly capable of doing its own filtering of input, viz:

Code: Select all

... | awk '/driver=/ {print $2}'
With that mindset, don't use AWK because BASH is more than capable of parsing data. :wink:
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”