4 # Copyright (C) 2006, 2007, 2008, 2009 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Chroot manager hypervisor
31 from ganeti import constants
32 from ganeti import errors # pylint: disable=W0611
33 from ganeti import utils
34 from ganeti import objects
35 from ganeti.hypervisor import hv_base
36 from ganeti.errors import HypervisorError
39 class ChrootManager(hv_base.BaseHypervisor):
42 This not-really hypervisor allows ganeti to manage chroots. It has
43 special behaviour and requirements on the OS definition and the node
45 - the start and stop of the chroot environment are done via a
46 script called ganeti-chroot located in the root directory of the
47 first drive, which should be created by the OS definition
48 - this script must accept the start and stop argument and, on
49 shutdown, it should cleanly shutdown the daemons/processes
51 - the daemons run in chroot should only bind to the instance IP
52 (to which the OS create script has access via the instance name)
53 - since some daemons in the node could be listening on the wildcard
54 address, some ports might be unavailable
55 - the instance listing will show no memory usage
56 - on shutdown, the chroot manager will try to find all mountpoints
57 under the root dir of the instance and unmount them
58 - instance alive check is based on whether any process is using the chroot
61 _ROOT_DIR = constants.RUN_GANETI_DIR + "/chroot-hypervisor"
64 constants.HV_INIT_SCRIPT: (True, utils.IsNormAbsPath,
65 "must be an absolute normalized path",
70 hv_base.BaseHypervisor.__init__(self)
71 utils.EnsureDirs([(self._ROOT_DIR, constants.RUN_DIRS_MODE)])
75 """Check if a directory looks like a live chroot.
78 if not os.path.ismount(path):
80 result = utils.RunCmd(["fuser", "-m", path])
81 return not result.failed
84 def _GetMountSubdirs(path):
85 """Return the list of mountpoints under a given path.
89 for _, mountpoint, _, _ in utils.GetMounts():
90 if (mountpoint.startswith(path) and
92 result.append(mountpoint)
94 result.sort(key=lambda x: x.count("/"), reverse=True)
98 def _InstanceDir(cls, instance_name):
99 """Return the root directory for an instance.
102 return utils.PathJoin(cls._ROOT_DIR, instance_name)
104 def ListInstances(self):
105 """Get the list of running instances.
108 return [name for name in os.listdir(self._ROOT_DIR)
109 if self._IsDirLive(utils.PathJoin(self._ROOT_DIR, name))]
111 def GetInstanceInfo(self, instance_name):
112 """Get instance properties.
114 @type instance_name: string
115 @param instance_name: the instance name
117 @return: (name, id, memory, vcpus, stat, times)
120 dir_name = self._InstanceDir(instance_name)
121 if not self._IsDirLive(dir_name):
122 raise HypervisorError("Instance %s is not running" % instance_name)
123 return (instance_name, 0, 0, 0, 0, 0)
125 def GetAllInstancesInfo(self):
126 """Get properties of all instances.
128 @return: [(name, id, memory, vcpus, stat, times),...]
132 for file_name in os.listdir(self._ROOT_DIR):
133 path = utils.PathJoin(self._ROOT_DIR, file_name)
134 if self._IsDirLive(path):
135 data.append((file_name, 0, 0, 0, 0, 0))
138 def StartInstance(self, instance, block_devices, startup_paused):
139 """Start an instance.
141 For the chroot manager, we try to mount the block device and
142 execute '/ganeti-chroot start'.
145 root_dir = self._InstanceDir(instance.name)
146 if not os.path.exists(root_dir):
150 raise HypervisorError("Failed to start instance %s: %s" %
151 (instance.name, err))
152 if not os.path.isdir(root_dir):
153 raise HypervisorError("Needed path %s is not a directory" % root_dir)
155 if not os.path.ismount(root_dir):
156 if not block_devices:
157 raise HypervisorError("The chroot manager needs at least one disk")
159 sda_dev_path = block_devices[0][1]
160 result = utils.RunCmd(["mount", sda_dev_path, root_dir])
162 raise HypervisorError("Can't mount the chroot dir: %s" % result.output)
163 init_script = instance.hvparams[constants.HV_INIT_SCRIPT]
164 result = utils.RunCmd(["chroot", root_dir, init_script, "start"])
166 raise HypervisorError("Can't run the chroot start script: %s" %
169 def StopInstance(self, instance, force=False, retry=False, name=None):
172 This method has complicated cleanup tests, as we must:
173 - try to kill all leftover processes
174 - try to unmount any additional sub-mountpoints
175 - finally unmount the instance dir
181 root_dir = self._InstanceDir(name)
182 if not os.path.exists(root_dir) or not self._IsDirLive(root_dir):
185 # Run the chroot stop script only once
186 if not retry and not force:
187 result = utils.RunCmd(["chroot", root_dir, "/ganeti-chroot", "stop"])
189 raise HypervisorError("Can't run the chroot stop script: %s" %
193 utils.RunCmd(["fuser", "-k", "-TERM", "-m", root_dir])
195 utils.RunCmd(["fuser", "-k", "-KILL", "-m", root_dir])
196 # 2 seconds at most should be enough for KILL to take action
199 if self._IsDirLive(root_dir):
201 raise HypervisorError("Can't stop the processes using the chroot")
204 def CleanupInstance(self, instance_name):
205 """Cleanup after a stopped instance
208 root_dir = self._InstanceDir(instance_name)
210 if not os.path.exists(root_dir):
213 if self._IsDirLive(root_dir):
214 raise HypervisorError("Processes are still using the chroot")
216 for mpath in self._GetMountSubdirs(root_dir):
217 utils.RunCmd(["umount", mpath])
219 result = utils.RunCmd(["umount", root_dir])
221 msg = ("Processes still alive in the chroot: %s" %
222 utils.RunCmd("fuser -vm %s" % root_dir).output)
224 raise HypervisorError("Can't umount the chroot dir: %s (%s)" %
225 (result.output, msg))
227 def RebootInstance(self, instance):
228 """Reboot an instance.
230 This is not (yet) implemented for the chroot manager.
233 raise HypervisorError("The chroot manager doesn't implement the"
234 " reboot functionality")
236 def BalloonInstanceMemory(self, instance, mem):
237 """Balloon an instance memory to a certain value.
239 @type instance: L{objects.Instance}
240 @param instance: instance to be accepted
242 @param mem: actual memory size to use for instance runtime
245 # Currently chroots don't have memory limits
248 def GetNodeInfo(self):
249 """Return information about the node.
251 This is just a wrapper over the base GetLinuxNodeInfo method.
253 @return: a dict with the following keys (values in MiB):
254 - memory_total: the total memory size on the node
255 - memory_free: the available memory on the node for instances
256 - memory_dom0: the memory used by the node itself, if available
259 return self.GetLinuxNodeInfo()
262 def GetInstanceConsole(cls, instance, # pylint: disable=W0221
263 hvparams, beparams, root_dir=None):
264 """Return information for connecting to the console of an instance.
268 root_dir = cls._InstanceDir(instance.name)
269 if not os.path.ismount(root_dir):
270 raise HypervisorError("Instance %s is not running" % instance.name)
272 return objects.InstanceConsole(instance=instance.name,
273 kind=constants.CONS_SSH,
274 host=instance.primary_node,
275 user=constants.GANETI_RUNAS,
276 command=["chroot", root_dir])
279 """Verify the hypervisor.
281 For the chroot manager, it just checks the existence of the base dir.
284 if not os.path.exists(self._ROOT_DIR):
285 return "The required directory '%s' does not exist." % self._ROOT_DIR
288 def PowercycleNode(cls):
289 """Chroot powercycle, just a wrapper over Linux powercycle.
292 cls.LinuxPowercycle()
294 def MigrateInstance(self, instance, target, live):
295 """Migrate an instance.
297 @type instance: L{objects.Instance}
298 @param instance: the instance to be migrated
300 @param target: hostname (usually ip) of the target node
302 @param live: whether to do a live or non-live migration
305 raise HypervisorError("Migration not supported by the chroot hypervisor")
307 def GetMigrationStatus(self, instance):
308 """Get the migration status
310 @type instance: L{objects.Instance}
311 @param instance: the instance that is being migrated
312 @rtype: L{objects.MigrationStatus}
313 @return: the status of the current migration (one of
314 L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
315 progress info that can be retrieved from the hypervisor
318 raise HypervisorError("Migration not supported by the chroot hypervisor")