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:
    pci_msi_sensitive = yes
    disable_pci_msi = yes
in subtests.cfg.
when disable_pci_msi =yes, will modify the kernel params in grub.conf
and append "pci=nomsi."
when you using, you need sure you python support guestfs.

chang_log:
  V2 from V1
        using python guestfs lib.

Signed-off-by: Yunping Zheng <[email protected]>
---
 virttest/env_process.py |   53 ++++++++++++++++++++---
 virttest/utils_disk.py  |  109 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 156 insertions(+), 6 deletions(-)

diff --git a/virttest/env_process.py b/virttest/env_process.py
index 0b3bd4e..1f89b82 100644
--- a/virttest/env_process.py
+++ b/virttest/env_process.py
@@ -3,7 +3,7 @@ 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 remote, ovirt
+import remote, ovirt, utils_disk
 
 try:
     import PIL.Image
@@ -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,14 @@ 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"
+        #and pci_msi_sensitive = "yes"
+        if params.get("pci_msi_sensitive", "no") == "yes":
+            disable_pci_msi = params.get("disable_pci_msi", "no")
+            enable_disable_pci_msi(test, params, env, image_filename,
+                                   disable_pci_msi)
+
+
 
 def preprocess_vm(test, params, env, name):
     """
@@ -111,7 +119,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.
 
@@ -225,14 +233,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()
@@ -588,3 +596,38 @@ def _take_screendumps(test, params, env):
                 _screendump_thread_termination_event = None
                 break
             _screendump_thread_termination_event.wait(delay)
+
+def enable_disable_pci_msi(test, params, env, image_filename, disable_pci_msi):
+    """
+    Add or delete "pci=nomsi" in the kernel config line, before guest is start.
+
+    @Parm  image_filename: image you want to modify.
+    @Param disable_pci_msi: flag of if disable pci msi.if disable_pci_msi is
+                            True will add pci=nomsi in kernel config line.
+    """
+
+    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")
+
+    f = utils_disk.Guest_Disk(image_filename)
+    kernel_config_ori = f.read_file(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 vm in env.get_all_vms():
+            if vm.is_alive():
+                vm.destroy()
+        time.sleep(1)
+        f.replace_image_file_content(grub_file, kernel_config_line,
+                                     kernel_config_set)
\ No newline at end of file
diff --git a/virttest/utils_disk.py b/virttest/utils_disk.py
index bc9ae25..0cd7dab 100644
--- a/virttest/utils_disk.py
+++ b/virttest/utils_disk.py
@@ -4,7 +4,7 @@ Virtualization test - Virtual disk related utility functions
 @copyright: Red Hat Inc.
 """
 
-import os, glob, shutil, tempfile, logging, ConfigParser
+import os, glob, shutil, tempfile, logging, ConfigParser, guestfs, re
 from autotest.client import utils
 from autotest.client.shared import error
 
@@ -74,6 +74,113 @@ class Disk(object):
         cleanup(self.mount)
         logging.debug("Disk %s successfuly set", self.path)
 
+class Guest_Disk(object):
+    """
+    class of guest disk using guestfs lib to do some operation(like read/write)
+    on guest disk:
+    """
+
+    def __init__(self, disk, readonly=False, need_check=False):
+        self.g = guestfs.GuestFS ()
+        # Attach the disk image read-only to libguestfs.
+        logging.info("Add guest disk %s -- writable" % disk)
+        self.g.add_drive_opts(disk, readonly)
+        # Run the libguestfs back-end.
+        logging.info("Launch the guest disk %s, maybe take some time..." % 
disk)
+        self.g.launch ()
+
+        # Ask libguestfs to inspect for operating systems.
+        if need_check:
+            self.roots = self.g.inspect_os ()
+            if len (self.roots) == 0:
+                raise (Error ("inspect_vm: no operating systems found"))
+
+    def mount_all(self):
+
+        def compare (a, b):
+         if len(a[0]) > len(b[0]):
+             return 1
+         elif len(a[0]) == len(b[0]):
+             return 0
+         else:
+             return -1
+
+        roots = self.g.inspect_os ()
+        if len (roots) != 0:
+            for root in roots:
+                mps = self.g.inspect_get_mountpoints (root)
+                # Mount up the disks, like guestfish -i.
+                #Sort keys by length, shortest first, so that we end up
+                # mounting the filesystems in the correct order.
+                mps.sort (compare)
+                for mp_dev in mps:
+                    try:
+                        logging.info("Mount device '%s' partitions '%s' to 
'%s'"
+                                     % (root, mp_dev[1], mp_dev[0]))
+                        self.g.mount (mp_dev[1], mp_dev[0])
+                    except RuntimeError as msg:
+                        logging.info("%s (ignored)" % msg)
+        else:
+            raise (Error ("inspect_vm: no operating systems found"))
+
+    def umount_all (self):
+        logging.info("Umount all device partitions")
+        self.g.umount_all()
+
+    def read_file(self,file_name):
+        """
+        read file from the guest disk, return the content of the file
+        @Param file_name: the file you want to read.
+        """
+
+        self.mount_all()
+        try:
+            o = self.g.cat(file_name)
+            if o :
+                return o
+            else:
+                raise error.TestError("can't read file '%s' or it is empty"
+                                      % file_name)
+        finally:
+            self.umount_all()
+
+    def write_to_image_file(self, file_name, content, w_append=False):
+        """
+        wirte content to the file on the guest disk.
+        when using this method all the original content will be overriding.
+        if you don't hope your original data be override using w_append=True
+
+        @Param  file_name: the file you want to write
+        @Param  content: the content you want to write.
+        @w_append append the content or override
+        """
+
+        self.mount_all()
+        try:
+            if w_append:
+                self.g.write_append(file_name, content)
+            else:
+                self.g.write(file_name, content)
+        except Exception:
+            raise error.TestError("write '%s' to file '%s' error!"
+                                  % (content, file_name ))
+        finally:
+            self.umount_all()
+
+    def replace_image_file_content(self, file_name, find_con, rep_con):
+        """
+        replace file content matchs in the filename with rep_con.
+        suport using Regular expression
+        @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 = self.read_file(file_name)
+        file_content_after_replace = re.sub(find_con, rep_con, file_content )
+        if file_content != file_content_after_replace :
+            self.write_to_image_file(file_name, file_content_after_replace)
+
 
 class FloppyDisk(Disk):
     """
-- 
1.7.9.5

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

Reply via email to