from ganeti import errors
from ganeti import constants
from ganeti import objects
+from ganeti import compat
# Size of reads in _CanReadDevice
try:
utils.ReadFile(path, size=_DEVICE_READ_SIZE)
return True
- except EnvironmentError, err:
+ except EnvironmentError:
logging.warning("Can't read from device %s", path, exc_info=True)
return False
"""Logical Volume block device.
"""
+ _VALID_NAME_RE = re.compile("^[a-zA-Z0-9+_.-]*$")
+ _INVALID_NAMES = frozenset([".", "..", "snapshot", "pvmove"])
+ _INVALID_SUBSTRINGS = frozenset(["_mlog", "_mimage"])
+
def __init__(self, unique_id, children, size):
"""Attaches to a LV device.
if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
raise ValueError("Invalid configuration data %s" % str(unique_id))
self._vg_name, self._lv_name = unique_id
- self.dev_path = "/dev/%s/%s" % (self._vg_name, self._lv_name)
+ self._ValidateName(self._vg_name)
+ self._ValidateName(self._lv_name)
+ self.dev_path = utils.PathJoin("/dev", self._vg_name, self._lv_name)
self._degraded = True
self.major = self.minor = self.pe_size = self.stripe_count = None
self.Attach()
raise errors.ProgrammerError("Invalid configuration data %s" %
str(unique_id))
vg_name, lv_name = unique_id
+ cls._ValidateName(vg_name)
+ cls._ValidateName(lv_name)
pvs_info = cls.GetPVInfo([vg_name])
if not pvs_info:
_ThrowError("Can't compute PV info for vg %s", vg_name)
pvs_info.reverse()
pvlist = [ pv[1] for pv in pvs_info ]
- if utils.any(pvlist, lambda v: ":" in v):
- _ThrowError("Some of your PVs have invalid character ':'"
- " in their name")
+ if compat.any(pvlist, lambda v: ":" in v):
+ _ThrowError("Some of your PVs have the invalid character ':' in their"
+ " name, this is not supported - please filter them out"
+ " in lvm.conf using either 'filter' or 'preferred_names'")
free_size = sum([ pv[0] for pv in pvs_info ])
current_pvs = len(pvlist)
stripes = min(current_pvs, constants.LVM_STRIPECOUNT)
return data
+ @classmethod
+ def _ValidateName(cls, name):
+ """Validates that a given name is valid as VG or LV name.
+
+ The list of valid characters and restricted names is taken out of
+ the lvm(8) manpage, with the simplification that we enforce both
+ VG and LV restrictions on the names.
+
+ """
+ if (not cls._VALID_NAME_RE.match(name) or
+ name in cls._INVALID_NAMES or
+ compat.any(cls._INVALID_SUBSTRINGS, lambda x: x in name)):
+ _ThrowError("Invalid LVM name '%s'", name)
+
def Remove(self):
"""Remove this logical volume.
if result.failed:
_ThrowError("Failed to rename the logical volume: %s", result.output)
self._lv_name = new_name
- self.dev_path = "/dev/%s/%s" % (self._vg_name, self._lv_name)
+ self.dev_path = utils.PathJoin("/dev", self._vg_name, self._lv_name)
def Attach(self):
"""Attach to an existing LV.
try:
major = int(major)
minor = int(minor)
- except ValueError, err:
+ except (TypeError, ValueError), err:
logging.error("lvs major/minor cannot be parsed: %s", str(err))
try:
_ThrowError("Can't compute PV info for vg %s", self._vg_name)
pvs_info.sort()
pvs_info.reverse()
- free_size, pv_name, _ = pvs_info[0]
+ free_size, _, _ = pvs_info[0]
if free_size < size:
_ThrowError("Not enough free space: required %s,"
" available %s", size, free_size)
"""
_VERSION_RE = re.compile(r"^version: (\d+)\.(\d+)\.(\d+)"
r" \(api:(\d+)/proto:(\d+)(?:-(\d+))?\)")
+ _VALID_LINE_RE = re.compile("^ *([0-9]+): cs:([^ ]+).*$")
+ _UNUSED_LINE_RE = re.compile("^ *([0-9]+): cs:Unconfigured$")
_DRBD_MAJOR = 147
_ST_UNCONFIGURED = "Unconfigured"
_ThrowError("Can't read any data from %s", filename)
return data
- @staticmethod
- def _MassageProcData(data):
+ @classmethod
+ def _MassageProcData(cls, data):
"""Transform the output of _GetProdData into a nicer form.
@return: a dictionary of minor: joined lines from /proc/drbd
for that minor
"""
- lmatch = re.compile("^ *([0-9]+):.*$")
results = {}
old_minor = old_line = None
for line in data:
if not line: # completely empty lines, as can be returned by drbd8.0+
continue
- lresult = lmatch.match(line)
+ lresult = cls._VALID_LINE_RE.match(line)
if lresult is not None:
if old_minor is not None:
results[old_minor] = old_line
data = cls._GetProcData()
used_devs = {}
- valid_line = re.compile("^ *([0-9]+): cs:([^ ]+).*$")
for line in data:
- match = valid_line.match(line)
+ match = cls._VALID_LINE_RE.match(line)
if not match:
continue
minor = int(match.group(1))
result.fail_reason, result.output)
try:
sectors = int(result.stdout)
- except ValueError:
+ except (TypeError, ValueError):
_ThrowError("Invalid output from blockdev: '%s'", result.stdout)
bytes = sectors * 512
if bytes < 128 * 1024 * 1024: # less than 128MiB
"""
data = cls._GetProcData()
- unused_line = re.compile("^ *([0-9]+): cs:Unconfigured$")
- used_line = re.compile("^ *([0-9]+): cs:")
highest = None
for line in data:
- match = unused_line.match(line)
+ match = cls._UNUSED_LINE_RE.match(line)
if match:
return int(match.group(1))
- match = used_line.match(line)
+ match = cls._VALID_LINE_RE.match(line)
if match:
minor = int(match.group(1))
highest = max(highest, minor)
"--create-device"]
if size:
args.extend(["-d", "%sm" % size])
+ if not constants.DRBD_BARRIERS: # disable barriers, if configured so
+ version = cls._GetVersion()
+ # various DRBD versions support different disk barrier options;
+ # what we aim here is to revert back to the 'drain' method of
+ # disk flushes and to disable metadata barriers, in effect going
+ # back to pre-8.0.7 behaviour
+ vmaj = version['k_major']
+ vmin = version['k_minor']
+ vrel = version['k_point']
+ assert vmaj == 8
+ if vmin == 0: # 8.0.x
+ if vrel >= 12:
+ args.extend(['-i', '-m'])
+ elif vmin == 2: # 8.2.x
+ if vrel >= 7:
+ args.extend(['-i', '-m'])
+ elif vmaj >= 3: # 8.3.x or newer
+ args.extend(['-i', '-a', 'm'])
result = utils.RunCmd(args)
if result.failed:
_ThrowError("drbd%d: can't attach local disk: %s", minor, result.output)
the attach if can return success.
"""
+ # TODO: Rewrite to not use a for loop just because there is 'break'
+ # pylint: disable-msg=W0631
net_data = (self._lhost, self._lport, self._rhost, self._rport)
for minor in (self._aminor,):
info = self._GetDevInfo(self._GetShowData(minor))
if err.errno != errno.ENOENT:
_ThrowError("Can't remove file '%s': %s", self.dev_path, err)
+ def Rename(self, new_id):
+ """Renames the file.
+
+ """
+ # TODO: implement rename for file-based storage
+ _ThrowError("Rename is not supported for file-based storage")
+
+ def Grow(self, amount):
+ """Grow the file
+
+ @param amount: the amount (in mebibytes) to grow with
+
+ """
+ # Check that the file exists
+ self.Assemble()
+ current_size = self.GetActualSize()
+ new_size = current_size + amount * 1024 * 1024
+ assert new_size > current_size, "Cannot Grow with a negative amount"
+ try:
+ f = open(self.dev_path, "a+")
+ f.truncate(new_size)
+ f.close()
+ except EnvironmentError, err:
+ _ThrowError("Error in file growth: %", str(err))
+
def Attach(self):
"""Attach to an existing file.
if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
raise ValueError("Invalid configuration data %s" % str(unique_id))
dev_path = unique_id[1]
- if os.path.exists(dev_path):
- _ThrowError("File already existing: %s", dev_path)
try:
- f = open(dev_path, 'w')
+ fd = os.open(dev_path, os.O_RDWR | os.O_CREAT | os.O_EXCL)
+ f = os.fdopen(fd, "w")
f.truncate(size * 1024 * 1024)
f.close()
- except IOError, err:
+ except EnvironmentError, err:
+ if err.errno == errno.EEXIST:
+ _ThrowError("File already existing: %s", dev_path)
_ThrowError("Error in file creation: %", str(err))
return FileStorage(unique_id, children, size)
DEV_MAP = {
constants.LD_LV: LogicalVolume,
constants.LD_DRBD8: DRBD8,
- constants.LD_FILE: FileStorage,
}
+if constants.ENABLE_FILE_STORAGE:
+ DEV_MAP[constants.LD_FILE] = FileStorage
+
def FindDevice(dev_type, unique_id, children, size):
"""Search for an existing, assembled device.