From: Yunping Zheng <[email protected]>

using this patch you can enable or disable pci msi before test.
if you want to disable pci msi before test,just set:
    disable_pci_msi = yes
in subtests.cfg
this patch also  support modify image file before test using libguestfs.

Signed-off-by: Yunping Zheng <[email protected]>
---
 client/tests/virt/virttest/env_process.py | 14 ++++--
 client/tests/virt/virttest/utils_disk.py  | 83 ++++++++++++++++++++++++++++++-
 2 files changed, 91 insertions(+), 6 deletions(-)

diff --git a/client/tests/virt/virttest/env_process.py 
b/client/tests/virt/virttest/env_process.py
index 6f9f7da..bbcc5f7 100644
--- a/client/tests/virt/virttest/env_process.py
+++ b/client/tests/virt/virttest/env_process.py
@@ -2,7 +2,7 @@ import os, time, commands, re, logging, glob, threading, shutil
 from autotest.client import utils
 from autotest.client.shared import error
 import aexpect, kvm_monitor, ppm_utils, test_setup, virt_vm, kvm_vm
-import libvirt_vm, video_maker, utils_misc, storage, kvm_storage
+import libvirt_vm, video_maker, utils_disk, utils_misc, storage, kvm_storage
 import remote, ovirt
 
 try:
@@ -17,7 +17,7 @@ _screendump_thread = None
 _screendump_thread_termination_event = None
 
 
-def preprocess_image(test, params, image_name):
+def preprocess_image(test, params, env, image_name):
     """
     Preprocess a single QEMU image according to the instructions in params.
 
@@ -44,6 +44,10 @@ def preprocess_image(test, params, image_name):
             if not image.create(params):
                 raise error.TestError("Could not create image")
 
+    #if you want set "pci=nomsi" before test, set "disable_pci_msi = yes"
+    disable_pci_msi = params.get("disable_pci_msi", "no")
+    utils_disk.enable_disable_pci_msi(test, params, env, image_filename,
+                                      disable_pci_msi)
 
 def preprocess_vm(test, params, env, name):
     """
@@ -114,7 +118,7 @@ def preprocess_vm(test, params, env, name):
         vm.params = params
 
 
-def postprocess_image(test, params, image_name):
+def postprocess_image(test, params, env, image_name):
     """
     Postprocess a single QEMU image according to the instructions in params.
 
@@ -228,14 +232,14 @@ def process(test, params, env, image_func, vm_func, 
vm_first=False):
                     if vm is not None and vm.is_alive():
                         vm.pause()
                     try:
-                        image_func(test, image_params, image_name)
+                        image_func(test, image_params, env, image_name)
                     finally:
                         if vm is not None and vm.is_alive():
                             vm.resume()
         else:
             for image_name in params.objects("images"):
                 image_params = params.object_params(image_name)
-                image_func(test, image_params, image_name)
+                image_func(test, image_params, env, image_name)
 
     if not vm_first:
         _call_image_func()
diff --git a/client/tests/virt/virttest/utils_disk.py 
b/client/tests/virt/virttest/utils_disk.py
index bc9ae25..356e416 100644
--- a/client/tests/virt/virttest/utils_disk.py
+++ b/client/tests/virt/virttest/utils_disk.py
@@ -4,9 +4,10 @@ Virtualization test - Virtual disk related utility functions
 @copyright: Red Hat Inc.
 """
 
-import os, glob, shutil, tempfile, logging, ConfigParser
+import os, time, re, glob, shutil, tempfile, logging, ConfigParser
 from autotest.client import utils
 from autotest.client.shared import error
+import utils_misc
 
 
 # Whether to print all shell commands called
@@ -47,6 +48,86 @@ def clean_old_image(image):
             utils.run('umount %s' % image, verbose=DEBUG)
         os.remove(image)
 
+def enable_disable_pci_msi(test, params, env, image_filename, disable_pci_msi):
+    """
+    Modify kernel config "pci=nomsi", before the guest is start.
+
+    @Parm  image_filename: image you want to modify.
+    @Param disable_pci_msi: flag of if disable pci msi.
+    """
+    grub_file = params.get("grub_file", "/boot/grub/grub.conf")
+    kernel_cfg_pos_reg =  params.get("kernel_cfg_pos_reg",
+                                      "\s*kernel\s*\/vmlinuz-\d+.*")
+    msi_keyword = params.get("msi_keyword", " pci=nomsi")
+
+    kernel_config_ori = read_file_from_image(image_filename, grub_file)
+    kernel_config_line = re.findall(kernel_cfg_pos_reg, kernel_config_ori)[0]
+    kernel_need_modify = False
+
+    if disable_pci_msi == "yes":
+        if not re.findall(msi_keyword, kernel_config_line):
+            kernel_config_set = kernel_config_line + msi_keyword
+            kernel_need_modify = True
+    elif disable_pci_msi == "no":
+        if re.findall(msi_keyword, kernel_config_line):
+            kernel_config_set = re.sub(msi_keyword, "", kernel_config_line)
+            kernel_need_modify = True
+    if kernel_need_modify:
+        for key in env.keys():
+            vm = env[key]
+            if not utils_misc.is_vm(vm):
+                continue
+            if vm.is_alive():
+                vm.destroy()
+        time.sleep(1)
+        replace_image_file_content(image_filename, grub_file,
+                                    kernel_config_line, kernel_config_set)
+
+def read_file_from_image(image_filename, file_name):
+    """
+    read image file, return the content of the file
+
+    @Param  image_filename: image you want to modify.
+    @Param file_name: the file you want to read.
+    """
+    cmd = "guestfish --rw -a %s -i " % image_filename
+    cmd_read = "cat %s" % file_name
+    o = utils.system_output(cmd + cmd_read)
+    if o:
+        return o
+    else:
+        raise error.TestError("can't read file %s or it is empty" % file_name)
+
+def write_to_image_file(image_filename, file_name, content):
+    """
+    wirte content to image file
+
+    @Param  image_filename: image you want to modify.
+    @Param  file_name: the file you want to write
+    @Param  content: the content you want to write.
+    """
+    cmd = "guestfish --rw -a %s -i " % image_filename
+    cmd_write = "write %s '%s'" % (file_name, content)
+    s = utils.system(cmd + cmd_write)
+    if s == 0:
+        return True
+    else:
+        return False
+
+def replace_image_file_content(image_filename, file_name, find_con, rep_con):
+    """
+    replace file in the image content.
+
+    @Param  image_filename: image you want to modify.
+    @Param  file_name: the file you want to replace
+    @Param  find_con: the orign content you want to replace.
+    @Param  rep_con: the replace content you want.
+    """
+    file_content = read_file_from_image(image_filename, file_name)
+    file_content_after_replace = re.sub(find_con, rep_con, file_content )
+    if file_content != file_content_after_replace :
+        return write_to_image_file(image_filename, file_name,
+                                        file_content_after_replace)
 
 class Disk(object):
     """
-- 
1.7.11.4

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to