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