blob: 6f3dc277439acf8cb5bdd0ff088b5922420b72cd [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001#
2# Copyright (c) 2015, Intel Corporation.
Brad Bishopd7bf8c12018-02-25 22:55:05 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005#
6# AUTHORS
7# Ed Bartosh <ed.bartosh@linux.intel.com>
8
9"""Test cases for wic."""
10
11import os
12import sys
13import unittest
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050014import hashlib
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015
16from glob import glob
17from shutil import rmtree, copy
18from functools import wraps, lru_cache
19from tempfile import NamedTemporaryFile
20
21from oeqa.selftest.case import OESelftestTestCase
22from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023
24
25@lru_cache(maxsize=32)
26def get_host_arch(recipe):
27 """A cached call to get_bb_var('HOST_ARCH', <recipe>)"""
28 return get_bb_var('HOST_ARCH', recipe)
29
30
31def only_for_arch(archs, image='core-image-minimal'):
32 """Decorator for wrapping test cases that can be run only for specific target
33 architectures. A list of compatible architectures is passed in `archs`.
34 Current architecture will be determined by parsing bitbake output for
35 `image` recipe.
36 """
37 def wrapper(func):
38 @wraps(func)
39 def wrapped_f(*args, **kwargs):
40 arch = get_host_arch(image)
41 if archs and arch not in archs:
42 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
43 return func(*args, **kwargs)
44 wrapped_f.__name__ = func.__name__
45 return wrapped_f
46 return wrapper
47
Andrew Geissler82c905d2020-04-13 13:39:40 -050048def extract_files(debugfs_output):
49 """
50 extract file names from the output of debugfs -R 'ls -p',
51 which looks like this:
52
53 /2/040755/0/0/.//\n
54 /2/040755/0/0/..//\n
55 /11/040700/0/0/lost+found^M//\n
56 /12/040755/1002/1002/run//\n
57 /13/040755/1002/1002/sys//\n
58 /14/040755/1002/1002/bin//\n
59 /80/040755/1002/1002/var//\n
60 /92/040755/1002/1002/tmp//\n
61 """
62 # NOTE the occasional ^M in file names
63 return [line.split('/')[5].strip() for line in \
64 debugfs_output.strip().split('/\n')]
65
66def files_own_by_root(debugfs_output):
67 for line in debugfs_output.strip().split('/\n'):
68 if line.split('/')[3:5] != ['0', '0']:
69 print(debugfs_output)
70 return False
71 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080073class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 """Wic test class."""
75
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 wicenv_cache = {}
78
79 def setUpLocal(self):
80 """This code is executed before each test method."""
Brad Bishopc4ea0752018-11-15 14:30:15 -080081 self.resultdir = self.builddir + "/wic-tmp/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080082 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083
84 # Do this here instead of in setUpClass as the base setUp does some
85 # clean up which can result in the native tools built earlier in
86 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080087 if not WicTestCase.image_is_ready:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 if get_bb_var('USE_NLS') == 'yes':
89 bitbake('wic-tools')
90 else:
91 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable')
92
93 bitbake('core-image-minimal')
Andrew Geissler82c905d2020-04-13 13:39:40 -050094 bitbake('core-image-minimal-mtdutils')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080095 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096
97 rmtree(self.resultdir, ignore_errors=True)
98
99 def tearDownLocal(self):
100 """Remove resultdir as it may contain images."""
101 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800102 super(WicTestCase, self).tearDownLocal()
103
104 def _get_image_env_path(self, image):
105 """Generate and obtain the path to <image>.env"""
106 if image not in WicTestCase.wicenv_cache:
107 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status)
108 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image)
109 stdir = bb_vars['STAGING_DIR']
110 machine = bb_vars['MACHINE']
111 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
112 return WicTestCase.wicenv_cache[image]
113
114class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 def test_version(self):
117 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800118 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 def test_help(self):
121 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 runCmd('wic --help')
123 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125 def test_createhelp(self):
126 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800127 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 def test_listhelp(self):
130 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800131 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133 def test_help_create(self):
134 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800135 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500137 def test_help_list(self):
138 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 def test_help_overview(self):
142 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800143 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 def test_help_plugins(self):
146 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500149 def test_help_kickstart(self):
150 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800151 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500152
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500153 def test_list_images(self):
154 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800155 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 def test_list_source_plugins(self):
158 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800159 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500160
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500161 def test_listed_images_help(self):
162 """Test wic listed images help"""
163 output = runCmd('wic list images').output
164 imagelist = [line.split()[0] for line in output.splitlines()]
165 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800166 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500167
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 def test_unsupported_subcommand(self):
169 """Test unsupported subcommand"""
170 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
171
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500172 def test_no_command(self):
173 """Test wic without command"""
174 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
175
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500176 def test_build_image_name(self):
177 """Test wic create wictestdisk --image-name=core-image-minimal"""
178 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800179 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500180 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
181
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500182 @only_for_arch(['i586', 'i686', 'x86_64'])
183 def test_gpt_image(self):
184 """Test creation of core-image-minimal with gpt table and UUID boot"""
185 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800186 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500187 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
188
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189 @only_for_arch(['i586', 'i686', 'x86_64'])
190 def test_iso_image(self):
191 """Test creation of hybrid iso image with legacy and EFI boot"""
192 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500193 'MACHINE_FEATURES:append = " efi"\n'\
194 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800196 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500197 self.remove_config(config)
198 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800199 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500200 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct")))
201 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso")))
202
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500203 @only_for_arch(['i586', 'i686', 'x86_64'])
204 def test_qemux86_directdisk(self):
205 """Test creation of qemux-86-directdisk image"""
206 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800207 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500208 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct")))
209
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500210 @only_for_arch(['i586', 'i686', 'x86_64'])
211 def test_mkefidisk(self):
212 """Test creation of mkefidisk image"""
213 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800214 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500215 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct")))
216
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217 @only_for_arch(['i586', 'i686', 'x86_64'])
218 def test_bootloader_config(self):
219 """Test creation of directdisk-bootloader-config image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500220 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500221 self.append_config(config)
222 bitbake('core-image-minimal')
223 self.remove_config(config)
224 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800225 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500226 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct")))
227
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500228 @only_for_arch(['i586', 'i686', 'x86_64'])
229 def test_systemd_bootdisk(self):
230 """Test creation of systemd-bootdisk image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500231 config = 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500232 self.append_config(config)
233 bitbake('core-image-minimal')
234 self.remove_config(config)
235 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800236 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500237 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct")))
238
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500239 def test_efi_bootpart(self):
240 """Test creation of efi-bootpart image"""
241 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
242 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
243 self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype)
244 runCmd(cmd)
245 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
246 images = glob(self.resultdir + "mkefidisk-*.direct")
247 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
248 self.assertIn("kernel",result.output)
249
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500250 def test_sdimage_bootpart(self):
251 """Test creation of sdimage-bootpart image"""
252 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
253 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
254 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800255 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500256 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
257
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500258 @only_for_arch(['i586', 'i686', 'x86_64'])
259 def test_default_output_dir(self):
260 """Test default output location"""
261 for fname in glob("directdisk-*.direct"):
262 os.remove(fname)
Patrick Williams213cb262021-08-07 19:21:33 -0500263 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500264 self.append_config(config)
265 bitbake('core-image-minimal')
266 self.remove_config(config)
267 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800268 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269 self.assertEqual(1, len(glob("directdisk-*.direct")))
270
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500271 @only_for_arch(['i586', 'i686', 'x86_64'])
272 def test_build_artifacts(self):
273 """Test wic create directdisk providing all artifacts."""
274 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
275 'wic-tools')
276 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
277 'core-image-minimal'))
278 bbvars = {key.lower(): value for key, value in bb_vars.items()}
279 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800280 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 "-b %(staging_datadir)s "
282 "-k %(deploy_dir_image)s "
283 "-n %(recipe_sysroot_native)s "
284 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800285 "-o %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
287
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500288 def test_compress_gzip(self):
289 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800290 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500291 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800292 "-c gzip -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500293 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz")))
294
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 def test_compress_bzip2(self):
296 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800297 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500298 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800299 "-c bzip2 -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500300 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2")))
301
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500302 def test_compress_xz(self):
303 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800304 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500305 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800306 "--compress-with=xz -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500307 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz")))
308
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 def test_wrong_compressor(self):
310 """Test how wic breaks if wrong compressor is provided"""
311 self.assertEqual(2, runCmd("wic create wictestdisk "
312 "--image-name=core-image-minimal "
313 "-c wrong -o %s" % self.resultdir,
314 ignore_status=True).status)
315
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500316 def test_debug_short(self):
317 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800318 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500319 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800320 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600322 self.assertEqual(1, len(glob(self.resultdir + "tmp.wic*")))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 def test_debug_long(self):
325 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800326 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500327 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800328 "--debug -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500329 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600330 self.assertEqual(1, len(glob(self.resultdir + "tmp.wic*")))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500332 def test_skip_build_check_short(self):
333 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800334 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500335 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800336 "-s -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500337 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
338
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500339 def test_skip_build_check_long(self):
340 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800341 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500342 "--image-name=core-image-minimal "
343 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800344 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500345 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
346
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 def test_build_rootfs_short(self):
348 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800349 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500350 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800351 "-f -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500352 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
353
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500354 def test_build_rootfs_long(self):
355 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800356 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500357 "--image-name=core-image-minimal "
358 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800359 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500360 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
361
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500362 @only_for_arch(['i586', 'i686', 'x86_64'])
363 def test_rootfs_indirect_recipes(self):
364 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800365 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500366 "--image-name=core-image-minimal "
367 "--rootfs rootfs1=core-image-minimal "
368 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800369 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500370 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct")))
371
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500372 @only_for_arch(['i586', 'i686', 'x86_64'])
373 def test_rootfs_artifacts(self):
374 """Test usage of rootfs plugin with rootfs paths"""
375 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
376 'wic-tools')
377 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
378 'core-image-minimal'))
379 bbvars = {key.lower(): value for key, value in bb_vars.items()}
380 bbvars['wks'] = "directdisk-multi-rootfs"
381 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800382 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500383 "--bootimg-dir=%(staging_datadir)s "
384 "--kernel-dir=%(deploy_dir_image)s "
385 "--native-sysroot=%(recipe_sysroot_native)s "
386 "--rootfs-dir rootfs1=%(image_rootfs)s "
387 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800388 "--outdir %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500389 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars)))
390
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500391 def test_exclude_path(self):
392 """Test --exclude-path wks option."""
393
394 oldpath = os.environ['PATH']
395 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
396
397 try:
398 wks_file = 'temp.wks'
399 with open(wks_file, 'w') as wks:
400 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
401 wks.write("""
402part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
403part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
404part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
405 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800406 runCmd("wic create %s -e core-image-minimal -o %s" \
407 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500408
409 os.remove(wks_file)
410 wicout = glob(self.resultdir + "%s-*direct" % 'temp')
411 self.assertEqual(1, len(wicout))
412
413 wicimg = wicout[0]
414
415 # verify partition size with wic
416 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500417
418 # parse parted output which looks like this:
419 # BYT;\n
420 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
421 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
422 partlns = res.output.splitlines()[2:]
423
424 self.assertEqual(3, len(partlns))
425
426 for part in [1, 2, 3]:
427 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
428 partln = partlns[part-1].split(":")
429 self.assertEqual(7, len(partln))
430 start = int(partln[1].rstrip("B")) / 512
431 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800432 runCmd("dd if=%s of=%s skip=%d count=%d" %
433 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500434
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500435 # Test partition 1, should contain the normal root directories, except
436 # /usr.
437 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
438 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500439 files = extract_files(res.output)
440 self.assertIn("etc", files)
441 self.assertNotIn("usr", files)
442
443 # Partition 2, should contain common directories for /usr, not root
444 # directories.
445 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
446 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500447 files = extract_files(res.output)
448 self.assertNotIn("etc", files)
449 self.assertNotIn("usr", files)
450 self.assertIn("share", files)
451
452 # Partition 3, should contain the same as partition 2, including the bin
453 # directory, but not the files inside it.
454 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
455 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500456 files = extract_files(res.output)
457 self.assertNotIn("etc", files)
458 self.assertNotIn("usr", files)
459 self.assertIn("share", files)
460 self.assertIn("bin", files)
461 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
462 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500463 files = extract_files(res.output)
464 self.assertIn(".", files)
465 self.assertIn("..", files)
466 self.assertEqual(2, len(files))
467
468 for part in [1, 2, 3]:
469 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
470 os.remove(part_file)
471
472 finally:
473 os.environ['PATH'] = oldpath
474
Andrew Geissler82c905d2020-04-13 13:39:40 -0500475 def test_include_path(self):
476 """Test --include-path wks option."""
477
478 oldpath = os.environ['PATH']
479 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
480
481 try:
482 include_path = os.path.join(self.resultdir, 'test-include')
483 os.makedirs(include_path)
484 with open(os.path.join(include_path, 'test-file'), 'w') as t:
485 t.write("test\n")
486 wks_file = os.path.join(include_path, 'temp.wks')
487 with open(wks_file, 'w') as wks:
488 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
489 wks.write("""
490part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
491part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
492 % (include_path))
493 runCmd("wic create %s -e core-image-minimal -o %s" \
494 % (wks_file, self.resultdir))
495
496 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
497 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
498
499 # Test partition 1, should not contain 'test-file'
500 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
501 files = extract_files(res.output)
502 self.assertNotIn('test-file', files)
503 self.assertEqual(True, files_own_by_root(res.output))
504
505 # Test partition 2, should contain 'test-file'
506 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
507 files = extract_files(res.output)
508 self.assertIn('test-file', files)
509 self.assertEqual(True, files_own_by_root(res.output))
510
511 finally:
512 os.environ['PATH'] = oldpath
513
514 def test_include_path_embeded(self):
515 """Test --include-path wks option."""
516
517 oldpath = os.environ['PATH']
518 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
519
520 try:
521 include_path = os.path.join(self.resultdir, 'test-include')
522 os.makedirs(include_path)
523 with open(os.path.join(include_path, 'test-file'), 'w') as t:
524 t.write("test\n")
525 wks_file = os.path.join(include_path, 'temp.wks')
526 with open(wks_file, 'w') as wks:
527 wks.write("""
528part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
529 % (include_path))
530 runCmd("wic create %s -e core-image-minimal -o %s" \
531 % (wks_file, self.resultdir))
532
533 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
534
535 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
536 files = extract_files(res.output)
537 self.assertIn('test-file', files)
538 self.assertEqual(True, files_own_by_root(res.output))
539
540 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
541 files = extract_files(res.output)
542 self.assertIn('passwd', files)
543 self.assertEqual(True, files_own_by_root(res.output))
544
545 finally:
546 os.environ['PATH'] = oldpath
547
548 def test_include_path_errors(self):
549 """Test --include-path wks option error handling."""
550 wks_file = 'temp.wks'
551
552 # Absolute argument.
553 with open(wks_file, 'w') as wks:
554 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
555 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
556 % (wks_file, self.resultdir), ignore_status=True).status)
557 os.remove(wks_file)
558
559 # Argument pointing to parent directory.
560 with open(wks_file, 'w') as wks:
561 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
562 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
563 % (wks_file, self.resultdir), ignore_status=True).status)
564 os.remove(wks_file)
565
566 # 3 Argument pointing to parent directory.
567 with open(wks_file, 'w') as wks:
568 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
569 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
570 % (wks_file, self.resultdir), ignore_status=True).status)
571 os.remove(wks_file)
572
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500573 def test_exclude_path_errors(self):
574 """Test --exclude-path wks option error handling."""
575 wks_file = 'temp.wks'
576
577 # Absolute argument.
578 with open(wks_file, 'w') as wks:
579 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
580 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
581 % (wks_file, self.resultdir), ignore_status=True).status)
582 os.remove(wks_file)
583
584 # Argument pointing to parent directory.
585 with open(wks_file, 'w') as wks:
586 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
587 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
588 % (wks_file, self.resultdir), ignore_status=True).status)
589 os.remove(wks_file)
590
Andrew Geissler82c905d2020-04-13 13:39:40 -0500591 def test_permissions(self):
592 """Test permissions are respected"""
593
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600594 # prepare wicenv and rootfs
595 bitbake('core-image-minimal core-image-minimal-mtdutils -c do_rootfs_wicenv')
596
Andrew Geissler82c905d2020-04-13 13:39:40 -0500597 oldpath = os.environ['PATH']
598 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
599
600 t_normal = """
601part / --source rootfs --fstype=ext4
602"""
603 t_exclude = """
604part / --source rootfs --fstype=ext4 --exclude-path=home
605"""
606 t_multi = """
607part / --source rootfs --ondisk sda --fstype=ext4
608part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
609"""
610 t_change = """
611part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
612part /etc --source rootfs --fstype=ext4 --change-directory=etc
613"""
614 tests = [t_normal, t_exclude, t_multi, t_change]
615
616 try:
617 for test in tests:
618 include_path = os.path.join(self.resultdir, 'test-include')
619 os.makedirs(include_path)
620 wks_file = os.path.join(include_path, 'temp.wks')
621 with open(wks_file, 'w') as wks:
622 wks.write(test)
623 runCmd("wic create %s -e core-image-minimal -o %s" \
624 % (wks_file, self.resultdir))
625
626 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
627 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
628 self.assertEqual(True, files_own_by_root(res.output))
629
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600630 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "%s"\n' % wks_file
631 self.append_config(config)
632 bitbake('core-image-minimal')
633 tmpdir = os.path.join(get_bb_var('WORKDIR', 'core-image-minimal'),'build-wic')
634
635 # check each partition for permission
636 for part in glob(os.path.join(tmpdir, 'temp-*.direct.p*')):
637 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
638 self.assertTrue(files_own_by_root(res.output)
639 ,msg='Files permission incorrect using wks set "%s"' % test)
640
641 # clean config and result directory for next cases
642 self.remove_config(config)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500643 rmtree(self.resultdir, ignore_errors=True)
644
645 finally:
646 os.environ['PATH'] = oldpath
647
648 def test_change_directory(self):
649 """Test --change-directory wks option."""
650
651 oldpath = os.environ['PATH']
652 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
653
654 try:
655 include_path = os.path.join(self.resultdir, 'test-include')
656 os.makedirs(include_path)
657 wks_file = os.path.join(include_path, 'temp.wks')
658 with open(wks_file, 'w') as wks:
659 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
660 runCmd("wic create %s -e core-image-minimal -o %s" \
661 % (wks_file, self.resultdir))
662
663 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
664
665 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
666 files = extract_files(res.output)
667 self.assertIn('passwd', files)
668
669 finally:
670 os.environ['PATH'] = oldpath
671
672 def test_change_directory_errors(self):
673 """Test --change-directory wks option error handling."""
674 wks_file = 'temp.wks'
675
676 # Absolute argument.
677 with open(wks_file, 'w') as wks:
678 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
679 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
680 % (wks_file, self.resultdir), ignore_status=True).status)
681 os.remove(wks_file)
682
683 # Argument pointing to parent directory.
684 with open(wks_file, 'w') as wks:
685 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
686 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
687 % (wks_file, self.resultdir), ignore_status=True).status)
688 os.remove(wks_file)
689
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500690 def test_no_fstab_update(self):
691 """Test --no-fstab-update wks option."""
692
693 oldpath = os.environ['PATH']
694 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
695
696 # Get stock fstab from base-files recipe
697 self.assertEqual(0, bitbake('base-files -c do_install').status)
698 bf_fstab = os.path.join(get_bb_var('D', 'base-files'), 'etc/fstab')
699 self.assertEqual(True, os.path.exists(bf_fstab))
700 bf_fstab_md5sum = runCmd('md5sum %s 2>/dev/null' % bf_fstab).output.split(" ")[0]
701
702 try:
703 no_fstab_update_path = os.path.join(self.resultdir, 'test-no-fstab-update')
704 os.makedirs(no_fstab_update_path)
705 wks_file = os.path.join(no_fstab_update_path, 'temp.wks')
706 with open(wks_file, 'w') as wks:
707 wks.writelines(['part / --source rootfs --fstype=ext4 --label rootfs\n',
708 'part /mnt/p2 --source rootfs --rootfs-dir=core-image-minimal ',
709 '--fstype=ext4 --label p2 --no-fstab-update\n'])
710 runCmd("wic create %s -e core-image-minimal -o %s" \
711 % (wks_file, self.resultdir))
712
713 part_fstab_md5sum = []
714 for i in range(1, 3):
715 part = glob(os.path.join(self.resultdir, 'temp-*.direct.p') + str(i))[0]
716 part_fstab = runCmd("debugfs -R 'cat etc/fstab' %s 2>/dev/null" % (part))
717 part_fstab_md5sum.append(hashlib.md5((part_fstab.output + "\n\n").encode('utf-8')).hexdigest())
718
719 # '/etc/fstab' in partition 2 should contain the same stock fstab file
720 # as the one installed by the base-file recipe.
721 self.assertEqual(bf_fstab_md5sum, part_fstab_md5sum[1])
722
723 # '/etc/fstab' in partition 1 should contain an updated fstab file.
724 self.assertNotEqual(bf_fstab_md5sum, part_fstab_md5sum[0])
725
726 finally:
727 os.environ['PATH'] = oldpath
728
729 def test_no_fstab_update_errors(self):
730 """Test --no-fstab-update wks option error handling."""
731 wks_file = 'temp.wks'
732
733 # Absolute argument.
734 with open(wks_file, 'w') as wks:
735 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update /etc")
736 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
737 % (wks_file, self.resultdir), ignore_status=True).status)
738 os.remove(wks_file)
739
740 # Argument pointing to parent directory.
741 with open(wks_file, 'w') as wks:
742 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update ././..")
743 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
744 % (wks_file, self.resultdir), ignore_status=True).status)
745 os.remove(wks_file)
746
Andrew Geissler5199d832021-09-24 16:47:35 -0500747 def test_extra_space(self):
748 """Test --extra-space wks option."""
749 extraspace = 1024**3
750 runCmd("wic create wictestdisk "
751 "--image-name core-image-minimal "
752 "--extra-space %i -o %s" % (extraspace ,self.resultdir))
753 wicout = glob(self.resultdir + "wictestdisk-*.direct")
754 self.assertEqual(1, len(wicout))
755 size = os.path.getsize(wicout[0])
756 self.assertTrue(size > extraspace)
757
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800758class Wic2(WicTestCase):
759
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500760 def test_bmap_short(self):
761 """Test generation of .bmap file -m option"""
762 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800763 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500764 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
765 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
766
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500767 def test_bmap_long(self):
768 """Test generation of .bmap file --bmap option"""
769 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800770 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500771 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
772 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
773
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500774 def test_image_env(self):
775 """Test generation of <image>.env files."""
776 image = 'core-image-minimal'
777 imgdatadir = self._get_image_env_path(image)
778
779 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
780 basename = bb_vars['IMAGE_BASENAME']
781 self.assertEqual(basename, image)
782 path = os.path.join(imgdatadir, basename) + '.env'
783 self.assertTrue(os.path.isfile(path))
784
785 wicvars = set(bb_vars['WICVARS'].split())
786 # filter out optional variables
787 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400788 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
Andrew Geissler82c905d2020-04-13 13:39:40 -0500789 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500790 'APPEND', 'IMAGE_EFI_BOOT_FILES'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500791 with open(path) as envfile:
792 content = dict(line.split("=", 1) for line in envfile)
793 # test if variables used by wic present in the .env file
794 for var in wicvars:
795 self.assertTrue(var in content, "%s is not in .env file" % var)
796 self.assertTrue(content[var])
797
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500798 def test_image_vars_dir_short(self):
799 """Test image vars directory selection -v option"""
800 image = 'core-image-minimal'
801 imgenvdir = self._get_image_env_path(image)
802 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
803
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800804 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500805 "--image-name=%s -v %s -n %s -o %s"
806 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800807 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500808 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
809
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500810 def test_image_vars_dir_long(self):
811 """Test image vars directory selection --vars option"""
812 image = 'core-image-minimal'
813 imgenvdir = self._get_image_env_path(image)
814 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
815
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800816 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500817 "--image-name=%s "
818 "--vars %s "
819 "--native-sysroot %s "
820 "--outdir %s"
821 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800822 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500823 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
824
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500825 @only_for_arch(['i586', 'i686', 'x86_64'])
826 def test_wic_image_type(self):
827 """Test building wic images by bitbake"""
828 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500829 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500830 self.append_config(config)
831 self.assertEqual(0, bitbake('wic-image-minimal').status)
832 self.remove_config(config)
833
834 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
835 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
836 machine = bb_vars['MACHINE']
837 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
838 # check if we have result image and manifests symlinks
839 # pointing to existing files
840 for suffix in ('wic', 'manifest'):
841 path = prefix + suffix
842 self.assertTrue(os.path.islink(path))
843 self.assertTrue(os.path.isfile(os.path.realpath(path)))
844
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500845 @only_for_arch(['i586', 'i686', 'x86_64'])
846 def test_qemu(self):
847 """Test wic-image-minimal under qemu"""
848 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500849 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500850 self.append_config(config)
851 self.assertEqual(0, bitbake('wic-image-minimal').status)
852 self.remove_config(config)
853
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000854 with runqemu('wic-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600855 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
856 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400857 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600858 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
859 self.assertEqual(output, '4')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400860 cmd = "grep UUID= /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500861 status, output = qemu.run_serial(cmd)
862 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400863 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500864
865 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500866 def test_qemu_efi(self):
867 """Test core-image-minimal efi image under qemu"""
868 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
869 self.append_config(config)
870 self.assertEqual(0, bitbake('core-image-minimal ovmf').status)
871 self.remove_config(config)
872
873 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000874 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500875 cmd = "grep sda. /proc/partitions |wc -l"
876 status, output = qemu.run_serial(cmd)
877 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
878 self.assertEqual(output, '3')
879
880 @staticmethod
881 def _make_fixed_size_wks(size):
882 """
883 Create a wks of an image with a single partition. Size of the partition is set
884 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
885 """
886 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
887 wkspath = tempf.name
888 tempf.write("part " \
889 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
890 "--fstype=ext4\n" % size)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500891
892 return wkspath
893
894 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False):
895 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir),
896 ignore_status=ignore_status)
897
898 if p.status:
899 return (p, None)
900
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500901 wksname = os.path.splitext(os.path.basename(wkspath))[0]
902
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500903 wicout = glob(self.resultdir + "%s-*direct" % wksname)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500904
905 if not wicout:
906 return (p, None)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500907
908 wicimg = wicout[0]
909
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500910 if not native_sysroot:
911 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800912
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500913 # verify partition size with wic
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500914 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800915 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500916
917 # parse parted output which looks like this:
918 # BYT;\n
919 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
920 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500921 return (p, res.output.splitlines()[2:])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500922
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500923 def test_fixed_size(self):
924 """
925 Test creation of a simple image with partition size controlled through
926 --fixed-size flag
927 """
928 wkspath = Wic2._make_fixed_size_wks(200)
929 _, partlns = self._get_wic_partitions(wkspath)
930 os.remove(wkspath)
931
932 self.assertEqual(partlns, [
933 "1:4.00kiB:204804kiB:204800kiB:ext4::;",
934 ])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500935
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500936 def test_fixed_size_error(self):
937 """
938 Test creation of a simple image with partition size controlled through
939 --fixed-size flag. The size of partition is intentionally set to 1MiB
940 in order to trigger an error in wic.
941 """
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500942 wkspath = Wic2._make_fixed_size_wks(1)
943 p, _ = self._get_wic_partitions(wkspath, ignore_status=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500944 os.remove(wkspath)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500945
946 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
947
948 def test_offset(self):
949 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
950
951 with NamedTemporaryFile("w", suffix=".wks") as tempf:
952 # Test that partitions are placed at the correct offsets, default KB
953 tempf.write("bootloader --ptable gpt\n" \
954 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
955 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
956 tempf.flush()
957
958 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
959 self.assertEqual(partlns, [
960 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
961 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
962 ])
963
964 with NamedTemporaryFile("w", suffix=".wks") as tempf:
965 # Test that partitions are placed at the correct offsets, same with explicit KB
966 tempf.write("bootloader --ptable gpt\n" \
967 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
968 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n")
969 tempf.flush()
970
971 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
972 self.assertEqual(partlns, [
973 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
974 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
975 ])
976
977 with NamedTemporaryFile("w", suffix=".wks") as tempf:
978 # Test that partitions are placed at the correct offsets using MB
979 tempf.write("bootloader --ptable gpt\n" \
980 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
981 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n")
982 tempf.flush()
983
984 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
985 self.assertEqual(partlns, [
986 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
987 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;",
988 ])
989
990 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500991 # Test that partitions can be placed on a 512 byte sector boundary
992 tempf.write("bootloader --ptable gpt\n" \
993 "part / --source rootfs --ondisk hda --offset 65s --fixed-size 99M --fstype=ext4\n" \
994 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
995 tempf.flush()
996
997 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
998 self.assertEqual(partlns, [
999 "1:32.5kiB:101408kiB:101376kiB:ext4:primary:;",
1000 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
1001 ])
1002
1003 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1004 # Test that a partition can be placed immediately after a MSDOS partition table
1005 tempf.write("bootloader --ptable msdos\n" \
1006 "part / --source rootfs --ondisk hda --offset 1s --fixed-size 100M --fstype=ext4\n")
1007 tempf.flush()
1008
1009 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1010 self.assertEqual(partlns, [
1011 "1:0.50kiB:102400kiB:102400kiB:ext4::;",
1012 ])
1013
1014 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001015 # Test that image creation fails if the partitions would overlap
1016 tempf.write("bootloader --ptable gpt\n" \
1017 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
1018 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n")
1019 tempf.flush()
1020
1021 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1022 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1023
1024 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1025 # Test that partitions are not allowed to overlap with the booloader
1026 tempf.write("bootloader --ptable gpt\n" \
1027 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n")
1028 tempf.flush()
1029
1030 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1031 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001032
Andrew Geissler5a43b432020-06-13 10:46:56 -05001033 def test_extra_space(self):
1034 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
1035
1036 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1037 tempf.write("bootloader --ptable gpt\n" \
1038 "part / --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n")
1039 tempf.flush()
1040
1041 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1042 self.assertEqual(len(partlns), 1)
1043 size = partlns[0].split(':')[3]
1044 self.assertRegex(size, r'^[0-9]+kiB$')
1045 size = int(size[:-3])
1046 self.assertGreaterEqual(size, 204800)
1047
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001048 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001049 def test_rawcopy_plugin_qemu(self):
1050 """Test rawcopy plugin in qemu"""
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001051 # build ext4 and then use it for a wic image
1052 config = 'IMAGE_FSTYPES = "ext4"\n'
1053 self.append_config(config)
1054 self.assertEqual(0, bitbake('core-image-minimal').status)
1055 self.remove_config(config)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001056
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001057 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n'
1058 self.append_config(config)
1059 self.assertEqual(0, bitbake('core-image-minimal-mtdutils').status)
1060 self.remove_config(config)
1061
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001062 with runqemu('core-image-minimal-mtdutils', ssh=False,
1063 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001064 cmd = "grep sda. /proc/partitions |wc -l"
1065 status, output = qemu.run_serial(cmd)
1066 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1067 self.assertEqual(output, '2')
1068
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001069 def _rawcopy_plugin(self, fstype):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001070 """Test rawcopy plugin"""
1071 img = 'core-image-minimal'
1072 machine = get_bb_var('MACHINE', img)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001073 params = ',unpack' if fstype.endswith('.gz') else ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001074 with NamedTemporaryFile("w", suffix=".wks") as wks:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001075 wks.write('part / --source rawcopy --sourceparams="file=%s-%s.%s%s"\n'\
1076 % (img, machine, fstype, params))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001077 wks.flush()
1078 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001079 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001080 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1081 out = glob(self.resultdir + "%s-*direct" % wksname)
1082 self.assertEqual(1, len(out))
1083
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001084 def test_rawcopy_plugin(self):
1085 self._rawcopy_plugin('ext4')
1086
1087 def test_rawcopy_plugin_unpack(self):
1088 fstype = 'ext4.gz'
1089 config = 'IMAGE_FSTYPES = "%s"\n' % fstype
1090 self.append_config(config)
1091 self.assertEqual(0, bitbake('core-image-minimal').status)
1092 self.remove_config(config)
1093 self._rawcopy_plugin(fstype)
1094
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001095 def test_empty_plugin(self):
1096 """Test empty plugin"""
1097 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n'
1098 self.append_config(config)
1099 self.assertEqual(0, bitbake('core-image-minimal').status)
1100 self.remove_config(config)
1101
1102 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
1103 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
1104 machine = bb_vars['MACHINE']
1105 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1106 self.assertEqual(True, os.path.exists(image_path))
1107
1108 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1109
1110 # Fstype column from 'wic ls' should be empty for the second partition
1111 # as listed in test_empty_plugin.wks
1112 result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot))
1113 self.assertEqual('1', result.output)
1114
Brad Bishop96ff1982019-08-19 13:50:42 -04001115 @only_for_arch(['i586', 'i686', 'x86_64'])
1116 def test_biosplusefi_plugin_qemu(self):
1117 """Test biosplusefi plugin in qemu"""
Patrick Williams213cb262021-08-07 19:21:33 -05001118 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001119 self.append_config(config)
1120 self.assertEqual(0, bitbake('core-image-minimal').status)
1121 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001122
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001123 with runqemu('core-image-minimal', ssh=False,
1124 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishop96ff1982019-08-19 13:50:42 -04001125 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
1126 cmd = "grep sda. /proc/partitions | wc -l"
1127 status, output = qemu.run_serial(cmd)
1128 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1129 self.assertEqual(output, '2')
1130 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
1131 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
1132 status, output = qemu.run_serial(cmd)
1133 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1134 self.assertEqual(output, '2')
1135 # Check that /boot has EFI bootx64.efi (required for EFI)
1136 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1137 status, output = qemu.run_serial(cmd)
1138 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1139 self.assertEqual(output, '1')
1140 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
1141 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
1142 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
1143 status, output = qemu.run_serial(cmd)
1144 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1145 self.assertEqual(output, '*')
1146
1147 @only_for_arch(['i586', 'i686', 'x86_64'])
1148 def test_biosplusefi_plugin(self):
1149 """Test biosplusefi plugin"""
1150 # Wic generation below may fail depending on the order of the unittests
1151 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
1152 # which may or may not exists depending on what was built already
1153 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
1154 # will raise with "Couldn't find correct bootimg_dir"
1155 # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call
Patrick Williams213cb262021-08-07 19:21:33 -05001156 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001157 self.append_config(config)
1158 self.assertEqual(0, bitbake('core-image-minimal').status)
1159 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001160
1161 img = 'core-image-minimal'
1162 with NamedTemporaryFile("w", suffix=".wks") as wks:
1163 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1164 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1165 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1166 wks.flush()
1167 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1168 runCmd(cmd)
1169 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1170 out = glob(self.resultdir + "%s-*.direct" % wksname)
1171 self.assertEqual(1, len(out))
1172
Patrick Williams93c203f2021-10-06 16:15:23 -05001173 @only_for_arch(['i586', 'i686', 'x86_64'])
1174 def test_efi_plugin_unified_kernel_image_qemu(self):
1175 """Test efi plugin's Unified Kernel Image feature in qemu"""
1176 config = 'IMAGE_FSTYPES = "wic"\n'\
1177 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
1178 'WKS_FILE = "test_efi_plugin.wks"\n'\
1179 'MACHINE_FEATURES:append = " efi"\n'
1180 self.append_config(config)
1181 self.assertEqual(0, bitbake('core-image-minimal core-image-minimal-initramfs ovmf').status)
1182 self.remove_config(config)
1183
1184 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001185 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Patrick Williams93c203f2021-10-06 16:15:23 -05001186 # Check that /boot has EFI bootx64.efi (required for EFI)
1187 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1188 status, output = qemu.run_serial(cmd)
1189 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1190 self.assertEqual(output, '1')
1191 # Check that /boot has EFI/Linux/linux.efi (required for Unified Kernel Images auto detection)
1192 cmd = "ls /boot/EFI/Linux/linux.efi | wc -l"
1193 status, output = qemu.run_serial(cmd)
1194 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1195 self.assertEqual(output, '1')
1196 # Check that /boot doesn't have loader/entries/boot.conf (Unified Kernel Images are auto detected by the bootloader)
1197 cmd = "ls /boot/loader/entries/boot.conf 2&>/dev/null | wc -l"
1198 status, output = qemu.run_serial(cmd)
1199 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1200 self.assertEqual(output, '0')
1201
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001202 def test_fs_types(self):
1203 """Test filesystem types for empty and not empty partitions"""
1204 img = 'core-image-minimal'
1205 with NamedTemporaryFile("w", suffix=".wks") as wks:
1206 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
1207 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
1208 'part squash --fstype squashfs --source rootfs\n',
1209 'part swap --fstype swap --size 1M\n',
1210 'part emptyvfat --fstype vfat --size 1M\n',
1211 'part emptymsdos --fstype msdos --size 1M\n',
1212 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001213 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001214 wks.flush()
1215 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001216 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001217 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1218 out = glob(self.resultdir + "%s-*direct" % wksname)
1219 self.assertEqual(1, len(out))
1220
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001221 def test_kickstart_parser(self):
1222 """Test wks parser options"""
1223 with NamedTemporaryFile("w", suffix=".wks") as wks:
1224 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1225 '--overhead-factor 1.2 --size 100k\n'])
1226 wks.flush()
1227 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001228 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001229 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1230 out = glob(self.resultdir + "%s-*direct" % wksname)
1231 self.assertEqual(1, len(out))
1232
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001233 def test_image_bootpart_globbed(self):
1234 """Test globbed sources with image-bootpart plugin"""
1235 img = "core-image-minimal"
1236 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1237 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1238 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001239 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001240 self.remove_config(config)
1241 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
1242
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001243 def test_sparse_copy(self):
1244 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
1245 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic')
1246 sys.path.insert(0, libpath)
1247 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1248 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1249 src_name = sparse.name
1250 src_size = 1024 * 10
1251 sparse.truncate(src_size)
1252 # write one byte to the file
1253 with open(src_name, 'r+b') as sfile:
1254 sfile.seek(1024 * 4)
1255 sfile.write(b'\x00')
1256 dest = sparse.name + '.out'
1257 # copy src file to dest using different filemap APIs
1258 for api in (FilemapFiemap, FilemapSeek, None):
1259 if os.path.exists(dest):
1260 os.unlink(dest)
1261 try:
1262 sparse_copy(sparse.name, dest, api=api)
1263 except ErrorNotSupp:
1264 continue # skip unsupported API
1265 dest_stat = os.stat(dest)
1266 self.assertEqual(dest_stat.st_size, src_size)
1267 # 8 blocks is 4K (physical sector size)
1268 self.assertEqual(dest_stat.st_blocks, 8)
1269 os.unlink(dest)
1270
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001271 def test_wic_ls(self):
1272 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001273 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001274 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001275 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001276 images = glob(self.resultdir + "wictestdisk-*.direct")
1277 self.assertEqual(1, len(images))
1278
1279 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1280
1281 # list partitions
1282 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001283 self.assertEqual(3, len(result.output.split('\n')))
1284
1285 # list directory content of the first partition
1286 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001287 self.assertEqual(6, len(result.output.split('\n')))
1288
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001289 def test_wic_cp(self):
1290 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001291 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001292 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001293 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001294 images = glob(self.resultdir + "wictestdisk-*.direct")
1295 self.assertEqual(1, len(images))
1296
1297 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1298
1299 # list directory content of the first partition
1300 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001301 self.assertEqual(6, len(result.output.split('\n')))
1302
1303 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1304 testfile.write("test")
1305
1306 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001307 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001308
1309 # check if file is there
1310 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001311 self.assertEqual(7, len(result.output.split('\n')))
1312 self.assertTrue(os.path.basename(testfile.name) in result.output)
1313
1314 # prepare directory
1315 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1316 testsubdir = os.path.join(testdir, 'subdir')
1317 os.makedirs(os.path.join(testsubdir))
1318 copy(testfile.name, testdir)
1319
1320 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001321 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001322
1323 # check if directory is there
1324 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001325 self.assertEqual(8, len(result.output.split('\n')))
1326 self.assertTrue(os.path.basename(testdir) in result.output)
1327
Andrew Geissler82c905d2020-04-13 13:39:40 -05001328 # copy the file from the partition and check if it success
1329 dest = '%s-cp' % testfile.name
1330 runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1331 os.path.basename(testfile.name), dest, sysroot))
1332 self.assertTrue(os.path.exists(dest))
1333
1334
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001335 def test_wic_rm(self):
1336 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001337 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001338 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001339 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001340 images = glob(self.resultdir + "mkefidisk-*.direct")
1341 self.assertEqual(1, len(images))
1342
1343 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1344
1345 # list directory content of the first partition
1346 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001347 self.assertIn('\nBZIMAGE ', result.output)
1348 self.assertIn('\nEFI <DIR> ', result.output)
1349
1350 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001351 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001352
1353 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001354 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001355
1356 # check if they're removed
1357 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001358 self.assertNotIn('\nBZIMAGE ', result.output)
1359 self.assertNotIn('\nEFI <DIR> ', result.output)
1360
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001361 def test_mkfs_extraopts(self):
1362 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1363 img = 'core-image-minimal'
1364 with NamedTemporaryFile("w", suffix=".wks") as wks:
1365 wks.writelines(
1366 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1367 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1368 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1369 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1370 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1371 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1372 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
1373 wks.flush()
1374 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001375 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001376 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1377 out = glob(self.resultdir + "%s-*direct" % wksname)
1378 self.assertEqual(1, len(out))
1379
1380 def test_expand_mbr_image(self):
1381 """Test wic write --expand command for mbr image"""
1382 # build an image
1383 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1384 self.append_config(config)
1385 self.assertEqual(0, bitbake('core-image-minimal').status)
1386
1387 # get path to the image
1388 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
1389 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
1390 machine = bb_vars['MACHINE']
1391 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1392
1393 self.remove_config(config)
1394
1395 try:
1396 # expand image to 1G
1397 new_image_path = None
1398 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
1399 dir=deploy_dir, delete=False) as sparse:
1400 sparse.truncate(1024 ** 3)
1401 new_image_path = sparse.name
1402
1403 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1404 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001405 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001406
1407 # check if partitions are expanded
1408 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1409 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1410 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1411 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1412 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
1413 self.assertTrue(orig_sizes[1] < exp_sizes[1])
1414
1415 # Check if all free space is partitioned
1416 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
1417 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
1418
Andrew Geisslerc926e172021-05-07 16:11:35 -05001419 bb.utils.rename(image_path, image_path + '.bak')
1420 bb.utils.rename(new_image_path, image_path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001421
1422 # Check if it boots in qemu
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001423 with runqemu('core-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001424 cmd = "ls /etc/"
1425 status, output = qemu.run_serial('true')
1426 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1427 finally:
1428 if os.path.exists(new_image_path):
1429 os.unlink(new_image_path)
1430 if os.path.exists(image_path + '.bak'):
Andrew Geisslerc926e172021-05-07 16:11:35 -05001431 bb.utils.rename(image_path + '.bak', image_path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001432
1433 def test_wic_ls_ext(self):
1434 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001435 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001436 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001437 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001438 images = glob(self.resultdir + "wictestdisk-*.direct")
1439 self.assertEqual(1, len(images))
1440
1441 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1442
1443 # list directory content of the second ext4 partition
1444 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001445 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
1446 set(line.split()[-1] for line in result.output.split('\n') if line)))
1447
1448 def test_wic_cp_ext(self):
1449 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001450 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001451 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001452 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001453 images = glob(self.resultdir + "wictestdisk-*.direct")
1454 self.assertEqual(1, len(images))
1455
1456 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1457
1458 # list directory content of the ext4 partition
1459 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001460 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1461 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
1462
1463 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1464 testfile.write("test")
1465
1466 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001467 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001468
1469 # check if file is there
1470 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001471 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1472 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1473
Andrew Geissler82c905d2020-04-13 13:39:40 -05001474 # check if the file to copy is in the partition
1475 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1476 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1477
1478 # copy file from the partition, replace the temporary file content with it and
1479 # check for the file size to validate the copy
1480 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
1481 self.assertTrue(os.stat(testfile.name).st_size > 0)
1482
1483
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001484 def test_wic_rm_ext(self):
1485 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001486 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001487 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001488 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001489 images = glob(self.resultdir + "mkefidisk-*.direct")
1490 self.assertEqual(1, len(images))
1491
1492 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1493
1494 # list directory content of the /etc directory on ext4 partition
1495 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001496 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1497
1498 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001499 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001500
1501 # check if it's removed
1502 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001503 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishop6dbb3162019-11-25 09:41:34 -05001504
1505 # remove non-empty directory
1506 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1507
1508 # check if it's removed
1509 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1510 self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line])