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-05-21 19:32:35 +00:00
|
|
|
# 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.
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
from diskimage_builder.block_device.blockdevicesetupexception \
|
|
|
|
import BlockDeviceSetupException
|
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-05-21 19:32:35 +00:00
|
|
|
from diskimage_builder.block_device.utils import parse_abs_size_spec
|
2016-07-16 20:16:13 +00:00
|
|
|
from diskimage_builder.graph.digraph import Digraph
|
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-05-21 19:32:35 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
class LocalLoop(Digraph.Node):
|
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-05-21 19:32:35 +00:00
|
|
|
"""Level0: Local loop image device handling.
|
|
|
|
|
|
|
|
This class handles local loop devices that can be used
|
|
|
|
for VM image installation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
type_string = "local_loop"
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
def __init__(self, config, default_config):
|
|
|
|
logger.debug("Creating LocalLoop object; config [%s] "
|
|
|
|
"default_config [%s]" % (config, default_config))
|
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-05-21 19:32:35 +00:00
|
|
|
if 'size' in config:
|
|
|
|
self.size = parse_abs_size_spec(config['size'])
|
|
|
|
logger.debug("Image size [%s]" % self.size)
|
|
|
|
else:
|
|
|
|
self.size = parse_abs_size_spec(default_config['image_size'])
|
|
|
|
logger.debug("Using default image size [%s]" % self.size)
|
|
|
|
if 'directory' in config:
|
|
|
|
self.image_dir = config['directory']
|
|
|
|
else:
|
|
|
|
self.image_dir = default_config['image_dir']
|
|
|
|
self.name = config['name']
|
2016-07-16 20:16:13 +00:00
|
|
|
Digraph.Node.__init__(self, self.name)
|
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-05-21 19:32:35 +00:00
|
|
|
self.filename = os.path.join(self.image_dir, self.name + ".raw")
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
def insert_nodes(self, dg):
|
|
|
|
dg.add_node(self)
|
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-05-21 19:32:35 +00:00
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
def insert_edges(self, dg):
|
|
|
|
"""Because this is created without base, there are no edges."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def image_create(filename, size):
|
|
|
|
logger.info("Create image file [%s]" % filename)
|
|
|
|
with open(filename, "w") as fd:
|
|
|
|
fd.seek(size - 1)
|
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-05-21 19:32:35 +00:00
|
|
|
fd.write("\0")
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def _image_delete(filename):
|
|
|
|
logger.info("Remove image file [%s]" % filename)
|
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _loopdev_attach(filename):
|
|
|
|
logger.info("loopdev attach")
|
|
|
|
logger.debug("Calling [sudo losetup --show -f %s]", filename)
|
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-05-21 19:32:35 +00:00
|
|
|
subp = subprocess.Popen(["sudo", "losetup", "--show", "-f",
|
2016-07-16 20:16:13 +00:00
|
|
|
filename], stdout=subprocess.PIPE)
|
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-05-21 19:32:35 +00:00
|
|
|
rval = subp.wait()
|
|
|
|
if rval == 0:
|
|
|
|
# [:-1]: Cut of the newline
|
2016-07-16 20:16:13 +00:00
|
|
|
block_device = subp.stdout.read()[:-1].decode("utf-8")
|
|
|
|
logger.info("New block device [%s]" % block_device)
|
|
|
|
return block_device
|
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-05-21 19:32:35 +00:00
|
|
|
else:
|
|
|
|
logger.error("losetup failed")
|
2016-07-16 20:16:13 +00:00
|
|
|
raise BlockDeviceSetupException("losetup failed")
|
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-05-21 19:32:35 +00:00
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
@staticmethod
|
|
|
|
def _loopdev_detach(loopdev):
|
|
|
|
logger.info("loopdev detach")
|
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-05-21 19:32:35 +00:00
|
|
|
# loopback dev may be tied up a bit by udev events triggered
|
|
|
|
# by partition events
|
|
|
|
for try_cnt in range(10, 1, -1):
|
2016-07-16 20:16:13 +00:00
|
|
|
logger.debug("Calling [sudo losetup -d %s]", loopdev)
|
|
|
|
subp = subprocess.Popen(["sudo", "losetup", "-d",
|
|
|
|
loopdev])
|
|
|
|
rval = subp.wait()
|
|
|
|
if rval == 0:
|
|
|
|
logger.info("Successfully detached [%s]" % loopdev)
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
logger.error("loopdev detach failed")
|
|
|
|
# Do not raise an error - maybe other cleanup methods
|
|
|
|
# can at least do some more work.
|
|
|
|
logger.debug("Gave up trying to detach [%s]" % loopdev)
|
|
|
|
return rval
|
|
|
|
|
|
|
|
def create(self, result, rollback):
|
|
|
|
logger.debug("[%s] Creating loop on [%s] with size [%d]" %
|
|
|
|
(self.name, self.filename, self.size))
|
|
|
|
|
|
|
|
rollback.append(lambda: self._image_delete(self.filename))
|
|
|
|
self.image_create(self.filename, self.size)
|
|
|
|
|
|
|
|
block_device = self._loopdev_attach(self.filename)
|
|
|
|
rollback.append(lambda: self._loopdev_detach(block_device))
|
|
|
|
|
|
|
|
result[self.name] = {"device": block_device,
|
|
|
|
"image": self.filename}
|
|
|
|
logger.debug("Created loop name [%s] device [%s] image [%s]"
|
|
|
|
% (self.name, block_device, self.filename))
|
|
|
|
return
|
|
|
|
|
|
|
|
def umount(self, state):
|
|
|
|
self._loopdev_detach(state[self.name]['device'])
|
|
|
|
|
|
|
|
def cleanup(self, state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delete(self, state):
|
|
|
|
self._image_delete(state[self.name]['image'])
|