Skip to contentSkip to conversation

tst_virt: Add fallback KVM detection via virtio block devices

PATCH In review Darren Chang via ltp 2 participants 2 messages May 2 – May 6 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tst_virt: Add fallback KVM detection via virtio block devices

Currently, is_kvm() relies on reading /proc/cpuinfo to find the "QEMU Virtual CPU" string.

This patch introduces a fallback mechanism to detect KVM environments by checking for the existence of standard virtio block devices when the CPU info check fails. Specifically, it probes for:
- /sys/block/vda (Reliable sysfs kernel interface)
- /dev/vda (Standard Linux device node)
- /dev/block/vda (Android's ueventd device node)

Including "/dev/block/vda" ensures that the detection works seamlessly within Android emulator environments (such as Cuttlefish), which use ueventd and place block devices under /dev/block/ instead of the standard Linux /dev/.

---
 lib/tst_virt.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
lib/tst_virt.c +13 -0
index 109d7a853..ac0aef665 100644
@@ -44,6 +44,19 @@ static int is_kvm(void)
 	}
 
 	SAFE_FCLOSE(NULL, cpuinfo);
+
+	/* * Fallback check for KVM:
+	 * Android's ueventd creates block devices in /dev/block/
+	 * instead of the standard Linux /dev/, so we check both.
+	 */
+	if (!found) {
+		if (access("/dev/vda", F_OK) == 0 ||
+			access("/sys/block/vda", F_OK) == 0 ||
+			access("/dev/block/vda", F_OK) == 0) {
+			found = 1;
+		}
+	}
+
 	return found;
 }
 
Signature · 5 lines
2.54.0.545.g6539524ca2-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Darren,

lib/tst_virt.c static int is_kvm(void) · line 51

Quoted 6 lines · Darren Chang

tst_virt: Add fallback KVM detection via virtio block devices

lib/tst_virt.c +4 -0
+       /* * Fallback check for KVM:
+        * Android's ueventd creates block devices in /dev/block/
+        * instead of the standard Linux /dev/, so we check both.
+        */

Malformed comment block. The /* opener must be on its own line with no text. Use:

	/*
	 * Fallback check for KVM:
	 * ...
	 */

lib/tst_virt.c static int is_kvm(void) · line 58

Quoted 7 lines
lib/tst_virt.c +7 -0
+       if (!found) {
+               if (access("/dev/vda", F_OK) == 0 ||
+                       access("/sys/block/vda", F_OK) == 0 ||
+                       access("/dev/block/vda", F_OK) == 0) {
+                       found = 1;
+               }
+       }

Virtio block devices are not KVM-exclusive. Hyper-V and VMware guests with virtio drivers also expose /dev/vda, so this will produce false positives. Either narrow the detection (e.g. check /sys/class/dmi/id/product_name for QEMU/KVM) or explicitly document this as a best-effort heuristic.

---
Note:

Our agent completed the review of the patch. The full review can be found at: (not available)

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp