Support 'viridian' parameter in Xen HVM
[ganeti-local] / lib / hypervisor / hv_chroot.py
index 36b37a8..5d88447 100644 (file)
@@ -29,8 +29,10 @@ import time
 import logging
 
 from ganeti import constants
-from ganeti import errors # pylint: disable-msg=W0611
+from ganeti import errors # pylint: disable=W0611
 from ganeti import utils
+from ganeti import objects
+from ganeti import pathutils
 from ganeti.hypervisor import hv_base
 from ganeti.errors import HypervisorError
 
@@ -57,7 +59,7 @@ class ChrootManager(hv_base.BaseHypervisor):
     - instance alive check is based on whether any process is using the chroot
 
   """
-  _ROOT_DIR = constants.RUN_GANETI_DIR + "/chroot-hypervisor"
+  _ROOT_DIR = pathutils.RUN_DIR + "/chroot-hypervisor"
 
   PARAMETERS = {
     constants.HV_INIT_SCRIPT: (True, utils.IsNormAbsPath,
@@ -67,11 +69,7 @@ class ChrootManager(hv_base.BaseHypervisor):
 
   def __init__(self):
     hv_base.BaseHypervisor.__init__(self)
-    if not os.path.exists(self._ROOT_DIR):
-      os.mkdir(self._ROOT_DIR)
-    if not os.path.isdir(self._ROOT_DIR):
-      raise HypervisorError("Needed path %s is not a directory" %
-                            self._ROOT_DIR)
+    utils.EnsureDirs([(self._ROOT_DIR, constants.RUN_DIRS_MODE)])
 
   @staticmethod
   def _IsDirLive(path):
@@ -87,23 +85,15 @@ class ChrootManager(hv_base.BaseHypervisor):
   def _GetMountSubdirs(path):
     """Return the list of mountpoints under a given path.
 
-    This function is Linux-specific.
-
     """
-    #TODO(iustin): investigate and document non-linux options
-    #(e.g. via mount output)
-    data = []
-    fh = open("/proc/mounts", "r")
-    try:
-      for line in fh:
-        _, mountpoint, _ = line.split(" ", 2)
-        if (mountpoint.startswith(path) and
-            mountpoint != path):
-          data.append(mountpoint)
-    finally:
-      fh.close()
-    data.sort(key=lambda x: x.count("/"), reverse=True)
-    return data
+    result = []
+    for _, mountpoint, _, _ in utils.GetMounts():
+      if (mountpoint.startswith(path) and
+          mountpoint != path):
+        result.append(mountpoint)
+
+    result.sort(key=lambda x: x.count("/"), reverse=True)
+    return result
 
   @classmethod
   def _InstanceDir(cls, instance_name):
@@ -146,7 +136,7 @@ class ChrootManager(hv_base.BaseHypervisor):
         data.append((file_name, 0, 0, 0, 0, 0))
     return data
 
-  def StartInstance(self, instance, block_devices):
+  def StartInstance(self, instance, block_devices, startup_paused):
     """Start an instance.
 
     For the chroot manager, we try to mount the block device and
@@ -212,11 +202,23 @@ class ChrootManager(hv_base.BaseHypervisor):
         raise HypervisorError("Can't stop the processes using the chroot")
       return
 
+  def CleanupInstance(self, instance_name):
+    """Cleanup after a stopped instance
+
+    """
+    root_dir = self._InstanceDir(instance_name)
+
+    if not os.path.exists(root_dir):
+      return
+
+    if self._IsDirLive(root_dir):
+      raise HypervisorError("Processes are still using the chroot")
+
     for mpath in self._GetMountSubdirs(root_dir):
       utils.RunCmd(["umount", mpath])
 
     result = utils.RunCmd(["umount", root_dir])
-    if result.failed and force:
+    if result.failed:
       msg = ("Processes still alive in the chroot: %s" %
              utils.RunCmd("fuser -vm %s" % root_dir).output)
       logging.error(msg)
@@ -232,6 +234,18 @@ class ChrootManager(hv_base.BaseHypervisor):
     raise HypervisorError("The chroot manager doesn't implement the"
                           " reboot functionality")
 
+  def BalloonInstanceMemory(self, instance, mem):
+    """Balloon an instance memory to a certain value.
+
+    @type instance: L{objects.Instance}
+    @param instance: instance to be accepted
+    @type mem: int
+    @param mem: actual memory size to use for instance runtime
+
+    """
+    # Currently chroots don't have memory limits
+    pass
+
   def GetNodeInfo(self):
     """Return information about the node.
 
@@ -246,24 +260,34 @@ class ChrootManager(hv_base.BaseHypervisor):
     return self.GetLinuxNodeInfo()
 
   @classmethod
-  def GetShellCommandForConsole(cls, instance, hvparams, beparams):
-    """Return a command for connecting to the console of an instance.
+  def GetInstanceConsole(cls, instance, # pylint: disable=W0221
+                         hvparams, beparams, root_dir=None):
+    """Return information for connecting to the console of an instance.
 
     """
-    root_dir = cls._InstanceDir(instance.name)
-    if not os.path.ismount(root_dir):
-      raise HypervisorError("Instance %s is not running" % instance.name)
+    if root_dir is None:
+      root_dir = cls._InstanceDir(instance.name)
+      if not os.path.ismount(root_dir):
+        raise HypervisorError("Instance %s is not running" % instance.name)
 
-    return "chroot %s" % root_dir
+    return objects.InstanceConsole(instance=instance.name,
+                                   kind=constants.CONS_SSH,
+                                   host=instance.primary_node,
+                                   user=constants.SSH_CONSOLE_USER,
+                                   command=["chroot", root_dir])
 
   def Verify(self):
     """Verify the hypervisor.
 
     For the chroot manager, it just checks the existence of the base dir.
 
+    @return: Problem description if something is wrong, C{None} otherwise
+
     """
-    if not os.path.exists(self._ROOT_DIR):
-      return "The required directory '%s' does not exist." % self._ROOT_DIR
+    if os.path.exists(self._ROOT_DIR):
+      return None
+    else:
+      return "The required directory '%s' does not exist" % self._ROOT_DIR
 
   @classmethod
   def PowercycleNode(cls):
@@ -284,3 +308,16 @@ class ChrootManager(hv_base.BaseHypervisor):
 
     """
     raise HypervisorError("Migration not supported by the chroot hypervisor")
+
+  def GetMigrationStatus(self, instance):
+    """Get the migration status
+
+    @type instance: L{objects.Instance}
+    @param instance: the instance that is being migrated
+    @rtype: L{objects.MigrationStatus}
+    @return: the status of the current migration (one of
+             L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
+             progress info that can be retrieved from the hypervisor
+
+    """
+    raise HypervisorError("Migration not supported by the chroot hypervisor")