diskimage-builder/diskimage_builder/block_device/levelbase.py
Andreas Florath 3d48a528c1 Refactor: block-device handling (local loop)
Block device handling can be somewhat complex - especially
when taking things like md, lvm or encryption into account.

This patch factors out the creation and deletion of the local
loop image device handling into a python library.

The main propose of this patch is to implement the needed
infrastructure.  Based on this, more advanced functions can be added.
Example: (advanced) partitioning, LVM, handling different boot
scenarios (BIOS, UEFI, ...), possibility of handling multiple images
(local loop image, iSCSI, physical hard disk, ...), handling of
different filesystems for different partitions / LVs.

Change-Id: Ib626b36a00f8a5dc3dbde8df3e2619a2438eaaf1
Signed-off-by: Andreas Florath <andreas@florath.net>
2016-09-08 04:31:01 +00:00

67 lines
2.3 KiB
Python

# Copyright 2016 Andreas Florath (andreas@florath.net)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import sys
logger = logging.getLogger(__name__)
class LevelBase(object):
def __init__(self, lvl, config, default_config, result, sub_modules):
self.level = lvl
self.config = config
self.default_config = default_config
self.result = result
self.sub_modules = sub_modules
def call_sub_modules(self, callback):
"""Generic way calling submodules"""
result = {}
if self.result is not None:
result = self.result.copy()
for name, cfg in self.config:
if name in self.sub_modules:
logger.info("Calling sub module [%s]" % name)
sm = self.sub_modules[name](cfg, self.default_config,
self.result)
lres = callback(sm)
result.update(lres)
else:
logger.error("Unknown sub module [%s]" % name)
sys.exit(1)
return result
def create_cb(self, obj):
return obj.create()
def create(self):
"""Create the configured block devices"""
logger.info("Starting to create level [%d] block devices" % self.level)
result = self.call_sub_modules(self.create_cb)
logger.info("Finished creating level [%d] block devices" % self.level)
return result
def delete_cb(self, obj):
return obj.delete()
def delete(self):
"""Delete the configured block devices"""
logger.info("Starting to delete level [%d] block devices" % self.level)
res = self.call_sub_modules(self.delete_cb)
logger.info("Finished deleting level [%d] block devices" % self.level)
return all(p for p in res.values())