Last active
July 25, 2024 11:12
-
-
Save Strykar/d02ded41b083b0d215f8da81d9f1f072 to your computer and use it in GitHub Desktop.
Python script to loop over the journal and reset the onboard Intel I225-V (rev 03) when it barfs with "igc: Failed to read reg 0xc030!"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import pyudev | |
import re | |
from systemd import journal | |
def get_pci_id(): | |
context = pyudev.Context() | |
pci_pattern = re.compile(r'^[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\.[0-9].*Ethernet controller: Intel Corporation.*I225-V.*rev 03$', re.IGNORECASE) | |
for device in context.list_devices(subsystem='pci'): | |
if 'ID_MODEL_FROM_DATABASE' in device: | |
device_info = f"{device.sys_name} {device['ID_MODEL_FROM_DATABASE']}" | |
if pci_pattern.search(device_info): | |
return device.sys_name | |
return None | |
def remove_and_rescan_pci_device(pci_id): | |
remove_path = f"/sys/bus/pci/devices/{pci_id}/remove" | |
rescan_path = f"/sys/bus/pci/rescan" | |
with open(remove_path, 'w') as f: | |
f.write('1') | |
with open(rescan_path, 'w') as f: | |
f.write('1') | |
def monitor_journal(): | |
j = journal.Reader() | |
j.this_boot() | |
j.log_level(journal.LOG_INFO) | |
j.add_match(_SYSTEMD_UNIT="network.target") | |
pci_id = None | |
while True: | |
j.seek_tail() | |
j.get_previous() | |
for entry in j: | |
message = entry['MESSAGE'] | |
if 'igc: Failed to read reg 0xc030!' in message: | |
if pci_id is None: | |
pci_id = get_pci_id() | |
if pci_id: | |
remove_and_rescan_pci_device(pci_id) | |
j.wait(1000000) | |
if __name__ == '__main__': | |
monitor_journal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment