4 # Copyright (C) 2010 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
31 from ganeti import constants
32 from ganeti import errors # pylint: disable-msg=W0611
33 from ganeti import utils
34 from ganeti.hypervisor import hv_base
35 from ganeti.errors import HypervisorError
38 class LXCHypervisor(hv_base.BaseHypervisor):
39 """LXC-based virtualization.
41 Since current (Spring 2010) distributions are not yet ready for
42 running under a container, the following changes must be done
45 - disable the kernel log component of sysklogd/rsyslog/etc.,
46 otherwise they will fail to read the log, and at least rsyslog
47 will fill the filesystem with error messages
50 - move hardcoded parameters into hypervisor parameters, once we
51 have the container-parameter support
52 - implement memory limits, but only optionally, depending on host
56 - LXC is very temperamental; in daemon mode, it succeeds or fails
57 in launching the instance silently, without any error
58 indication, and when failing it can leave network interfaces
59 around, and future successful startups will list the instance
61 - shutdown sequence of containers leaves the init 'dead', and the
62 container effectively stopped, but LXC still believes the
63 container to be running; need to investigate using the
64 notify_on_release and release_agent feature of cgroups
67 _ROOT_DIR = constants.RUN_GANETI_DIR + "/lxc"
68 _LOG_FILE = constants.LOG_DIR + "hv_lxc.log"
73 "c 1:8", # /dev/random
74 "c 1:9", # /dev/urandom
77 "c 5:1", # /dev/console
79 "c 136:*", # first block of Unix98 PTY slaves
81 _DENIED_CAPABILITIES = [
82 "mac_override", # Allow MAC configuration or state changes
83 # TODO: remove sys_admin too, for safety
84 #"sys_admin", # Perform a range of system administration operations
85 "sys_boot", # Use reboot(2) and kexec_load(2)
86 "sys_module", # Load and unload kernel modules
87 "sys_time", # Set system clock, set real-time (hardware) clock
95 hv_base.BaseHypervisor.__init__(self)
96 utils.EnsureDirs([(self._ROOT_DIR, self._DIR_MODE)])
99 def _GetMountSubdirs(path):
100 """Return the list of mountpoints under a given path.
104 for _, mountpoint, _, _ in utils.GetMounts():
105 if (mountpoint.startswith(path) and
107 result.append(mountpoint)
109 result.sort(key=lambda x: x.count("/"), reverse=True)
113 def _InstanceDir(cls, instance_name):
114 """Return the root directory for an instance.
117 return utils.PathJoin(cls._ROOT_DIR, instance_name)
120 def _InstanceConfFile(cls, instance_name):
121 """Return the configuration file for an instance.
124 return utils.PathJoin(cls._ROOT_DIR, instance_name + ".conf")
126 def ListInstances(self):
127 """Get the list of running instances.
130 result = utils.RunCmd(["lxc-ls"])
132 raise errors.HypervisorError("Can't run lxc-ls: %s" % result.output)
133 return result.stdout.splitlines()
135 def GetInstanceInfo(self, instance_name):
136 """Get instance properties.
138 @type instance_name: string
139 @param instance_name: the instance name
141 @return: (name, id, memory, vcpus, stat, times)
144 # TODO: read container info from the cgroup mountpoint
146 result = utils.RunCmd(["lxc-info", "-n", instance_name])
148 raise errors.HypervisorError("Can't run lxc-info: %s" % result.output)
149 # lxc-info output examples:
150 # 'ganeti-lxc-test1' is STOPPED
151 # 'ganeti-lxc-test1' is RUNNING
152 _, state = result.stdout.rsplit(None, 1)
153 if state == "RUNNING":
154 return (instance_name, 0, 0, 0, 0, 0)
157 def GetAllInstancesInfo(self):
158 """Get properties of all instances.
160 @return: [(name, id, memory, vcpus, stat, times),...]
163 # TODO: read container info from the cgroup mountpoint
165 for name in self.ListInstances():
166 data.append((name, 0, 0, 0, 0, 0))
169 def _CreateConfigFile(self, instance, root_dir):
170 """Create an lxc.conf file for an instance"""
173 out.append("lxc.utsname = %s" % instance.name)
175 # separate pseudo-TTY instances
176 out.append("lxc.pts = 255")
178 out.append("lxc.tty = 6")
180 console_log = utils.PathJoin(self._ROOT_DIR, instance.name + ".console")
182 utils.WriteFile(console_log, data="", mode=constants.SECURE_FILE_MODE)
183 except EnvironmentError, err:
184 raise errors.HypervisorError("Creating console log file %s for"
185 " instance %s failed: %s" %
186 (console_log, instance.name, err))
187 out.append("lxc.console = %s" % console_log)
190 out.append("lxc.rootfs = %s" % root_dir)
192 # TODO: additional mounts, if we disable CAP_SYS_ADMIN
195 # deny direct device access
196 out.append("lxc.cgroup.devices.deny = a")
197 for devinfo in self._DEVS:
198 out.append("lxc.cgroup.devices.allow = %s rw" % devinfo)
201 for idx, nic in enumerate(instance.nics):
202 out.append("# NIC %d" % idx)
203 mode = nic.nicparams[constants.NIC_MODE]
204 link = nic.nicparams[constants.NIC_LINK]
205 if mode == constants.NIC_MODE_BRIDGED:
206 out.append("lxc.network.type = veth")
207 out.append("lxc.network.link = %s" % link)
209 raise errors.HypervisorError("LXC hypervisor only supports"
210 " bridged mode (NIC %d has mode %s)" %
212 out.append("lxc.network.hwaddr = %s" % nic.mac)
213 out.append("lxc.network.flags = up")
216 for cap in self._DENIED_CAPABILITIES:
217 out.append("lxc.cap.drop = %s" % cap)
219 return "\n".join(out) + "\n"
221 def StartInstance(self, instance, block_devices):
222 """Start an instance.
224 For LCX, we try to mount the block device and execute 'lxc-start
225 start' (we use volatile containers).
228 root_dir = self._InstanceDir(instance.name)
230 utils.EnsureDirs([(root_dir, self._DIR_MODE)])
231 except errors.GenericError, err:
232 raise HypervisorError("Cannot create instance directory: %s", str(err))
234 conf_file = self._InstanceConfFile(instance.name)
235 utils.WriteFile(conf_file, data=self._CreateConfigFile(instance, root_dir))
237 if not os.path.ismount(root_dir):
238 if not block_devices:
239 raise HypervisorError("LXC needs at least one disk")
241 sda_dev_path = block_devices[0][1]
242 result = utils.RunCmd(["mount", sda_dev_path, root_dir])
244 raise HypervisorError("Can't mount the chroot dir: %s" % result.output)
245 # TODO: replace the global log file with a per-instance log file
246 result = utils.RunCmd(["lxc-start", "-n", instance.name,
247 "-o", self._LOG_FILE, "-l", "DEBUG",
248 "-f", conf_file, "-d"])
250 raise HypervisorError("Running the lxc-start script failed: %s" %
253 def StopInstance(self, instance, force=False, retry=False, name=None):
256 This method has complicated cleanup tests, as we must:
257 - try to kill all leftover processes
258 - try to unmount any additional sub-mountpoints
259 - finally unmount the instance dir
265 root_dir = self._InstanceDir(name)
266 if not os.path.exists(root_dir):
269 if name in self.ListInstances():
270 # Signal init to shutdown; this is a hack
271 if not retry and not force:
272 result = utils.RunCmd(["chroot", root_dir, "poweroff"])
274 raise HypervisorError("Can't run 'poweroff' for the instance: %s" %
277 result = utils.RunCmd(["lxc-stop", "-n", name])
279 logging.warning("Error while doing lxc-stop for %s: %s", name,
282 for mpath in self._GetMountSubdirs(root_dir):
283 result = utils.RunCmd(["umount", mpath])
285 logging.warning("Error while umounting subpath %s for instance %s: %s",
286 mpath, name, result.output)
288 result = utils.RunCmd(["umount", root_dir])
289 if result.failed and force:
290 msg = ("Processes still alive in the chroot: %s" %
291 utils.RunCmd("fuser -vm %s" % root_dir).output)
293 raise HypervisorError("Can't umount the chroot dir: %s (%s)" %
294 (result.output, msg))
296 def RebootInstance(self, instance):
297 """Reboot an instance.
299 This is not (yet) implemented (in Ganeti) for the LXC hypervisor.
302 # TODO: implement reboot
303 raise HypervisorError("The LXC hypervisor doesn't implement the"
304 " reboot functionality")
306 def GetNodeInfo(self):
307 """Return information about the node.
309 This is just a wrapper over the base GetLinuxNodeInfo method.
311 @return: a dict with the following keys (values in MiB):
312 - memory_total: the total memory size on the node
313 - memory_free: the available memory on the node for instances
314 - memory_dom0: the memory used by the node itself, if available
317 return self.GetLinuxNodeInfo()
320 def GetShellCommandForConsole(cls, instance, hvparams, beparams):
321 """Return a command for connecting to the console of an instance.
324 return "lxc-console -n %s" % instance.name
327 """Verify the hypervisor.
329 For the chroot manager, it just checks the existence of the base dir.
332 if not os.path.exists(self._ROOT_DIR):
333 return "The required directory '%s' does not exist." % self._ROOT_DIR
336 def PowercycleNode(cls):
337 """LXC powercycle, just a wrapper over Linux powercycle.
340 cls.LinuxPowercycle()
342 def MigrateInstance(self, instance, target, live):
343 """Migrate an instance.
345 @type instance: L{objects.Instance}
346 @param instance: the instance to be migrated
348 @param target: hostname (usually ip) of the target node
350 @param live: whether to do a live or non-live migration
353 raise HypervisorError("Migration not supported by the LXC hypervisor")