Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How identify the primary video card on Linux programmatically?

Tags:

linux

xorg

bios

The primary video card usually can be set in the BIOS (option Primary VGA card), and it will be the first card used by the system.

My question is how can I programmatically identify (using a shell script and utilities is preferable) which of my two video cards is the primary card?

Edit: I was hoping that I wouldn't have to elaborate why I need this, because it is a bit complex, but I will explain the problem below.

I have a configuration wizard which allows the dynamic configuration of a multiseat system in a LiveCD, with two independent displays, keyboards and mice, my wizard works in this way:

  1. Start one Xorg server per video card (after generating xorg.conf with the correct PCI bus ID).
  2. Start a ui in each of Xorgs with messages for configuration (press key and press mouse). One seat can be configured each time.
  3. After the first seat is configured, the wizard closes the first Xorg and start the definitive Xorg in this seat with the desktop environment (already with the correct keyboard and mouse set).
  4. The second seat now is able to be configured (press key and mouse), after this pass 3 is repeated for seat two.

The problem is: If I start the first Xorg in the primary video card, everything works, but if I start the first Xorg in the secondary card this is what happens:

The passes 1, 2, and 3 works, but at the end of pass 3, when the Xorg of the first seat is closed, the Xorg of the second seat blinks and doesn't come back, just show a blank screen with a _ cursor at top, the desktop of the first seat loads, but I don't see the wizard screen in the second seat, the first Xorg just comes back if I execute a kill -HUP , and I need to start the ui from it again.

So, it is why I need to identify the primary video card before I can start Xorg (sorry I didn't mention this before). I tested this system in different computers, with different video cards and the behavior is the same. I also tested with the lasted packages of the kernel and Xorg in Ubuntu 12.04 (packages of the raring release).

like image 369
Juliao Avatar asked May 29 '26 16:05

Juliao


2 Answers

Assuming X11 is running, you could suppose that primary card is the one used by Xorg... then you might try

 ls -l /proc/$(pidof X)/fd |grep /dev/dri

on my system Debian/Sid/x86-64 with Linux 3.12 kernel (which has an Nvidia card on a Intel3770K which also has its VGA) I'm getting /dev/dri/card0 etc...

But you should explain really why you are asking and what problem do you want to solve.... Why does that matter to you?

I am not at all sure that Linux has a notion of primary graphic card like the BIOS knows it.

And probably hwinfo is telling you everything about your graphical cards.

like image 189
Basile Starynkevitch Avatar answered Jun 01 '26 08:06

Basile Starynkevitch


There are several command line tools in Linux that give you human readable information from the BIOS. Maybe you can find your Video boards in there and see which one is made primary. From what I see in my output, it does not look like something says "this is the primary video", but I do see quite a lot of information. You could output that information to a file when video card A is primary, again when B is primary, then compare those two files and see whether there is a difference.

The command I used, which gives me a lot of information, is dmidecode:

sudo dmidecode | less

If you look at the manual page:

man dmidecode

You will notice that the programmers offer a few other similar tools such as biosdecode and vpddecode.

From those you learn that the BIOS information is available from the /dev/mem device. Although you need to be root to read it, if you know the address (I don't) then you could go in there and peek and poke as required to find out where that one information of which video card is defined as the primary video card.

Running dmidecode, there are some details about my motherboard:

Handle 0x0002, DMI type 2, 15 bytes
Base Board Information
  Manufacturer: Supermicro
  Product Name: X9SCI/X9SCA
  Version: 1.01
  Serial Number: ZM25U44192
  Asset Tag: To be filled by O.E.M.
  Features:
    Board is a hosting board
    Board is replaceable
  Location In Chassis: To be filled by O.E.M.
  Chassis Handle: 0x0003
  Type: Motherboard
  Contained Object Handles: 0

Here I have one video entry:

Handle 0x000E, DMI type 10, 6 bytes
On Board Device Information
  Type: Video
  Status: Enabled
  Description:    To Be Filled By O.E.M.

Then the other entry looks like this:

Handle 0x0036, DMI type 41, 11 bytes
Onboard Device
   Reference Designation:  Onboard IGD
   Type: Video
   Status: Enabled
   Type Instance: 1
   Bus Address: 0000:00:02.0

It could also be something you need to read from the Flash memory your BIOS uses. This is done with flashrom (that you may need to install):

sudo flashrom --programmer internal --read my-flash.bin

In my case the ROM on my computer is 2Gb of data. So quite large. However, you can be sure that the information you are looking for exists within that data block since that is the only mean for the BIOS to save data that will stay around when the computer gets turned off.

like image 31
Alexis Wilke Avatar answered Jun 01 '26 09:06

Alexis Wilke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!