blob: d9b4e57747e784c802efda51d4aa793b7a20c1bc [file] [log] [blame]
Brad Bishop96ff1982019-08-19 13:50:42 -04001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002#
3# Copyright (c) 2011 Intel, Inc.
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008__all__ = ['ImagerPlugin', 'SourcePlugin']
Patrick Williamsc0f7c042017-02-23 20:41:17 -06009
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010import os
11import logging
12
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013from collections import defaultdict
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014from importlib.machinery import SourceFileLoader
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016from wic import WicError
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017from wic.misc import get_bitbake_var
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018
19PLUGIN_TYPES = ["imager", "source"]
20
Andrew Geissler82c905d2020-04-13 13:39:40 -050021SCRIPTS_PLUGIN_DIR = ["scripts/lib/wic/plugins", "lib/wic/plugins"]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022
23logger = logging.getLogger('wic')
24
25PLUGINS = defaultdict(dict)
26
27class PluginMgr:
28 _plugin_dirs = []
29
30 @classmethod
31 def get_plugins(cls, ptype):
32 """Get dictionary of <plugin_name>:<class> pairs."""
33 if ptype not in PLUGIN_TYPES:
34 raise WicError('%s is not valid plugin type' % ptype)
35
36 # collect plugin directories
37 if not cls._plugin_dirs:
38 cls._plugin_dirs = [os.path.join(os.path.dirname(__file__), 'plugins')]
39 layers = get_bitbake_var("BBLAYERS") or ''
40 for layer_path in layers.split():
Andrew Geissler82c905d2020-04-13 13:39:40 -050041 for script_plugin_dir in SCRIPTS_PLUGIN_DIR:
42 path = os.path.join(layer_path, script_plugin_dir)
43 path = os.path.abspath(os.path.expanduser(path))
44 if path not in cls._plugin_dirs and os.path.isdir(path):
45 cls._plugin_dirs.insert(0, path)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046
47 if ptype not in PLUGINS:
48 # load all ptype plugins
49 for pdir in cls._plugin_dirs:
50 ppath = os.path.join(pdir, ptype)
51 if os.path.isdir(ppath):
52 for fname in os.listdir(ppath):
53 if fname.endswith('.py'):
54 mname = fname[:-3]
55 mpath = os.path.join(ppath, fname)
56 logger.debug("loading plugin module %s", mpath)
57 SourceFileLoader(mname, mpath).load_module()
58
59 return PLUGINS.get(ptype)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
Patrick Williamsc0f7c042017-02-23 20:41:17 -060061class PluginMeta(type):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060062 def __new__(cls, name, bases, attrs):
63 class_type = type.__new__(cls, name, bases, attrs)
64 if 'name' in attrs:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050065 PLUGINS[class_type.wic_plugin_type][attrs['name']] = class_type
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067 return class_type
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
Brad Bishop6e60e8b2018-02-01 10:27:11 -050069class ImagerPlugin(metaclass=PluginMeta):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 wic_plugin_type = "imager"
71
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 def do_create(self):
73 raise WicError("Method %s.do_create is not implemented" %
74 self.__class__.__name__)
75
76class SourcePlugin(metaclass=PluginMeta):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 wic_plugin_type = "source"
78 """
79 The methods that can be implemented by --source plugins.
80
81 Any methods not implemented in a subclass inherit these.
82 """
83
84 @classmethod
85 def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir,
86 bootimg_dir, kernel_dir, native_sysroot):
87 """
88 Called after all partitions have been prepared and assembled into a
89 disk image. This provides a hook to allow finalization of a
90 disk image e.g. to write an MBR to it.
91 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092 logger.debug("SourcePlugin: do_install_disk: disk: %s", disk_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093
94 @classmethod
95 def do_stage_partition(cls, part, source_params, creator, cr_workdir,
96 oe_builddir, bootimg_dir, kernel_dir,
97 native_sysroot):
98 """
99 Special content staging hook called before do_prepare_partition(),
100 normally empty.
101
102 Typically, a partition will just use the passed-in parame e.g
103 straight bootimg_dir, etc, but in some cases, things need to
104 be more tailored e.g. to use a deploy dir + /boot, etc. This
105 hook allows those files to be staged in a customized fashion.
106 Not that get_bitbake_var() allows you to acces non-standard
107 variables that you might want to use for this.
108 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 logger.debug("SourcePlugin: do_stage_partition: part: %s", part)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
111 @classmethod
112 def do_configure_partition(cls, part, source_params, creator, cr_workdir,
113 oe_builddir, bootimg_dir, kernel_dir,
114 native_sysroot):
115 """
116 Called before do_prepare_partition(), typically used to create
117 custom configuration files for a partition, for example
118 syslinux or grub config files.
119 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500120 logger.debug("SourcePlugin: do_configure_partition: part: %s", part)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
122 @classmethod
123 def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
124 oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
125 native_sysroot):
126 """
127 Called to do the actual content population for a partition i.e. it
128 'prepares' the partition to be incorporated into the image.
129 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 logger.debug("SourcePlugin: do_prepare_partition: part: %s", part)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400131
132 @classmethod
133 def do_post_partition(cls, part, source_params, creator, cr_workdir,
134 oe_builddir, bootimg_dir, kernel_dir, rootfs_dir,
135 native_sysroot):
136 """
137 Called after the partition is created. It is useful to add post
138 operations e.g. security signing the partition.
139 """
140 logger.debug("SourcePlugin: do_post_partition: part: %s", part)