2017-05-02 01:52:21 +00:00
|
|
|
# Copyright 2016-2017 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 argparse
|
|
|
|
import logging
|
2017-05-02 02:08:33 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import yaml
|
2017-05-02 01:52:21 +00:00
|
|
|
|
|
|
|
from diskimage_builder.block_device.blockdevice import BlockDevice
|
|
|
|
from diskimage_builder import logging_config
|
|
|
|
|
2017-05-02 04:24:35 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-05-02 01:52:21 +00:00
|
|
|
|
|
|
|
|
2017-05-02 04:24:35 +00:00
|
|
|
class BlockDeviceCmd(object):
|
2017-05-02 01:52:21 +00:00
|
|
|
|
2017-05-02 03:29:34 +00:00
|
|
|
def cmd_init(self):
|
|
|
|
self.bd.cmd_init()
|
|
|
|
|
|
|
|
def cmd_getval(self):
|
2017-05-02 05:15:26 +00:00
|
|
|
self.bd.cmd_getval(self.args.symbol)
|
2017-05-02 03:29:34 +00:00
|
|
|
|
|
|
|
def cmd_create(self):
|
|
|
|
self.bd.cmd_create()
|
|
|
|
|
|
|
|
def cmd_umount(self):
|
|
|
|
self.bd.cmd_umount()
|
|
|
|
|
|
|
|
def cmd_cleanup(self):
|
|
|
|
self.bd.cmd_cleanup()
|
|
|
|
|
|
|
|
def cmd_delete(self):
|
|
|
|
self.bd.cmd_delete()
|
|
|
|
|
|
|
|
def cmd_writefstab(self):
|
|
|
|
self.bd.cmd_writefstab()
|
2017-05-02 04:24:35 +00:00
|
|
|
|
|
|
|
def main(self):
|
|
|
|
logging_config.setup()
|
2017-05-02 03:29:34 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="DIB Block Device helper")
|
2017-05-02 04:24:35 +00:00
|
|
|
parser.add_argument('--params', required=False,
|
2018-11-21 08:07:28 +00:00
|
|
|
help="YAML file containing parameters for "
|
2017-05-02 04:24:35 +00:00
|
|
|
"block-device handling. Default is "
|
|
|
|
"DIB_BLOCK_DEVICE_PARAMS_YAML")
|
2017-05-02 03:29:34 +00:00
|
|
|
|
|
|
|
subparsers = parser.add_subparsers(title='commands',
|
|
|
|
description='valid commands',
|
|
|
|
dest='command',
|
|
|
|
help='additional help')
|
|
|
|
|
|
|
|
cmd_init = subparsers.add_parser('init',
|
|
|
|
help='Initialize configuration')
|
|
|
|
cmd_init.set_defaults(func=self.cmd_init)
|
|
|
|
|
|
|
|
cmd_getval = subparsers.add_parser('getval',
|
2018-11-21 08:07:28 +00:00
|
|
|
help='Retrieve information about '
|
2017-05-02 03:29:34 +00:00
|
|
|
'internal state')
|
|
|
|
cmd_getval.set_defaults(func=self.cmd_getval)
|
|
|
|
cmd_getval.add_argument('symbol', help='symbol to print')
|
|
|
|
|
|
|
|
cmd_create = subparsers.add_parser('create',
|
|
|
|
help='Create the block device')
|
|
|
|
cmd_create.set_defaults(func=self.cmd_create)
|
|
|
|
|
|
|
|
cmd_umount = subparsers.add_parser('umount',
|
2018-11-21 08:07:28 +00:00
|
|
|
help='Unmount blockdevice and '
|
2017-05-02 03:29:34 +00:00
|
|
|
'cleanup resources')
|
|
|
|
cmd_umount.set_defaults(func=self.cmd_umount)
|
|
|
|
|
|
|
|
cmd_cleanup = subparsers.add_parser('cleanup', help='Final cleanup')
|
|
|
|
cmd_cleanup.set_defaults(func=self.cmd_cleanup)
|
|
|
|
|
|
|
|
cmd_delete = subparsers.add_parser('delete', help='Error cleanup')
|
|
|
|
cmd_delete.set_defaults(func=self.cmd_delete)
|
|
|
|
|
|
|
|
cmd_writefstab = subparsers.add_parser('writefstab',
|
|
|
|
help='Create fstab for system')
|
|
|
|
cmd_writefstab.set_defaults(func=self.cmd_writefstab)
|
|
|
|
|
|
|
|
self.args = parser.parse_args()
|
2017-05-02 04:24:35 +00:00
|
|
|
|
|
|
|
# Find, open and parse the parameters file
|
2017-05-02 03:29:34 +00:00
|
|
|
if not self.args.params:
|
2017-05-02 04:24:35 +00:00
|
|
|
if 'DIB_BLOCK_DEVICE_PARAMS_YAML' in os.environ:
|
|
|
|
param_file = os.environ['DIB_BLOCK_DEVICE_PARAMS_YAML']
|
|
|
|
else:
|
|
|
|
parser.error(
|
|
|
|
"DIB_BLOCK_DEVICE_PARAMS_YAML or --params not set")
|
|
|
|
else:
|
2017-05-02 03:29:34 +00:00
|
|
|
param_file = self.args.params
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.info("params [%s]", param_file)
|
2017-05-02 04:24:35 +00:00
|
|
|
try:
|
|
|
|
with open(param_file) as f:
|
2017-05-02 03:29:34 +00:00
|
|
|
self.params = yaml.safe_load(f)
|
2017-05-02 04:24:35 +00:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Failed to open parameter YAML")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-05-02 03:29:34 +00:00
|
|
|
# Setup main BlockDevice object from args
|
2017-05-02 05:15:26 +00:00
|
|
|
self.bd = BlockDevice(self.params)
|
2017-05-02 04:24:35 +00:00
|
|
|
|
2017-05-02 03:29:34 +00:00
|
|
|
self.args.func()
|
2017-05-02 04:24:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
bdc = BlockDeviceCmd()
|
|
|
|
return bdc.main()
|
2017-05-02 01:52:21 +00:00
|
|
|
|
2020-02-22 23:25:38 +00:00
|
|
|
|
2017-05-02 01:52:21 +00:00
|
|
|
if __name__ == "__main__":
|
2017-05-02 04:24:35 +00:00
|
|
|
sys.exit(main())
|