_DIR_MODE = 0755
PARAMETERS = {
+ constants.HV_CPU_MASK: hv_base.OPT_CPU_MASK_CHECK,
}
def __init__(self):
except EnvironmentError, err:
raise errors.HypervisorError("Getting CPU list for instance"
" %s failed: %s" % (instance_name, err))
- # cpuset.cpus format: comma-separated list of CPU ids
- # or dash-separated id ranges
- # Example: "0-1,3"
- cpu_list = []
- for range_def in cpus.split(","):
- boundaries = range_def.split("-")
- n_elements = len(boundaries)
- lower = int(boundaries[0])
- higher = int(boundaries[n_elements - 1])
- cpu_list.extend(range(lower, higher + 1))
- return cpu_list
+
+ return utils.ParseCpuMask(cpus)
def ListInstances(self):
"""Get the list of running instances.
"""
result = utils.RunCmd(["lxc-ls"])
if result.failed:
- raise errors.HypervisorError("Can't run lxc-ls: %s" % result.output)
+ raise errors.HypervisorError("Running lxc-ls failed: %s" % result.output)
return result.stdout.splitlines()
def GetInstanceInfo(self, instance_name):
result = utils.RunCmd(["lxc-info", "-n", instance_name])
if result.failed:
- raise errors.HypervisorError("Can't run lxc-info: %s" % result.output)
+ raise errors.HypervisorError("Running lxc-info failed: %s" %
+ result.output)
# lxc-info output examples:
# 'ganeti-lxc-test1' is STOPPED
# 'ganeti-lxc-test1' is RUNNING
_, state = result.stdout.rsplit(None, 1)
+ if state != "RUNNING":
+ return None
cpu_list = self._GetCgroupCpuList(instance_name)
-
- if state == "RUNNING":
- return (instance_name, 0, 0, len(cpu_list), 0, 0)
- return None
+ return (instance_name, 0, 0, len(cpu_list), 0, 0)
def GetAllInstancesInfo(self):
"""Get properties of all instances.
return data
def _CreateConfigFile(self, instance, root_dir):
- """Create an lxc.conf file for an instance"""
+ """Create an lxc.conf file for an instance.
+
+ """
out = []
# hostname
out.append("lxc.utsname = %s" % instance.name)
# TODO: additional mounts, if we disable CAP_SYS_ADMIN
+ # CPUs
+ if instance.hvparams[constants.HV_CPU_MASK]:
+ cpu_list = utils.ParseCpuMask(instance.hvparams[constants.HV_CPU_MASK])
+ cpus_in_mask = len(cpu_list)
+ if cpus_in_mask != instance.beparams["vcpus"]:
+ raise errors.HypervisorError("Number of VCPUs (%d) doesn't match"
+ " the number of CPUs in the"
+ " cpu_mask (%d)" %
+ (instance.beparams["vcpus"],
+ cpus_in_mask))
+ out.append("lxc.cgroup.cpuset.cpus = %s" %
+ instance.hvparams[constants.HV_CPU_MASK])
+
# Device control
# deny direct device access
out.append("lxc.cgroup.devices.deny = a")
def StartInstance(self, instance, block_devices):
"""Start an instance.
- For LCX, we try to mount the block device and execute 'lxc-start
- start' (we use volatile containers).
+ For LCX, we try to mount the block device and execute 'lxc-start'.
+ We use volatile containers.
"""
root_dir = self._InstanceDir(instance.name)
try:
utils.EnsureDirs([(root_dir, self._DIR_MODE)])
except errors.GenericError, err:
- raise HypervisorError("Cannot create instance directory: %s", str(err))
+ raise HypervisorError("Creating instance directory failed: %s", str(err))
conf_file = self._InstanceConfFile(instance.name)
utils.WriteFile(conf_file, data=self._CreateConfigFile(instance, root_dir))
sda_dev_path = block_devices[0][1]
result = utils.RunCmd(["mount", sda_dev_path, root_dir])
if result.failed:
- raise HypervisorError("Can't mount the chroot dir: %s" % result.output)
+ raise HypervisorError("Mounting the root dir of LXC instance %s"
+ " failed: %s" % (instance.name, result.output))
result = utils.RunCmd(["lxc-start", "-n", instance.name,
"-o", log_file,
"-l", "DEBUG",
if not retry and not force:
result = utils.RunCmd(["chroot", root_dir, "poweroff"])
if result.failed:
- raise HypervisorError("Can't run 'poweroff' for the instance: %s" %
- result.output)
+ raise HypervisorError("Running 'poweroff' on the instance"
+ " failed: %s" % result.output)
time.sleep(2)
result = utils.RunCmd(["lxc-stop", "-n", name])
if result.failed:
msg = ("Processes still alive in the chroot: %s" %
utils.RunCmd("fuser -vm %s" % root_dir).output)
logging.error(msg)
- raise HypervisorError("Can't umount the chroot dir: %s (%s)" %
+ raise HypervisorError("Unmounting the chroot dir failed: %s (%s)" %
(result.output, msg))
def RebootInstance(self, instance):
@param live: whether to do a live or non-live migration
"""
- raise HypervisorError("Migration not supported by the LXC hypervisor")
+ raise HypervisorError("Migration is not supported by the LXC hypervisor")