From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jim Fehlig Subject: [PATCH] [RFC] Add lock on domain start Date: Wed, 06 Aug 2008 00:06:00 -0600 Message-ID: <48993F48.9030705@novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------040302050704010509060908" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org This is a multi-part message in MIME format. --------------040302050704010509060908 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit This patch adds a simple lock mechanism when starting domains by placing a lock file in xend-domains-path/. 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 --------------040302050704010509060908 Content-Type: text/x-patch; name="xend-domain-lock.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="xend-domain-lock.patch" 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): --------------040302050704010509060908 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --------------040302050704010509060908--