2019-11-06 07:33:15 -05:00
|
|
|
import datetime
|
2019-12-23 15:43:23 -05:00
|
|
|
import logging
|
2019-11-10 01:37:15 -05:00
|
|
|
import os
|
2019-11-06 03:47:08 -05:00
|
|
|
import re
|
2019-11-06 07:33:15 -05:00
|
|
|
import uuid
|
2019-11-06 03:47:08 -05:00
|
|
|
##
|
2019-12-23 15:43:23 -05:00
|
|
|
from lxml import etree
|
|
|
|
##
|
2019-11-06 03:47:08 -05:00
|
|
|
import aif.utils
|
|
|
|
import aif.constants
|
2019-11-01 02:54:51 -04:00
|
|
|
from . import _common
|
2019-11-06 03:47:08 -05:00
|
|
|
import aif.disk.block as block
|
|
|
|
import aif.disk.luks as luks
|
|
|
|
import aif.disk.lvm as lvm
|
2019-10-28 01:26:31 -04:00
|
|
|
|
2019-11-06 03:47:08 -05:00
|
|
|
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-11-06 03:47:08 -05:00
|
|
|
_BlockDev = _common.BlockDev
|
|
|
|
|
|
|
|
|
|
|
|
class Member(object):
|
|
|
|
def __init__(self, member_xml, partobj):
|
|
|
|
self.xml = member_xml
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.debug('member_xml: {0}'.format(etree.tostring(self.xml, with_tail = False).decode('utf-8')))
|
2019-11-06 03:47:08 -05:00
|
|
|
self.device = partobj
|
2019-11-06 12:48:18 -05:00
|
|
|
if not isinstance(self.device, (block.Disk,
|
|
|
|
block.Partition,
|
2019-11-06 03:47:08 -05:00
|
|
|
Array,
|
2019-11-06 12:48:18 -05:00
|
|
|
luks.LUKS,
|
|
|
|
lvm.LV)):
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.error(('partobj must be of type '
|
|
|
|
'aif.disk.block.Disk, '
|
|
|
|
'aif.disk.block.Partition, '
|
|
|
|
'aif.disk.luks.LUKS, '
|
|
|
|
'aif.disk.lvm.LV, or'
|
|
|
|
'aif.disk.mdadm.Array.'))
|
2019-12-24 01:56:29 -05:00
|
|
|
raise TypeError('Invalid partobj type')
|
2019-11-06 07:33:15 -05:00
|
|
|
_common.addBDPlugin('mdraid')
|
2019-11-06 03:47:08 -05:00
|
|
|
self.devpath = self.device.devpath
|
|
|
|
self.is_superblocked = None
|
|
|
|
self.superblock = None
|
|
|
|
self._parseDeviceBlock()
|
|
|
|
|
|
|
|
def _parseDeviceBlock(self):
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.info('Parsing {0} device block metainfo.'.format(self.devpath))
|
2019-11-06 07:33:15 -05:00
|
|
|
# TODO: parity with mdadm_fallback.Member._parseDeviceBlock
|
|
|
|
# key names currently (probably) don't match and need to confirm the information's all present
|
|
|
|
block = {}
|
|
|
|
try:
|
|
|
|
_block = _BlockDev.md.examine(self.devpath)
|
|
|
|
except _BlockDev.MDRaidError:
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.debug('Member device is not a member yet.')
|
2019-11-06 07:33:15 -05:00
|
|
|
self.is_superblocked = False
|
|
|
|
self.superblock = None
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 07:33:15 -05:00
|
|
|
for k in dir(_block):
|
|
|
|
if k.startswith('_'):
|
|
|
|
continue
|
|
|
|
elif k in ('copy', 'eval'):
|
|
|
|
continue
|
|
|
|
v = getattr(_block, k)
|
|
|
|
if k == 'level':
|
|
|
|
v = int(re.sub(r'^raid', '', v))
|
|
|
|
elif k == 'update_time':
|
|
|
|
v = datetime.datetime.fromtimestamp(v)
|
|
|
|
elif re.search('^(dev_)?uuid$', k):
|
|
|
|
v = uuid.UUID(hex = v)
|
|
|
|
block[k] = v
|
|
|
|
self.superblock = block
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.debug('Rendered superblock info: {0}'.format(block))
|
2019-11-06 07:33:15 -05:00
|
|
|
self.is_superblocked = True
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
def prepare(self):
|
2019-11-06 07:33:15 -05:00
|
|
|
try:
|
|
|
|
_BlockDev.md.denominate(self.devpath)
|
|
|
|
except _BlockDev.MDRaidError:
|
|
|
|
pass
|
|
|
|
_BlockDev.md.destroy(self.devpath)
|
2019-11-06 12:48:18 -05:00
|
|
|
self._parseDeviceBlock()
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Array(object):
|
|
|
|
def __init__(self, array_xml, homehost, devpath = None):
|
|
|
|
self.xml = array_xml
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.debug('array_xml: {0}'.format(etree.tostring(array_xml, with_tail = False).decode('utf-8')))
|
2019-11-08 16:49:41 -05:00
|
|
|
self.id = self.xml.attrib['id']
|
2019-11-06 03:47:08 -05:00
|
|
|
self.level = int(self.xml.attrib['level'])
|
|
|
|
if self.level not in aif.constants.MDADM_SUPPORTED_LEVELS:
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.error(('RAID level ({0}) must be one of: '
|
|
|
|
'{1}.').format(self.level,
|
2019-12-24 01:56:29 -05:00
|
|
|
', '.join([str(i) for i in aif.constants.MDADM_SUPPORTED_LEVELS])))
|
2019-12-23 15:43:23 -05:00
|
|
|
raise ValueError('Invalid RAID level')
|
2019-11-06 03:47:08 -05:00
|
|
|
self.metadata = self.xml.attrib.get('meta', '1.2')
|
|
|
|
if self.metadata not in aif.constants.MDADM_SUPPORTED_METADATA:
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.error(('Metadata version ({0}) must be one of: '
|
|
|
|
'{1}.').format(self.metadata, ', '.join(aif.constants.MDADM_SUPPORTED_METADATA)))
|
|
|
|
raise ValueError('Invalid metadata version')
|
2019-11-06 07:33:15 -05:00
|
|
|
_common.addBDPlugin('mdraid')
|
|
|
|
self.chunksize = int(self.xml.attrib.get('chunkSize', 512))
|
|
|
|
if self.level in (4, 5, 6, 10):
|
|
|
|
if not aif.utils.isPowerofTwo(self.chunksize):
|
2019-12-23 15:43:23 -05:00
|
|
|
# TODO: warn instead of raise exception? Will mdadm lose its marbles if it *isn't* a proper number?
|
|
|
|
_logger.error('Chunksize ({0}) must be a power of 2 for RAID level {1}.'.format(self.chunksize,
|
|
|
|
self.level))
|
|
|
|
raise ValueError('Invalid chunksize')
|
2019-11-06 07:33:15 -05:00
|
|
|
if self.level in (0, 4, 5, 6, 10):
|
|
|
|
if not aif.utils.hasSafeChunks(self.chunksize):
|
2019-12-23 15:43:23 -05:00
|
|
|
# TODO: warn instead of raise exception? Will mdadm lose its marbles if it *isn't* a proper number?
|
|
|
|
_logger.error('Chunksize ({0}) must be divisible by 4 for RAID level {1}'.format(self.chunksize,
|
|
|
|
self.level))
|
|
|
|
raise ValueError('Invalid chunksize')
|
2019-11-06 07:33:15 -05:00
|
|
|
self.layout = self.xml.attrib.get('layout', 'none')
|
|
|
|
if self.level in aif.constants.MDADM_SUPPORTED_LAYOUTS.keys():
|
|
|
|
matcher, layout_default = aif.constants.MDADM_SUPPORTED_LAYOUTS[self.level]
|
|
|
|
if not matcher.search(self.layout):
|
|
|
|
if layout_default:
|
|
|
|
self.layout = layout_default
|
|
|
|
else:
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.warning('Did not detect a valid layout.')
|
|
|
|
self.layout = None
|
2019-11-06 07:33:15 -05:00
|
|
|
else:
|
|
|
|
self.layout = None
|
2019-11-06 16:58:58 -05:00
|
|
|
self.name = self.xml.attrib['name']
|
|
|
|
self.fullname = '{0}:{1}'.format(self.homehost, self.name)
|
2019-11-06 07:33:15 -05:00
|
|
|
self.devpath = devpath
|
|
|
|
if not self.devpath:
|
2019-11-06 16:58:58 -05:00
|
|
|
self.devpath = '/dev/md/{0}'.format(self.name)
|
2019-11-06 07:33:15 -05:00
|
|
|
self.updateStatus()
|
|
|
|
self.homehost = homehost
|
|
|
|
self.members = []
|
|
|
|
self.state = None
|
|
|
|
self.info = None
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
def addMember(self, memberobj):
|
2019-11-06 07:33:15 -05:00
|
|
|
if not isinstance(memberobj, Member):
|
2019-12-23 15:43:23 -05:00
|
|
|
_logger.error('memberobj must be of type aif.disk.mdadm.Member.')
|
|
|
|
raise TypeError('Invalid memberobj type')
|
2019-11-06 07:33:15 -05:00
|
|
|
memberobj.prepare()
|
|
|
|
self.members.append(memberobj)
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
def create(self):
|
2019-11-06 07:33:15 -05:00
|
|
|
if not self.members:
|
2019-12-24 01:56:29 -05:00
|
|
|
_logger.error('Cannot create an array with no members.')
|
|
|
|
raise RuntimeError('Missing members')
|
2019-11-06 07:33:15 -05:00
|
|
|
opts = [_BlockDev.ExtraArg.new('--homehost',
|
|
|
|
self.homehost),
|
|
|
|
_BlockDev.ExtraArg.new('--name',
|
2019-11-06 16:58:58 -05:00
|
|
|
self.name)]
|
2019-11-06 07:33:15 -05:00
|
|
|
if self.layout:
|
|
|
|
opts.append(_BlockDev.ExtraArg.new('--layout',
|
|
|
|
self.layout))
|
2019-11-06 16:58:58 -05:00
|
|
|
_BlockDev.md.create(self.name,
|
2019-11-06 07:33:15 -05:00
|
|
|
str(self.level),
|
|
|
|
[i.devpath for i in self.members],
|
|
|
|
0,
|
|
|
|
self.metadata,
|
|
|
|
True,
|
|
|
|
(self.chunksize * 1024),
|
|
|
|
opts)
|
|
|
|
for m in self.members:
|
|
|
|
m._parseDeviceBlock()
|
|
|
|
self.updateStatus()
|
|
|
|
self.writeConf()
|
|
|
|
self.devpath = self.info['device']
|
|
|
|
self.state = 'new'
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
def start(self, scan = False):
|
2019-12-24 01:56:29 -05:00
|
|
|
_logger.info('Starting array {0}.'.format(self.name))
|
2019-11-06 07:33:15 -05:00
|
|
|
if not any((self.members, self.devpath)):
|
2019-12-24 01:56:29 -05:00
|
|
|
_logger.error('Cannot assemble an array with no members (for hints) or device path.')
|
|
|
|
raise RuntimeError('Cannot start unspecified array')
|
2019-11-06 07:33:15 -05:00
|
|
|
if scan:
|
|
|
|
target = None
|
|
|
|
else:
|
2019-11-06 16:58:58 -05:00
|
|
|
target = self.name
|
2019-11-06 07:33:15 -05:00
|
|
|
_BlockDev.md.activate(target,
|
|
|
|
[i.devpath for i in self.members], # Ignored if scan mode enabled
|
|
|
|
None,
|
|
|
|
True,
|
|
|
|
None)
|
|
|
|
self.state = 'assembled'
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
def stop(self):
|
2019-12-24 01:56:29 -05:00
|
|
|
_logger.error('Stopping aray {0}.'.format(self.name))
|
2019-11-06 16:58:58 -05:00
|
|
|
_BlockDev.md.deactivate(self.name)
|
2019-11-06 07:33:15 -05:00
|
|
|
self.state = 'disassembled'
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 03:47:08 -05:00
|
|
|
|
|
|
|
def updateStatus(self):
|
2019-11-06 16:58:58 -05:00
|
|
|
_status = _BlockDev.md.detail(self.name)
|
2019-11-06 07:33:15 -05:00
|
|
|
# TODO: parity with mdadm_fallback.Array.updateStatus
|
|
|
|
# key names currently (probably) don't match and need to confirm the information's all present
|
|
|
|
info = {}
|
|
|
|
for k in dir(_status):
|
|
|
|
if k.startswith('_'):
|
|
|
|
continue
|
|
|
|
elif k in ('copy',):
|
|
|
|
continue
|
|
|
|
v = getattr(_status, k)
|
|
|
|
if k == 'level':
|
|
|
|
v = int(re.sub(r'^raid', '', v))
|
|
|
|
elif k == 'creation_time':
|
|
|
|
# TODO: Is this portable/correct? Or do I need to do something like '%a %b %d %H:%M:%s %Y'?
|
|
|
|
v = datetime.datetime.strptime(v, '%c')
|
|
|
|
elif k == 'uuid':
|
|
|
|
v = uuid.UUID(hex = v)
|
|
|
|
info[k] = v
|
|
|
|
self.info = info
|
2019-12-24 01:56:29 -05:00
|
|
|
_logger.debug('Rendered info: {0}'.format(info))
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|
2019-11-06 07:33:15 -05:00
|
|
|
|
2019-12-24 01:56:29 -05:00
|
|
|
def writeConf(self, chroot_base):
|
|
|
|
conf = os.path.join(chroot_base, 'etc', 'mdadm.conf')
|
2019-11-06 07:33:15 -05:00
|
|
|
with open(conf, 'r') as fh:
|
|
|
|
conflines = fh.read().splitlines()
|
|
|
|
arrayinfo = ('ARRAY '
|
|
|
|
'{device} '
|
|
|
|
'metadata={metadata} '
|
|
|
|
'name={name} '
|
|
|
|
'UUID={converted_uuid}').format(**self.info,
|
|
|
|
converted_uuid = _BlockDev.md.get_md_uuid(str(self.info['uuid'])))
|
|
|
|
if arrayinfo not in conflines:
|
|
|
|
r = re.compile(r'^ARRAY\s+{0}'.format(self.info['device']))
|
|
|
|
nodev = True
|
|
|
|
for l in conflines:
|
|
|
|
if r.search(l):
|
|
|
|
nodev = False
|
2019-12-24 01:56:29 -05:00
|
|
|
# TODO: warning and skip instead?
|
|
|
|
_logger.error('An array already exists with that name but not with the same opts/GUID/etc.')
|
|
|
|
raise RuntimeError('Duplicate array')
|
2019-11-06 07:33:15 -05:00
|
|
|
if nodev:
|
|
|
|
with open(conf, 'a') as fh:
|
|
|
|
fh.write('{0}\n'.format(arrayinfo))
|
2019-12-11 04:33:15 -05:00
|
|
|
return(None)
|