Hello All,

        With this patch , you will be able to run tests that use multiple VMs 
with libvirt. One such
test is VLAN test ; Which libvirt autotest currently is not capable of running.
        Capability to run multiple VMs simultaneoulsy and 'explicit' 
sharing/restricting of resources with
permissions important for my forthcoming security patches which will test 
libvirt security infrastructure
(namely svirt). This patch only attempts segregate VM disk resources (CDROM, VM 
images) under a VM
directory which is created per VM at the time of installation. Forthcoming 
patches will address
*explicit* sharing/restricting of resources with configuration options.
        My intention here was to 'do more with less code' , i.e., minimally 
disturb existing autotest code.
        For unattended_install , all you need to do is :
(1)  create a VM directory under /tmp/libvirt_autotest_root/ whose name must 
exactly match
     param 'main_vm' ( in base.cfg ) for e.g., /tmp/libvirt_autotest_root/vm1/ 
, and
(2)  create iso directory under VM directory (/tmp/libvirt_autotest_root/vm1/)- 
namely
     'linux/iso' (or simply 'iso' - recommended , but requires small change in 
guest-os.cfg for
     your guest at param 'cdrom_cd1' , for instance originally your 'cdrom_cd1' 
would look like
     'cdrom_cd1 = isos/linux/RHELX.iso' , but now it can just be 'cdrom_cd1 = 
isos/RHELX.iso' )
(3)  Place your guest CDROM in the driectory created in (1) and (2)
     i.e., /tmp/libvirt_autotest_root/vm1/linux/iso ( or simply 
/tmp/libvirt_autotest_root/vm1/iso
     , see recomendation in (2))

Signed-off-by: Onkar N Mahajan <onkar.n.maha...@linux.vnet.ibm.com>

---
 client/tests/libvirt/tests-shared.cfg.sample |   12 ++--
 client/virt/libvirt_vm.py                    |   69 +++++++++++++++++++++++++-
 client/virt/virt_utils.py                    |    3 +
 3 files changed, 77 insertions(+), 7 deletions(-)

diff --git a/client/tests/libvirt/tests-shared.cfg.sample 
b/client/tests/libvirt/tests-shared.cfg.sample
index 1fe0894..64cf516 100644
--- a/client/tests/libvirt/tests-shared.cfg.sample
+++ b/client/tests/libvirt/tests-shared.cfg.sample
@@ -28,13 +28,13 @@ include virtio-win.cfg
 # * The parameters cdrom_unattended, floppy, kernel and initrd are generated
 #   by LIBVIRT autotest, so remember to put them under a writable location
 #   (for example, the cdrom share can be read only)
-image_name(_.*)? ?<= /tmp/libvirt_autotest_root/images/
-cdrom(_.*)? ?<= /tmp/libvirt_autotest_root/
-floppy ?<= /tmp/libvirt_autotest_root/
-image_dir = /tmp/libvirt_autotest_root/
+image_name(_.*)? ?<= /tmp/libvirt_autotest_root/$main_vm/
+cdrom(_.*)? ?<= /tmp/libvirt_autotest_root/$main_vm/
+floppy ?<= /tmp/libvirt_autotest_root/$main_vm/
+image_dir = /tmp/libvirt_autotest_root/$main_vm/
 Linux..unattended_install:
-    kernel ?<= /tmp/libvirt_autotest_root/
-    initrd ?<= /tmp/libvirt_autotest_root/
+    kernel ?<= /tmp/libvirt_autotest_root/$main_vm/
+    initrd ?<= /tmp/libvirt_autotest_root/$main_vm/

 # Uncomment the following lines to enable abort-on-error mode:
 #abort_on_error = yes
diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py
index cbed8aa..035cedb 100644
--- a/client/virt/libvirt_vm.py
+++ b/client/virt/libvirt_vm.py
@@ -457,6 +457,34 @@ def virsh_detach_device(name, xml_file, extra = "", uri = 
""):
         logging.error("Detaching device from VM %s failed." % name)
         return False

+def virsh_list(option="--all", uri = ""):
+    """
+    - list domains -
+    @param option: Can take various values see 'virsh help list'
+                   for more details.
+    @param uri   : connect uri
+
+    Returns a list :
+    ( <rc of virsh list command> , <dictionary 'dom-name':
+    [ <dom-id> , <dom-status> ])
+
+    """
+    cmd = "list %s" % option
+    try:
+        cmd_res = virsh_cmd(cmd, uri)
+        d = {}
+        for line in cmd_res.split("\n"):
+            l = []
+            m = re.search('^\s([0-9]*\-?)\s+(\w+)\s+(.*)', line)
+            if m:
+                l.append(m.group(1))
+                l.append(m.group(3))
+                d[m.group(2)] = l
+        return (True, d)
+    except error.CmdError, detail:
+        logging.error("Failed listing domains : %s" % (detail))
+        return (False, None)
+

 class VM(virt_vm.BaseVM):
     """
@@ -529,6 +557,43 @@ class VM(virt_vm.BaseVM):
         """
         return virsh_is_alive(self.name, self.connect_uri)

+    def list_peer_domain(self, option="--all"):
+        """
+        Returns list of domains for the uri
+        """
+        (rc, val) = virsh_list(option, self.connect_uri)
+        return (rc, val)
+
+
+    def nxt_free_vnc_port(self):
+        """
+        Returns max VNC port in use for a particular uri
+        """
+        (rc, val) =  self.list_peer_domain()
+        if not rc:
+            logging.error("Unable to get peer domain information")
+            sys.exit(1)
+        else:
+            port = 0
+            for key in val.keys():
+                """
+                This currently , only works with VMs installed with
+                valid VNC port , and doesn't work with domains installed
+                with <graphics type='vnc' port='-1' autoport='yes'/>. So
+                domains installed with autotest will be installed with valid
+                port numbers , externally installed VMs (installed with for
+                instance virt-manager, virt-install - must be started before 
this
+                test is performed.To get valid results and for libvirt
+                autotest to work successfully.
+                """
+                cmd_res = virsh_cmd("vncdisplay %s" % key, self.connect_uri)
+                m = re.search('(.*)\:(\d+)', cmd_res)
+                if m.group(2) >= port:
+                    port = m.group(2)
+            p = int(port)
+            if p <= self.vnc_port:
+                p += self.vnc_port
+        return p+1

     def is_dead(self):
         """
@@ -1161,7 +1226,9 @@ class VM(virt_vm.BaseVM):

             # Find available VNC port, if needed
             if params.get("display") == "vnc":
-                self.vnc_port = virt_utils.find_free_port(5900, 6100)
+                p = self.nxt_free_vnc_port()
+                self.vnc_port = virt_utils.find_free_port(p, 6100)
+

             # Find available spice port, if needed
             if params.get("spice"):
diff --git a/client/virt/virt_utils.py b/client/virt/virt_utils.py
index 4bbfc7b..e9ce755 100644
--- a/client/virt/virt_utils.py
+++ b/client/virt/virt_utils.py
@@ -678,6 +678,9 @@ def create_image(params, root_dir):
     """
     format = params.get("image_format", "qcow2")
     image_filename = get_image_filename(params, root_dir)
+
+    if not os.path.exists(os.path.dirname(image_filename)):
+        os.mkdir(os.path.dirname(image_filename))
     size = params.get("image_size", "10G")
     if params.get("create_with_dd") == "yes" and format == "raw":
         # maps K,M,G,T => (count, bs)
--
1.7.4.4


-- 
Onkar N Mahajan
System Software Engineer,
IBM Linux Technology Center,
Bangalore,India

_______________________________________________
Autotest mailing list
Autotest@test.kernel.org
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to