diff options
Diffstat (limited to 'scripts/wic')
| -rwxr-xr-x | scripts/wic | 302 |
1 files changed, 302 insertions, 0 deletions
diff --git a/scripts/wic b/scripts/wic new file mode 100755 index 0000000000..6d239fa0fc --- /dev/null +++ b/scripts/wic | |||
| @@ -0,0 +1,302 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # ex:ts=4:sw=4:sts=4:et | ||
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
| 4 | # | ||
| 5 | # Copyright (c) 2013, Intel Corporation. | ||
| 6 | # All rights reserved. | ||
| 7 | # | ||
| 8 | # This program is free software; you can redistribute it and/or modify | ||
| 9 | # it under the terms of the GNU General Public License version 2 as | ||
| 10 | # published by the Free Software Foundation. | ||
| 11 | # | ||
| 12 | # This program is distributed in the hope that it will be useful, | ||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | # GNU General Public License for more details. | ||
| 16 | # | ||
| 17 | # You should have received a copy of the GNU General Public License along | ||
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | # | ||
| 21 | # DESCRIPTION 'wic' is the OpenEmbedded Image Creator that users can | ||
| 22 | # use to generate bootable images. Invoking it without any arguments | ||
| 23 | # will display help screens for the 'wic' command and list the | ||
| 24 | # available 'wic' subcommands. Invoking a subcommand without any | ||
| 25 | # arguments will likewise display help screens for the specified | ||
| 26 | # subcommand. Please use that interface for detailed help. | ||
| 27 | # | ||
| 28 | # AUTHORS | ||
| 29 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> | ||
| 30 | # | ||
| 31 | |||
| 32 | __version__ = "0.1.0" | ||
| 33 | |||
| 34 | # Python Standard Library modules | ||
| 35 | import os | ||
| 36 | import sys | ||
| 37 | import optparse | ||
| 38 | import logging | ||
| 39 | |||
| 40 | # External modules | ||
| 41 | scripts_path = os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0]))) | ||
| 42 | lib_path = scripts_path + '/lib' | ||
| 43 | sys.path = sys.path + [lib_path] | ||
| 44 | |||
| 45 | from image.help import * | ||
| 46 | from image.engine import * | ||
| 47 | |||
| 48 | def rootfs_dir_to_args(krootfs_dir): | ||
| 49 | """ | ||
| 50 | Get a rootfs_dir dict and serialize to string | ||
| 51 | """ | ||
| 52 | rootfs_dir = '' | ||
| 53 | for k, v in krootfs_dir.items(): | ||
| 54 | rootfs_dir += ' ' | ||
| 55 | rootfs_dir += '='.join([k, v]) | ||
| 56 | return rootfs_dir.strip() | ||
| 57 | |||
| 58 | def callback_rootfs_dir(option, opt, value, parser): | ||
| 59 | """ | ||
| 60 | Build a dict using --rootfs_dir connection=dir | ||
| 61 | """ | ||
| 62 | if not type(parser.values.rootfs_dir) is dict: | ||
| 63 | parser.values.rootfs_dir = dict() | ||
| 64 | |||
| 65 | if '=' in value: | ||
| 66 | (key, rootfs_dir) = value.split('=') | ||
| 67 | else: | ||
| 68 | key = 'ROOTFS_DIR' | ||
| 69 | rootfs_dir = value | ||
| 70 | |||
| 71 | parser.values.rootfs_dir[key] = rootfs_dir | ||
| 72 | |||
| 73 | def wic_create_subcommand(args, usage_str): | ||
| 74 | """ | ||
| 75 | Command-line handling for image creation. The real work is done | ||
| 76 | by image.engine.wic_create() | ||
| 77 | """ | ||
| 78 | parser = optparse.OptionParser(usage = usage_str) | ||
| 79 | |||
| 80 | parser.add_option("-o", "--outdir", dest = "outdir", | ||
| 81 | action = "store", help = "name of directory to create image in") | ||
| 82 | parser.add_option("-i", "--infile", dest = "properties_file", | ||
| 83 | action = "store", help = "name of file containing the values for image properties as a JSON file") | ||
| 84 | parser.add_option("-e", "--image-name", dest = "image_name", | ||
| 85 | action = "store", help = "name of the image to use the artifacts from e.g. core-image-sato") | ||
| 86 | parser.add_option("-r", "--rootfs-dir", dest = "rootfs_dir", | ||
| 87 | action = "callback", callback = callback_rootfs_dir, type = "string", | ||
| 88 | help = "path to the /rootfs dir to use as the .wks rootfs source") | ||
| 89 | parser.add_option("-b", "--bootimg-dir", dest = "bootimg_dir", | ||
| 90 | action = "store", help = "path to the dir containing the boot artifacts (e.g. /EFI or /syslinux dirs) to use as the .wks bootimg source") | ||
| 91 | parser.add_option("-k", "--kernel-dir", dest = "kernel_dir", | ||
| 92 | action = "store", help = "path to the dir containing the kernel to use in the .wks bootimg") | ||
| 93 | parser.add_option("-n", "--native-sysroot", dest = "native_sysroot", | ||
| 94 | action = "store", help = "path to the native sysroot containing the tools to use to build the image") | ||
| 95 | parser.add_option("-p", "--skip-build-check", dest = "build_check", | ||
| 96 | action = "store_false", default = True, help = "skip the build check") | ||
| 97 | parser.add_option("-D", "--debug", dest = "debug", action = "store_true", | ||
| 98 | default = False, help = "output debug information") | ||
| 99 | |||
| 100 | (options, args) = parser.parse_args(args) | ||
| 101 | |||
| 102 | if options.debug: | ||
| 103 | loglevel = logging.DEBUG | ||
| 104 | start_logging(loglevel) | ||
| 105 | |||
| 106 | if len(args) != 1: | ||
| 107 | logging.error("Wrong number of arguments, exiting\n") | ||
| 108 | parser.print_help() | ||
| 109 | sys.exit(1) | ||
| 110 | |||
| 111 | if not options.image_name and not (options.rootfs_dir and | ||
| 112 | options.bootimg_dir and | ||
| 113 | options.kernel_dir and | ||
| 114 | options.native_sysroot or | ||
| 115 | options.native_sysroot): | ||
| 116 | print "Build artifacts not completely specified, exiting." | ||
| 117 | print " (Use 'wic -e' or 'wic -r -b -k -n' or 'wic -r -n' to specify artifacts)" | ||
| 118 | print options | ||
| 119 | sys.exit(1) | ||
| 120 | |||
| 121 | if not options.image_name: | ||
| 122 | options.build_check = False | ||
| 123 | |||
| 124 | if options.build_check and not options.properties_file: | ||
| 125 | print "Checking basic build environment..." | ||
| 126 | if not verify_build_env(): | ||
| 127 | print "Couldn't verify build environment, exiting\n" | ||
| 128 | sys.exit(1) | ||
| 129 | else: | ||
| 130 | print "Done.\n" | ||
| 131 | |||
| 132 | print "Creating image(s)...\n" | ||
| 133 | |||
| 134 | # If '-e' option is used the values are extracted from bitbake env. | ||
| 135 | if options.image_name: | ||
| 136 | bitbake_env_lines = find_bitbake_env_lines(options.image_name) | ||
| 137 | if not bitbake_env_lines: | ||
| 138 | print "Couldn't get bitbake environment, exiting." | ||
| 139 | sys.exit(1) | ||
| 140 | set_bitbake_env_lines(bitbake_env_lines) | ||
| 141 | |||
| 142 | bootimg_dir = staging_data_dir = hdddir = "" | ||
| 143 | rootfs_dir = native_sysroot = kernel_dir = image_output_dir = "" | ||
| 144 | |||
| 145 | if options.image_name: | ||
| 146 | (rootfs_dir, kernel_dir, hdddir, staging_data_dir, native_sysroot) = \ | ||
| 147 | find_artifacts(options.image_name) | ||
| 148 | |||
| 149 | wks_file = args[0] | ||
| 150 | |||
| 151 | if not wks_file.endswith(".wks"): | ||
| 152 | # Return full path of the .wks file | ||
| 153 | wks_file = find_canned_image(scripts_path, wks_file) | ||
| 154 | if not wks_file: | ||
| 155 | print "No image named %s found, exiting.\n" % wks_file | ||
| 156 | print "(Use 'wic list images' to list available images, or specify a fully-qualified OE kickstart (.wks) filename)\n" | ||
| 157 | sys.exit(1) | ||
| 158 | |||
| 159 | if options.outdir: | ||
| 160 | image_output_dir = options.outdir | ||
| 161 | |||
| 162 | if options.native_sysroot: | ||
| 163 | native_sysroot = options.native_sysroot | ||
| 164 | print "Using native_sysroot from user command: %s" % native_sysroot | ||
| 165 | |||
| 166 | if not os.path.isdir(native_sysroot): | ||
| 167 | print "--native-sysroot (-n) not found, exiting" | ||
| 168 | sys.exit(1) | ||
| 169 | |||
| 170 | native_sysroot = os.path.abspath(native_sysroot) | ||
| 171 | |||
| 172 | if not options.image_name: | ||
| 173 | if (options.bootimg_dir and options.kernel_dir and | ||
| 174 | options.rootfs_dir and options.native_sysroot): | ||
| 175 | rootfs_dir = '' | ||
| 176 | if 'ROOTFS_DIR' in options.rootfs_dir: | ||
| 177 | rootfs_dir = options.rootfs_dir['ROOTFS_DIR'] | ||
| 178 | bootimg_dir = options.bootimg_dir | ||
| 179 | kernel_dir = options.kernel_dir | ||
| 180 | native_sysroot = options.native_sysroot | ||
| 181 | |||
| 182 | if rootfs_dir and not os.path.isdir(rootfs_dir): | ||
| 183 | print "--roofs-dir (-r) not found, exiting\n" | ||
| 184 | sys.exit(1) | ||
| 185 | if not os.path.isdir(bootimg_dir): | ||
| 186 | print "--bootimg-dir (-b) not found, exiting\n" | ||
| 187 | sys.exit(1) | ||
| 188 | if not os.path.isdir(kernel_dir): | ||
| 189 | print "--kernel-dir (-k) not found, exiting\n" | ||
| 190 | sys.exit(1) | ||
| 191 | if not os.path.isdir(native_sysroot): | ||
| 192 | print "--native-sysroot (-n) not found, exiting\n" | ||
| 193 | sys.exit(1) | ||
| 194 | else: | ||
| 195 | print 'Build image from rootfs and a package list using native rootfs\n' | ||
| 196 | if options.rootfs_dir and 'ROOTFS_DIR' in options.rootfs_dir: | ||
| 197 | rootfs_dir = options.rootfs_dir['ROOTFS_DIR'] | ||
| 198 | elif options.rootfs_dir: | ||
| 199 | rootfs_dir = options.rootfs_dir | ||
| 200 | else: | ||
| 201 | rootfs_dir = "" | ||
| 202 | |||
| 203 | native_sysroot = options.native_sysroot | ||
| 204 | |||
| 205 | if rootfs_dir and not os.path.isdir(rootfs_dir): | ||
| 206 | print "--roofs-dir (-r) not found, exiting\n" | ||
| 207 | sys.exit(1) | ||
| 208 | if not os.path.isdir(native_sysroot): | ||
| 209 | print "--native-sysroot (-n) not found, exiting\n" | ||
| 210 | sys.exit(1) | ||
| 211 | else: | ||
| 212 | not_found = not_found_dir = "" | ||
| 213 | if not os.path.isdir(rootfs_dir): | ||
| 214 | (not_found, not_found_dir) = ("rootfs-dir", rootfs_dir) | ||
| 215 | elif not os.path.isdir(hdddir) and not os.path.isdir(staging_data_dir): | ||
| 216 | (not_found, not_found_dir) = ("bootimg-dir", bootimg_dir) | ||
| 217 | elif not os.path.isdir(kernel_dir): | ||
| 218 | (not_found, not_found_dir) = ("kernel-dir", kernel_dir) | ||
| 219 | elif not os.path.isdir(native_sysroot): | ||
| 220 | (not_found, not_found_dir) = ("native-sysroot", native_sysroot) | ||
| 221 | if not_found: | ||
| 222 | if not not_found_dir: | ||
| 223 | not_found_dir = "Completely missing artifact - wrong image (.wks) used?" | ||
| 224 | print "Build artifacts not found, exiting." | ||
| 225 | print " (Please check that the build artifacts for the machine" | ||
| 226 | print " selected in local.conf actually exist and that they" | ||
| 227 | print " are the correct artifacts for the image (.wks file)).\n" | ||
| 228 | print "The artifact that couldn't be found was %s:\n %s" % \ | ||
| 229 | (not_found, not_found_dir) | ||
| 230 | sys.exit(1) | ||
| 231 | |||
| 232 | krootfs_dir = options.rootfs_dir | ||
| 233 | if krootfs_dir is None: | ||
| 234 | krootfs_dir = {} | ||
| 235 | krootfs_dir['ROOTFS_DIR'] = rootfs_dir | ||
| 236 | |||
| 237 | rootfs_dir = rootfs_dir_to_args(krootfs_dir) | ||
| 238 | |||
| 239 | wic_create(args, wks_file, rootfs_dir, bootimg_dir, kernel_dir, | ||
| 240 | native_sysroot, hdddir, staging_data_dir, scripts_path, | ||
| 241 | image_output_dir, options.debug, options.properties_file) | ||
| 242 | |||
| 243 | |||
| 244 | def wic_list_subcommand(args, usage_str): | ||
| 245 | """ | ||
| 246 | Command-line handling for listing available image properties and | ||
| 247 | values. The real work is done by image.engine.wic_list() | ||
| 248 | """ | ||
| 249 | parser = optparse.OptionParser(usage = usage_str) | ||
| 250 | |||
| 251 | parser.add_option("-o", "--outfile", action = "store", | ||
| 252 | dest = "properties_file", | ||
| 253 | help = "dump the possible values for image properties to a JSON file") | ||
| 254 | |||
| 255 | (options, args) = parser.parse_args(args) | ||
| 256 | |||
| 257 | if not wic_list(args, scripts_path, options.properties_file): | ||
| 258 | logging.error("Bad list arguments, exiting\n") | ||
| 259 | parser.print_help() | ||
| 260 | sys.exit(1) | ||
| 261 | |||
| 262 | |||
| 263 | subcommands = { | ||
| 264 | "create": [wic_create_subcommand, | ||
| 265 | wic_create_usage, | ||
| 266 | wic_create_help], | ||
| 267 | "list": [wic_list_subcommand, | ||
| 268 | wic_list_usage, | ||
| 269 | wic_list_help], | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | def start_logging(loglevel): | ||
| 274 | logging.basicConfig(filname = 'wic.log', filemode = 'w', level=loglevel) | ||
| 275 | |||
| 276 | |||
| 277 | def main(): | ||
| 278 | parser = optparse.OptionParser(version = "wic version %s" % __version__, | ||
| 279 | usage = wic_usage) | ||
| 280 | |||
| 281 | parser.disable_interspersed_args() | ||
| 282 | |||
| 283 | (options, args) = parser.parse_args() | ||
| 284 | |||
| 285 | if len(args): | ||
| 286 | if args[0] == "help": | ||
| 287 | if len(args) == 1: | ||
| 288 | parser.print_help() | ||
| 289 | sys.exit(1) | ||
| 290 | |||
| 291 | invoke_subcommand(args, parser, wic_help_usage, subcommands) | ||
| 292 | |||
| 293 | |||
| 294 | if __name__ == "__main__": | ||
| 295 | try: | ||
| 296 | ret = main() | ||
| 297 | except Exception: | ||
| 298 | ret = 1 | ||
| 299 | import traceback | ||
| 300 | traceback.print_exc(5) | ||
| 301 | sys.exit(ret) | ||
| 302 | |||
