From 9433923ee619e8235a67047fa8ee3762d4d7ab46 Mon Sep 17 00:00:00 2001
From: Alexander Wels <awels@redhat.com>
Date: Fri, 8 Jul 2022 07:13:59 -0500
Subject: [PATCH] Set cache mode on hotplugged disks.

Added code to set the proper cache mode on hotplugged disks
right now they are not set and the recommended setting is
cache=none. This uses the same mechanism as regular disks.
Modified attach/detech to update both config and live libvirt.

Signed-off-by: Alexander Wels <awels@redhat.com>
---
 pkg/virt-launcher/virtwrap/manager.go      | 23 ++++++++++++++++------
 pkg/virt-launcher/virtwrap/manager_test.go |  4 ++--
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/pkg/virt-launcher/virtwrap/manager.go b/pkg/virt-launcher/virtwrap/manager.go
index dee0ea944..b2994d966 100644
--- a/pkg/virt-launcher/virtwrap/manager.go
+++ b/pkg/virt-launcher/virtwrap/manager.go
@@ -83,10 +83,11 @@ import (
 )
 
 const (
-	failedSyncGuestTime    = "failed to sync guest time"
-	failedGetDomain        = "Getting the domain failed."
-	failedGetDomainState   = "Getting the domain state failed."
-	failedDomainMemoryDump = "Domain memory dump failed"
+	failedSyncGuestTime             = "failed to sync guest time"
+	failedGetDomain                 = "Getting the domain failed."
+	failedGetDomainState            = "Getting the domain state failed."
+	failedDomainMemoryDump          = "Domain memory dump failed"
+	affectLiveAndConfigLibvirtFlags = libvirt.DOMAIN_DEVICE_MODIFY_LIVE | libvirt.DOMAIN_DEVICE_MODIFY_CONFIG
 )
 
 const (
@@ -923,7 +924,7 @@ func (l *LibvirtDomainManager) SyncVMI(vmi *v1.VirtualMachineInstance, allowEmul
 			logger.Reason(err).Error("marshalling detached disk failed")
 			return nil, err
 		}
-		err = dom.DetachDevice(strings.ToLower(string(detachBytes)))
+		err = dom.DetachDeviceFlags(strings.ToLower(string(detachBytes)), affectLiveAndConfigLibvirtFlags)
 		if err != nil {
 			logger.Reason(err).Error("detaching device")
 			return nil, err
@@ -939,12 +940,22 @@ func (l *LibvirtDomainManager) SyncVMI(vmi *v1.VirtualMachineInstance, allowEmul
 			continue
 		}
 		logger.V(1).Infof("Attaching disk %s, target %s", attachDisk.Alias.GetName(), attachDisk.Target.Device)
+		// set drivers cache mode
+		err = converter.SetDriverCacheMode(&attachDisk, l.directIOChecker)
+		if err != nil {
+			return nil, err
+		}
+		err = converter.SetOptimalIOMode(&attachDisk)
+		if err != nil {
+			return nil, err
+		}
+
 		attachBytes, err := xml.Marshal(attachDisk)
 		if err != nil {
 			logger.Reason(err).Error("marshalling attached disk failed")
 			return nil, err
 		}
-		err = dom.AttachDevice(strings.ToLower(string(attachBytes)))
+		err = dom.AttachDeviceFlags(strings.ToLower(string(attachBytes)), affectLiveAndConfigLibvirtFlags)
 		if err != nil {
 			logger.Reason(err).Error("attaching device")
 			return nil, err
diff --git a/pkg/virt-launcher/virtwrap/manager_test.go b/pkg/virt-launcher/virtwrap/manager_test.go
index afd666c53..b0f193961 100644
--- a/pkg/virt-launcher/virtwrap/manager_test.go
+++ b/pkg/virt-launcher/virtwrap/manager_test.go
@@ -743,7 +743,7 @@ var _ = Describe("Manager", func() {
 			mockConn.EXPECT().DomainDefineXML(string(xmlDomain)).Return(mockDomain, nil)
 			mockDomain.EXPECT().GetState().Return(libvirt.DOMAIN_SHUTDOWN, 1, nil)
 			mockDomain.EXPECT().CreateWithFlags(libvirt.DOMAIN_NONE).Return(nil)
-			mockDomain.EXPECT().AttachDevice(strings.ToLower(string(attachBytes)))
+			mockDomain.EXPECT().AttachDeviceFlags(strings.ToLower(string(attachBytes)), affectLiveAndConfigLibvirtFlags)
 			mockDomain.EXPECT().GetXMLDesc(libvirt.DomainXMLFlags(0)).MaxTimes(2).Return(string(xmlDomain2), nil)
 			manager, _ := newLibvirtDomainManager(mockConn, testVirtShareDir, testEphemeralDiskDir, nil, "/usr/share/OVMF", ephemeralDiskCreatorMock, mockDirectIOChecker)
 			newspec, err := manager.SyncVMI(vmi, true, &cmdv1.VirtualMachineOptions{VirtualMachineSMBios: &cmdv1.SMBios{}})
@@ -853,7 +853,7 @@ var _ = Describe("Manager", func() {
 			mockConn.EXPECT().DomainDefineXML(gomock.Any()).Return(mockDomain, nil)
 			mockDomain.EXPECT().GetState().Return(libvirt.DOMAIN_SHUTDOWN, 1, nil)
 			mockDomain.EXPECT().CreateWithFlags(libvirt.DOMAIN_NONE).Return(nil)
-			mockDomain.EXPECT().DetachDevice(strings.ToLower(string(detachBytes)))
+			mockDomain.EXPECT().DetachDeviceFlags(strings.ToLower(string(detachBytes)), affectLiveAndConfigLibvirtFlags)
 			mockDomain.EXPECT().GetXMLDesc(libvirt.DomainXMLFlags(0)).MaxTimes(2).Return(string(xmlDomain), nil)
 			manager, _ := newLibvirtDomainManager(mockConn, testVirtShareDir, testEphemeralDiskDir, nil, "/usr/share/OVMF", ephemeralDiskCreatorMock, mockDirectIOChecker)
 			newspec, err := manager.SyncVMI(vmi, true, &cmdv1.VirtualMachineOptions{VirtualMachineSMBios: &cmdv1.SMBios{}})
-- 
2.41.0

