blob: 76b93e82f282194e94fc529b010e46a93f733735 [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"):
47 print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)"
48 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]
110 print " %s\t\t%s" % (basename.ljust(30), desc)
111
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:
123 print
124 print line[idx + len("long-description:"):].strip()
125 found = True
126 continue
127 if not line.strip():
128 break
129 idx = line.find("#")
130 if idx != -1:
131 print line[idx + len("#:"):].rstrip()
132 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:
143 print " %s" % plugin
144
145
146def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
147 native_sysroot, scripts_path, image_output_dir,
148 compressor, debug):
149 """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
159
160 Normally, the values for the build artifacts values are determined
161 by 'wic -e' from the output of the 'bitbake -e' command given an
162 image name e.g. 'core-image-minimal' and a given machine set in
163 local.conf. If that's the case, the variables get the following
164 values from the output of 'bitbake -e':
165
166 rootfs_dir: IMAGE_ROOTFS
167 kernel_dir: DEPLOY_DIR_IMAGE
168 native_sysroot: STAGING_DIR_NATIVE
169
170 In the above case, bootimg_dir remains unset and the
171 plugin-specific image creation code is responsible for finding the
172 bootimg artifacts.
173
174 In the case where the values are passed in explicitly i.e 'wic -e'
175 is not used but rather the individual 'wic' options are used to
176 explicitly specify these values.
177 """
178 try:
179 oe_builddir = os.environ["BUILDDIR"]
180 except KeyError:
181 print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)"
182 sys.exit(1)
183
184 if debug:
185 msger.set_loglevel('debug')
186
187 crobj = creator.Creator()
188
189 crobj.main(["direct", native_sysroot, kernel_dir, bootimg_dir, rootfs_dir,
190 wks_file, image_output_dir, oe_builddir, compressor or ""])
191
192 print "\nThe image(s) were created using OE kickstart file:\n %s" % wks_file
193
194
195def wic_list(args, scripts_path):
196 """
197 Print the list of images or source plugins.
198 """
199 if len(args) < 1:
200 return False
201
202 if args == ["images"]:
203 list_canned_images(scripts_path)
204 return True
205 elif args == ["source-plugins"]:
206 list_source_plugins()
207 return True
208 elif len(args) == 2 and args[1] == "help":
209 wks_file = args[0]
210 fullpath = find_canned_image(scripts_path, wks_file)
211 if not fullpath:
212 print "No image named %s found, exiting. "\
213 "(Use 'wic list images' to list available images, or "\
214 "specify a fully-qualified OE kickstart (.wks) "\
215 "filename)\n" % wks_file
216 sys.exit(1)
217 list_canned_image_help(scripts_path, fullpath)
218 return True
219
220 return False