Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2011 Intel, Inc. |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | __all__ = ['ImagerPlugin', 'SourcePlugin'] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 9 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | import os |
| 11 | import logging |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 12 | import types |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 14 | from collections import defaultdict |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 15 | import importlib |
| 16 | import importlib.util |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 17 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 18 | from wic import WicError |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 19 | from wic.misc import get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | |
| 21 | PLUGIN_TYPES = ["imager", "source"] |
| 22 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 23 | SCRIPTS_PLUGIN_DIR = ["scripts/lib/wic/plugins", "lib/wic/plugins"] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | |
| 25 | logger = logging.getLogger('wic') |
| 26 | |
| 27 | PLUGINS = defaultdict(dict) |
| 28 | |
| 29 | class PluginMgr: |
| 30 | _plugin_dirs = [] |
| 31 | |
| 32 | @classmethod |
| 33 | def get_plugins(cls, ptype): |
| 34 | """Get dictionary of <plugin_name>:<class> pairs.""" |
| 35 | if ptype not in PLUGIN_TYPES: |
| 36 | raise WicError('%s is not valid plugin type' % ptype) |
| 37 | |
| 38 | # collect plugin directories |
| 39 | if not cls._plugin_dirs: |
| 40 | cls._plugin_dirs = [os.path.join(os.path.dirname(__file__), 'plugins')] |
| 41 | layers = get_bitbake_var("BBLAYERS") or '' |
| 42 | for layer_path in layers.split(): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 43 | for script_plugin_dir in SCRIPTS_PLUGIN_DIR: |
| 44 | path = os.path.join(layer_path, script_plugin_dir) |
| 45 | path = os.path.abspath(os.path.expanduser(path)) |
| 46 | if path not in cls._plugin_dirs and os.path.isdir(path): |
| 47 | cls._plugin_dirs.insert(0, path) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | |
| 49 | if ptype not in PLUGINS: |
| 50 | # load all ptype plugins |
| 51 | for pdir in cls._plugin_dirs: |
| 52 | ppath = os.path.join(pdir, ptype) |
| 53 | if os.path.isdir(ppath): |
| 54 | for fname in os.listdir(ppath): |
| 55 | if fname.endswith('.py'): |
| 56 | mname = fname[:-3] |
| 57 | mpath = os.path.join(ppath, fname) |
| 58 | logger.debug("loading plugin module %s", mpath) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 59 | spec = importlib.util.spec_from_file_location(mname, mpath) |
| 60 | module = importlib.util.module_from_spec(spec) |
| 61 | spec.loader.exec_module(module) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 62 | |
| 63 | return PLUGINS.get(ptype) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 65 | class PluginMeta(type): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | def __new__(cls, name, bases, attrs): |
| 67 | class_type = type.__new__(cls, name, bases, attrs) |
| 68 | if 'name' in attrs: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 69 | PLUGINS[class_type.wic_plugin_type][attrs['name']] = class_type |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | return class_type |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 73 | class ImagerPlugin(metaclass=PluginMeta): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | wic_plugin_type = "imager" |
| 75 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | def do_create(self): |
| 77 | raise WicError("Method %s.do_create is not implemented" % |
| 78 | self.__class__.__name__) |
| 79 | |
| 80 | class SourcePlugin(metaclass=PluginMeta): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | wic_plugin_type = "source" |
| 82 | """ |
| 83 | The methods that can be implemented by --source plugins. |
| 84 | |
| 85 | Any methods not implemented in a subclass inherit these. |
| 86 | """ |
| 87 | |
| 88 | @classmethod |
| 89 | def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, |
| 90 | bootimg_dir, kernel_dir, native_sysroot): |
| 91 | """ |
| 92 | Called after all partitions have been prepared and assembled into a |
| 93 | disk image. This provides a hook to allow finalization of a |
| 94 | disk image e.g. to write an MBR to it. |
| 95 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 96 | logger.debug("SourcePlugin: do_install_disk: disk: %s", disk_name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
| 98 | @classmethod |
| 99 | def do_stage_partition(cls, part, source_params, creator, cr_workdir, |
| 100 | oe_builddir, bootimg_dir, kernel_dir, |
| 101 | native_sysroot): |
| 102 | """ |
| 103 | Special content staging hook called before do_prepare_partition(), |
| 104 | normally empty. |
| 105 | |
| 106 | Typically, a partition will just use the passed-in parame e.g |
| 107 | straight bootimg_dir, etc, but in some cases, things need to |
| 108 | be more tailored e.g. to use a deploy dir + /boot, etc. This |
| 109 | hook allows those files to be staged in a customized fashion. |
| 110 | Not that get_bitbake_var() allows you to acces non-standard |
| 111 | variables that you might want to use for this. |
| 112 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 113 | logger.debug("SourcePlugin: do_stage_partition: part: %s", part) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | |
| 115 | @classmethod |
| 116 | def do_configure_partition(cls, part, source_params, creator, cr_workdir, |
| 117 | oe_builddir, bootimg_dir, kernel_dir, |
| 118 | native_sysroot): |
| 119 | """ |
| 120 | Called before do_prepare_partition(), typically used to create |
| 121 | custom configuration files for a partition, for example |
| 122 | syslinux or grub config files. |
| 123 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | logger.debug("SourcePlugin: do_configure_partition: part: %s", part) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
| 126 | @classmethod |
| 127 | def do_prepare_partition(cls, part, source_params, creator, cr_workdir, |
| 128 | oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, |
| 129 | native_sysroot): |
| 130 | """ |
| 131 | Called to do the actual content population for a partition i.e. it |
| 132 | 'prepares' the partition to be incorporated into the image. |
| 133 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 134 | logger.debug("SourcePlugin: do_prepare_partition: part: %s", part) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 135 | |
| 136 | @classmethod |
| 137 | def do_post_partition(cls, part, source_params, creator, cr_workdir, |
| 138 | oe_builddir, bootimg_dir, kernel_dir, rootfs_dir, |
| 139 | native_sysroot): |
| 140 | """ |
| 141 | Called after the partition is created. It is useful to add post |
| 142 | operations e.g. security signing the partition. |
| 143 | """ |
| 144 | logger.debug("SourcePlugin: do_post_partition: part: %s", part) |