]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Use os.access() in qemu support checks
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 13 Oct 2023 11:13:41 +0000 (13:13 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 13 Oct 2023 11:32:26 +0000 (13:32 +0200)
mkosi/util.py

index 61ea4210f38a1b5dea9b143bdc1bb0b39c0100b2..dec893e1e98e2af8d1f3d4c42554364404c19fb7 100644 (file)
@@ -4,7 +4,6 @@ import ast
 import contextlib
 import copy
 import enum
-import errno
 import fcntl
 import functools
 import importlib
@@ -104,40 +103,33 @@ def chdir(directory: PathString) -> Iterator[None]:
 
 def qemu_check_kvm_support(log: bool) -> bool:
     # some CI runners may present a non-working KVM device
-    try:
-        os.close(os.open("/dev/kvm", os.O_RDWR|os.O_CLOEXEC))
-    except OSError as e:
-        if e.errno == errno.ENOENT:
-            if log:
-                logging.warning("/dev/kvm not found. Not using KVM acceleration.")
-            return False
-        elif e.errno in (errno.EPERM, errno.EACCES):
-            if log:
-                logging.warning("Permission denied to access /dev/kvm. Not using KVM acceleration")
-            return False
-
-        raise e
+
+    if not os.access("/dev/kvm", os.F_OK):
+        if log:
+            logging.warning("/dev/kvm not found. Not using KVM acceleration.")
+        return False
+
+    if not os.access("/dev/kvm", os.R_OK|os.W_OK):
+        if log:
+            logging.warning("Permission denied to access /dev/kvm. Not using KVM acceleration")
+        return False
 
     return True
 
 
 def qemu_check_vsock_support(log: bool) -> bool:
-    try:
-        os.close(os.open("/dev/vhost-vsock", os.O_RDWR|os.O_CLOEXEC))
-    except OSError as e:
-        if e.errno == errno.ENOENT:
-            if log:
-                logging.warning("/dev/vhost-vsock not found. Not adding a vsock device to the virtual machine.")
-            return False
-        elif e.errno in (errno.EPERM, errno.EACCES):
-            if log:
-                logging.warning(
-                    "Permission denied to access /dev/vhost-vsock. "
-                    "Not adding a vsock device to the virtual machine."
-                )
-            return False
-
-        raise e
+    if not os.access("/dev/vhost-vsock", os.F_OK):
+        if log:
+            logging.warning("/dev/vhost-vsock not found. Not adding a vsock device to the virtual machine.")
+        return False
+
+    if not os.access("/dev/vhost-vsock", os.R_OK|os.W_OK):
+        if log:
+            logging.warning(
+                "Permission denied to access /dev/vhost-vsock. "
+                "Not adding a vsock device to the virtual machine."
+            )
+        return False
 
     return True