diff options
Diffstat (limited to 'scripts/lib/image/engine.py')
| -rw-r--r-- | scripts/lib/image/engine.py | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/scripts/lib/image/engine.py b/scripts/lib/image/engine.py new file mode 100644 index 0000000000..0643780f1a --- /dev/null +++ b/scripts/lib/image/engine.py | |||
| @@ -0,0 +1,287 @@ | |||
| 1 | # ex:ts=4:sw=4:sts=4:et | ||
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
| 3 | # | ||
| 4 | # Copyright (c) 2013, Intel Corporation. | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or modify | ||
| 8 | # it under the terms of the GNU General Public License version 2 as | ||
| 9 | # published by the Free Software Foundation. | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License along | ||
| 17 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
| 18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 19 | # | ||
| 20 | # DESCRIPTION | ||
| 21 | |||
| 22 | # This module implements the image creation engine used by 'wic' to | ||
| 23 | # create images. The engine parses through the OpenEmbedded kickstart | ||
| 24 | # (wks) file specified and generates images that can then be directly | ||
| 25 | # written onto media. | ||
| 26 | # | ||
| 27 | # AUTHORS | ||
| 28 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 29 | # | ||
| 30 | |||
| 31 | import os | ||
| 32 | import sys | ||
| 33 | from abc import ABCMeta, abstractmethod | ||
| 34 | import shlex | ||
| 35 | import json | ||
| 36 | import subprocess | ||
| 37 | import shutil | ||
| 38 | |||
| 39 | import os, sys, errno | ||
| 40 | from mic import msger, creator | ||
| 41 | from mic.utils import cmdln, misc, errors | ||
| 42 | from mic.conf import configmgr | ||
| 43 | from mic.plugin import pluginmgr | ||
| 44 | from mic.__version__ import VERSION | ||
| 45 | from mic.utils.oe.misc import * | ||
| 46 | |||
| 47 | |||
| 48 | def verify_build_env(): | ||
| 49 | """ | ||
| 50 | Verify that the build environment is sane. | ||
| 51 | |||
| 52 | Returns True if it is, false otherwise | ||
| 53 | """ | ||
| 54 | try: | ||
| 55 | builddir = os.environ["BUILDDIR"] | ||
| 56 | except KeyError: | ||
| 57 | print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)" | ||
| 58 | sys.exit(1) | ||
| 59 | |||
| 60 | return True | ||
| 61 | |||
| 62 | |||
| 63 | def find_bitbake_env_lines(image_name): | ||
| 64 | """ | ||
| 65 | If image_name is empty, plugins might still be able to use the | ||
| 66 | environment, so set it regardless. | ||
| 67 | """ | ||
| 68 | if image_name: | ||
| 69 | bitbake_env_cmd = "bitbake -e %s" % image_name | ||
| 70 | else: | ||
| 71 | bitbake_env_cmd = "bitbake -e" | ||
| 72 | rc, bitbake_env_lines = exec_cmd(bitbake_env_cmd) | ||
| 73 | if rc != 0: | ||
| 74 | print "Couldn't get '%s' output." % bitbake_env_cmd | ||
| 75 | return None | ||
| 76 | |||
| 77 | return bitbake_env_lines | ||
| 78 | |||
| 79 | |||
| 80 | def find_artifacts(image_name): | ||
| 81 | """ | ||
| 82 | Gather the build artifacts for the current image (the image_name | ||
| 83 | e.g. core-image-minimal) for the current MACHINE set in local.conf | ||
| 84 | """ | ||
| 85 | bitbake_env_lines = get_bitbake_env_lines() | ||
| 86 | |||
| 87 | rootfs_dir = kernel_dir = hdddir = staging_data_dir = native_sysroot = "" | ||
| 88 | |||
| 89 | for line in bitbake_env_lines.split('\n'): | ||
| 90 | if (get_line_val(line, "IMAGE_ROOTFS")): | ||
| 91 | rootfs_dir = get_line_val(line, "IMAGE_ROOTFS") | ||
| 92 | continue | ||
| 93 | if (get_line_val(line, "STAGING_KERNEL_DIR")): | ||
| 94 | kernel_dir = get_line_val(line, "STAGING_KERNEL_DIR") | ||
| 95 | continue | ||
| 96 | if (get_line_val(line, "HDDDIR")): | ||
| 97 | hdddir = get_line_val(line, "HDDDIR") | ||
| 98 | continue | ||
| 99 | if (get_line_val(line, "STAGING_DATADIR")): | ||
| 100 | staging_data_dir = get_line_val(line, "STAGING_DATADIR") | ||
| 101 | continue | ||
| 102 | if (get_line_val(line, "STAGING_DIR_NATIVE")): | ||
| 103 | native_sysroot = get_line_val(line, "STAGING_DIR_NATIVE") | ||
| 104 | continue | ||
| 105 | |||
| 106 | return (rootfs_dir, kernel_dir, hdddir, staging_data_dir, native_sysroot) | ||
| 107 | |||
| 108 | |||
| 109 | CANNED_IMAGE_DIR = "lib/image/canned-wks" # relative to scripts | ||
| 110 | |||
| 111 | def find_canned_image(scripts_path, wks_file): | ||
| 112 | """ | ||
| 113 | Find a .wks file with the given name in the canned files dir. | ||
| 114 | |||
| 115 | Return False if not found | ||
| 116 | """ | ||
| 117 | canned_wks_dir = os.path.join(scripts_path, CANNED_IMAGE_DIR) | ||
| 118 | |||
| 119 | for root, dirs, files in os.walk(canned_wks_dir): | ||
| 120 | for file in files: | ||
| 121 | if file.endswith("~") or file.endswith("#"): | ||
| 122 | continue | ||
| 123 | if file.endswith(".wks") and wks_file + ".wks" == file: | ||
| 124 | fullpath = os.path.join(canned_wks_dir, file) | ||
| 125 | return fullpath | ||
| 126 | return None | ||
| 127 | |||
| 128 | |||
| 129 | def list_canned_images(scripts_path): | ||
| 130 | """ | ||
| 131 | List the .wks files in the canned image dir, minus the extension. | ||
| 132 | """ | ||
| 133 | canned_wks_dir = os.path.join(scripts_path, CANNED_IMAGE_DIR) | ||
| 134 | |||
| 135 | for root, dirs, files in os.walk(canned_wks_dir): | ||
| 136 | for file in files: | ||
| 137 | if file.endswith("~") or file.endswith("#"): | ||
| 138 | continue | ||
| 139 | if file.endswith(".wks"): | ||
| 140 | fullpath = os.path.join(canned_wks_dir, file) | ||
| 141 | f = open(fullpath, "r") | ||
| 142 | lines = f.readlines() | ||
| 143 | for line in lines: | ||
| 144 | desc = "" | ||
| 145 | idx = line.find("short-description:") | ||
| 146 | if idx != -1: | ||
| 147 | desc = line[idx + len("short-description:"):].strip() | ||
| 148 | break | ||
| 149 | basename = os.path.splitext(file)[0] | ||
| 150 | print " %s\t\t%s" % (basename, desc) | ||
| 151 | |||
| 152 | |||
| 153 | def list_canned_image_help(scripts_path, fullpath): | ||
| 154 | """ | ||
| 155 | List the help and params in the specified canned image. | ||
| 156 | """ | ||
| 157 | canned_wks_dir = os.path.join(scripts_path, CANNED_IMAGE_DIR) | ||
| 158 | |||
| 159 | f = open(fullpath, "r") | ||
| 160 | lines = f.readlines() | ||
| 161 | found = False | ||
| 162 | for line in lines: | ||
| 163 | if not found: | ||
| 164 | idx = line.find("long-description:") | ||
| 165 | if idx != -1: | ||
| 166 | |||
| 167 | print line[idx + len("long-description:"):].strip() | ||
| 168 | found = True | ||
| 169 | continue | ||
| 170 | if not line.strip(): | ||
| 171 | break | ||
| 172 | idx = line.find("#") | ||
| 173 | if idx != -1: | ||
| 174 | print line[idx + len("#:"):].rstrip() | ||
| 175 | else: | ||
| 176 | break | ||
| 177 | |||
| 178 | |||
| 179 | def wic_create(args, wks_file, rootfs_dir, bootimg_dir, kernel_dir, | ||
| 180 | native_sysroot, hdddir, staging_data_dir, scripts_path, | ||
| 181 | image_output_dir, debug, properties_file, properties=None): | ||
| 182 | """ | ||
| 183 | Create image | ||
| 184 | |||
| 185 | wks_file - user-defined OE kickstart file | ||
| 186 | rootfs_dir - absolute path to the build's /rootfs dir | ||
| 187 | bootimg_dir - absolute path to the build's boot artifacts directory | ||
| 188 | kernel_dir - absolute path to the build's kernel directory | ||
| 189 | native_sysroot - absolute path to the build's native sysroots dir | ||
| 190 | hdddir - absolute path to the build's HDDDIR dir | ||
| 191 | staging_data_dir - absolute path to the build's STAGING_DATA_DIR dir | ||
| 192 | scripts_path - absolute path to /scripts dir | ||
| 193 | image_output_dir - dirname to create for image | ||
| 194 | properties_file - use values from this file if nonempty i.e no prompting | ||
| 195 | properties - use values from this string if nonempty i.e no prompting | ||
| 196 | |||
| 197 | Normally, the values for the build artifacts values are determined | ||
| 198 | by 'wic -e' from the output of the 'bitbake -e' command given an | ||
| 199 | image name e.g. 'core-image-minimal' and a given machine set in | ||
| 200 | local.conf. If that's the case, the variables get the following | ||
| 201 | values from the output of 'bitbake -e': | ||
| 202 | |||
| 203 | rootfs_dir: IMAGE_ROOTFS | ||
| 204 | kernel_dir: STAGING_KERNEL_DIR | ||
| 205 | native_sysroot: STAGING_DIR_NATIVE | ||
| 206 | hdddir: HDDDIR | ||
| 207 | staging_data_dir: STAGING_DATA_DIR | ||
| 208 | |||
| 209 | In the above case, bootimg_dir remains unset and the image | ||
| 210 | creation code determines which of the passed-in directories to | ||
| 211 | use. | ||
| 212 | |||
| 213 | In the case where the values are passed in explicitly i.e 'wic -e' | ||
| 214 | is not used but rather the individual 'wic' options are used to | ||
| 215 | explicitly specify these values, hdddir and staging_data_dir will | ||
| 216 | be unset, but bootimg_dir must be explicit i.e. explicitly set to | ||
| 217 | either hdddir or staging_data_dir, depending on the image being | ||
| 218 | generated. The other values (rootfs_dir, kernel_dir, and | ||
| 219 | native_sysroot) correspond to the same values found above via | ||
| 220 | 'bitbake -e'). | ||
| 221 | |||
| 222 | """ | ||
| 223 | try: | ||
| 224 | oe_builddir = os.environ["BUILDDIR"] | ||
| 225 | except KeyError: | ||
| 226 | print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)" | ||
| 227 | sys.exit(1) | ||
| 228 | |||
| 229 | direct_args = list() | ||
| 230 | direct_args.insert(0, oe_builddir) | ||
| 231 | direct_args.insert(0, image_output_dir) | ||
| 232 | direct_args.insert(0, wks_file) | ||
| 233 | direct_args.insert(0, rootfs_dir) | ||
| 234 | direct_args.insert(0, bootimg_dir) | ||
| 235 | direct_args.insert(0, kernel_dir) | ||
| 236 | direct_args.insert(0, native_sysroot) | ||
| 237 | direct_args.insert(0, hdddir) | ||
| 238 | direct_args.insert(0, staging_data_dir) | ||
| 239 | direct_args.insert(0, "direct") | ||
| 240 | |||
| 241 | if debug: | ||
| 242 | msger.set_loglevel('debug') | ||
| 243 | |||
| 244 | cr = creator.Creator() | ||
| 245 | |||
| 246 | cr.main(direct_args) | ||
| 247 | |||
| 248 | print "\nThe image(s) were created using OE kickstart file:\n %s" % wks_file | ||
| 249 | |||
| 250 | |||
| 251 | def wic_list(args, scripts_path, properties_file): | ||
| 252 | """ | ||
| 253 | Print the complete list of properties defined by the image, or the | ||
| 254 | possible values for a particular image property. | ||
| 255 | """ | ||
| 256 | if len(args) < 1: | ||
| 257 | return False | ||
| 258 | |||
| 259 | if len(args) == 1: | ||
| 260 | if args[0] == "images": | ||
| 261 | list_canned_images(scripts_path) | ||
| 262 | return True | ||
| 263 | elif args[0] == "properties": | ||
| 264 | return True | ||
| 265 | else: | ||
| 266 | return False | ||
| 267 | |||
| 268 | if len(args) == 2: | ||
| 269 | if args[0] == "properties": | ||
| 270 | wks_file = args[1] | ||
| 271 | print "print properties contained in wks file: %s" % wks_file | ||
| 272 | return True | ||
| 273 | elif args[0] == "property": | ||
| 274 | print "print property values for property: %s" % args[1] | ||
| 275 | return True | ||
| 276 | elif args[1] == "help": | ||
| 277 | wks_file = args[0] | ||
| 278 | fullpath = find_canned_image(scripts_path, wks_file) | ||
| 279 | if not fullpath: | ||
| 280 | print "No image named %s found, exiting. (Use 'wic list images' to list available images, or specify a fully-qualified OE kickstart (.wks) filename)\n" % wks_file | ||
| 281 | sys.exit(1) | ||
| 282 | list_canned_image_help(scripts_path, fullpath) | ||
| 283 | return True | ||
| 284 | else: | ||
| 285 | return False | ||
| 286 | |||
| 287 | return False | ||
