From patchwork Tue Jun 9 00:34:46 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 28778 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n590Yv3j011515 for ; Tue, 9 Jun 2009 00:34:57 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754365AbZFIAex (ORCPT ); Mon, 8 Jun 2009 20:34:53 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753344AbZFIAex (ORCPT ); Mon, 8 Jun 2009 20:34:53 -0400 Received: from mx2.redhat.com ([66.187.237.31]:43857 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753242AbZFIAew (ORCPT ); Mon, 8 Jun 2009 20:34:52 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n590YtFW000350; Mon, 8 Jun 2009 20:34:55 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n590YsDn010940; Mon, 8 Jun 2009 20:34:54 -0400 Received: from localhost.localdomain (vpn-10-92.bos.redhat.com [10.16.10.92]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n590Yqdo011766; Mon, 8 Jun 2009 20:34:52 -0400 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues , Michael Goldish 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 Message-Id: <1244507691-9575-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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):