"""
if not isinstance(unique_id, (tuple, list)) or len(unique_id) != 2:
- raise ValueError("Invalid configuration data %s" % str(unique_id))
+ raise errors.ProgrammerError("Invalid configuration data %s" %
+ str(unique_id))
vg_name, lv_name = unique_id
pvs_info = cls.GetPVInfo(vg_name)
if not pvs_info:
result = utils.RunCmd(["lvcreate", "-L%dm" % size, "-n%s" % lv_name,
vg_name] + pvlist)
if result.failed:
- raise errors.BlockDeviceError("%s - %s" % (result.fail_reason,
- result.output))
+ raise errors.BlockDeviceError("LV create failed (%s): %s" %
+ (result.fail_reason, result.output))
return LogicalVolume(unique_id, children)
@staticmethod
Note that this doesn't support unconfigured devices (cs:Unconfigured).
"""
+ UNCONF_RE = re.compile(r"\s*[0-9]+:\s*cs:Unconfigured$")
LINE_RE = re.compile(r"\s*[0-9]+:\s*cs:(\S+)\s+st:([^/]+)/(\S+)"
"\s+ds:([^/]+)/(\S+)\s+.*$")
SYNC_RE = re.compile(r"^.*\ssync'ed:\s*([0-9.]+)%.*"
"\sfinish: ([0-9]+):([0-9]+):([0-9]+)\s.*$")
def __init__(self, procline):
- m = self.LINE_RE.match(procline)
- if not m:
- raise errors.BlockDeviceError("Can't parse input data '%s'" % procline)
- self.cstatus = m.group(1)
- self.lrole = m.group(2)
- self.rrole = m.group(3)
- self.ldisk = m.group(4)
- self.rdisk = m.group(5)
+ u = self.UNCONF_RE.match(procline)
+ if u:
+ self.cstatus = "Unconfigured"
+ self.lrole = self.rrole = self.ldisk = self.rdisk = None
+ else:
+ m = self.LINE_RE.match(procline)
+ if not m:
+ raise errors.BlockDeviceError("Can't parse input data '%s'" % procline)
+ self.cstatus = m.group(1)
+ self.lrole = m.group(2)
+ self.rrole = m.group(3)
+ self.ldisk = m.group(4)
+ self.rdisk = m.group(5)
+
+ # end reading of data from the LINE_RE or UNCONF_RE
self.is_standalone = self.cstatus == "StandAlone"
self.is_wfconn = self.cstatus == "WFConnection"
self.is_diskless = self.ldisk == "Diskless"
self.is_disk_uptodate = self.ldisk == "UpToDate"
+ self.is_in_resync = self.cstatus in ("SyncSource", "SyncTarget")
+ self.is_in_use = self.cstatus != "Unconfigured"
+
m = self.SYNC_RE.match(procline)
if m:
self.sync_percent = float(m.group(1))
return "/dev/drbd%d" % minor
@classmethod
- def _GetUsedDevs(cls):
+ def GetUsedDevs(cls):
"""Compute the list of used DRBD devices.
"""
return highest + 1
@classmethod
- def _IsValidMeta(cls, meta_device):
- """Check if the given meta device looks like a valid one.
-
- """
- minor = cls._FindUnusedMinor()
- minor_path = cls._DevPath(minor)
- result = utils.RunCmd(["drbdmeta", minor_path,
- "v08", meta_device, "0",
- "dstate"])
- if result.failed:
- logging.error("Invalid meta device %s: %s", meta_device, result.output)
- return False
- return True
-
- @classmethod
def _GetShowParser(cls):
"""Return a parser for `drbd show` output.
def _AssembleLocal(cls, minor, backend, meta):
"""Configure the local part of a DRBD device.
- This is the first thing that must be done on an unconfigured DRBD
- device. And it must be done only once.
-
"""
- if not cls._IsValidMeta(meta):
- return False
args = ["drbdsetup", cls._DevPath(minor), "disk",
backend, meta, "0", "-e", "detach", "--create-device"]
result = utils.RunCmd(args)
if not self._CheckMetaSize(meta.dev_path):
raise errors.BlockDeviceError("Invalid meta device size")
self._InitMeta(self._FindUnusedMinor(), meta.dev_path)
- if not self._IsValidMeta(meta.dev_path):
- raise errors.BlockDeviceError("Cannot initalize meta device")
if not self._AssembleLocal(self.minor, backend.dev_path, meta.dev_path):
raise errors.BlockDeviceError("Can't attach to local storage")
"C", dual_pri=multimaster)
def Attach(self):
- """Find a DRBD device which matches our config and attach to it.
+ """Check if our minor is configured.
+
+ This doesn't do any device configurations - it only checks if the
+ minor is in a state different from Unconfigured.
+
+ Note that this function will not change the state of the system in
+ any way (except in case of side-effects caused by reading from
+ /proc).
+
+ """
+ used_devs = self.GetUsedDevs()
+ if self._aminor in used_devs:
+ minor = self._aminor
+ else:
+ minor = None
+
+ self._SetFromMinor(minor)
+ return minor is not None
+
+ def Assemble(self):
+ """Assemble the drbd.
+
+ Method:
+ - if we have a configured device, we try to ensure that it matches
+ our config
+ - if not, we create it from zero
+
+ """
+ result = super(DRBD8, self).Assemble()
+ if not result:
+ return result
+
+ self.Attach()
+ if self.minor is None:
+ # local device completely unconfigured
+ return self._FastAssemble()
+ else:
+ # we have to recheck the local and network status and try to fix
+ # the device
+ return self._SlowAssemble()
+
+ def _SlowAssemble(self):
+ """Assembles the DRBD device from a (partially) configured device.
In case of partially attached (local device matches but no network
setup), we perform the network attach. If successful, we re-test
self._SetFromMinor(minor)
return minor is not None
- def Assemble(self):
- """Assemble the drbd.
+ def _FastAssemble(self):
+ """Assemble the drbd device from zero.
- Method:
- - if we have a local backing device, we bind to it by:
- - checking the list of used drbd devices
- - check if the local minor use of any of them is our own device
- - if yes, abort?
- - if not, bind
- - if we have a local/remote net info:
- - redo the local backing device step for the remote device
- - check if any drbd device is using the local port,
- if yes abort
- - check if any remote drbd device is using the remote
- port, if yes abort (for now)
- - bind our net port
- - bind the remote net port
+ This is run when in Assemble we detect our minor is unused.
"""
- self.Attach()
- if self.minor is not None:
- logging.info("Already assembled")
- return True
-
- result = super(DRBD8, self).Assemble()
- if not result:
- return result
-
# TODO: maybe completely tear-down the minor (drbdsetup ... down)
# before attaching our own?
minor = self._aminor
"""
if len(children) != 2:
raise errors.ProgrammerError("Invalid setup for the drbd device")
+ # check that the minor is unused
+ aminor = unique_id[4]
+ proc_info = cls._MassageProcData(cls._GetProcData())
+ if aminor in proc_info:
+ status = DRBD8Status(proc_info[aminor])
+ in_use = status.is_in_use
+ else:
+ in_use = False
+ if in_use:
+ raise errors.BlockDeviceError("DRBD minor %d already in use at"
+ " Create() time" % aminor)
meta = children[1]
meta.Assemble()
if not meta.Attach():
raise errors.BlockDeviceError("Can't attach to meta device")
if not cls._CheckMetaSize(meta.dev_path):
raise errors.BlockDeviceError("Invalid meta device size")
- cls._InitMeta(cls._FindUnusedMinor(), meta.dev_path)
- if not cls._IsValidMeta(meta.dev_path):
- raise errors.BlockDeviceError("Cannot initalize meta device")
+ cls._InitMeta(aminor, meta.dev_path)
return cls(unique_id, children)
def Grow(self, amount):
@return: an instance of FileStorage
"""
+ # TODO: decide whether we should check for existing files and
+ # abort or not
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]
try:
f = open(dev_path, 'w')
- except IOError, err:
- raise errors.BlockDeviceError("Could not create '%'" % err)
- else:
f.truncate(size * 1024 * 1024)
f.close()
+ except IOError, err:
+ raise errors.BlockDeviceError("Error in file creation: %" % str(err))
return FileStorage(unique_id, children)
return device
-def AttachOrAssemble(dev_type, unique_id, children):
+def Assemble(dev_type, unique_id, children):
"""Try to attach or assemble an existing device.
- This will attach to an existing assembled device or will assemble
- the device, as needed, to bring it fully up.
+ This will attach to assemble the device, as needed, to bring it
+ fully up. It must be safe to run on already-assembled devices.
"""
if dev_type not in DEV_MAP:
raise errors.ProgrammerError("Invalid block device type '%s'" % dev_type)
device = DEV_MAP[dev_type](unique_id, children)
- if not device.attached:
- device.Assemble()
- if not device.attached:
- raise errors.BlockDeviceError("Can't find a valid block device for"
- " %s/%s/%s" %
- (dev_type, unique_id, children))
+ if not device.Assemble():
+ raise errors.BlockDeviceError("Can't find a valid block device for"
+ " %s/%s/%s" %
+ (dev_type, unique_id, children))
return device