blob: 8b58285c3254d356574c632ce3c90803e3b163bb [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
14
15from glob import glob
16from shutil import rmtree, copy
17from functools import wraps, lru_cache
18from tempfile import NamedTemporaryFile
19
20from oeqa.selftest.case import OESelftestTestCase
21from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022
23
24@lru_cache(maxsize=32)
25def get_host_arch(recipe):
26 """A cached call to get_bb_var('HOST_ARCH', <recipe>)"""
27 return get_bb_var('HOST_ARCH', recipe)
28
29
30def only_for_arch(archs, image='core-image-minimal'):
31 """Decorator for wrapping test cases that can be run only for specific target
32 architectures. A list of compatible architectures is passed in `archs`.
33 Current architecture will be determined by parsing bitbake output for
34 `image` recipe.
35 """
36 def wrapper(func):
37 @wraps(func)
38 def wrapped_f(*args, **kwargs):
39 arch = get_host_arch(image)
40 if archs and arch not in archs:
41 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch)
42 return func(*args, **kwargs)
43 wrapped_f.__name__ = func.__name__
44 return wrapped_f
45 return wrapper
46
Andrew Geissler82c905d2020-04-13 13:39:40 -050047def extract_files(debugfs_output):
48 """
49 extract file names from the output of debugfs -R 'ls -p',
50 which looks like this:
51
52 /2/040755/0/0/.//\n
53 /2/040755/0/0/..//\n
54 /11/040700/0/0/lost+found^M//\n
55 /12/040755/1002/1002/run//\n
56 /13/040755/1002/1002/sys//\n
57 /14/040755/1002/1002/bin//\n
58 /80/040755/1002/1002/var//\n
59 /92/040755/1002/1002/tmp//\n
60 """
61 # NOTE the occasional ^M in file names
62 return [line.split('/')[5].strip() for line in \
63 debugfs_output.strip().split('/\n')]
64
65def files_own_by_root(debugfs_output):
66 for line in debugfs_output.strip().split('/\n'):
67 if line.split('/')[3:5] != ['0', '0']:
68 print(debugfs_output)
69 return False
70 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080072class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073 """Wic test class."""
74
Brad Bishopd7bf8c12018-02-25 22:55:05 -050075 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 wicenv_cache = {}
77
78 def setUpLocal(self):
79 """This code is executed before each test method."""
Brad Bishopc4ea0752018-11-15 14:30:15 -080080 self.resultdir = self.builddir + "/wic-tmp/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080081 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082
83 # Do this here instead of in setUpClass as the base setUp does some
84 # clean up which can result in the native tools built earlier in
85 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080086 if not WicTestCase.image_is_ready:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087 if get_bb_var('USE_NLS') == 'yes':
88 bitbake('wic-tools')
89 else:
90 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable')
91
92 bitbake('core-image-minimal')
Andrew Geissler82c905d2020-04-13 13:39:40 -050093 bitbake('core-image-minimal-mtdutils')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080094 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095
96 rmtree(self.resultdir, ignore_errors=True)
97
98 def tearDownLocal(self):
99 """Remove resultdir as it may contain images."""
100 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800101 super(WicTestCase, self).tearDownLocal()
102
103 def _get_image_env_path(self, image):
104 """Generate and obtain the path to <image>.env"""
105 if image not in WicTestCase.wicenv_cache:
106 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status)
107 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image)
108 stdir = bb_vars['STAGING_DIR']
109 machine = bb_vars['MACHINE']
110 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
111 return WicTestCase.wicenv_cache[image]
112
113class Wic(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 def test_version(self):
116 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800117 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 def test_help(self):
120 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800121 runCmd('wic --help')
122 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124 def test_createhelp(self):
125 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800126 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128 def test_listhelp(self):
129 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800130 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 def test_help_create(self):
133 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800134 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136 def test_help_list(self):
137 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 def test_help_overview(self):
141 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800142 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144 def test_help_plugins(self):
145 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800146 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500147
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148 def test_help_kickstart(self):
149 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800150 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500152 def test_list_images(self):
153 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800154 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500155
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156 def test_list_source_plugins(self):
157 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800158 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500159
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500160 def test_listed_images_help(self):
161 """Test wic listed images help"""
162 output = runCmd('wic list images').output
163 imagelist = [line.split()[0] for line in output.splitlines()]
164 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800165 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500166
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500167 def test_unsupported_subcommand(self):
168 """Test unsupported subcommand"""
169 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
170
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500171 def test_no_command(self):
172 """Test wic without command"""
173 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
174
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500175 def test_build_image_name(self):
176 """Test wic create wictestdisk --image-name=core-image-minimal"""
177 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800178 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
180
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500181 @only_for_arch(['i586', 'i686', 'x86_64'])
182 def test_gpt_image(self):
183 """Test creation of core-image-minimal with gpt table and UUID boot"""
184 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800185 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500186 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
187
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500188 @only_for_arch(['i586', 'i686', 'x86_64'])
189 def test_iso_image(self):
190 """Test creation of hybrid iso image with legacy and EFI boot"""
191 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
192 'MACHINE_FEATURES_append = " efi"\n'\
193 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
194 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800195 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500196 self.remove_config(config)
197 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800198 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500199 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct")))
200 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso")))
201
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 @only_for_arch(['i586', 'i686', 'x86_64'])
203 def test_qemux86_directdisk(self):
204 """Test creation of qemux-86-directdisk image"""
205 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800206 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500207 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct")))
208
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209 @only_for_arch(['i586', 'i686', 'x86_64'])
210 def test_mkefidisk(self):
211 """Test creation of mkefidisk image"""
212 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800213 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500214 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct")))
215
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500216 @only_for_arch(['i586', 'i686', 'x86_64'])
217 def test_bootloader_config(self):
218 """Test creation of directdisk-bootloader-config image"""
219 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
220 self.append_config(config)
221 bitbake('core-image-minimal')
222 self.remove_config(config)
223 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800224 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500225 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct")))
226
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500227 @only_for_arch(['i586', 'i686', 'x86_64'])
228 def test_systemd_bootdisk(self):
229 """Test creation of systemd-bootdisk image"""
230 config = 'MACHINE_FEATURES_append = " efi"\n'
231 self.append_config(config)
232 bitbake('core-image-minimal')
233 self.remove_config(config)
234 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800235 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500236 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct")))
237
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500238 def test_sdimage_bootpart(self):
239 """Test creation of sdimage-bootpart image"""
240 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
241 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
242 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800243 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500244 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
245
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500246 @only_for_arch(['i586', 'i686', 'x86_64'])
247 def test_default_output_dir(self):
248 """Test default output location"""
249 for fname in glob("directdisk-*.direct"):
250 os.remove(fname)
251 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n'
252 self.append_config(config)
253 bitbake('core-image-minimal')
254 self.remove_config(config)
255 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800256 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500257 self.assertEqual(1, len(glob("directdisk-*.direct")))
258
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259 @only_for_arch(['i586', 'i686', 'x86_64'])
260 def test_build_artifacts(self):
261 """Test wic create directdisk providing all artifacts."""
262 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
263 'wic-tools')
264 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
265 'core-image-minimal'))
266 bbvars = {key.lower(): value for key, value in bb_vars.items()}
267 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800268 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269 "-b %(staging_datadir)s "
270 "-k %(deploy_dir_image)s "
271 "-n %(recipe_sysroot_native)s "
272 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800273 "-o %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500274 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct")))
275
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500276 def test_compress_gzip(self):
277 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800278 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800280 "-c gzip -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz")))
282
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500283 def test_compress_bzip2(self):
284 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800285 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800287 "-c bzip2 -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500288 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2")))
289
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290 def test_compress_xz(self):
291 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800292 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500293 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800294 "--compress-with=xz -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz")))
296
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500297 def test_wrong_compressor(self):
298 """Test how wic breaks if wrong compressor is provided"""
299 self.assertEqual(2, runCmd("wic create wictestdisk "
300 "--image-name=core-image-minimal "
301 "-c wrong -o %s" % self.resultdir,
302 ignore_status=True).status)
303
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500304 def test_debug_short(self):
305 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800306 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500307 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800308 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500309 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
310
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500311 def test_debug_long(self):
312 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800313 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500314 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800315 "--debug -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500316 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
317
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500318 def test_skip_build_check_short(self):
319 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800320 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500321 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800322 "-s -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
324
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500325 def test_skip_build_check_long(self):
326 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800327 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500328 "--image-name=core-image-minimal "
329 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800330 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500331 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
332
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500333 def test_build_rootfs_short(self):
334 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800335 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500336 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800337 "-f -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500338 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
339
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500340 def test_build_rootfs_long(self):
341 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800342 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500343 "--image-name=core-image-minimal "
344 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800345 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500346 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct")))
347
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500348 @only_for_arch(['i586', 'i686', 'x86_64'])
349 def test_rootfs_indirect_recipes(self):
350 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800351 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500352 "--image-name=core-image-minimal "
353 "--rootfs rootfs1=core-image-minimal "
354 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800355 "--outdir %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500356 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct")))
357
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500358 @only_for_arch(['i586', 'i686', 'x86_64'])
359 def test_rootfs_artifacts(self):
360 """Test usage of rootfs plugin with rootfs paths"""
361 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
362 'wic-tools')
363 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
364 'core-image-minimal'))
365 bbvars = {key.lower(): value for key, value in bb_vars.items()}
366 bbvars['wks'] = "directdisk-multi-rootfs"
367 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800368 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500369 "--bootimg-dir=%(staging_datadir)s "
370 "--kernel-dir=%(deploy_dir_image)s "
371 "--native-sysroot=%(recipe_sysroot_native)s "
372 "--rootfs-dir rootfs1=%(image_rootfs)s "
373 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800374 "--outdir %(resultdir)s" % bbvars)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500375 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars)))
376
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500377 def test_exclude_path(self):
378 """Test --exclude-path wks option."""
379
380 oldpath = os.environ['PATH']
381 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
382
383 try:
384 wks_file = 'temp.wks'
385 with open(wks_file, 'w') as wks:
386 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
387 wks.write("""
388part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
389part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
390part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
391 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800392 runCmd("wic create %s -e core-image-minimal -o %s" \
393 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500394
395 os.remove(wks_file)
396 wicout = glob(self.resultdir + "%s-*direct" % 'temp')
397 self.assertEqual(1, len(wicout))
398
399 wicimg = wicout[0]
400
401 # verify partition size with wic
402 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500403
404 # parse parted output which looks like this:
405 # BYT;\n
406 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
407 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
408 partlns = res.output.splitlines()[2:]
409
410 self.assertEqual(3, len(partlns))
411
412 for part in [1, 2, 3]:
413 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
414 partln = partlns[part-1].split(":")
415 self.assertEqual(7, len(partln))
416 start = int(partln[1].rstrip("B")) / 512
417 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800418 runCmd("dd if=%s of=%s skip=%d count=%d" %
419 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500420
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500421 # Test partition 1, should contain the normal root directories, except
422 # /usr.
423 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
424 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500425 files = extract_files(res.output)
426 self.assertIn("etc", files)
427 self.assertNotIn("usr", files)
428
429 # Partition 2, should contain common directories for /usr, not root
430 # directories.
431 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
432 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500433 files = extract_files(res.output)
434 self.assertNotIn("etc", files)
435 self.assertNotIn("usr", files)
436 self.assertIn("share", files)
437
438 # Partition 3, should contain the same as partition 2, including the bin
439 # directory, but not the files inside it.
440 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
441 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500442 files = extract_files(res.output)
443 self.assertNotIn("etc", files)
444 self.assertNotIn("usr", files)
445 self.assertIn("share", files)
446 self.assertIn("bin", files)
447 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
448 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500449 files = extract_files(res.output)
450 self.assertIn(".", files)
451 self.assertIn("..", files)
452 self.assertEqual(2, len(files))
453
454 for part in [1, 2, 3]:
455 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
456 os.remove(part_file)
457
458 finally:
459 os.environ['PATH'] = oldpath
460
Andrew Geissler82c905d2020-04-13 13:39:40 -0500461 def test_include_path(self):
462 """Test --include-path wks option."""
463
464 oldpath = os.environ['PATH']
465 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
466
467 try:
468 include_path = os.path.join(self.resultdir, 'test-include')
469 os.makedirs(include_path)
470 with open(os.path.join(include_path, 'test-file'), 'w') as t:
471 t.write("test\n")
472 wks_file = os.path.join(include_path, 'temp.wks')
473 with open(wks_file, 'w') as wks:
474 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
475 wks.write("""
476part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
477part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
478 % (include_path))
479 runCmd("wic create %s -e core-image-minimal -o %s" \
480 % (wks_file, self.resultdir))
481
482 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
483 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
484
485 # Test partition 1, should not contain 'test-file'
486 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
487 files = extract_files(res.output)
488 self.assertNotIn('test-file', files)
489 self.assertEqual(True, files_own_by_root(res.output))
490
491 # Test partition 2, should contain 'test-file'
492 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
493 files = extract_files(res.output)
494 self.assertIn('test-file', files)
495 self.assertEqual(True, files_own_by_root(res.output))
496
497 finally:
498 os.environ['PATH'] = oldpath
499
500 def test_include_path_embeded(self):
501 """Test --include-path wks option."""
502
503 oldpath = os.environ['PATH']
504 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
505
506 try:
507 include_path = os.path.join(self.resultdir, 'test-include')
508 os.makedirs(include_path)
509 with open(os.path.join(include_path, 'test-file'), 'w') as t:
510 t.write("test\n")
511 wks_file = os.path.join(include_path, 'temp.wks')
512 with open(wks_file, 'w') as wks:
513 wks.write("""
514part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
515 % (include_path))
516 runCmd("wic create %s -e core-image-minimal -o %s" \
517 % (wks_file, self.resultdir))
518
519 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
520
521 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
522 files = extract_files(res.output)
523 self.assertIn('test-file', files)
524 self.assertEqual(True, files_own_by_root(res.output))
525
526 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
527 files = extract_files(res.output)
528 self.assertIn('passwd', files)
529 self.assertEqual(True, files_own_by_root(res.output))
530
531 finally:
532 os.environ['PATH'] = oldpath
533
534 def test_include_path_errors(self):
535 """Test --include-path wks option error handling."""
536 wks_file = 'temp.wks'
537
538 # Absolute argument.
539 with open(wks_file, 'w') as wks:
540 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
541 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
542 % (wks_file, self.resultdir), ignore_status=True).status)
543 os.remove(wks_file)
544
545 # Argument pointing to parent directory.
546 with open(wks_file, 'w') as wks:
547 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
548 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
549 % (wks_file, self.resultdir), ignore_status=True).status)
550 os.remove(wks_file)
551
552 # 3 Argument pointing to parent directory.
553 with open(wks_file, 'w') as wks:
554 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
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
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500559 def test_exclude_path_errors(self):
560 """Test --exclude-path wks option error handling."""
561 wks_file = 'temp.wks'
562
563 # Absolute argument.
564 with open(wks_file, 'w') as wks:
565 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
566 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
567 % (wks_file, self.resultdir), ignore_status=True).status)
568 os.remove(wks_file)
569
570 # Argument pointing to parent directory.
571 with open(wks_file, 'w') as wks:
572 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
573 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
574 % (wks_file, self.resultdir), ignore_status=True).status)
575 os.remove(wks_file)
576
Andrew Geissler82c905d2020-04-13 13:39:40 -0500577 def test_permissions(self):
578 """Test permissions are respected"""
579
580 oldpath = os.environ['PATH']
581 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
582
583 t_normal = """
584part / --source rootfs --fstype=ext4
585"""
586 t_exclude = """
587part / --source rootfs --fstype=ext4 --exclude-path=home
588"""
589 t_multi = """
590part / --source rootfs --ondisk sda --fstype=ext4
591part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
592"""
593 t_change = """
594part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
595part /etc --source rootfs --fstype=ext4 --change-directory=etc
596"""
597 tests = [t_normal, t_exclude, t_multi, t_change]
598
599 try:
600 for test in tests:
601 include_path = os.path.join(self.resultdir, 'test-include')
602 os.makedirs(include_path)
603 wks_file = os.path.join(include_path, 'temp.wks')
604 with open(wks_file, 'w') as wks:
605 wks.write(test)
606 runCmd("wic create %s -e core-image-minimal -o %s" \
607 % (wks_file, self.resultdir))
608
609 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
610 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
611 self.assertEqual(True, files_own_by_root(res.output))
612
613 rmtree(self.resultdir, ignore_errors=True)
614
615 finally:
616 os.environ['PATH'] = oldpath
617
618 def test_change_directory(self):
619 """Test --change-directory wks option."""
620
621 oldpath = os.environ['PATH']
622 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
623
624 try:
625 include_path = os.path.join(self.resultdir, 'test-include')
626 os.makedirs(include_path)
627 wks_file = os.path.join(include_path, 'temp.wks')
628 with open(wks_file, 'w') as wks:
629 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
630 runCmd("wic create %s -e core-image-minimal -o %s" \
631 % (wks_file, self.resultdir))
632
633 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
634
635 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
636 files = extract_files(res.output)
637 self.assertIn('passwd', files)
638
639 finally:
640 os.environ['PATH'] = oldpath
641
642 def test_change_directory_errors(self):
643 """Test --change-directory wks option error handling."""
644 wks_file = 'temp.wks'
645
646 # Absolute argument.
647 with open(wks_file, 'w') as wks:
648 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
649 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
650 % (wks_file, self.resultdir), ignore_status=True).status)
651 os.remove(wks_file)
652
653 # Argument pointing to parent directory.
654 with open(wks_file, 'w') as wks:
655 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
656 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
657 % (wks_file, self.resultdir), ignore_status=True).status)
658 os.remove(wks_file)
659
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800660class Wic2(WicTestCase):
661
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500662 def test_bmap_short(self):
663 """Test generation of .bmap file -m option"""
664 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800665 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500666 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
667 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
668
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500669 def test_bmap_long(self):
670 """Test generation of .bmap file --bmap option"""
671 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800672 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500673 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
674 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap")))
675
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500676 def test_image_env(self):
677 """Test generation of <image>.env files."""
678 image = 'core-image-minimal'
679 imgdatadir = self._get_image_env_path(image)
680
681 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
682 basename = bb_vars['IMAGE_BASENAME']
683 self.assertEqual(basename, image)
684 path = os.path.join(imgdatadir, basename) + '.env'
685 self.assertTrue(os.path.isfile(path))
686
687 wicvars = set(bb_vars['WICVARS'].split())
688 # filter out optional variables
689 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400690 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
Andrew Geissler82c905d2020-04-13 13:39:40 -0500691 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
692 'APPEND'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500693 with open(path) as envfile:
694 content = dict(line.split("=", 1) for line in envfile)
695 # test if variables used by wic present in the .env file
696 for var in wicvars:
697 self.assertTrue(var in content, "%s is not in .env file" % var)
698 self.assertTrue(content[var])
699
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500700 def test_image_vars_dir_short(self):
701 """Test image vars directory selection -v option"""
702 image = 'core-image-minimal'
703 imgenvdir = self._get_image_env_path(image)
704 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
705
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800706 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500707 "--image-name=%s -v %s -n %s -o %s"
708 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800709 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500710 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
711
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500712 def test_image_vars_dir_long(self):
713 """Test image vars directory selection --vars option"""
714 image = 'core-image-minimal'
715 imgenvdir = self._get_image_env_path(image)
716 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
717
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800718 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500719 "--image-name=%s "
720 "--vars %s "
721 "--native-sysroot %s "
722 "--outdir %s"
723 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800724 self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500725 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct")))
726
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500727 @only_for_arch(['i586', 'i686', 'x86_64'])
728 def test_wic_image_type(self):
729 """Test building wic images by bitbake"""
730 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
731 'MACHINE_FEATURES_append = " efi"\n'
732 self.append_config(config)
733 self.assertEqual(0, bitbake('wic-image-minimal').status)
734 self.remove_config(config)
735
736 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
737 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
738 machine = bb_vars['MACHINE']
739 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine)
740 # check if we have result image and manifests symlinks
741 # pointing to existing files
742 for suffix in ('wic', 'manifest'):
743 path = prefix + suffix
744 self.assertTrue(os.path.islink(path))
745 self.assertTrue(os.path.isfile(os.path.realpath(path)))
746
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500747 @only_for_arch(['i586', 'i686', 'x86_64'])
748 def test_qemu(self):
749 """Test wic-image-minimal under qemu"""
750 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
751 'MACHINE_FEATURES_append = " efi"\n'
752 self.append_config(config)
753 self.assertEqual(0, bitbake('wic-image-minimal').status)
754 self.remove_config(config)
755
756 with runqemu('wic-image-minimal', ssh=False) as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600757 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
758 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400759 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600760 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
761 self.assertEqual(output, '4')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400762 cmd = "grep UUID= /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500763 status, output = qemu.run_serial(cmd)
764 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400765 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500766
767 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500768 def test_qemu_efi(self):
769 """Test core-image-minimal efi image under qemu"""
770 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
771 self.append_config(config)
772 self.assertEqual(0, bitbake('core-image-minimal ovmf').status)
773 self.remove_config(config)
774
775 with runqemu('core-image-minimal', ssh=False,
776 runqemuparams='ovmf', image_fstype='wic') as qemu:
777 cmd = "grep sda. /proc/partitions |wc -l"
778 status, output = qemu.run_serial(cmd)
779 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
780 self.assertEqual(output, '3')
781
782 @staticmethod
783 def _make_fixed_size_wks(size):
784 """
785 Create a wks of an image with a single partition. Size of the partition is set
786 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
787 """
788 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
789 wkspath = tempf.name
790 tempf.write("part " \
791 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
792 "--fstype=ext4\n" % size)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500793
794 return wkspath
795
796 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False):
797 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir),
798 ignore_status=ignore_status)
799
800 if p.status:
801 return (p, None)
802
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500803 wksname = os.path.splitext(os.path.basename(wkspath))[0]
804
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500805 wicout = glob(self.resultdir + "%s-*direct" % wksname)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500806
807 if not wicout:
808 return (p, None)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500809
810 wicimg = wicout[0]
811
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500812 if not native_sysroot:
813 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800814
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500815 # verify partition size with wic
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500816 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800817 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500818
819 # parse parted output which looks like this:
820 # BYT;\n
821 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
822 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500823 return (p, res.output.splitlines()[2:])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500824
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500825 def test_fixed_size(self):
826 """
827 Test creation of a simple image with partition size controlled through
828 --fixed-size flag
829 """
830 wkspath = Wic2._make_fixed_size_wks(200)
831 _, partlns = self._get_wic_partitions(wkspath)
832 os.remove(wkspath)
833
834 self.assertEqual(partlns, [
835 "1:4.00kiB:204804kiB:204800kiB:ext4::;",
836 ])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500837
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500838 def test_fixed_size_error(self):
839 """
840 Test creation of a simple image with partition size controlled through
841 --fixed-size flag. The size of partition is intentionally set to 1MiB
842 in order to trigger an error in wic.
843 """
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500844 wkspath = Wic2._make_fixed_size_wks(1)
845 p, _ = self._get_wic_partitions(wkspath, ignore_status=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500846 os.remove(wkspath)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500847
848 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
849
850 def test_offset(self):
851 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
852
853 with NamedTemporaryFile("w", suffix=".wks") as tempf:
854 # Test that partitions are placed at the correct offsets, default KB
855 tempf.write("bootloader --ptable gpt\n" \
856 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
857 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
858 tempf.flush()
859
860 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
861 self.assertEqual(partlns, [
862 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
863 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
864 ])
865
866 with NamedTemporaryFile("w", suffix=".wks") as tempf:
867 # Test that partitions are placed at the correct offsets, same with explicit KB
868 tempf.write("bootloader --ptable gpt\n" \
869 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
870 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n")
871 tempf.flush()
872
873 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
874 self.assertEqual(partlns, [
875 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
876 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
877 ])
878
879 with NamedTemporaryFile("w", suffix=".wks") as tempf:
880 # Test that partitions are placed at the correct offsets using MB
881 tempf.write("bootloader --ptable gpt\n" \
882 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
883 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n")
884 tempf.flush()
885
886 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
887 self.assertEqual(partlns, [
888 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
889 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;",
890 ])
891
892 with NamedTemporaryFile("w", suffix=".wks") as tempf:
893 # Test that image creation fails if the partitions would overlap
894 tempf.write("bootloader --ptable gpt\n" \
895 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
896 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n")
897 tempf.flush()
898
899 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
900 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
901
902 with NamedTemporaryFile("w", suffix=".wks") as tempf:
903 # Test that partitions are not allowed to overlap with the booloader
904 tempf.write("bootloader --ptable gpt\n" \
905 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n")
906 tempf.flush()
907
908 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
909 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500910
Andrew Geissler5a43b432020-06-13 10:46:56 -0500911 def test_extra_space(self):
912 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
913
914 with NamedTemporaryFile("w", suffix=".wks") as tempf:
915 tempf.write("bootloader --ptable gpt\n" \
916 "part / --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n")
917 tempf.flush()
918
919 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
920 self.assertEqual(len(partlns), 1)
921 size = partlns[0].split(':')[3]
922 self.assertRegex(size, r'^[0-9]+kiB$')
923 size = int(size[:-3])
924 self.assertGreaterEqual(size, 204800)
925
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500926 @only_for_arch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500927 def test_rawcopy_plugin_qemu(self):
928 """Test rawcopy plugin in qemu"""
929 # build ext4 and wic images
930 for fstype in ("ext4", "wic"):
931 config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype
932 self.append_config(config)
933 self.assertEqual(0, bitbake('core-image-minimal').status)
934 self.remove_config(config)
935
936 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
937 cmd = "grep sda. /proc/partitions |wc -l"
938 status, output = qemu.run_serial(cmd)
939 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
940 self.assertEqual(output, '2')
941
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500942 def test_rawcopy_plugin(self):
943 """Test rawcopy plugin"""
944 img = 'core-image-minimal'
945 machine = get_bb_var('MACHINE', img)
946 with NamedTemporaryFile("w", suffix=".wks") as wks:
947 wks.writelines(['part /boot --active --source bootimg-pcbios\n',
948 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\
949 % (img, machine),
950 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
951 wks.flush()
952 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800953 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500954 wksname = os.path.splitext(os.path.basename(wks.name))[0]
955 out = glob(self.resultdir + "%s-*direct" % wksname)
956 self.assertEqual(1, len(out))
957
Brad Bishop96ff1982019-08-19 13:50:42 -0400958 @only_for_arch(['i586', 'i686', 'x86_64'])
959 def test_biosplusefi_plugin_qemu(self):
960 """Test biosplusefi plugin in qemu"""
Brad Bishop08902b02019-08-20 09:16:51 -0400961 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n'
962 self.append_config(config)
963 self.assertEqual(0, bitbake('core-image-minimal').status)
964 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -0400965
966 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu:
967 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
968 cmd = "grep sda. /proc/partitions | wc -l"
969 status, output = qemu.run_serial(cmd)
970 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
971 self.assertEqual(output, '2')
972 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
973 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
974 status, output = qemu.run_serial(cmd)
975 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
976 self.assertEqual(output, '2')
977 # Check that /boot has EFI bootx64.efi (required for EFI)
978 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
979 status, output = qemu.run_serial(cmd)
980 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
981 self.assertEqual(output, '1')
982 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
983 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
984 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
985 status, output = qemu.run_serial(cmd)
986 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
987 self.assertEqual(output, '*')
988
989 @only_for_arch(['i586', 'i686', 'x86_64'])
990 def test_biosplusefi_plugin(self):
991 """Test biosplusefi plugin"""
992 # Wic generation below may fail depending on the order of the unittests
993 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
994 # which may or may not exists depending on what was built already
995 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
996 # will raise with "Couldn't find correct bootimg_dir"
997 # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call
Brad Bishop08902b02019-08-20 09:16:51 -0400998 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n'
999 self.append_config(config)
1000 self.assertEqual(0, bitbake('core-image-minimal').status)
1001 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001002
1003 img = 'core-image-minimal'
1004 with NamedTemporaryFile("w", suffix=".wks") as wks:
1005 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1006 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1007 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1008 wks.flush()
1009 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1010 runCmd(cmd)
1011 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1012 out = glob(self.resultdir + "%s-*.direct" % wksname)
1013 self.assertEqual(1, len(out))
1014
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001015 def test_fs_types(self):
1016 """Test filesystem types for empty and not empty partitions"""
1017 img = 'core-image-minimal'
1018 with NamedTemporaryFile("w", suffix=".wks") as wks:
1019 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
1020 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
1021 'part squash --fstype squashfs --source rootfs\n',
1022 'part swap --fstype swap --size 1M\n',
1023 'part emptyvfat --fstype vfat --size 1M\n',
1024 'part emptymsdos --fstype msdos --size 1M\n',
1025 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001026 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001027 wks.flush()
1028 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001029 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001030 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1031 out = glob(self.resultdir + "%s-*direct" % wksname)
1032 self.assertEqual(1, len(out))
1033
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001034 def test_kickstart_parser(self):
1035 """Test wks parser options"""
1036 with NamedTemporaryFile("w", suffix=".wks") as wks:
1037 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1038 '--overhead-factor 1.2 --size 100k\n'])
1039 wks.flush()
1040 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001041 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001042 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1043 out = glob(self.resultdir + "%s-*direct" % wksname)
1044 self.assertEqual(1, len(out))
1045
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001046 def test_image_bootpart_globbed(self):
1047 """Test globbed sources with image-bootpart plugin"""
1048 img = "core-image-minimal"
1049 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1050 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1051 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001052 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001053 self.remove_config(config)
1054 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct")))
1055
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001056 def test_sparse_copy(self):
1057 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
1058 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic')
1059 sys.path.insert(0, libpath)
1060 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1061 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1062 src_name = sparse.name
1063 src_size = 1024 * 10
1064 sparse.truncate(src_size)
1065 # write one byte to the file
1066 with open(src_name, 'r+b') as sfile:
1067 sfile.seek(1024 * 4)
1068 sfile.write(b'\x00')
1069 dest = sparse.name + '.out'
1070 # copy src file to dest using different filemap APIs
1071 for api in (FilemapFiemap, FilemapSeek, None):
1072 if os.path.exists(dest):
1073 os.unlink(dest)
1074 try:
1075 sparse_copy(sparse.name, dest, api=api)
1076 except ErrorNotSupp:
1077 continue # skip unsupported API
1078 dest_stat = os.stat(dest)
1079 self.assertEqual(dest_stat.st_size, src_size)
1080 # 8 blocks is 4K (physical sector size)
1081 self.assertEqual(dest_stat.st_blocks, 8)
1082 os.unlink(dest)
1083
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001084 def test_wic_ls(self):
1085 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001086 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001087 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001088 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001089 images = glob(self.resultdir + "wictestdisk-*.direct")
1090 self.assertEqual(1, len(images))
1091
1092 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1093
1094 # list partitions
1095 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001096 self.assertEqual(3, len(result.output.split('\n')))
1097
1098 # list directory content of the first partition
1099 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001100 self.assertEqual(6, len(result.output.split('\n')))
1101
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001102 def test_wic_cp(self):
1103 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001104 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001105 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001106 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001107 images = glob(self.resultdir + "wictestdisk-*.direct")
1108 self.assertEqual(1, len(images))
1109
1110 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1111
1112 # list directory content of the first partition
1113 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001114 self.assertEqual(6, len(result.output.split('\n')))
1115
1116 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1117 testfile.write("test")
1118
1119 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001120 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001121
1122 # check if file is there
1123 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001124 self.assertEqual(7, len(result.output.split('\n')))
1125 self.assertTrue(os.path.basename(testfile.name) in result.output)
1126
1127 # prepare directory
1128 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1129 testsubdir = os.path.join(testdir, 'subdir')
1130 os.makedirs(os.path.join(testsubdir))
1131 copy(testfile.name, testdir)
1132
1133 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001134 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001135
1136 # check if directory is there
1137 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001138 self.assertEqual(8, len(result.output.split('\n')))
1139 self.assertTrue(os.path.basename(testdir) in result.output)
1140
Andrew Geissler82c905d2020-04-13 13:39:40 -05001141 # copy the file from the partition and check if it success
1142 dest = '%s-cp' % testfile.name
1143 runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1144 os.path.basename(testfile.name), dest, sysroot))
1145 self.assertTrue(os.path.exists(dest))
1146
1147
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001148 def test_wic_rm(self):
1149 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001150 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001151 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001152 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001153 images = glob(self.resultdir + "mkefidisk-*.direct")
1154 self.assertEqual(1, len(images))
1155
1156 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1157
1158 # list directory content of the first partition
1159 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001160 self.assertIn('\nBZIMAGE ', result.output)
1161 self.assertIn('\nEFI <DIR> ', result.output)
1162
1163 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001164 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001165
1166 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001167 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001168
1169 # check if they're removed
1170 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001171 self.assertNotIn('\nBZIMAGE ', result.output)
1172 self.assertNotIn('\nEFI <DIR> ', result.output)
1173
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001174 def test_mkfs_extraopts(self):
1175 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1176 img = 'core-image-minimal'
1177 with NamedTemporaryFile("w", suffix=".wks") as wks:
1178 wks.writelines(
1179 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1180 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1181 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1182 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1183 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1184 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1185 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
1186 wks.flush()
1187 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001188 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001189 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1190 out = glob(self.resultdir + "%s-*direct" % wksname)
1191 self.assertEqual(1, len(out))
1192
1193 def test_expand_mbr_image(self):
1194 """Test wic write --expand command for mbr image"""
1195 # build an image
1196 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1197 self.append_config(config)
1198 self.assertEqual(0, bitbake('core-image-minimal').status)
1199
1200 # get path to the image
1201 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE'])
1202 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE']
1203 machine = bb_vars['MACHINE']
1204 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine)
1205
1206 self.remove_config(config)
1207
1208 try:
1209 # expand image to 1G
1210 new_image_path = None
1211 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
1212 dir=deploy_dir, delete=False) as sparse:
1213 sparse.truncate(1024 ** 3)
1214 new_image_path = sparse.name
1215
1216 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1217 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001218 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001219
1220 # check if partitions are expanded
1221 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1222 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1223 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1224 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1225 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
1226 self.assertTrue(orig_sizes[1] < exp_sizes[1])
1227
1228 # Check if all free space is partitioned
1229 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
1230 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
1231
1232 os.rename(image_path, image_path + '.bak')
1233 os.rename(new_image_path, image_path)
1234
1235 # Check if it boots in qemu
1236 with runqemu('core-image-minimal', ssh=False) as qemu:
1237 cmd = "ls /etc/"
1238 status, output = qemu.run_serial('true')
1239 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1240 finally:
1241 if os.path.exists(new_image_path):
1242 os.unlink(new_image_path)
1243 if os.path.exists(image_path + '.bak'):
1244 os.rename(image_path + '.bak', image_path)
1245
1246 def test_wic_ls_ext(self):
1247 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001248 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001249 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001250 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001251 images = glob(self.resultdir + "wictestdisk-*.direct")
1252 self.assertEqual(1, len(images))
1253
1254 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1255
1256 # list directory content of the second ext4 partition
1257 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001258 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
1259 set(line.split()[-1] for line in result.output.split('\n') if line)))
1260
1261 def test_wic_cp_ext(self):
1262 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001263 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001264 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001265 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001266 images = glob(self.resultdir + "wictestdisk-*.direct")
1267 self.assertEqual(1, len(images))
1268
1269 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1270
1271 # list directory content of the ext4 partition
1272 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001273 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1274 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs))
1275
1276 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1277 testfile.write("test")
1278
1279 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001280 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001281
1282 # check if file is there
1283 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001284 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1285 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1286
Andrew Geissler82c905d2020-04-13 13:39:40 -05001287 # check if the file to copy is in the partition
1288 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
1289 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1290
1291 # copy file from the partition, replace the temporary file content with it and
1292 # check for the file size to validate the copy
1293 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
1294 self.assertTrue(os.stat(testfile.name).st_size > 0)
1295
1296
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001297 def test_wic_rm_ext(self):
1298 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001299 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001300 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001301 "-D -o %s" % self.resultdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001302 images = glob(self.resultdir + "mkefidisk-*.direct")
1303 self.assertEqual(1, len(images))
1304
1305 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1306
1307 # list directory content of the /etc directory on ext4 partition
1308 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001309 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line])
1310
1311 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001312 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001313
1314 # check if it's removed
1315 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001316 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishop6dbb3162019-11-25 09:41:34 -05001317
1318 # remove non-empty directory
1319 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1320
1321 # check if it's removed
1322 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
1323 self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line])