2013-09-18 00:46:58 +02:00
|
|
|
from abc import ABCMeta
|
|
|
|
from abc import abstractmethod
|
2015-01-01 21:09:16 +01:00
|
|
|
from ..partitions.gap import PartitionGap
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.common.tools import log_check_call
|
|
|
|
from bootstrapvz.common.fsm_proxy import FSMProxy
|
2013-09-18 00:46:58 +02:00
|
|
|
from ..exceptions import PartitionError
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
|
2013-09-22 21:20:09 +02:00
|
|
|
class AbstractPartitionMap(FSMProxy):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Abstract representation of a partiton map
|
|
|
|
This class is a finite state machine and represents the state of the real partition map
|
|
|
|
"""
|
2013-09-15 13:19:45 +02:00
|
|
|
|
2013-09-18 00:46:58 +02:00
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# States the partition map can be in
|
2013-09-22 21:20:09 +02:00
|
|
|
events = [{'name': 'create', 'src': 'nonexistent', 'dst': 'unmapped'},
|
|
|
|
{'name': 'map', 'src': 'unmapped', 'dst': 'mapped'},
|
|
|
|
{'name': 'unmap', 'src': 'mapped', 'dst': 'unmapped'},
|
|
|
|
]
|
|
|
|
|
2014-01-19 01:02:29 +01:00
|
|
|
def __init__(self, bootloader):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2014-05-04 19:31:53 +02:00
|
|
|
:param str bootloader: Name of the bootloader we will use for bootstrapping
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
|
|
|
# Create the configuration for the state machine
|
2013-09-22 21:20:09 +02:00
|
|
|
cfg = {'initial': 'nonexistent', 'events': self.events, 'callbacks': {}}
|
|
|
|
super(AbstractPartitionMap, self).__init__(cfg)
|
|
|
|
|
|
|
|
def is_blocking(self):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Returns whether the partition map is blocking volume detach operations
|
|
|
|
|
2014-05-04 19:31:53 +02:00
|
|
|
:rtype: bool
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2013-10-05 22:01:05 +02:00
|
|
|
return self.fsm.current == 'mapped'
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
def get_total_size(self):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Returns the total size the partitions occupy
|
|
|
|
|
2014-05-04 19:31:53 +02:00
|
|
|
:return: The size of all partitions
|
2015-01-03 12:44:57 +01:00
|
|
|
:rtype: Sectors
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
|
|
|
# We just need the endpoint of the last partition
|
2014-01-19 01:02:29 +01:00
|
|
|
return self.partitions[-1].get_end()
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
def create(self, volume):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Creates the partition map
|
|
|
|
|
2014-05-04 19:31:53 +02:00
|
|
|
:param Volume volume: The volume to create the partition map on
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2013-09-22 21:20:09 +02:00
|
|
|
self.fsm.create(volume=volume)
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _before_create(self, event):
|
2013-09-18 00:46:58 +02:00
|
|
|
pass
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
def map(self, volume):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Maps the partition map to device nodes
|
|
|
|
|
2014-05-04 19:31:53 +02:00
|
|
|
:param Volume volume: The volume the partition map resides on
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2013-09-22 21:20:09 +02:00
|
|
|
self.fsm.map(volume=volume)
|
|
|
|
|
|
|
|
def _before_map(self, event):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2014-05-04 19:31:53 +02:00
|
|
|
:raises PartitionError: In case a partition could not be mapped.
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2013-09-22 21:20:09 +02:00
|
|
|
volume = event.volume
|
2013-09-15 13:19:45 +02:00
|
|
|
try:
|
2014-03-23 16:04:03 +01:00
|
|
|
# Ask kpartx how the partitions will be mapped before actually attaching them.
|
2014-02-23 22:16:10 +01:00
|
|
|
mappings = log_check_call(['kpartx', '-l', volume.device_path])
|
2013-09-15 13:19:45 +02:00
|
|
|
import re
|
|
|
|
regexp = re.compile('^(?P<name>.+[^\d](?P<p_idx>\d+)) : '
|
|
|
|
'(?P<start_blk>\d) (?P<num_blks>\d+) '
|
|
|
|
'{device_path} (?P<blk_offset>\d+)$'
|
|
|
|
.format(device_path=volume.device_path))
|
2014-05-02 17:36:08 +02:00
|
|
|
log_check_call(['kpartx', '-as', volume.device_path])
|
2015-01-19 01:20:29 +01:00
|
|
|
|
|
|
|
mappable_partitions = filter(lambda p: not isinstance(p, PartitionGap), self.partitions)
|
|
|
|
|
2013-09-15 13:19:45 +02:00
|
|
|
import os.path
|
2014-03-23 16:04:03 +01:00
|
|
|
# Run through the kpartx output and map the paths to the partitions
|
2013-09-15 13:19:45 +02:00
|
|
|
for mapping in mappings:
|
|
|
|
match = regexp.match(mapping)
|
|
|
|
if match is None:
|
2014-05-03 22:24:13 +02:00
|
|
|
raise PartitionError('Unable to parse kpartx output: ' + mapping)
|
2013-09-15 13:19:45 +02:00
|
|
|
partition_path = os.path.join('/dev/mapper', match.group('name'))
|
2013-12-14 19:37:23 +01:00
|
|
|
p_idx = int(match.group('p_idx')) - 1
|
2015-01-19 01:20:29 +01:00
|
|
|
mappable_partitions[p_idx].map(partition_path)
|
2013-09-15 13:19:45 +02:00
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# Check if any partition was not mapped
|
2013-09-15 13:19:45 +02:00
|
|
|
for idx, partition in enumerate(self.partitions):
|
2015-01-01 21:09:16 +01:00
|
|
|
if isinstance(partition, PartitionGap):
|
|
|
|
continue
|
2013-10-05 22:01:05 +02:00
|
|
|
if partition.fsm.current not in ['mapped', 'formatted']:
|
2015-01-19 01:20:29 +01:00
|
|
|
raise PartitionError('kpartx did not map partition #' + str(partition.get_index()))
|
2013-09-15 13:19:45 +02:00
|
|
|
|
2015-01-16 23:49:08 +01:00
|
|
|
except PartitionError:
|
2014-03-23 16:04:03 +01:00
|
|
|
# Revert any mapping and reraise the error
|
2013-09-15 13:19:45 +02:00
|
|
|
for partition in self.partitions:
|
2015-01-19 01:20:29 +01:00
|
|
|
if partition.fsm.can('unmap'):
|
2013-09-15 13:19:45 +02:00
|
|
|
partition.unmap()
|
2014-05-02 17:36:08 +02:00
|
|
|
log_check_call(['kpartx', '-ds', volume.device_path])
|
2015-01-16 23:49:08 +01:00
|
|
|
raise
|
2013-09-15 13:19:45 +02:00
|
|
|
|
|
|
|
def unmap(self, volume):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Unmaps the partition
|
|
|
|
|
2014-05-04 19:31:53 +02:00
|
|
|
:param Volume volume: The volume to unmap the partition map from
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2013-09-22 21:20:09 +02:00
|
|
|
self.fsm.unmap(volume=volume)
|
2013-09-15 13:19:45 +02:00
|
|
|
|
2013-09-22 21:20:09 +02:00
|
|
|
def _before_unmap(self, event):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2014-05-04 19:31:53 +02:00
|
|
|
:raises PartitionError: If the a partition cannot be unmapped
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2013-09-22 21:20:09 +02:00
|
|
|
volume = event.volume
|
2014-03-23 16:04:03 +01:00
|
|
|
# Run through all partitions before unmapping and make sure they can all be unmapped
|
2013-09-15 13:19:45 +02:00
|
|
|
for partition in self.partitions:
|
2015-01-01 21:09:16 +01:00
|
|
|
if isinstance(partition, PartitionGap):
|
|
|
|
continue
|
2013-10-05 22:01:05 +02:00
|
|
|
if partition.fsm.cannot('unmap'):
|
2013-09-22 21:20:09 +02:00
|
|
|
msg = 'The partition {partition} prevents the unmap procedure'.format(partition=partition)
|
|
|
|
raise PartitionError(msg)
|
2014-03-23 16:04:03 +01:00
|
|
|
# Actually unmap the partitions
|
2014-05-02 17:36:08 +02:00
|
|
|
log_check_call(['kpartx', '-ds', volume.device_path])
|
2014-03-23 16:04:03 +01:00
|
|
|
# Call unmap on all partitions
|
2013-10-05 21:55:15 +02:00
|
|
|
for partition in self.partitions:
|
2015-01-01 21:09:16 +01:00
|
|
|
if isinstance(partition, PartitionGap):
|
|
|
|
continue
|
2013-10-05 21:55:15 +02:00
|
|
|
partition.unmap()
|