neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone

Blame SOURCES/0019-Use-dasd-disklabel-for-vm-disks-backed-by-dasds.patch

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