public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Lucas Meneghel Rodrigues <lmr@redhat.com>
To: autotest@test.kernel.org
Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues <lmr@redhat.com>,
	Michael Goldish <mgoldish@redhat.com>
Subject: [KVM-AUTOTEST PATCH 01/06] kvm_vm.py: create a lock file to avoid VM creation in parallel
Date: Mon,  8 Jun 2009 21:34:46 -0300	[thread overview]
Message-ID: <1244507691-9575-1-git-send-email-lmr@redhat.com> (raw)

VM.create() does a few things (such as finding free ports) which
are not safe to execute in parallel. Use a lock file to make
sure this doesn't happen. The lock is released only after the
VM is started or fails to start.

Signed-off-by: Michael Goldish <mgoldish@redhat.com>
---
 client/tests/kvm/kvm_vm.py |   91 ++++++++++++++++++++++++--------------------
 1 files changed, 50 insertions(+), 41 deletions(-)

diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py
index eb9717b..e2b684c 100644
--- a/client/tests/kvm/kvm_vm.py
+++ b/client/tests/kvm/kvm_vm.py
@@ -335,51 +335,60 @@ class VM:
                     logging.error("Actual MD5 sum differs from expected one")
                     return False
 
-        # Handle port redirections
-        redir_names = kvm_utils.get_sub_dict_names(params, "redirs")
-        host_ports = kvm_utils.find_free_ports(5000, 6000, len(redir_names))
-        self.redirs = {}
-        for i in range(len(redir_names)):
-            redir_params = kvm_utils.get_sub_dict(params, redir_names[i])
-            guest_port = int(redir_params.get("guest_port"))
-            self.redirs[guest_port] = host_ports[i]
-
-        # Find available VNC port, if needed
-        if params.get("display") == "vnc":
-            self.vnc_port = kvm_utils.find_free_port(5900, 6000)
-
-        # Make qemu command
-        qemu_command = self.make_qemu_command()
+        # Make sure the following code is not executed by more than one thread
+        # at the same time
+        lockfile = open("/tmp/kvm-autotest-vm-create.lock", "w+")
+        fcntl.lockf(lockfile, fcntl.LOCK_EX)
 
-        # Is this VM supposed to accept incoming migrations?
-        if for_migration:
-            # Find available migration port
-            self.migration_port = kvm_utils.find_free_port(5200, 6000)
-            # Add -incoming option to the qemu command
-            qemu_command += " -incoming tcp:0:%d" % self.migration_port
-
-        logging.debug("Running qemu command:\n%s" % qemu_command)
-        (status, pid, output) = kvm_utils.run_bg(qemu_command, None,
-                                                 logging.debug, "(qemu) ")
-
-        if status:
-            logging.debug("qemu exited with status %d" % status)
-            logging.error("VM could not be created -- qemu command"
-                          " failed:\n%s" % qemu_command)
-            return False
+        try:
+            # Handle port redirections
+            redir_names = kvm_utils.get_sub_dict_names(params, "redirs")
+            host_ports = kvm_utils.find_free_ports(5000, 6000, len(redir_names))
+            self.redirs = {}
+            for i in range(len(redir_names)):
+                redir_params = kvm_utils.get_sub_dict(params, redir_names[i])
+                guest_port = int(redir_params.get("guest_port"))
+                self.redirs[guest_port] = host_ports[i]
+
+            # Find available VNC port, if needed
+            if params.get("display") == "vnc":
+                self.vnc_port = kvm_utils.find_free_port(5900, 6000)
+
+            # Make qemu command
+            qemu_command = self.make_qemu_command()
+
+            # Is this VM supposed to accept incoming migrations?
+            if for_migration:
+                # Find available migration port
+                self.migration_port = kvm_utils.find_free_port(5200, 6000)
+                # Add -incoming option to the qemu command
+                qemu_command += " -incoming tcp:0:%d" % self.migration_port
+
+            logging.debug("Running qemu command:\n%s", qemu_command)
+            (status, pid, output) = kvm_utils.run_bg(qemu_command, None,
+                                                     logging.debug, "(qemu) ")
+
+            if status:
+                logging.debug("qemu exited with status %d", status)
+                logging.error("VM could not be created -- qemu command"
+                              " failed:\n%s", qemu_command)
+                return False
 
-        self.pid = pid
+            self.pid = pid
 
-        if not kvm_utils.wait_for(self.is_alive, timeout, 0, 1):
-            logging.debug("VM is not alive for some reason")
-            logging.error("VM could not be created with"
-                          " command:\n%s" % qemu_command)
-            self.destroy()
-            return False
+            if not kvm_utils.wait_for(self.is_alive, timeout, 0, 1):
+                logging.debug("VM is not alive for some reason")
+                logging.error("VM could not be created with"
+                              " command:\n%s", qemu_command)
+                self.destroy()
+                return False
 
-        logging.debug("VM appears to be alive with PID %d" % self.pid)
+            logging.debug("VM appears to be alive with PID %d", self.pid)
+            return True
 
-        return True
+        finally:
+            fcntl.lockf(lockfile, fcntl.LOCK_UN)
+            lockfile.close()
 
 
     def send_monitor_cmd(self, command, block=True, timeout=20.0):
-- 
1.6.2.2


             reply	other threads:[~2009-06-09  0:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-09  0:34 Lucas Meneghel Rodrigues [this message]
2009-06-09  0:34 ` [KVM-AUTOTEST PATCH 02/06] kvm_runtest_2.py: Use env filename specified by the 'env' parameter Lucas Meneghel Rodrigues
2009-06-09  0:34   ` [KVM-AUTOTEST PATCH 03/06] kvm_vm.py: add new VM parameter 'x11_display' that controls $DISPLAY Lucas Meneghel Rodrigues
2009-06-09  0:34     ` [KVM-AUTOTEST PATCH 04/06] VM.create(): always destroy() the VM before attempting to start it Lucas Meneghel Rodrigues
2009-06-09  0:34       ` [KVM-AUTOTEST PATCH 05/06] kvm_utils.py: remote_login(): improve regular expression matching Lucas Meneghel Rodrigues
2009-06-09  0:34         ` [KVM-AUTOTEST PATCH 06/06] kvm_config: Make split_and_strip function to strip the right quotes Lucas Meneghel Rodrigues
2009-06-10 19:30           ` Lucas Meneghel Rodrigues
2009-06-10 19:30         ` [KVM-AUTOTEST PATCH 05/06] kvm_utils.py: remote_login(): improve regular expression matching Lucas Meneghel Rodrigues
2009-06-10 19:29       ` [KVM-AUTOTEST PATCH 04/06] VM.create(): always destroy() the VM before attempting to start it Lucas Meneghel Rodrigues
2009-06-10 19:29     ` [KVM-AUTOTEST PATCH 03/06] kvm_vm.py: add new VM parameter 'x11_display' that controls $DISPLAY Lucas Meneghel Rodrigues
2009-06-10 19:29   ` [KVM-AUTOTEST PATCH 02/06] kvm_runtest_2.py: Use env filename specified by the 'env' parameter Lucas Meneghel Rodrigues
2009-06-10 19:29 ` [KVM-AUTOTEST PATCH 01/06] kvm_vm.py: create a lock file to avoid VM creation in parallel Lucas Meneghel Rodrigues

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1244507691-9575-1-git-send-email-lmr@redhat.com \
    --to=lmr@redhat.com \
    --cc=autotest@test.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=mgoldish@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox