Skip to content
Snippets Groups Projects
Commit 9a18892a authored by Chris Hines's avatar Chris Hines Committed by Andreas Hamacher
Browse files

Xconf gen

parent 4eaf5f78
No related branches found
No related tags found
1 merge request!530Xconf gen
#!/bin/python -E
from jinja2 import Template, Environment, FileSystemLoader
import itertools
import subprocess
import datetime
import os
import sys
import time
import socket
from subprocess import call
import re
import json
def grab_card_ids():
# This method runs nvidia-smi to grab the card ids, then returns a list
if not os.path.isfile("/bin/nvidia-smi"):
print("nvidia-smi binary not found!")
exit(1)
cmd = ["/bin/nvidia-smi", "--query-gpu=pci.bus_id","--format=csv,noheader"]
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
cards = []
for line in p.stdout.readlines():
line = line.rstrip().split(":")[2]
pcibus_num = int(re.sub('[.:]', '', line).rstrip("0"),16)
card = "PCI:0:{}:0".format(str(pcibus_num))
cards.append(card)
return cards
def grab_card_boardname():
if not os.path.isfile("/bin/nvidia-smi"):
print("nvidia-smi binary not found!")
exit(1)
cmd = ["/bin/nvidia-smi", "--query-gpu=name","--format=csv,noheader"]
cards = []
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
line = line.rstrip()
cards.append(line)
return cards
def write_xorg_conf(cards):
num_of_cards = len(cards) + 1
boardname = (grab_card_boardname())[0]
gpus = []
file_loader = FileSystemLoader('/opt/generate-xorg/template')
env = Environment(loader=file_loader)
template = env.get_template('xorg.conf.j2')
for i in range(1, num_of_cards):
monitors = []
screens = []
res = list(itertools.combinations(cards,i))
for j in range(i):
monitors.append("Monitor"+str(j))
screens.append("Screen"+str(j))
for card in res:
filename = "/etc/X11/xorg.conf." + str(i) + str(res.index(card))
template.stream({'boardname':boardname,'monitors':monitors,'screens':screens,'devices':card}).dump(filename)
if __name__ == "__main__":
cards = grab_card_ids()
write_xorg_conf(cards)
# nvidia-xconfig: X configuration file generated by nvidia-xconfig
# nvidia-xconfig: version 375.66 (buildmeister@swio-display-x86-rhel47-06) Mon May 1 15:45:32 PDT 2017
Section "DRI"
Mode 0666
EndSection
Section "ServerLayout"
#InputDevice "Keyboard0" "CoreKeyboard"
#InputDevice "Mouse0" "CorePointer"
Identifier "Layout0"
{% for screen in screens %}
{% if screens.index(screen) == 0 %}
Screen 0 "Screen{{screens.index(screen)}}"
{% else %}
Screen {{screens.index(screen)}} "Screen{{screens.index(screen)}}" RightOf "Screen{{screens.index(screen)-1}}"
{% endif %}
{% endfor %}
#InputDevice "Keyboard0" "CoreKeyboard"
#InputDevice "Mouse0" "CorePointer"
EndSection
Section "Files"
FontPath "/usr/share/fonts/default/Type1"
EndSection
Section "InputDevice"
# generated from default
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/input/mice"
Option "Emulate3Buttons" "no"
Option "ZAxisMapping" "4 5"
EndSection
Section "InputDevice"
# generated from default
Identifier "Keyboard0"
Driver "kbd"
EndSection
{% for monitor in monitors %}
Section "Monitor"
Identifier "{{monitor}}"
VendorName "Unknown"
ModelName "Unknown"
HorizSync 28.0 - 33.0
VertRefresh 43.0 - 72.0
Option "DPMS"
EndSection
{% endfor %}
{% for device in devices %}
Section "Device"
Identifier "Device{{devices.index(device)}}"
Driver "nvidia"
VendorName "NVIDIA Corporation"
boardname "{{boardname}}"
BusID "{{device}}"
EndSection
{% endfor %}
{% for screen in screens %}
Section "Screen"
Identifier "Screen{{screens.index(screen)}}"
Device "Device{{screens.index(screen)}}"
Monitor "Monitor{{screens.index(screen)}}"
DefaultDepth 24
Option "ProbeAllGpus" "false"
{% if boardname == 'GRID K1' %}
Option "UseDisplayDevice" "None"
{% endif %}
SubSection "Display"
Virtual 1920 1200
Depth 24
EndSubSection
EndSection
{% endfor -%}
---
- name: create /opt/generate-xorg and template dirs
become: yes
file:
path: /opt/generate-xorg/template
state: directory
mode: '0755'
- name: copy nvidia-xconf-gen.py
become: yes
copy:
src: nvidia-xconf-gen.py
dest: /opt/generate-xorg/nvidia-xconf-gen.py
owner: root
mode: '0755'
- name: copy xorg.conf.j2 template
become: yes
copy:
src: xorg.conf.j2
dest: /opt/generate-xorg/template/xorg.conf.j2
owner: root
mode: '0644'
- name: Creates ansible-generate-xorg file under /etc/cron.d
become: yes
cron:
name: cron job to generate xorg after reboot
special_time: reboot
user: root
job: "/opt/generate-xorg/nvidia-xconf-gen.py"
cron_file: ansible-generate-xorg
......@@ -11,44 +11,34 @@ from subprocess import call
import re
import json
def check_nvidia_smi():
try:
smi = subprocess.check_output(["which","nvidia-smi"])
except subprocess.CalledProcessError:
print("nvidia-smi binary not found!")
exit(1)
def grab_card_ids():
# This method runs nvidia-smi to grab the card ids, then returns a list
check_nvidia_smi()
cmd = ["nvidia-smi", "--query-gpu=pci.bus_id","--format=csv,noheader"]
if not os.path.isfile("/bin/nvidia-smi"):
print("nvidia-smi binary not found!")
exit(1)
cmd = ["/bin/nvidia-smi", "--query-gpu=pci.bus_id","--format=csv,noheader"]
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
cards = []
for line in p.stdout.readlines():
stripped_line = line.rstrip().split(":")[2]
#check for different format of pcibus_id. This happens on baremetals
# i.e. 00000000:06:00.0 not 00000000:00:06.0
pcibus_id = re.sub('[.:]', '', stripped_line).rstrip("0")
if not pcibus_id: # empty string, try the other way
stripped_line = line.rstrip().split(":")[1]
pcibus_id = re.sub('[.:]', '', stripped_line).rstrip("0")
if not pcibus_id:
print("Error in grab_card_ids: we can not parse the line {}".format(line))
print("Command that generated it is: {}".format(cmd))
system.exit(1)
pcibus_num = int(pcibus_id,16)
card = "PCI:0:{}:0".format(str(pcibus_num))
bus_id_parts = line.rstrip().split(":")
bus = int(bus_id_parts[1],16)
dev_func = bus_id_parts[2].split(".")
device = int(dev_func[0],16)
function = int(dev_func[1],16)
card = "PCI:{}:{}:{}".format(str(bus),str(device),str(function))
cards.append(card)
return cards
def grab_card_boardname():
check_nvidia_smi()
if not os.path.isfile("/bin/nvidia-smi"):
print("nvidia-smi binary not found!")
exit(1)
cmd = ["nvidia-smi", "--query-gpu=name","--format=csv,noheader"]
cmd = ["/bin/nvidia-smi", "--query-gpu=name","--format=csv,noheader"]
cards = []
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
......@@ -76,12 +66,10 @@ def write_xorg_conf(cards):
for card in res:
filename = "/etc/X11/xorg.conf." + str(i) + str(res.index(card))
template.stream({'boardname':boardname,'monitors':monitors,'screens':screens,'devices':card}).dump(filename)
gpus.append({'filename':filename,'boardname':boardname,'monitors':monitors,'screens':screens,'devices':card})
print(json.dumps(gpus))
if __name__ == "__main__":
cards = grab_card_ids()
write_xorg_conf(cards)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment