From acf93f146d0666194aa3163cea5cd3ca120ff8e1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Nowak?= <luke@nexedi.com>
Date: Mon, 22 Nov 2021 11:26:38 +0100
Subject: [PATCH] kvm: Support exporter for disk-type=ide

kvm-exporter supported only disk-type=virtio, as qemuqmpclient.driveBackup
source_drive defaults to virtio0.

For disk-type=ide the source_drive found to be ide0-hd0 by query-block during
runtime, and as it's used, now such disk type is supported.
---
 software/kvm/buildout.hash.cfg              |  4 +--
 software/kvm/instance-kvm-export.cfg.jinja2 | 10 ++++++++
 software/kvm/template/kvm-export.sh.jinja2  |  2 +-
 software/kvm/test/test.py                   | 28 +++++++++++++++++++++
 4 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/software/kvm/buildout.hash.cfg b/software/kvm/buildout.hash.cfg
index 51ece6f17..740f8c34e 100644
--- a/software/kvm/buildout.hash.cfg
+++ b/software/kvm/buildout.hash.cfg
@@ -39,11 +39,11 @@ md5sum = cd0008f1689dfca9b77370bc4d275b70
 
 [template-kvm-export]
 filename = instance-kvm-export.cfg.jinja2
-md5sum = 4c9efdc9ef35d1096173084541be712d
+md5sum = 09252c282ef86f4bb3a88e91869b0f97
 
 [template-kvm-export-script]
 filename = template/kvm-export.sh.jinja2
-md5sum = b617d64de73de1eed518185f310bbc82
+md5sum = 64aa1ce8785f6b94aabd787fa3443082
 
 [template-nbd]
 filename = instance-nbd.cfg.jinja2
diff --git a/software/kvm/instance-kvm-export.cfg.jinja2 b/software/kvm/instance-kvm-export.cfg.jinja2
index 9160ab3a7..6236c77cb 100644
--- a/software/kvm/instance-kvm-export.cfg.jinja2
+++ b/software/kvm/instance-kvm-export.cfg.jinja2
@@ -24,10 +24,20 @@ rendered = ${directory:bin}/${slap-parameter:namebase}-exporter
 # Resilient stack wants a "wrapper" parameter
 wrapper = ${:rendered}
 mode = 0700
+{%- set disk_type = slapparameter_dict.get('disk-type', 'virtio') %}
+{%- if disk_type == "virtio" %}
+device = virtio0
+{%- elif disk_type == "ide" %}
+{#-   Manually found device name in case of disk-type == ide #}
+device = ide0-hd0
+{%- else %}
+# unsupported disk-type {{ disk_type }}
+{%- endif %}
 context =
     section directory directory
     section buildout buildout
     key socket_path kvm-instance:socket-path
+    key device :device
     raw gzip_binary {{ gzip_binary }}
 
 # Extends publish section with resilient parameters
diff --git a/software/kvm/template/kvm-export.sh.jinja2 b/software/kvm/template/kvm-export.sh.jinja2
index 6c0296bcf..c6891e31c 100644
--- a/software/kvm/template/kvm-export.sh.jinja2
+++ b/software/kvm/template/kvm-export.sh.jinja2
@@ -10,7 +10,7 @@ BACKUP_FILE=virtual.qcow2
 
 QMP_CLIENT={{ buildout['directory'] }}/software_release/bin/qemu-qmp-client
 
-$QMP_CLIENT --socket {{ socket_path }} --drive-backup $BACKUP_DIR/$BACKUP_FILE
+$QMP_CLIENT --socket {{ socket_path }} --drive-backup $BACKUP_DIR/$BACKUP_FILE {{ device }}
 
 # Due to the way qmp works, the VM file cannot be compressed on the fly.
 # Although the compression step is optional, the importer uses the .gz file
diff --git a/software/kvm/test/test.py b/software/kvm/test/test.py
index c1ad0b34a..537aaf899 100644
--- a/software/kvm/test/test.py
+++ b/software/kvm/test/test.py
@@ -573,6 +573,25 @@ class TestInstanceResilient(InstanceTestCase, KvmMixin):
   def getInstanceSoftwareType(cls):
     return 'kvm-resilient'
 
+  def test_kvm_exporter(self):
+    exporter_partition = os.path.join(
+      self.slap.instance_directory,
+      self.__partition_reference__ + '2')
+    backup_path = os.path.join(
+      exporter_partition, 'srv', 'backup', 'kvm', 'virtual.qcow2.gz')
+    exporter = os.path.join(exporter_partition, 'bin', 'exporter')
+    if os.path.exists(backup_path):
+      os.unlink(backup_path)
+
+    def call_exporter():
+      try:
+        return (0, subprocess.check_output(
+          [exporter], stderr=subprocess.STDOUT).decode('utf-8'))
+      except subprocess.CalledProcessError as e:
+        return (e.returncode, e.output.decode('utf-8'))
+    status_code, status_text = call_exporter()
+    self.assertEqual(0, status_code, status_text)
+
   def test(self):
     connection_parameter_dict = self\
       .computer_partition.getConnectionParameterDict()
@@ -646,6 +665,15 @@ ir3:sshd-on-watch RUNNING""",
     )
 
 
+@skipUnlessKvm
+class TestInstanceResilientDiskTypeIde(InstanceTestCase, KvmMixin):
+  @classmethod
+  def getInstanceParameterDict(cls):
+    return {
+      'disk-type': 'ide'
+    }
+
+
 @skipUnlessKvm
 class TestAccessResilientAdditional(InstanceTestCase):
   __partition_reference__ = 'ara'
-- 
2.30.9