summaryrefslogtreecommitdiffstats
path: root/scripts/lib/image/engine.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/image/engine.py')
-rw-r--r--scripts/lib/image/engine.py280
1 files changed, 280 insertions, 0 deletions
diff --git a/scripts/lib/image/engine.py b/scripts/lib/image/engine.py
new file mode 100644
index 0000000000..be29222df1
--- /dev/null
+++ b/scripts/lib/image/engine.py
@@ -0,0 +1,280 @@
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
31import os
32import sys
33from abc import ABCMeta, abstractmethod
34import shlex
35import json
36import subprocess
37import shutil
38
39import os, sys, errno
40from mic import msger, creator
41from mic.utils import cmdln, misc, errors
42from mic.conf import configmgr
43from mic.plugin import pluginmgr
44from mic.__version__ import VERSION
45from mic.utils.oe.misc import *
46
47
48def 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
63def get_line_val(line, key):
64 """
65 Extract the value from the VAR="val" string
66 """
67 if line.startswith(key + "="):
68 stripped_line = line.split('=')[1]
69 stripped_line = stripped_line.replace('\"', '')
70 return stripped_line
71 return None
72
73
74def find_artifacts(image_name):
75 """
76 Gather the build artifacts for the current image (the image_name
77 e.g. core-image-minimal) for the current MACHINE set in local.conf
78 """
79 bitbake_env_cmd = "bitbake -e %s" % image_name
80 rc, bitbake_env_lines = exec_cmd(bitbake_env_cmd)
81 if rc != 0:
82 print "Couldn't get '%s' output, exiting." % bitbake_env_cmd
83 sys.exit(1)
84
85 for line in bitbake_env_lines.split('\n'):
86 if (get_line_val(line, "IMAGE_ROOTFS")):
87 rootfs_dir = get_line_val(line, "IMAGE_ROOTFS")
88 continue
89 if (get_line_val(line, "STAGING_KERNEL_DIR")):
90 kernel_dir = get_line_val(line, "STAGING_KERNEL_DIR")
91 continue
92 if (get_line_val(line, "HDDDIR")):
93 hdddir = get_line_val(line, "HDDDIR")
94 continue
95 if (get_line_val(line, "STAGING_DATADIR")):
96 staging_data_dir = get_line_val(line, "STAGING_DATADIR")
97 continue
98 if (get_line_val(line, "STAGING_DIR_NATIVE")):
99 native_sysroot = get_line_val(line, "STAGING_DIR_NATIVE")
100 continue
101
102 return (rootfs_dir, kernel_dir, hdddir, staging_data_dir, native_sysroot)
103
104
105CANNED_IMAGE_DIR = "lib/image/canned-wks" # relative to scripts
106
107def find_canned_image(scripts_path, wks_file):
108 """
109 Find a .wks file with the given name in the canned files dir.
110
111 Return False if not found
112 """
113 canned_wks_dir = os.path.join(scripts_path, CANNED_IMAGE_DIR)
114
115 for root, dirs, files in os.walk(canned_wks_dir):
116 for file in files:
117 if file.endswith("~") or file.endswith("#"):
118 continue
119 if file.endswith(".wks") and wks_file + ".wks" == file:
120 fullpath = os.path.join(canned_wks_dir, file)
121 return fullpath
122 return None
123
124
125def list_canned_images(scripts_path):
126 """
127 List the .wks files in the canned image dir, minus the extension.
128 """
129 canned_wks_dir = os.path.join(scripts_path, CANNED_IMAGE_DIR)
130
131 for root, dirs, files in os.walk(canned_wks_dir):
132 for file in files:
133 if file.endswith("~") or file.endswith("#"):
134 continue
135 if file.endswith(".wks"):
136 fullpath = os.path.join(canned_wks_dir, file)
137 f = open(fullpath, "r")
138 lines = f.readlines()
139 for line in lines:
140 desc = ""
141 idx = line.find("short-description:")
142 if idx != -1:
143 desc = line[idx + len("short-description:"):].strip()
144 break
145 basename = os.path.splitext(file)[0]
146 print " %s\t\t%s" % (basename, desc)
147
148
149def list_canned_image_help(scripts_path, fullpath):
150 """
151 List the help and params in the specified canned image.
152 """
153 canned_wks_dir = os.path.join(scripts_path, CANNED_IMAGE_DIR)
154
155 f = open(fullpath, "r")
156 lines = f.readlines()
157 found = False
158 for line in lines:
159 if not found:
160 idx = line.find("long-description:")
161 if idx != -1:
162 print
163 print line[idx + len("long-description:"):].strip()
164 found = True
165 continue
166 if not line.strip():
167 break
168 idx = line.find("#")
169 if idx != -1:
170 print line[idx + len("#:"):].rstrip()
171 else:
172 break
173
174
175def wic_create(args, wks_file, rootfs_dir, bootimg_dir, kernel_dir,
176 native_sysroot, hdddir, staging_data_dir, scripts_path,
177 image_output_dir, properties_file, properties=None):
178 """
179 Create image
180
181 wks_file - user-defined OE kickstart file
182 rootfs_dir - absolute path to the build's /rootfs dir
183 bootimg_dir - absolute path to the build's boot artifacts directory
184 kernel_dir - absolute path to the build's kernel directory
185 native_sysroot - absolute path to the build's native sysroots dir
186 hdddir - absolute path to the build's HDDDIR dir
187 staging_data_dir - absolute path to the build's STAGING_DATA_DIR dir
188 scripts_path - absolute path to /scripts dir
189 image_output_dir - dirname to create for image
190 properties_file - use values from this file if nonempty i.e no prompting
191 properties - use values from this string if nonempty i.e no prompting
192
193 Normally, the values for the build artifacts values are determined
194 by 'wic -e' from the output of the 'bitbake -e' command given an
195 image name e.g. 'core-image-minimal' and a given machine set in
196 local.conf. If that's the case, the variables get the following
197 values from the output of 'bitbake -e':
198
199 rootfs_dir: IMAGE_ROOTFS
200 kernel_dir: STAGING_KERNEL_DIR
201 native_sysroot: STAGING_DIR_NATIVE
202 hdddir: HDDDIR
203 staging_data_dir: STAGING_DATA_DIR
204
205 In the above case, bootimg_dir remains unset and the image
206 creation code determines which of the passed-in directories to
207 use.
208
209 In the case where the values are passed in explicitly i.e 'wic -e'
210 is not used but rather the individual 'wic' options are used to
211 explicitly specify these values, hdddir and staging_data_dir will
212 be unset, but bootimg_dir must be explicit i.e. explicitly set to
213 either hdddir or staging_data_dir, depending on the image being
214 generated. The other values (rootfs_dir, kernel_dir, and
215 native_sysroot) correspond to the same values found above via
216 'bitbake -e').
217
218 """
219 try:
220 oe_builddir = os.environ["BUILDDIR"]
221 except KeyError:
222 print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)"
223 sys.exit(1)
224
225 direct_args = list()
226 direct_args.insert(0, oe_builddir)
227 direct_args.insert(0, image_output_dir)
228 direct_args.insert(0, wks_file)
229 direct_args.insert(0, rootfs_dir)
230 direct_args.insert(0, bootimg_dir)
231 direct_args.insert(0, kernel_dir)
232 direct_args.insert(0, native_sysroot)
233 direct_args.insert(0, hdddir)
234 direct_args.insert(0, staging_data_dir)
235 direct_args.insert(0, "direct")
236
237 cr = creator.Creator()
238
239 cr.main(direct_args)
240
241 print "\nThe image(s) were created using OE kickstart file:\n %s" % wks_file
242
243
244def wic_list(args, scripts_path, properties_file):
245 """
246 Print the complete list of properties defined by the image, or the
247 possible values for a particular image property.
248 """
249 if len(args) < 1:
250 return False
251
252 if len(args) == 1:
253 if args[0] == "images":
254 list_canned_images(scripts_path)
255 return True
256 elif args[0] == "properties":
257 return True
258 else:
259 return False
260
261 if len(args) == 2:
262 if args[0] == "properties":
263 wks_file = args[1]
264 print "print properties contained in wks file: %s" % wks_file
265 return True
266 elif args[0] == "property":
267 print "print property values for property: %s" % args[1]
268 return True
269 elif args[1] == "help":
270 wks_file = args[0]
271 fullpath = find_canned_image(scripts_path, wks_file)
272 if not fullpath:
273 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
274 sys.exit(1)
275 list_canned_image_help(scripts_path, fullpath)
276 return True
277 else:
278 return False
279
280 return False