WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH] [RFC] Add lock on domain start

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH] [RFC] Add lock on domain start
From: Jim Fehlig <jfehlig@xxxxxxxxxx>
Date: Wed, 06 Aug 2008 00:06:00 -0600
Delivery-date: Tue, 05 Aug 2008 23:05:45 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
User-agent: Thunderbird 2.0.0.16 (X11/20080707)
This patch adds a simple lock mechanism when starting domains by placing a lock file in xend-domains-path/<dom_uuid>. The lock file is removed when domain is stopped. The motivation for such a mechanism is to prevent starting the same domain from multiple hosts.

If xend-domains-path is set to shared mount point, a domain will fail to start on host B if it is already running on host A. I've added an option to XendOptions to control the behavior with default of no lock.

The patch certainly needs some testing (and probably adjustment) to ensure the lock is handled properly on save, restore, migrate, domain crash, etc. but wanted to get folks' thought on this approach before continuing this endeavor. Some simple improvements could include adding info (domain name/id, start time, vmm hostname) to the lock file, allowing such messages as "domain foo seems to be already running on host bar" and a --force option to create/start to override the lock. A per-domain config option could also be added to allow more fine-grained control.

Comments, suggestions, alternative approaches, ... are welcome and appreciated :-).

Regards,
Jim
diff -r f20fb83dac2c tools/examples/xend-config.sxp
--- a/tools/examples/xend-config.sxp    Tue Aug 05 13:55:14 2008 +0100
+++ b/tools/examples/xend-config.sxp    Tue Aug 05 23:28:55 2008 -0600
@@ -245,3 +245,9 @@
 
 # Rotation count of qemu-dm log file.
 #(qemu-dm-logrotate-count 10)
+
+# Create a lock file when domains are started.  Lock file is
+# placed in xend-domains-path on domain startup and removed
+# when domain is stopped.  By default, a lock file is not
+# created.  Set to yes to enable lock file creation.
+#(xend-domain-lock no)
diff -r f20fb83dac2c tools/python/xen/xend/XendDomainInfo.py
--- a/tools/python/xen/xend/XendDomainInfo.py   Tue Aug 05 13:55:14 2008 +0100
+++ b/tools/python/xen/xend/XendDomainInfo.py   Tue Aug 05 23:28:55 2008 -0600
@@ -34,7 +34,7 @@ from types import StringTypes
 from types import StringTypes
 
 import xen.lowlevel.xc
-from xen.util import asserts
+from xen.util import asserts, mkdir
 from xen.util.blkif import blkdev_uname_to_file, blkdev_uname_to_taptype
 import xen.util.xsm.xsm as security
 from xen.util import xsconstants
@@ -420,7 +420,10 @@ class XendDomainInfo:
         from xen.xend import XendDomain
 
         if self._stateGet() in (XEN_API_VM_POWER_STATE_HALTED, 
XEN_API_VM_POWER_STATE_SUSPENDED, XEN_API_VM_POWER_STATE_CRASHED):
-            try:
+            if self._is_locked():
+                raise XendError('VM is locked.  Is it running on another 
node?')
+
+             try:
                 XendTask.log_progress(0, 30, self._constructDomain)
                 XendTask.log_progress(31, 60, self._initDomain)
                 
@@ -441,6 +444,9 @@ class XendDomainInfo:
                     xendomains.domain_sched_credit_set(self.getDomid(),
                                                        self.getWeight(),
                                                        self.getCap())
+
+                self._create_lock()
+
             except:
                 log.exception('VM start failed')
                 self.destroy()
@@ -1104,6 +1110,53 @@ class XendDomainInfo:
     # internal functions ... TODO: re-categorised
     # 
 
+    def _is_locked(self):
+        if not xoptions.get_xend_domain_lock():
+            return False
+
+        from xen.xend import XendDomain
+        path = XendDomain.instance()._managed_path(self.get_uuid())
+        try:
+            if not os.path.exists(path):
+                return False
+            path += "/lock"
+            return os.access(path, os.F_OK)
+
+        except:
+            log.exception("%s could not be accessed")
+            return False
+
+    def _create_lock(self):
+        if not xoptions.get_xend_domain_lock():
+            return
+
+        from xen.xend import XendDomain
+        path = XendDomain.instance()._managed_path(self.get_uuid())
+        try:
+            if not os.path.exists(path):
+                mkdir.parents(path, stat.S_IRWXU)
+            path += "/lock"
+            oflags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
+            fd = os.open(path, oflags)
+            os.close(fd)
+
+        except:
+            log.exception("%s could not be created." % path)
+
+    def _remove_lock(self):
+        if not xoptions.get_xend_domain_lock():
+            return
+
+        from xen.xend import XendDomain
+        path = XendDomain.instance()._managed_path(self.get_uuid())
+        lock = path + "/lock"
+        try:
+            os.unlink(lock)
+            if not XendDomain.instance().is_domain_managed(self):
+                shutil.rmtree(path)
+        except:
+            log.exception("%s could not be created." % path)
+
     def _augmentInfo(self, priv):
         """Augment self.info, as given to us through L{recreate}, with
         values taken from the store.  This recovers those values known
@@ -2388,6 +2441,8 @@ class XendDomainInfo:
 
         log.debug("XendDomainInfo.destroy: domid=%s", str(self.domid))
 
+        self._remove_lock()
+
         paths = self._prepare_phantom_paths()
 
         self._cleanupVm()
diff -r f20fb83dac2c tools/python/xen/xend/XendOptions.py
--- a/tools/python/xen/xend/XendOptions.py      Tue Aug 05 13:55:14 2008 +0100
+++ b/tools/python/xen/xend/XendOptions.py      Tue Aug 05 23:28:55 2008 -0600
@@ -135,6 +135,11 @@ class XendOptions:
     """Default rotation count of qemu-dm log file."""
     qemu_dm_logrotate_count = 10
 
+    """Default for the flag indicating whether xend should create
+    a lock file for domains when they are started."""
+    xend_domain_lock = 'no'
+
+
     def __init__(self):
         self.configure()
 
@@ -357,6 +362,11 @@ class XendOptions:
     def get_qemu_dm_logrotate_count(self):
         return self.get_config_int("qemu-dm-logrotate-count",
                                    self.qemu_dm_logrotate_count)
+
+    def get_xend_domain_lock(self):
+        """Get the flag indicating whether xend should create a lock file
+        for domains when they are started."""
+        return self.get_config_bool("xend-domain-lock", self.xend_domain_lock)
 
 
 class XendOptionsFile(XendOptions):
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel