2017-04-22 07:49:22 +00:00
|
|
|
# Copyright 2016-2017 Andreas Florath (andreas@florath.net)
|
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
|
|
|
#
|
|
|
|
# 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
|
|
|
import codecs
|
2017-05-31 01:24:55 +00:00
|
|
|
import collections
|
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 json
|
|
|
|
import logging
|
|
|
|
import os
|
Use picked nodes for later cmd_* calls
Currently the later cmd_* calls -- umount, cleanup, delete -- all
recreate the node graph by parsing the config file using
create_graph()
There is some need, however, to have a sense of global state when
building the node list. The problem is, this is a one time operation
-- we do not want to rebuild that state for these later calls (see the
"loaded" checks in proposed
Ic3b805f9258128d5233b21ff25579c03487c7fcc).
An insight here seems to be that these cmd_* calls do not actually
want to re-parse the configuration file and rebuild the node list;
they just want to walk the node list in reverse with the state as
provided after cmd_create().
So, rather than re-creating the node list, we might as well just
pickle it, save it to disk along side the state dictionary dump and
reload it for cmd_*.
After this, I think we can safely have PluginBase.__init__() be passed
the state. We will now know that this will only be called once,
during initial creation.
Change-Id: I68840594a34af28d41d9522addcfd830bd203b97
2017-05-31 05:09:37 +00:00
|
|
|
import pickle
|
2017-05-30 02:06:41 +00:00
|
|
|
import pprint
|
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 shutil
|
2016-07-16 20:16:13 +00:00
|
|
|
import yaml
|
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
|
|
|
|
2017-05-26 00:32:59 +00:00
|
|
|
from diskimage_builder.block_device.config import config_tree_to_graph
|
|
|
|
from diskimage_builder.block_device.config import create_graph
|
2017-05-30 02:06:41 +00:00
|
|
|
from diskimage_builder.block_device.exception import \
|
|
|
|
BlockDeviceSetupException
|
2017-01-29 23:52:40 +00:00
|
|
|
from diskimage_builder.block_device.utils import exec_sudo
|
2017-05-02 03:43:43 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
def _load_json(file_name):
|
|
|
|
"""Load file from .json file on disk, return None if not existing"""
|
|
|
|
if os.path.exists(file_name):
|
|
|
|
with codecs.open(file_name, encoding="utf-8", mode="r") as fd:
|
|
|
|
return json.load(fd)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-05-31 01:24:55 +00:00
|
|
|
class BlockDeviceState(collections.MutableMapping):
|
2017-05-30 02:06:41 +00:00
|
|
|
"""The global state singleton
|
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
An reference to an instance of this object is saved into nodes as
|
|
|
|
a global repository. It wraps a single dictionary "state" and
|
|
|
|
provides a few helper functions.
|
2017-05-30 02:06:41 +00:00
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
The state ends up used in two contexts:
|
2017-05-30 02:06:41 +00:00
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
- The node list (including this state) is pickled and dumped
|
|
|
|
between cmd_create() and later cmd_* calls that need to call
|
|
|
|
the nodes.
|
2017-05-30 02:06:41 +00:00
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
- Some other cmd_* calls, such as cmd_writefstab, only need
|
|
|
|
access to values inside the state and not the whole node list,
|
|
|
|
and load it from the json dump created after cmd_create()
|
2017-05-30 02:06:41 +00:00
|
|
|
"""
|
|
|
|
# XXX:
|
|
|
|
# - we could implement getters/setters such that if loaded from
|
|
|
|
# disk, the state is read-only? or make it append-only
|
|
|
|
# (i.e. you can't overwrite existing keys)
|
|
|
|
def __init__(self, filename=None):
|
|
|
|
"""Initialise state
|
|
|
|
|
|
|
|
:param filename: if :param:`filename` is passed and exists, it
|
|
|
|
will be loaded as the state. If it does not exist an
|
|
|
|
exception is raised. If :param:`filename` is not
|
|
|
|
passed, state will be initalised to a blank dictionary.
|
|
|
|
"""
|
|
|
|
if filename:
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
raise BlockDeviceSetupException("State dump not found")
|
|
|
|
else:
|
|
|
|
self.state = _load_json(filename)
|
|
|
|
assert self.state is not None
|
|
|
|
else:
|
|
|
|
self.state = {}
|
|
|
|
|
2017-05-31 01:24:55 +00:00
|
|
|
def __delitem__(self, key):
|
|
|
|
del self.state[key]
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.state[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.state[key] = value
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.state)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.state)
|
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
def save_state(self, filename):
|
|
|
|
"""Persist the state to disk
|
|
|
|
|
|
|
|
:param filename: The file to persist state to
|
|
|
|
"""
|
|
|
|
logger.debug("Writing state to: %s", filename)
|
|
|
|
self.debug_dump()
|
|
|
|
with open(filename, "w") as fd:
|
|
|
|
json.dump(self.state, fd)
|
|
|
|
|
|
|
|
def debug_dump(self):
|
|
|
|
"""Log state to debug"""
|
|
|
|
# This is pretty good for human consumption, but maybe a bit
|
|
|
|
# verbose.
|
|
|
|
nice_output = pprint.pformat(self.state, width=40)
|
2020-05-12 20:20:31 +00:00
|
|
|
for line in nice_output.split('\n'):
|
|
|
|
logger.debug('{0:{fill}{align}50}'.format(
|
|
|
|
line, fill=' ', align='<'))
|
2017-05-30 02:06:41 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
class BlockDevice(object):
|
2017-01-26 18:33:02 +00:00
|
|
|
"""Handles block devices.
|
|
|
|
|
|
|
|
This class handles the complete setup and deletion of all aspects
|
|
|
|
of the block device level.
|
|
|
|
|
|
|
|
A typical call sequence:
|
|
|
|
|
2017-05-02 03:29:34 +00:00
|
|
|
cmd_init: initialize the block device level config. After this
|
2017-04-22 07:49:22 +00:00
|
|
|
call it is possible to e.g. query information from the (partially
|
|
|
|
automatic generated) internal state like root-label.
|
|
|
|
|
2017-05-01 07:19:49 +00:00
|
|
|
cmd_getval: retrieve information about the (internal) block device
|
|
|
|
state like the block image device (for bootloader) or the
|
|
|
|
root-label (for writing fstab).
|
|
|
|
|
2017-01-26 18:33:02 +00:00
|
|
|
cmd_create: creates all the different aspects of the block
|
|
|
|
device. When this call is successful, the complete block level
|
|
|
|
device is set up, filesystems are created and are mounted at
|
|
|
|
the correct position.
|
|
|
|
After this call it is possible to copy / install all the needed
|
|
|
|
files into the appropriate directories.
|
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
cmd_writefstab: creates the (complete) fstab for the system.
|
|
|
|
|
2017-01-26 18:33:02 +00:00
|
|
|
cmd_umount: unmount and detaches all directories and used many
|
|
|
|
resources. After this call the used (e.g.) images are still
|
|
|
|
available for further handling, e.g. converting from raw in
|
|
|
|
some other format.
|
|
|
|
|
|
|
|
cmd_cleanup: removes everything that was created with the
|
|
|
|
'cmd_create' call, i.e. all images files themselves and
|
|
|
|
internal temporary configuration.
|
|
|
|
|
|
|
|
cmd_delete: unmounts and removes everything that was created
|
|
|
|
during the 'cmd_create' all. This call should be used in error
|
|
|
|
conditions when there is the need to remove all allocated
|
|
|
|
resources immediately and as good as possible.
|
|
|
|
From the functional point of view this is mostly the same as a
|
|
|
|
call to 'cmd_umount' and 'cmd_cleanup' - but is typically more
|
|
|
|
error tolerance.
|
|
|
|
|
|
|
|
In a script this should be called in the following way:
|
|
|
|
|
2017-05-02 03:29:34 +00:00
|
|
|
dib-block-device init ...
|
2017-04-22 07:49:22 +00:00
|
|
|
# From that point the database can be queried, like
|
2017-05-02 03:29:34 +00:00
|
|
|
ROOT_LABEL=$(dib-block-device getval root-label)
|
2017-04-22 07:49:22 +00:00
|
|
|
|
|
|
|
Please note that currently the dib-block-device executable can
|
|
|
|
only be used outside the chroot.
|
|
|
|
|
2017-05-02 03:29:34 +00:00
|
|
|
dib-block-device create ...
|
|
|
|
trap "dib-block-device delete ..." EXIT
|
2017-01-26 18:33:02 +00:00
|
|
|
# copy / install files
|
2017-05-02 03:29:34 +00:00
|
|
|
dib-block-device umount ...
|
2017-01-26 18:33:02 +00:00
|
|
|
# convert image(s)
|
2017-05-02 03:29:34 +00:00
|
|
|
dib-block-device cleanup ...
|
2017-01-26 18:33:02 +00:00
|
|
|
trap - EXIT
|
|
|
|
"""
|
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
|
|
|
|
2018-01-09 18:20:51 +00:00
|
|
|
def _merge_rootfs_params(self):
|
|
|
|
"""Merge rootfs related parameters into configuration
|
|
|
|
|
|
|
|
To maintain compatability with some old block-device
|
|
|
|
environment variables from before we had a specific
|
|
|
|
block-device config, disk-image-create provides a "parameters"
|
|
|
|
file that translates the old bash-environment variables into a
|
|
|
|
YAML based configuration file (``self.params``).
|
|
|
|
|
|
|
|
Here we merge the values in this parameters file that relate
|
|
|
|
to the root file-system into the final configuration. We look
|
|
|
|
for the ``mkfs_root`` node in the new config, and pull the
|
|
|
|
relevant settings from the parameters into it.
|
2017-04-22 07:49:22 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
for entry in self.config:
|
|
|
|
for k, v in entry.items():
|
|
|
|
if k == 'mkfs':
|
|
|
|
if 'name' not in v:
|
|
|
|
continue
|
|
|
|
if v['name'] != 'mkfs_root':
|
|
|
|
continue
|
|
|
|
if 'type' not in v \
|
|
|
|
and 'root-fs-type' in self.params:
|
|
|
|
v['type'] = self.params['root-fs-type']
|
|
|
|
if 'opts' not in v \
|
|
|
|
and 'root-fs-opts' in self.params:
|
|
|
|
v['opts'] = self.params['root-fs-opts']
|
|
|
|
if 'label' not in v \
|
|
|
|
and 'root-label' in self.params:
|
|
|
|
if self.params['root-label'] is not None:
|
|
|
|
v['label'] = self.params['root-label']
|
|
|
|
else:
|
2018-01-09 18:20:51 +00:00
|
|
|
# The default label is "cloudimg-rootfs"
|
|
|
|
# for historical reasons (upstream
|
|
|
|
# images/EC2 defaults/cloud-init etc). We
|
|
|
|
# want to remain backwards compatible, but
|
|
|
|
# unfortunately that's too long for XFS so
|
|
|
|
# we've decided on 'img-rootfs' in that
|
|
|
|
# case. Note there's separate checks if
|
|
|
|
# the label is specified explicitly.
|
|
|
|
if v.get('type') == 'xfs':
|
|
|
|
v['label'] = 'img-rootfs'
|
|
|
|
else:
|
|
|
|
v['label'] = 'cloudimg-rootfs'
|
2017-04-22 07:49:22 +00:00
|
|
|
|
2017-05-02 05:15:26 +00:00
|
|
|
def __init__(self, params):
|
2017-05-02 02:08:33 +00:00
|
|
|
"""Create BlockDevice object
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
:param params: YAML file from --params
|
|
|
|
"""
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
logger.debug("Creating BlockDevice object")
|
|
|
|
|
2017-05-02 02:08:33 +00:00
|
|
|
self.params = params
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Params [%s]", self.params)
|
2017-04-22 07:49:22 +00:00
|
|
|
|
|
|
|
self.state_dir = os.path.join(
|
|
|
|
self.params['build-dir'], "states/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
|
|
|
self.state_json_file_name \
|
|
|
|
= os.path.join(self.state_dir, "state.json")
|
2017-04-22 07:49:22 +00:00
|
|
|
self.config_json_file_name \
|
|
|
|
= os.path.join(self.state_dir, "config.json")
|
Use picked nodes for later cmd_* calls
Currently the later cmd_* calls -- umount, cleanup, delete -- all
recreate the node graph by parsing the config file using
create_graph()
There is some need, however, to have a sense of global state when
building the node list. The problem is, this is a one time operation
-- we do not want to rebuild that state for these later calls (see the
"loaded" checks in proposed
Ic3b805f9258128d5233b21ff25579c03487c7fcc).
An insight here seems to be that these cmd_* calls do not actually
want to re-parse the configuration file and rebuild the node list;
they just want to walk the node list in reverse with the state as
provided after cmd_create().
So, rather than re-creating the node list, we might as well just
pickle it, save it to disk along side the state dictionary dump and
reload it for cmd_*.
After this, I think we can safely have PluginBase.__init__() be passed
the state. We will now know that this will only be called once,
during initial creation.
Change-Id: I68840594a34af28d41d9522addcfd830bd203b97
2017-05-31 05:09:37 +00:00
|
|
|
self.node_pickle_file_name \
|
|
|
|
= os.path.join(self.state_dir, "nodes.pickle")
|
2017-04-22 07:49:22 +00:00
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
self.config = _load_json(self.config_json_file_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
|
|
|
|
2017-04-22 07:49:22 +00:00
|
|
|
# This needs to exists for the state and config files
|
|
|
|
try:
|
|
|
|
os.makedirs(self.state_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def cmd_init(self):
|
|
|
|
"""Initialize block device setup
|
|
|
|
|
|
|
|
This initializes the block device setup layer. One major task
|
|
|
|
is to parse and check the configuration, write it down for
|
|
|
|
later examiniation and execution.
|
|
|
|
"""
|
|
|
|
with open(self.params['config'], "rt") as config_fd:
|
|
|
|
self.config = yaml.safe_load(config_fd)
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Config before merge [%s]", self.config)
|
2017-05-17 05:54:02 +00:00
|
|
|
self.config = config_tree_to_graph(self.config)
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Config before merge [%s]", self.config)
|
2018-01-09 18:20:51 +00:00
|
|
|
self._merge_rootfs_params()
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Final config [%s]", self.config)
|
2017-04-22 07:49:22 +00:00
|
|
|
# Write the final config
|
|
|
|
with open(self.config_json_file_name, "wt") as fd:
|
|
|
|
json.dump(self.config, fd)
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.info("Wrote final block device config to [%s]",
|
|
|
|
self.config_json_file_name)
|
2017-04-22 07:49:22 +00:00
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
def _config_get_mount(self, path):
|
|
|
|
for entry in self.config:
|
|
|
|
for k, v in entry.items():
|
|
|
|
if k == 'mount' and v['mount_point'] == path:
|
|
|
|
return v
|
|
|
|
assert False
|
|
|
|
|
|
|
|
def _config_get_all_mount_points(self):
|
|
|
|
rvec = []
|
|
|
|
for entry in self.config:
|
|
|
|
for k, v in entry.items():
|
|
|
|
if k == 'mount':
|
|
|
|
rvec.append(v['mount_point'])
|
|
|
|
return rvec
|
|
|
|
|
|
|
|
def _config_get_mkfs(self, name):
|
|
|
|
for entry in self.config:
|
|
|
|
for k, v in entry.items():
|
|
|
|
if k == 'mkfs' and v['name'] == name:
|
|
|
|
return v
|
|
|
|
assert False
|
|
|
|
|
2017-05-02 05:15:26 +00:00
|
|
|
def cmd_getval(self, symbol):
|
2017-05-01 07:19:49 +00:00
|
|
|
"""Retrieve value from block device level
|
|
|
|
|
2017-05-02 05:15:26 +00:00
|
|
|
The value of SYMBOL is printed to stdout. This is intended to
|
|
|
|
be captured into bash-variables for backward compatibility
|
|
|
|
(non python) access to internal configuration.
|
|
|
|
|
|
|
|
Arguments:
|
2017-01-29 23:52:40 +00:00
|
|
|
:param symbol: the symbol to get
|
2017-05-01 07:19:49 +00:00
|
|
|
"""
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.info("Getting value for [%s]", symbol)
|
2018-01-09 18:20:51 +00:00
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
if symbol == "root-label":
|
|
|
|
root_mount = self._config_get_mount("/")
|
|
|
|
root_fs = self._config_get_mkfs(root_mount['base'])
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("root-label [%s]", root_fs['label'])
|
2017-01-29 23:52:40 +00:00
|
|
|
print("%s" % root_fs['label'])
|
|
|
|
return 0
|
2018-01-09 18:20:51 +00:00
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
if symbol == "root-fstype":
|
|
|
|
root_mount = self._config_get_mount("/")
|
|
|
|
root_fs = self._config_get_mkfs(root_mount['base'])
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("root-fstype [%s]", root_fs['type'])
|
2017-01-29 23:52:40 +00:00
|
|
|
print("%s" % root_fs['type'])
|
|
|
|
return 0
|
2018-01-09 18:20:51 +00:00
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
if symbol == 'mount-points':
|
|
|
|
mount_points = self._config_get_all_mount_points()
|
2017-03-17 17:22:17 +00:00
|
|
|
# we return the mountpoints joined by a pipe, because it is not
|
|
|
|
# a valid char in directories, so it is a safe separator for the
|
|
|
|
# mountpoints list
|
|
|
|
print("%s" % "|".join(mount_points))
|
2017-01-29 23:52:40 +00:00
|
|
|
return 0
|
2017-05-30 02:06:41 +00:00
|
|
|
|
|
|
|
# the following symbols all come from the global state
|
|
|
|
# dictionary. They can only be accessed after the state has
|
|
|
|
# been dumped; i.e. after cmd_create() called.
|
|
|
|
state = BlockDeviceState(self.state_json_file_name)
|
|
|
|
|
2017-06-06 02:09:24 +00:00
|
|
|
# The path to the .raw file for conversion
|
2017-05-01 07:19:49 +00:00
|
|
|
if symbol == 'image-path':
|
2017-05-30 02:06:41 +00:00
|
|
|
print("%s" % state['blockdev']['image0']['image'])
|
2017-05-01 07:19:49 +00:00
|
|
|
return 0
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-06-06 02:09:24 +00:00
|
|
|
# This is the loopback device where the above image is setup
|
|
|
|
if symbol == 'image-block-device':
|
|
|
|
print("%s" % state['blockdev']['image0']['device'])
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# Full list of created devices by name. Some bootloaders, for
|
|
|
|
# example, want to be able to see their boot partitions to
|
|
|
|
# copy things in. Intended to be read into a bash array
|
|
|
|
if symbol == 'image-block-devices':
|
|
|
|
out = ""
|
|
|
|
for k, v in state['blockdev'].items():
|
|
|
|
out += " [%s]=%s " % (k, v['device'])
|
|
|
|
print(out)
|
|
|
|
return 0
|
|
|
|
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.error("Invalid symbol [%s] for getval", symbol)
|
2017-05-01 07:19:49 +00:00
|
|
|
return 1
|
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
def cmd_writefstab(self):
|
|
|
|
"""Creates the fstab"""
|
|
|
|
logger.info("Creating fstab")
|
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
# State should have been created by prior calls; we only need
|
|
|
|
# the dict
|
|
|
|
state = BlockDeviceState(self.state_json_file_name)
|
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
tmp_fstab = os.path.join(self.state_dir, "fstab")
|
|
|
|
with open(tmp_fstab, "wt") as fstab_fd:
|
|
|
|
# This gives the order in which this must be mounted
|
2017-05-30 02:06:41 +00:00
|
|
|
for mp in state['mount_order']:
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Writing fstab entry for [%s]", mp)
|
2017-05-30 02:06:41 +00:00
|
|
|
fs_base = state['mount'][mp]['base']
|
|
|
|
fs_name = state['mount'][mp]['name']
|
|
|
|
fs_val = state['filesys'][fs_base]
|
2017-01-29 23:52:40 +00:00
|
|
|
if 'label' in fs_val:
|
|
|
|
diskid = "LABEL=%s" % fs_val['label']
|
|
|
|
else:
|
|
|
|
diskid = "UUID=%s" % fs_val['uuid']
|
|
|
|
|
|
|
|
# If there is no fstab entry - do not write anything
|
2017-05-30 02:06:41 +00:00
|
|
|
if 'fstab' not in state:
|
2017-01-29 23:52:40 +00:00
|
|
|
continue
|
2017-05-30 02:06:41 +00:00
|
|
|
if fs_name not in state['fstab']:
|
2017-01-29 23:52:40 +00:00
|
|
|
continue
|
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
options = state['fstab'][fs_name]['options']
|
|
|
|
dump_freq = state['fstab'][fs_name]['dump-freq']
|
|
|
|
fsck_passno = state['fstab'][fs_name]['fsck-passno']
|
2017-01-29 23:52:40 +00:00
|
|
|
|
|
|
|
fstab_fd.write("%s %s %s %s %s %s\n"
|
|
|
|
% (diskid, mp, fs_val['fstype'],
|
|
|
|
options, dump_freq, fsck_passno))
|
|
|
|
|
|
|
|
target_etc_dir = os.path.join(self.params['build-dir'], 'built', 'etc')
|
|
|
|
exec_sudo(['mkdir', '-p', target_etc_dir])
|
|
|
|
exec_sudo(['cp', tmp_fstab, os.path.join(target_etc_dir, "fstab")])
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
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
|
|
|
def cmd_create(self):
|
|
|
|
"""Creates the block device"""
|
|
|
|
|
|
|
|
logger.info("create() called")
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Using config [%s]", self.config)
|
2016-07-16 20:16:13 +00:00
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
# Create a new, empty state
|
|
|
|
state = BlockDeviceState()
|
2016-07-16 20:16:13 +00:00
|
|
|
try:
|
2017-06-01 04:31:49 +00:00
|
|
|
dg, call_order = create_graph(self.config, self.params, state)
|
2017-05-30 02:06:41 +00:00
|
|
|
for node in call_order:
|
2017-06-02 00:57:06 +00:00
|
|
|
node.create()
|
2017-05-09 01:01:11 +00:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Create failed; rollback initiated")
|
2017-06-02 00:57:06 +00:00
|
|
|
reverse_order = reversed(call_order)
|
|
|
|
for node in reverse_order:
|
|
|
|
node.rollback()
|
|
|
|
# save the state for debugging
|
|
|
|
state.save_state(self.state_json_file_name)
|
|
|
|
logger.error("Rollback complete, exiting")
|
|
|
|
raise
|
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
|
|
|
|
Use picked nodes for later cmd_* calls
Currently the later cmd_* calls -- umount, cleanup, delete -- all
recreate the node graph by parsing the config file using
create_graph()
There is some need, however, to have a sense of global state when
building the node list. The problem is, this is a one time operation
-- we do not want to rebuild that state for these later calls (see the
"loaded" checks in proposed
Ic3b805f9258128d5233b21ff25579c03487c7fcc).
An insight here seems to be that these cmd_* calls do not actually
want to re-parse the configuration file and rebuild the node list;
they just want to walk the node list in reverse with the state as
provided after cmd_create().
So, rather than re-creating the node list, we might as well just
pickle it, save it to disk along side the state dictionary dump and
reload it for cmd_*.
After this, I think we can safely have PluginBase.__init__() be passed
the state. We will now know that this will only be called once,
during initial creation.
Change-Id: I68840594a34af28d41d9522addcfd830bd203b97
2017-05-31 05:09:37 +00:00
|
|
|
# dump state and nodes, in order
|
|
|
|
# XXX: we only dump the call_order (i.e. nodes) not the whole
|
|
|
|
# graph here, because later calls do not need the graph
|
|
|
|
# at this stage. might they?
|
2017-05-30 02:06:41 +00:00
|
|
|
state.save_state(self.state_json_file_name)
|
Use picked nodes for later cmd_* calls
Currently the later cmd_* calls -- umount, cleanup, delete -- all
recreate the node graph by parsing the config file using
create_graph()
There is some need, however, to have a sense of global state when
building the node list. The problem is, this is a one time operation
-- we do not want to rebuild that state for these later calls (see the
"loaded" checks in proposed
Ic3b805f9258128d5233b21ff25579c03487c7fcc).
An insight here seems to be that these cmd_* calls do not actually
want to re-parse the configuration file and rebuild the node list;
they just want to walk the node list in reverse with the state as
provided after cmd_create().
So, rather than re-creating the node list, we might as well just
pickle it, save it to disk along side the state dictionary dump and
reload it for cmd_*.
After this, I think we can safely have PluginBase.__init__() be passed
the state. We will now know that this will only be called once,
during initial creation.
Change-Id: I68840594a34af28d41d9522addcfd830bd203b97
2017-05-31 05:09:37 +00:00
|
|
|
pickle.dump(call_order, open(self.node_pickle_file_name, 'wb'))
|
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
|
|
|
|
|
|
|
logger.info("create() finished")
|
|
|
|
return 0
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
def cmd_umount(self):
|
|
|
|
"""Unmounts the blockdevice and cleanup resources"""
|
2017-05-30 02:06:41 +00:00
|
|
|
|
2017-06-01 04:57:34 +00:00
|
|
|
# If the state is not here, cmd_cleanup removed it? Nothing
|
|
|
|
# more to do?
|
|
|
|
# XXX: better understand this...
|
|
|
|
if not os.path.exists(self.node_pickle_file_name):
|
2017-05-01 10:17:21 +00:00
|
|
|
logger.info("State already cleaned - no way to do anything here")
|
|
|
|
return 0
|
2016-07-16 20:16:13 +00:00
|
|
|
|
Use picked nodes for later cmd_* calls
Currently the later cmd_* calls -- umount, cleanup, delete -- all
recreate the node graph by parsing the config file using
create_graph()
There is some need, however, to have a sense of global state when
building the node list. The problem is, this is a one time operation
-- we do not want to rebuild that state for these later calls (see the
"loaded" checks in proposed
Ic3b805f9258128d5233b21ff25579c03487c7fcc).
An insight here seems to be that these cmd_* calls do not actually
want to re-parse the configuration file and rebuild the node list;
they just want to walk the node list in reverse with the state as
provided after cmd_create().
So, rather than re-creating the node list, we might as well just
pickle it, save it to disk along side the state dictionary dump and
reload it for cmd_*.
After this, I think we can safely have PluginBase.__init__() be passed
the state. We will now know that this will only be called once,
during initial creation.
Change-Id: I68840594a34af28d41d9522addcfd830bd203b97
2017-05-31 05:09:37 +00:00
|
|
|
call_order = pickle.load(open(self.node_pickle_file_name, 'rb'))
|
2017-04-22 07:49:22 +00:00
|
|
|
reverse_order = reversed(call_order)
|
2017-05-01 10:17:21 +00:00
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
for node in reverse_order:
|
2017-06-01 04:57:34 +00:00
|
|
|
node.umount()
|
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
|
|
|
return 0
|
|
|
|
|
|
|
|
def cmd_cleanup(self):
|
|
|
|
"""Cleanup all remaining relicts - in good case"""
|
|
|
|
|
2017-06-01 04:57:34 +00:00
|
|
|
# Cleanup must be done in reverse order
|
|
|
|
try:
|
|
|
|
call_order = pickle.load(open(self.node_pickle_file_name, 'rb'))
|
|
|
|
except IOError:
|
|
|
|
raise BlockDeviceSetupException("Pickle file not found")
|
|
|
|
|
2017-04-22 07:49:22 +00:00
|
|
|
reverse_order = reversed(call_order)
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
for node in reverse_order:
|
2017-06-01 04:57:34 +00:00
|
|
|
node.cleanup()
|
2016-07-16 20:16:13 +00:00
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
logger.info("Removing temporary state dir [%s]", self.state_dir)
|
2016-07-16 20:16:13 +00:00
|
|
|
shutil.rmtree(self.state_dir)
|
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
|
|
|
return 0
|
|
|
|
|
|
|
|
def cmd_delete(self):
|
|
|
|
"""Cleanup all remaining relicts - in case of an error"""
|
|
|
|
|
2017-04-22 07:49:22 +00:00
|
|
|
# Deleting must be done in reverse order
|
2017-06-01 04:57:34 +00:00
|
|
|
try:
|
|
|
|
call_order = pickle.load(open(self.node_pickle_file_name, 'rb'))
|
|
|
|
except IOError:
|
|
|
|
raise BlockDeviceSetupException("Pickle file not found")
|
2017-04-22 07:49:22 +00:00
|
|
|
reverse_order = reversed(call_order)
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
for node in reverse_order:
|
2017-06-01 04:57:34 +00:00
|
|
|
node.delete()
|
2016-07-16 20:16:13 +00:00
|
|
|
|
2017-05-30 02:06:41 +00:00
|
|
|
logger.info("Removing temporary state dir [%s]", self.state_dir)
|
2016-07-16 20:16:13 +00:00
|
|
|
shutil.rmtree(self.state_dir)
|
|
|
|
|
|
|
|
return 0
|