3187bc
From 1d9dc59ab2c471d7dcc39cd6982bd14380d5f726 Mon Sep 17 00:00:00 2001
3187bc
From: David Lehman <dlehman@redhat.com>
3187bc
Date: Thu, 13 Jun 2019 11:22:16 -0400
3187bc
Subject: [PATCH 1/3] Add a function to detect if running in a vm.
3187bc
3187bc
Related: rhbz#1676935
3187bc
---
3187bc
 blivet/util.py | 14 ++++++++++++++
3187bc
 1 file changed, 14 insertions(+)
3187bc
3187bc
diff --git a/blivet/util.py b/blivet/util.py
3187bc
index 542bc93f..fa5e9e35 100644
3187bc
--- a/blivet/util.py
3187bc
+++ b/blivet/util.py
3187bc
@@ -1,4 +1,5 @@
3187bc
 import copy
3187bc
+from distutils.spawn import find_executable
3187bc
 import functools
3187bc
 import glob
3187bc
 import itertools
3187bc
@@ -1100,3 +1101,16 @@ def decorated(*args, **kwargs):
3187bc
                     return None
3187bc
             return decorated
3187bc
         return decorator
3187bc
+
3187bc
+
3187bc
+def detect_virt():
3187bc
+    """ Return True if we are running in a virtual machine. """
3187bc
+    in_vm = False
3187bc
+    detect_virt_prog = find_executable('systemd-detect-virt')
3187bc
+    if detect_virt_prog:
3187bc
+        try:
3187bc
+            in_vm = run_program([detect_virt_prog, "--vm"]) == 0
3187bc
+        except OSError:
3187bc
+            pass
3187bc
+
3187bc
+    return in_vm
3187bc
3187bc
From 26d4b48ab5eca44695dced52c6170ec04610bc1d Mon Sep 17 00:00:00 2001
3187bc
From: David Lehman <dlehman@redhat.com>
3187bc
Date: Thu, 13 Jun 2019 10:57:48 -0400
3187bc
Subject: [PATCH 2/3] Use dasd disklabel for vm disks backed by dasds.
3187bc
3187bc
Resolves: rhbz#1676935
3187bc
---
3187bc
 blivet/formats/disklabel.py | 9 +++++++++
3187bc
 1 file changed, 9 insertions(+)
3187bc
3187bc
diff --git a/blivet/formats/disklabel.py b/blivet/formats/disklabel.py
3187bc
index 8186d1a1..0c4fce35 100644
3187bc
--- a/blivet/formats/disklabel.py
3187bc
+++ b/blivet/formats/disklabel.py
3187bc
@@ -261,6 +261,15 @@ def _get_best_label_type(self):
3187bc
             elif self.parted_device.type == parted.DEVICE_DASD:
3187bc
                 # the device is DASD
3187bc
                 return "dasd"
3187bc
+            elif util.detect_virt():
3187bc
+                # check for dasds exported into qemu as normal virtio/scsi disks
3187bc
+                try:
3187bc
+                    _parted_disk = parted.Disk(device=self.parted_device)
3187bc
+                except (_ped.DiskLabelException, _ped.IOException, NotImplementedError):
3187bc
+                    pass
3187bc
+                else:
3187bc
+                    if _parted_disk.type == "dasd":
3187bc
+                        return "dasd"
3187bc
 
3187bc
         for lt in label_types:
3187bc
             if self._label_type_size_check(lt):
3187bc
3187bc
From c93d1207bb2942736a390bd58adafda3deb1c25c Mon Sep 17 00:00:00 2001
3187bc
From: David Lehman <dlehman@redhat.com>
3187bc
Date: Thu, 13 Jun 2019 12:04:23 -0400
3187bc
Subject: [PATCH 3/3] Use DBus call to see if we're in a vm.
3187bc
3187bc
---
3187bc
 blivet/util.py | 22 +++++++++++++---------
3187bc
 1 file changed, 13 insertions(+), 9 deletions(-)
3187bc
3187bc
diff --git a/blivet/util.py b/blivet/util.py
3187bc
index fa5e9e35..2932e8b5 100644
3187bc
--- a/blivet/util.py
3187bc
+++ b/blivet/util.py
3187bc
@@ -1,5 +1,4 @@
3187bc
 import copy
3187bc
-from distutils.spawn import find_executable
3187bc
 import functools
3187bc
 import glob
3187bc
 import itertools
3187bc
@@ -20,6 +19,7 @@
3187bc
 from enum import Enum
3187bc
 
3187bc
 from .errors import DependencyError
3187bc
+from . import safe_dbus
3187bc
 
3187bc
 import gi
3187bc
 gi.require_version("BlockDev", "2.0")
3187bc
@@ -39,6 +39,12 @@
3187bc
 program_log_lock = Lock()
3187bc
 
3187bc
 
3187bc
+SYSTEMD_SERVICE = "org.freedesktop.systemd1"
3187bc
+SYSTEMD_MANAGER_PATH = "/org/freedesktop/systemd1/Manager"
3187bc
+SYSTEMD_MANAGER_IFACE = "org.freedesktop.systemd1.Manager"
3187bc
+VIRT_PROP_NAME = "Virtualization"
3187bc
+
3187bc
+
3187bc
 class Path(str):
3187bc
 
3187bc
     """ Path(path, root=None) provides a filesystem path object, which
3187bc
@@ -1105,12 +1111,10 @@ def decorated(*args, **kwargs):
3187bc
 
3187bc
 def detect_virt():
3187bc
     """ Return True if we are running in a virtual machine. """
3187bc
-    in_vm = False
3187bc
-    detect_virt_prog = find_executable('systemd-detect-virt')
3187bc
-    if detect_virt_prog:
3187bc
-        try:
3187bc
-            in_vm = run_program([detect_virt_prog, "--vm"]) == 0
3187bc
-        except OSError:
3187bc
-            pass
3187bc
+    try:
3187bc
+        vm = safe_dbus.get_property_sync(SYSTEMD_SERVICE, SYSTEMD_MANAGER_PATH,
3187bc
+                                         SYSTEMD_MANAGER_IFACE, VIRT_PROP_NAME)
3187bc
+    except (safe_dbus.DBusCallError, safe_dbus.DBusPropertyError):
3187bc
+        vm = None
3187bc
 
3187bc
-    return in_vm
3187bc
+    return vm in ('qemu', 'kvm')