Scan sysfs for NVIDIA GPUs eligible for VFIO passthrough.
(sysfs: &SysfsRoot)
| 25 | |
| 26 | /// Scan sysfs for NVIDIA GPUs eligible for VFIO passthrough. |
| 27 | pub fn probe_host_nvidia_vfio_readiness(sysfs: &SysfsRoot) -> Vec<PciInfo> { |
| 28 | let devices_dir = sysfs.pci_devices_dir(); |
| 29 | let entries = match fs::read_dir(&devices_dir) { |
| 30 | Ok(e) => e, |
| 31 | Err(err) => { |
| 32 | tracing::warn!(path = %devices_dir.display(), %err, "cannot read PCI devices directory"); |
| 33 | return Vec::new(); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | let mut gpus = Vec::new(); |
| 38 | |
| 39 | for entry in entries.filter_map(Result::ok) { |
| 40 | let bdf = entry.file_name().to_string_lossy().into_owned(); |
| 41 | let device = sysfs.pci_device_ref(&bdf); |
| 42 | |
| 43 | let Ok(vendor) = device.vendor() else { |
| 44 | continue; |
| 45 | }; |
| 46 | if vendor != NVIDIA_VENDOR_ID { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | let Ok(class) = device.class() else { |
| 51 | continue; |
| 52 | }; |
| 53 | if !is_gpu_class(&class) { |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | let device_id = device.device_id().unwrap_or_default(); |
| 58 | |
| 59 | let name = device |
| 60 | .read_trimmed("label") |
| 61 | .unwrap_or_else(|_| format!("NVIDIA {device_id}")); |
| 62 | |
| 63 | let Ok(iommu_group) = sysfs.iommu_group(&bdf) else { |
| 64 | continue; |
| 65 | }; |
| 66 | |
| 67 | gpus.push(PciInfo { |
| 68 | bdf, |
| 69 | name, |
| 70 | vendor, |
| 71 | device: device_id, |
| 72 | iommu_group, |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | gpus |
| 77 | } |
| 78 | |
| 79 | /// Bind a GPU to `vfio-pci`, returning an RAII guard that restores it on drop. |
| 80 | /// |