neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone
Blob Blame History Raw
From 1d9dc59ab2c471d7dcc39cd6982bd14380d5f726 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Thu, 13 Jun 2019 11:22:16 -0400
Subject: [PATCH 1/3] Add a function to detect if running in a vm.

Related: rhbz#1676935
---
 blivet/util.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/blivet/util.py b/blivet/util.py
index 542bc93f..fa5e9e35 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -1,4 +1,5 @@
 import copy
+from distutils.spawn import find_executable
 import functools
 import glob
 import itertools
@@ -1100,3 +1101,16 @@ def decorated(*args, **kwargs):
                     return None
             return decorated
         return decorator
+
+
+def detect_virt():
+    """ Return True if we are running in a virtual machine. """
+    in_vm = False
+    detect_virt_prog = find_executable('systemd-detect-virt')
+    if detect_virt_prog:
+        try:
+            in_vm = run_program([detect_virt_prog, "--vm"]) == 0
+        except OSError:
+            pass
+
+    return in_vm

From 26d4b48ab5eca44695dced52c6170ec04610bc1d Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Thu, 13 Jun 2019 10:57:48 -0400
Subject: [PATCH 2/3] Use dasd disklabel for vm disks backed by dasds.

Resolves: rhbz#1676935
---
 blivet/formats/disklabel.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/blivet/formats/disklabel.py b/blivet/formats/disklabel.py
index 8186d1a1..0c4fce35 100644
--- a/blivet/formats/disklabel.py
+++ b/blivet/formats/disklabel.py
@@ -261,6 +261,15 @@ def _get_best_label_type(self):
             elif self.parted_device.type == parted.DEVICE_DASD:
                 # the device is DASD
                 return "dasd"
+            elif util.detect_virt():
+                # check for dasds exported into qemu as normal virtio/scsi disks
+                try:
+                    _parted_disk = parted.Disk(device=self.parted_device)
+                except (_ped.DiskLabelException, _ped.IOException, NotImplementedError):
+                    pass
+                else:
+                    if _parted_disk.type == "dasd":
+                        return "dasd"
 
         for lt in label_types:
             if self._label_type_size_check(lt):

From c93d1207bb2942736a390bd58adafda3deb1c25c Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Thu, 13 Jun 2019 12:04:23 -0400
Subject: [PATCH 3/3] Use DBus call to see if we're in a vm.

---
 blivet/util.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/blivet/util.py b/blivet/util.py
index fa5e9e35..2932e8b5 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -1,5 +1,4 @@
 import copy
-from distutils.spawn import find_executable
 import functools
 import glob
 import itertools
@@ -20,6 +19,7 @@
 from enum import Enum
 
 from .errors import DependencyError
+from . import safe_dbus
 
 import gi
 gi.require_version("BlockDev", "2.0")
@@ -39,6 +39,12 @@
 program_log_lock = Lock()
 
 
+SYSTEMD_SERVICE = "org.freedesktop.systemd1"
+SYSTEMD_MANAGER_PATH = "/org/freedesktop/systemd1/Manager"
+SYSTEMD_MANAGER_IFACE = "org.freedesktop.systemd1.Manager"
+VIRT_PROP_NAME = "Virtualization"
+
+
 class Path(str):
 
     """ Path(path, root=None) provides a filesystem path object, which
@@ -1105,12 +1111,10 @@ def decorated(*args, **kwargs):
 
 def detect_virt():
     """ Return True if we are running in a virtual machine. """
-    in_vm = False
-    detect_virt_prog = find_executable('systemd-detect-virt')
-    if detect_virt_prog:
-        try:
-            in_vm = run_program([detect_virt_prog, "--vm"]) == 0
-        except OSError:
-            pass
+    try:
+        vm = safe_dbus.get_property_sync(SYSTEMD_SERVICE, SYSTEMD_MANAGER_PATH,
+                                         SYSTEMD_MANAGER_IFACE, VIRT_PROP_NAME)
+    except (safe_dbus.DBusCallError, safe_dbus.DBusPropertyError):
+        vm = None
 
-    return in_vm
+    return vm in ('qemu', 'kvm')