diff --git a/roles/common/defaults/main.yaml b/roles/common/defaults/main.yaml index 385eb64..f866a29 100644 --- a/roles/common/defaults/main.yaml +++ b/roles/common/defaults/main.yaml @@ -1,3 +1,10 @@ --- common_journal_retention_days: 21 common_journal_max_disk_limit: "5%" + +gpu_unbind_devices: [] +# Example: +# gpu_unbind_devices: +# - pci_id: "0000:06:00.0" +# driver: "amdgpu" +# label: "amd_igpu_cezanne" diff --git a/roles/common/handlers/main.yaml b/roles/common/handlers/main.yaml index 6665708..5bdd33e 100644 --- a/roles/common/handlers/main.yaml +++ b/roles/common/handlers/main.yaml @@ -3,3 +3,7 @@ ansible.builtin.service: name: ssh state: restarted + +- name: Reload systemd + ansible.builtin.systemd: + daemon_reload: true diff --git a/roles/common/tasks/optimization.yaml b/roles/common/tasks/optimization.yaml index 43c5440..fa15cdf 100644 --- a/roles/common/tasks/optimization.yaml +++ b/roles/common/tasks/optimization.yaml @@ -23,3 +23,22 @@ - { user: "*", type: "soft", limit: "65536" } - { user: "*", type: "hard", limit: "65536" } tags: limits + +- name: Deploy GPU unbind systemd units + become: true + ansible.builtin.template: + src: gpu-unbind.service.j2 + dest: /etc/systemd/system/gpu-unbind-{{ item.label }}.service + mode: "0644" + loop: "{{ gpu_unbind_devices }}" + notify: Reload systemd + tags: optimization + +- name: Enable and start GPU unbind servie + become: true + ansible.builtin.systemd: + name: gpu-unbind-{{ item.label }} + enabled: true + state: started + loop: "{{ gpu_unbind_devices }}" + tags: optimization diff --git a/roles/common/templates/gpu-unbind.service.j2 b/roles/common/templates/gpu-unbind.service.j2 new file mode 100644 index 0000000..eda4287 --- /dev/null +++ b/roles/common/templates/gpu-unbind.service.j2 @@ -0,0 +1,18 @@ +[Unit] +Description=Unbind {{ item.label }} ({{ item.pci_id }}) and enable runtime PM +After=multi-user.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/sh -c '\ + if [ -e /sys/bus/pci/drivers/{{ item.driver }}/{{ item.pci_id }} ]; then \ + echo "{{ item.pci_id }}" > /sys/bus/pci/drivers/{{ item.driver }}/unbind; \ + fi && \ + echo auto > /sys/bus/pci/devices/{{ item.pci_id }}/power/control' +ExecStop=/bin/sh -c '\ + echo on > /sys/bus/pci/devices/{{ item.pci_id }}/power/control && \ + echo "{{ item.pci_id }}" > /sys/bus/pci/drivers/{{ item.driver }}/bind' + +[Install] +WantedBy=multi-user.target