blob: 5b104631ca635b0183b601b5552036cc8e4a5f24 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
31import os
32import sys
33
34from wic import msger, creator
35from wic.utils import misc
36from wic.plugin import pluginmgr
37from wic.utils.oe import misc
38
39
40def verify_build_env():
41 """
42 Verify that the build environment is sane.
43
44 Returns True if it is, false otherwise
45 """
46 if not os.environ.get("BUILDDIR"):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047 print("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 sys.exit(1)
49
50 return True
51
52
53CANNED_IMAGE_DIR = "lib/wic/canned-wks" # relative to scripts
54SCRIPTS_CANNED_IMAGE_DIR = "scripts/" + CANNED_IMAGE_DIR
55
56def build_canned_image_list(path):
57 layers_path = misc.get_bitbake_var("BBLAYERS")
58 canned_wks_layer_dirs = []
59
60 if layers_path is not None:
61 for layer_path in layers_path.split():
62 cpath = os.path.join(layer_path, SCRIPTS_CANNED_IMAGE_DIR)
63 canned_wks_layer_dirs.append(cpath)
64
65 cpath = os.path.join(path, CANNED_IMAGE_DIR)
66 canned_wks_layer_dirs.append(cpath)
67
68 return canned_wks_layer_dirs
69
70def find_canned_image(scripts_path, wks_file):
71 """
72 Find a .wks file with the given name in the canned files dir.
73
74 Return False if not found
75 """
76 layers_canned_wks_dir = build_canned_image_list(scripts_path)
77
78 for canned_wks_dir in layers_canned_wks_dir:
79 for root, dirs, files in os.walk(canned_wks_dir):
80 for fname in files:
81 if fname.endswith("~") or fname.endswith("#"):
82 continue
83 if fname.endswith(".wks") and wks_file + ".wks" == fname:
84 fullpath = os.path.join(canned_wks_dir, fname)
85 return fullpath
86 return None
87
88
89def list_canned_images(scripts_path):
90 """
91 List the .wks files in the canned image dir, minus the extension.
92 """
93 layers_canned_wks_dir = build_canned_image_list(scripts_path)
94
95 for canned_wks_dir in layers_canned_wks_dir:
96 for root, dirs, files in os.walk(canned_wks_dir):
97 for fname in files:
98 if fname.endswith("~") or fname.endswith("#"):
99 continue
100 if fname.endswith(".wks"):
101 fullpath = os.path.join(canned_wks_dir, fname)
102 with open(fullpath) as wks:
103 for line in wks:
104 desc = ""
105 idx = line.find("short-description:")
106 if idx != -1:
107 desc = line[idx + len("short-description:"):].strip()
108 break
109 basename = os.path.splitext(fname)[0]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600110 print(" %s\t\t%s" % (basename.ljust(30), desc))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
112
113def list_canned_image_help(scripts_path, fullpath):
114 """
115 List the help and params in the specified canned image.
116 """
117 found = False
118 with open(fullpath) as wks:
119 for line in wks:
120 if not found:
121 idx = line.find("long-description:")
122 if idx != -1:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600123 print()
124 print(line[idx + len("long-description:"):].strip())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 found = True
126 continue
127 if not line.strip():
128 break
129 idx = line.find("#")
130 if idx != -1:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600131 print(line[idx + len("#:"):].rstrip())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 else:
133 break
134
135
136def list_source_plugins():
137 """
138 List the available source plugins i.e. plugins available for --source.
139 """
140 plugins = pluginmgr.get_source_plugins()
141
142 for plugin in plugins:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600143 print(" %s" % plugin)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144
145
146def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
147 native_sysroot, scripts_path, image_output_dir,
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600148 compressor, bmap, debug):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 """Create image
150
151 wks_file - user-defined OE kickstart file
152 rootfs_dir - absolute path to the build's /rootfs dir
153 bootimg_dir - absolute path to the build's boot artifacts directory
154 kernel_dir - absolute path to the build's kernel directory
155 native_sysroot - absolute path to the build's native sysroots dir
156 scripts_path - absolute path to /scripts dir
157 image_output_dir - dirname to create for image
158 compressor - compressor utility to compress the image
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600159 bmap - enable generation of .bmap
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160
161 Normally, the values for the build artifacts values are determined
162 by 'wic -e' from the output of the 'bitbake -e' command given an
163 image name e.g. 'core-image-minimal' and a given machine set in
164 local.conf. If that's the case, the variables get the following
165 values from the output of 'bitbake -e':
166
167 rootfs_dir: IMAGE_ROOTFS
168 kernel_dir: DEPLOY_DIR_IMAGE
169 native_sysroot: STAGING_DIR_NATIVE
170
171 In the above case, bootimg_dir remains unset and the
172 plugin-specific image creation code is responsible for finding the
173 bootimg artifacts.
174
175 In the case where the values are passed in explicitly i.e 'wic -e'
176 is not used but rather the individual 'wic' options are used to
177 explicitly specify these values.
178 """
179 try:
180 oe_builddir = os.environ["BUILDDIR"]
181 except KeyError:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182 print("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 sys.exit(1)
184
185 if debug:
186 msger.set_loglevel('debug')
187
188 crobj = creator.Creator()
189
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600190 cmdline = ["direct", native_sysroot, kernel_dir, bootimg_dir, rootfs_dir,
191 wks_file, image_output_dir, oe_builddir, compressor or ""]
192 if bmap:
193 cmdline.append('--bmap')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600195 crobj.main(cmdline)
196
197 print("\nThe image(s) were created using OE kickstart file:\n %s" % wks_file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
199
200def wic_list(args, scripts_path):
201 """
202 Print the list of images or source plugins.
203 """
204 if len(args) < 1:
205 return False
206
207 if args == ["images"]:
208 list_canned_images(scripts_path)
209 return True
210 elif args == ["source-plugins"]:
211 list_source_plugins()
212 return True
213 elif len(args) == 2 and args[1] == "help":
214 wks_file = args[0]
215 fullpath = find_canned_image(scripts_path, wks_file)
216 if not fullpath:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600217 print("No image named %s found, exiting. "\
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218 "(Use 'wic list images' to list available images, or "\
219 "specify a fully-qualified OE kickstart (.wks) "\
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 "filename)\n" % wks_file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 sys.exit(1)
222 list_canned_image_help(scripts_path, fullpath)
223 return True
224
225 return False