wic 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/usr/bin/env python3
  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. __version__ = "0.2.0"
  32. # Python Standard Library modules
  33. import os
  34. import sys
  35. import optparse
  36. import logging
  37. from distutils import spawn
  38. # External modules
  39. scripts_path = os.path.abspath(os.path.dirname(__file__))
  40. lib_path = scripts_path + '/lib'
  41. sys.path.insert(0, lib_path)
  42. bitbake_exe = spawn.find_executable('bitbake')
  43. if bitbake_exe:
  44. bitbake_path = os.path.join(os.path.dirname(bitbake_exe), '../lib')
  45. sys.path.insert(0, bitbake_path)
  46. from bb import cookerdata
  47. from bb.main import bitbake_main, BitBakeConfigParameters
  48. else:
  49. bitbake_main = None
  50. from wic.utils.oe.misc import get_bitbake_var, BB_VARS
  51. from wic.utils.errors import WicError
  52. from wic import engine
  53. from wic import help as hlp
  54. def rootfs_dir_to_args(krootfs_dir):
  55. """
  56. Get a rootfs_dir dict and serialize to string
  57. """
  58. rootfs_dir = ''
  59. for key, val in krootfs_dir.items():
  60. rootfs_dir += ' '
  61. rootfs_dir += '='.join([key, val])
  62. return rootfs_dir.strip()
  63. def callback_rootfs_dir(option, opt, value, parser):
  64. """
  65. Build a dict using --rootfs_dir connection=dir
  66. """
  67. if not type(parser.values.rootfs_dir) is dict:
  68. parser.values.rootfs_dir = dict()
  69. if '=' in value:
  70. (key, rootfs_dir) = value.split('=')
  71. else:
  72. key = 'ROOTFS_DIR'
  73. rootfs_dir = value
  74. parser.values.rootfs_dir[key] = rootfs_dir
  75. def wic_create_subcommand(args, usage_str):
  76. """
  77. Command-line handling for image creation. The real work is done
  78. by image.engine.wic_create()
  79. """
  80. parser = optparse.OptionParser(usage=usage_str)
  81. parser.add_option("-o", "--outdir", dest="outdir",
  82. help="name of directory to create image in")
  83. parser.add_option("-e", "--image-name", dest="image_name",
  84. help="name of the image to use the artifacts from "
  85. "e.g. core-image-sato")
  86. parser.add_option("-r", "--rootfs-dir", dest="rootfs_dir", type="string",
  87. action="callback", callback=callback_rootfs_dir,
  88. help="path to the /rootfs dir to use as the "
  89. ".wks rootfs source")
  90. parser.add_option("-b", "--bootimg-dir", dest="bootimg_dir",
  91. help="path to the dir containing the boot artifacts "
  92. "(e.g. /EFI or /syslinux dirs) to use as the "
  93. ".wks bootimg source")
  94. parser.add_option("-k", "--kernel-dir", dest="kernel_dir",
  95. help="path to the dir containing the kernel to use "
  96. "in the .wks bootimg")
  97. parser.add_option("-n", "--native-sysroot", dest="native_sysroot",
  98. help="path to the native sysroot containing the tools "
  99. "to use to build the image")
  100. parser.add_option("-p", "--skip-build-check", dest="build_check",
  101. action="store_false", default=True, help="skip the build check")
  102. parser.add_option("-f", "--build-rootfs", action="store_true", help="build rootfs")
  103. parser.add_option("-c", "--compress-with", choices=("gzip", "bzip2", "xz"),
  104. dest='compressor',
  105. help="compress image with specified compressor")
  106. parser.add_option("-m", "--bmap", action="store_true", help="generate .bmap")
  107. parser.add_option("-v", "--vars", dest='vars_dir',
  108. help="directory with <image>.env files that store "
  109. "bitbake variables")
  110. parser.add_option("-D", "--debug", dest="debug", action="store_true",
  111. default=False, help="output debug information")
  112. (options, args) = parser.parse_args(args)
  113. if len(args) != 1:
  114. logging.error("Wrong number of arguments, exiting\n")
  115. parser.print_help()
  116. sys.exit(1)
  117. if options.build_rootfs and not bitbake_main:
  118. logging.error("Can't build roofs as bitbake is not in the $PATH")
  119. sys.exit(1)
  120. if not options.image_name:
  121. missed = []
  122. for val, opt in [(options.rootfs_dir, 'rootfs-dir'),
  123. (options.bootimg_dir, 'bootimg-dir'),
  124. (options.kernel_dir, 'kernel-dir'),
  125. (options.native_sysroot, 'native-sysroot')]:
  126. if not val:
  127. missed.append(opt)
  128. if missed:
  129. print("The following build artifacts are not specified:")
  130. print(" " + ", ".join(missed))
  131. sys.exit(1)
  132. if options.image_name:
  133. BB_VARS.default_image = options.image_name
  134. else:
  135. options.build_check = False
  136. if options.vars_dir:
  137. BB_VARS.vars_dir = options.vars_dir
  138. if options.build_check:
  139. print("Checking basic build environment...")
  140. if not engine.verify_build_env():
  141. print("Couldn't verify build environment, exiting\n")
  142. sys.exit(1)
  143. else:
  144. print("Done.\n")
  145. bootimg_dir = ""
  146. if options.image_name:
  147. if options.build_rootfs:
  148. argv = ["bitbake", options.image_name]
  149. if options.debug:
  150. argv.append("--debug")
  151. print("Building rootfs...\n")
  152. if bitbake_main(BitBakeConfigParameters(argv),
  153. cookerdata.CookerConfiguration()):
  154. sys.exit(1)
  155. rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", options.image_name)
  156. kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE", options.image_name)
  157. native_sysroot = get_bitbake_var("STAGING_DIR_NATIVE",
  158. options.image_name)
  159. else:
  160. if options.build_rootfs:
  161. print("Image name is not specified, exiting. (Use -e/--image-name to specify it)\n")
  162. sys.exit(1)
  163. wks_file = args[0]
  164. if not wks_file.endswith(".wks"):
  165. wks_file = engine.find_canned_image(scripts_path, wks_file)
  166. if not wks_file:
  167. print("No image named %s found, exiting. (Use 'wic list images' "\
  168. "to list available images, or specify a fully-qualified OE "\
  169. "kickstart (.wks) filename)\n" % args[0])
  170. sys.exit(1)
  171. image_output_dir = ""
  172. if options.outdir:
  173. image_output_dir = options.outdir
  174. if not options.image_name:
  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. if rootfs_dir and not os.path.isdir(rootfs_dir):
  182. print("--roofs-dir (-r) not found, exiting\n")
  183. sys.exit(1)
  184. if not os.path.isdir(bootimg_dir):
  185. print("--bootimg-dir (-b) not found, exiting\n")
  186. sys.exit(1)
  187. if not os.path.isdir(kernel_dir):
  188. print("--kernel-dir (-k) not found, exiting\n")
  189. sys.exit(1)
  190. if not os.path.isdir(native_sysroot):
  191. print("--native-sysroot (-n) not found, exiting\n")
  192. sys.exit(1)
  193. else:
  194. not_found = not_found_dir = ""
  195. if not os.path.isdir(rootfs_dir):
  196. (not_found, not_found_dir) = ("rootfs-dir", rootfs_dir)
  197. elif not os.path.isdir(kernel_dir):
  198. (not_found, not_found_dir) = ("kernel-dir", kernel_dir)
  199. elif not os.path.isdir(native_sysroot):
  200. (not_found, not_found_dir) = ("native-sysroot", native_sysroot)
  201. if not_found:
  202. if not not_found_dir:
  203. not_found_dir = "Completely missing artifact - wrong image (.wks) used?"
  204. print("Build artifacts not found, exiting.")
  205. print(" (Please check that the build artifacts for the machine")
  206. print(" selected in local.conf actually exist and that they")
  207. print(" are the correct artifacts for the image (.wks file)).\n")
  208. print("The artifact that couldn't be found was %s:\n %s" % \
  209. (not_found, not_found_dir))
  210. sys.exit(1)
  211. krootfs_dir = options.rootfs_dir
  212. if krootfs_dir is None:
  213. krootfs_dir = {}
  214. krootfs_dir['ROOTFS_DIR'] = rootfs_dir
  215. rootfs_dir = rootfs_dir_to_args(krootfs_dir)
  216. print("Creating image(s)...\n")
  217. engine.wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
  218. native_sysroot, scripts_path, image_output_dir,
  219. options.compressor, options.bmap, options.debug)
  220. def wic_list_subcommand(args, usage_str):
  221. """
  222. Command-line handling for listing available images.
  223. The real work is done by image.engine.wic_list()
  224. """
  225. parser = optparse.OptionParser(usage=usage_str)
  226. args = parser.parse_args(args)[1]
  227. if not engine.wic_list(args, scripts_path):
  228. logging.error("Bad list arguments, exiting\n")
  229. parser.print_help()
  230. sys.exit(1)
  231. def wic_help_topic_subcommand(args, usage_str):
  232. """
  233. Command-line handling for help-only 'subcommands'. This is
  234. essentially a dummy command that doesn nothing but allow users to
  235. use the existing subcommand infrastructure to display help on a
  236. particular topic not attached to any particular subcommand.
  237. """
  238. pass
  239. wic_help_topic_usage = """
  240. """
  241. subcommands = {
  242. "create": [wic_create_subcommand,
  243. hlp.wic_create_usage,
  244. hlp.wic_create_help],
  245. "list": [wic_list_subcommand,
  246. hlp.wic_list_usage,
  247. hlp.wic_list_help],
  248. "plugins": [wic_help_topic_subcommand,
  249. wic_help_topic_usage,
  250. hlp.get_wic_plugins_help],
  251. "overview": [wic_help_topic_subcommand,
  252. wic_help_topic_usage,
  253. hlp.wic_overview_help],
  254. "kickstart": [wic_help_topic_subcommand,
  255. wic_help_topic_usage,
  256. hlp.wic_kickstart_help],
  257. }
  258. def start_logging(loglevel):
  259. logging.basicConfig(filename='wic.log', filemode='w', level=loglevel)
  260. def main(argv):
  261. parser = optparse.OptionParser(version="wic version %s" % __version__,
  262. usage=hlp.wic_usage)
  263. parser.disable_interspersed_args()
  264. args = parser.parse_args(argv)[1]
  265. if len(args):
  266. if args[0] == "help":
  267. if len(args) == 1:
  268. parser.print_help()
  269. sys.exit(1)
  270. return hlp.invoke_subcommand(args, parser, hlp.wic_help_usage, subcommands)
  271. if __name__ == "__main__":
  272. try:
  273. sys.exit(main(sys.argv[1:]))
  274. except WicError as err:
  275. print("ERROR:", err, file=sys.stderr)
  276. sys.exit(1)