blob: 4ebcb76cc45c845974b3b7e726ad5256e3f4816c [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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018from tempfile import NamedTemporaryFile
Patrick Williamsda295312023-12-05 16:48:56 -060019from tempfile import TemporaryDirectory
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020
21from oeqa.selftest.case import OESelftestTestCase
Patrick Williams45852732022-04-02 08:58:32 -050022from oeqa.core.decorator import OETestTag
Patrick Williams7784c422022-11-17 07:29:11 -060023from oeqa.core.decorator.data import skipIfNotArch
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu
Brad Bishopd7bf8c12018-02-25 22:55:05 -050025
26
Andrew Geissler82c905d2020-04-13 13:39:40 -050027def extract_files(debugfs_output):
28 """
29 extract file names from the output of debugfs -R 'ls -p',
30 which looks like this:
31
32 /2/040755/0/0/.//\n
33 /2/040755/0/0/..//\n
34 /11/040700/0/0/lost+found^M//\n
35 /12/040755/1002/1002/run//\n
36 /13/040755/1002/1002/sys//\n
37 /14/040755/1002/1002/bin//\n
38 /80/040755/1002/1002/var//\n
39 /92/040755/1002/1002/tmp//\n
40 """
41 # NOTE the occasional ^M in file names
42 return [line.split('/')[5].strip() for line in \
43 debugfs_output.strip().split('/\n')]
44
45def files_own_by_root(debugfs_output):
46 for line in debugfs_output.strip().split('/\n'):
47 if line.split('/')[3:5] != ['0', '0']:
48 print(debugfs_output)
49 return False
50 return True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080052class WicTestCase(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053 """Wic test class."""
54
Brad Bishopd7bf8c12018-02-25 22:55:05 -050055 image_is_ready = False
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056 wicenv_cache = {}
57
58 def setUpLocal(self):
59 """This code is executed before each test method."""
Patrick Williams45852732022-04-02 08:58:32 -050060 self.resultdir = os.path.join(self.builddir, "wic-tmp")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080061 super(WicTestCase, self).setUpLocal()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062
63 # Do this here instead of in setUpClass as the base setUp does some
64 # clean up which can result in the native tools built earlier in
65 # setUpClass being unavailable.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080066 if not WicTestCase.image_is_ready:
Patrick Williams45852732022-04-02 08:58:32 -050067 if self.td['USE_NLS'] != 'yes':
68 self.skipTest('wic-tools needs USE_NLS=yes')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069
Patrick Williams45852732022-04-02 08:58:32 -050070 bitbake('wic-tools core-image-minimal core-image-minimal-mtdutils')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080071 WicTestCase.image_is_ready = True
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072 rmtree(self.resultdir, ignore_errors=True)
73
74 def tearDownLocal(self):
75 """Remove resultdir as it may contain images."""
76 rmtree(self.resultdir, ignore_errors=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080077 super(WicTestCase, self).tearDownLocal()
78
79 def _get_image_env_path(self, image):
80 """Generate and obtain the path to <image>.env"""
81 if image not in WicTestCase.wicenv_cache:
Patrick Williams45852732022-04-02 08:58:32 -050082 bitbake('%s -c do_rootfs_wicenv' % image)
83 stdir = get_bb_var('STAGING_DIR', image)
84 machine = self.td["MACHINE"]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080085 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata')
86 return WicTestCase.wicenv_cache[image]
87
Patrick Williams45852732022-04-02 08:58:32 -050088class CLITests(OESelftestTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050089 def test_version(self):
90 """Test wic --version"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080091 runCmd('wic --version')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050092
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 def test_help(self):
94 """Test wic --help and wic -h"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080095 runCmd('wic --help')
96 runCmd('wic -h')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098 def test_createhelp(self):
99 """Test wic create --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800100 runCmd('wic create --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102 def test_listhelp(self):
103 """Test wic list --help"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800104 runCmd('wic list --help')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106 def test_help_create(self):
107 """Test wic help create"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800108 runCmd('wic help create')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500109
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110 def test_help_list(self):
111 """Test wic help list"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800112 runCmd('wic help list')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114 def test_help_overview(self):
115 """Test wic help overview"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800116 runCmd('wic help overview')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118 def test_help_plugins(self):
119 """Test wic help plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800120 runCmd('wic help plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122 def test_help_kickstart(self):
123 """Test wic help kickstart"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800124 runCmd('wic help kickstart')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 def test_list_images(self):
127 """Test wic list images"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800128 runCmd('wic list images')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130 def test_list_source_plugins(self):
131 """Test wic list source-plugins"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800132 runCmd('wic list source-plugins')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134 def test_listed_images_help(self):
135 """Test wic listed images help"""
136 output = runCmd('wic list images').output
137 imagelist = [line.split()[0] for line in output.splitlines()]
138 for image in imagelist:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800139 runCmd('wic list %s help' % image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 def test_unsupported_subcommand(self):
142 """Test unsupported subcommand"""
143 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status)
144
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 def test_no_command(self):
146 """Test wic without command"""
147 self.assertEqual(1, runCmd('wic', ignore_status=True).status)
148
Patrick Williams45852732022-04-02 08:58:32 -0500149class Wic(WicTestCase):
Patrick Williamsda295312023-12-05 16:48:56 -0600150 def test_skip_kernel_install(self):
151 """Test the functionality of not installing the kernel in the boot directory using the wic plugin"""
152 # create a temporary file for the WKS content
153 with NamedTemporaryFile("w", suffix=".wks") as wks:
154 wks.write(
155 'part --source bootimg-efi '
156 '--sourceparams="loader=grub-efi,install-kernel-into-boot-dir=false" '
157 '--label boot --active\n'
158 )
159 wks.flush()
160 # create a temporary directory to extract the disk image to
161 with TemporaryDirectory() as tmpdir:
162 img = 'core-image-minimal'
163 # build the image using the WKS file
164 cmd = "wic create %s -e %s -o %s" % (
165 wks.name, img, self.resultdir)
166 runCmd(cmd)
167 wksname = os.path.splitext(os.path.basename(wks.name))[0]
168 out = glob(os.path.join(
169 self.resultdir, "%s-*.direct" % wksname))
170 self.assertEqual(1, len(out))
171 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
172 # extract the content of the disk image to the temporary directory
173 cmd = "wic cp %s:1 %s -n %s" % (out[0], tmpdir, sysroot)
174 runCmd(cmd)
175 # check if the kernel is installed or not
176 kimgtype = get_bb_var('KERNEL_IMAGETYPE', img)
177 for file in os.listdir(tmpdir):
178 if file == kimgtype:
179 raise AssertionError(
180 "The kernel image '{}' was found in the partition".format(kimgtype)
181 )
182
183 def test_kernel_install(self):
184 """Test the installation of the kernel to the boot directory in the wic plugin"""
185 # create a temporary file for the WKS content
186 with NamedTemporaryFile("w", suffix=".wks") as wks:
187 wks.write(
188 'part --source bootimg-efi '
189 '--sourceparams="loader=grub-efi,install-kernel-into-boot-dir=true" '
190 '--label boot --active\n'
191 )
192 wks.flush()
193 # create a temporary directory to extract the disk image to
194 with TemporaryDirectory() as tmpdir:
195 img = 'core-image-minimal'
196 # build the image using the WKS file
197 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
198 runCmd(cmd)
199 wksname = os.path.splitext(os.path.basename(wks.name))[0]
200 out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
201 self.assertEqual(1, len(out))
202 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
203 # extract the content of the disk image to the temporary directory
204 cmd = "wic cp %s:1 %s -n %s" % (out[0], tmpdir, sysroot)
205 runCmd(cmd)
206 # check if the kernel is installed or not
207 kimgtype = get_bb_var('KERNEL_IMAGETYPE', img)
208 found = False
209 for file in os.listdir(tmpdir):
210 if file == kimgtype:
211 found = True
212 break
213 self.assertTrue(
214 found, "The kernel image '{}' was not found in the boot partition".format(kimgtype)
215 )
216
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217 def test_build_image_name(self):
218 """Test wic create wictestdisk --image-name=core-image-minimal"""
219 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800220 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500221 self.assertEqual(1, len(glob(os.path.join (self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500222
Patrick Williams7784c422022-11-17 07:29:11 -0600223 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500224 def test_gpt_image(self):
225 """Test creation of core-image-minimal with gpt table and UUID boot"""
226 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800227 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500228 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500229
Patrick Williams7784c422022-11-17 07:29:11 -0600230 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500231 def test_iso_image(self):
232 """Test creation of hybrid iso image with legacy and EFI boot"""
233 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500234 'MACHINE_FEATURES:append = " efi"\n'\
235 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500236 self.append_config(config)
Brad Bishopc4ea0752018-11-15 14:30:15 -0800237 bitbake('core-image-minimal core-image-minimal-initramfs')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500238 self.remove_config(config)
239 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800240 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500241 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "HYBRID_ISO_IMG-*.direct"))))
242 self.assertEqual(1, len(glob(os.path.join (self.resultdir, "HYBRID_ISO_IMG-*.iso"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500243
Patrick Williams7784c422022-11-17 07:29:11 -0600244 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500245 def test_qemux86_directdisk(self):
246 """Test creation of qemux-86-directdisk image"""
247 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800248 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500249 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "qemux86-directdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500250
Patrick Williams7784c422022-11-17 07:29:11 -0600251 @skipIfNotArch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500252 def test_mkefidisk(self):
253 """Test creation of mkefidisk image"""
254 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800255 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500256 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "mkefidisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500257
Patrick Williams7784c422022-11-17 07:29:11 -0600258 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259 def test_bootloader_config(self):
260 """Test creation of directdisk-bootloader-config image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500261 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500262 self.append_config(config)
263 bitbake('core-image-minimal')
264 self.remove_config(config)
265 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800266 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500267 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-bootloader-config-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500268
Patrick Williams7784c422022-11-17 07:29:11 -0600269 @skipIfNotArch(['i586', 'i686', 'x86_64', 'aarch64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500270 def test_systemd_bootdisk(self):
271 """Test creation of systemd-bootdisk image"""
Patrick Williams213cb262021-08-07 19:21:33 -0500272 config = 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500273 self.append_config(config)
274 bitbake('core-image-minimal')
275 self.remove_config(config)
276 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800277 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500278 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "systemd-bootdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500279
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500280 def test_efi_bootpart(self):
281 """Test creation of efi-bootpart image"""
282 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir
283 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
284 self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype)
285 runCmd(cmd)
286 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
Patrick Williams45852732022-04-02 08:58:32 -0500287 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500288 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
289 self.assertIn("kernel",result.output)
290
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500291 def test_sdimage_bootpart(self):
292 """Test creation of sdimage-bootpart image"""
293 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir
294 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal')
295 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800296 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500297 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500298
Patrick Williams45852732022-04-02 08:58:32 -0500299 # TODO this doesn't have to be x86-specific
Patrick Williams7784c422022-11-17 07:29:11 -0600300 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500301 def test_default_output_dir(self):
302 """Test default output location"""
303 for fname in glob("directdisk-*.direct"):
304 os.remove(fname)
Patrick Williams213cb262021-08-07 19:21:33 -0500305 config = 'DEPENDS:pn-core-image-minimal += "syslinux"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500306 self.append_config(config)
307 bitbake('core-image-minimal')
308 self.remove_config(config)
309 cmd = "wic create directdisk -e core-image-minimal"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800310 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500311 self.assertEqual(1, len(glob("directdisk-*.direct")))
312
Patrick Williams7784c422022-11-17 07:29:11 -0600313 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500314 def test_build_artifacts(self):
315 """Test wic create directdisk providing all artifacts."""
316 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
317 'wic-tools')
318 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
319 'core-image-minimal'))
320 bbvars = {key.lower(): value for key, value in bb_vars.items()}
321 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800322 runCmd("wic create directdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500323 "-b %(staging_datadir)s "
324 "-k %(deploy_dir_image)s "
325 "-n %(recipe_sysroot_native)s "
326 "-r %(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800327 "-o %(resultdir)s" % bbvars)
Patrick Williams45852732022-04-02 08:58:32 -0500328 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500329
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500330 def test_compress_gzip(self):
331 """Test compressing an image with gzip"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800332 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500333 "--image-name core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800334 "-c gzip -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500335 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.gz"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500336
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500337 def test_compress_bzip2(self):
338 """Test compressing an image with bzip2"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800339 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500340 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800341 "-c bzip2 -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500342 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.bz2"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500343
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500344 def test_compress_xz(self):
345 """Test compressing an image with xz"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800346 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800348 "--compress-with=xz -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500349 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct.xz"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500350
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500351 def test_wrong_compressor(self):
352 """Test how wic breaks if wrong compressor is provided"""
353 self.assertEqual(2, runCmd("wic create wictestdisk "
354 "--image-name=core-image-minimal "
355 "-c wrong -o %s" % self.resultdir,
356 ignore_status=True).status)
357
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500358 def test_debug_short(self):
359 """Test -D option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800360 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500361 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800362 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500363 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
364 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500365
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500366 def test_debug_long(self):
367 """Test --debug option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800368 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500369 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800370 "--debug -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500371 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
372 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "tmp.wic*"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500373
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500374 def test_skip_build_check_short(self):
375 """Test -s option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800376 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500377 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800378 "-s -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500379 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500380
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500381 def test_skip_build_check_long(self):
382 """Test --skip-build-check option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800383 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500384 "--image-name=core-image-minimal "
385 "--skip-build-check "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800386 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500387 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500388
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500389 def test_build_rootfs_short(self):
390 """Test -f option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800391 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500392 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800393 "-f -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500394 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500395
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500396 def test_build_rootfs_long(self):
397 """Test --build-rootfs option"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800398 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500399 "--image-name=core-image-minimal "
400 "--build-rootfs "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800401 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500402 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500403
Patrick Williams45852732022-04-02 08:58:32 -0500404 # TODO this doesn't have to be x86-specific
Patrick Williams7784c422022-11-17 07:29:11 -0600405 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500406 def test_rootfs_indirect_recipes(self):
407 """Test usage of rootfs plugin with rootfs recipes"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800408 runCmd("wic create directdisk-multi-rootfs "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500409 "--image-name=core-image-minimal "
410 "--rootfs rootfs1=core-image-minimal "
411 "--rootfs rootfs2=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800412 "--outdir %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -0500413 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "directdisk-multi-rootfs*.direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500414
Patrick Williams45852732022-04-02 08:58:32 -0500415 # TODO this doesn't have to be x86-specific
Patrick Williams7784c422022-11-17 07:29:11 -0600416 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500417 def test_rootfs_artifacts(self):
418 """Test usage of rootfs plugin with rootfs paths"""
419 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'],
420 'wic-tools')
421 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'],
422 'core-image-minimal'))
423 bbvars = {key.lower(): value for key, value in bb_vars.items()}
424 bbvars['wks'] = "directdisk-multi-rootfs"
425 bbvars['resultdir'] = self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800426 runCmd("wic create %(wks)s "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500427 "--bootimg-dir=%(staging_datadir)s "
428 "--kernel-dir=%(deploy_dir_image)s "
429 "--native-sysroot=%(recipe_sysroot_native)s "
430 "--rootfs-dir rootfs1=%(image_rootfs)s "
431 "--rootfs-dir rootfs2=%(image_rootfs)s "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800432 "--outdir %(resultdir)s" % bbvars)
Patrick Williams45852732022-04-02 08:58:32 -0500433 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "%(wks)s-*.direct" % bbvars))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500434
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500435 def test_exclude_path(self):
436 """Test --exclude-path wks option."""
437
438 oldpath = os.environ['PATH']
439 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
440
441 try:
442 wks_file = 'temp.wks'
443 with open(wks_file, 'w') as wks:
444 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
445 wks.write("""
446part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr
447part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr
448part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr"""
449 % (rootfs_dir, rootfs_dir))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800450 runCmd("wic create %s -e core-image-minimal -o %s" \
451 % (wks_file, self.resultdir))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500452
453 os.remove(wks_file)
Patrick Williams45852732022-04-02 08:58:32 -0500454 wicout = glob(os.path.join(self.resultdir, "%s-*direct" % 'temp'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500455 self.assertEqual(1, len(wicout))
456
457 wicimg = wicout[0]
458
459 # verify partition size with wic
460 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500461
462 # parse parted output which looks like this:
463 # BYT;\n
464 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
465 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
466 partlns = res.output.splitlines()[2:]
467
468 self.assertEqual(3, len(partlns))
469
470 for part in [1, 2, 3]:
471 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
472 partln = partlns[part-1].split(":")
473 self.assertEqual(7, len(partln))
474 start = int(partln[1].rstrip("B")) / 512
475 length = int(partln[3].rstrip("B")) / 512
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800476 runCmd("dd if=%s of=%s skip=%d count=%d" %
477 (wicimg, part_file, start, length))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500478
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500479 # Test partition 1, should contain the normal root directories, except
480 # /usr.
481 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
482 os.path.join(self.resultdir, "selftest_img.part1"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500483 files = extract_files(res.output)
484 self.assertIn("etc", files)
485 self.assertNotIn("usr", files)
486
487 # Partition 2, should contain common directories for /usr, not root
488 # directories.
489 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
490 os.path.join(self.resultdir, "selftest_img.part2"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500491 files = extract_files(res.output)
492 self.assertNotIn("etc", files)
493 self.assertNotIn("usr", files)
494 self.assertIn("share", files)
495
496 # Partition 3, should contain the same as partition 2, including the bin
497 # directory, but not the files inside it.
498 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \
499 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500500 files = extract_files(res.output)
501 self.assertNotIn("etc", files)
502 self.assertNotIn("usr", files)
503 self.assertIn("share", files)
504 self.assertIn("bin", files)
505 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \
506 os.path.join(self.resultdir, "selftest_img.part3"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500507 files = extract_files(res.output)
508 self.assertIn(".", files)
509 self.assertIn("..", files)
510 self.assertEqual(2, len(files))
511
512 for part in [1, 2, 3]:
513 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part)
514 os.remove(part_file)
515
516 finally:
517 os.environ['PATH'] = oldpath
518
Andrew Geissler82c905d2020-04-13 13:39:40 -0500519 def test_include_path(self):
520 """Test --include-path wks option."""
521
522 oldpath = os.environ['PATH']
523 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
524
525 try:
526 include_path = os.path.join(self.resultdir, 'test-include')
527 os.makedirs(include_path)
528 with open(os.path.join(include_path, 'test-file'), 'w') as t:
529 t.write("test\n")
530 wks_file = os.path.join(include_path, 'temp.wks')
531 with open(wks_file, 'w') as wks:
532 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal')
533 wks.write("""
534part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4
535part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s"""
536 % (include_path))
537 runCmd("wic create %s -e core-image-minimal -o %s" \
538 % (wks_file, self.resultdir))
539
540 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
541 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0]
542
543 # Test partition 1, should not contain 'test-file'
544 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
545 files = extract_files(res.output)
546 self.assertNotIn('test-file', files)
547 self.assertEqual(True, files_own_by_root(res.output))
548
549 # Test partition 2, should contain 'test-file'
550 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2))
551 files = extract_files(res.output)
552 self.assertIn('test-file', files)
553 self.assertEqual(True, files_own_by_root(res.output))
554
555 finally:
556 os.environ['PATH'] = oldpath
557
558 def test_include_path_embeded(self):
559 """Test --include-path wks option."""
560
561 oldpath = os.environ['PATH']
562 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
563
564 try:
565 include_path = os.path.join(self.resultdir, 'test-include')
566 os.makedirs(include_path)
567 with open(os.path.join(include_path, 'test-file'), 'w') as t:
568 t.write("test\n")
569 wks_file = os.path.join(include_path, 'temp.wks')
570 with open(wks_file, 'w') as wks:
571 wks.write("""
572part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/"""
573 % (include_path))
574 runCmd("wic create %s -e core-image-minimal -o %s" \
575 % (wks_file, self.resultdir))
576
577 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
578
579 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
580 files = extract_files(res.output)
581 self.assertIn('test-file', files)
582 self.assertEqual(True, files_own_by_root(res.output))
583
584 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1))
585 files = extract_files(res.output)
586 self.assertIn('passwd', files)
587 self.assertEqual(True, files_own_by_root(res.output))
588
589 finally:
590 os.environ['PATH'] = oldpath
591
592 def test_include_path_errors(self):
593 """Test --include-path wks option error handling."""
594 wks_file = 'temp.wks'
595
596 # Absolute argument.
597 with open(wks_file, 'w') as wks:
598 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export")
599 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
600 % (wks_file, self.resultdir), ignore_status=True).status)
601 os.remove(wks_file)
602
603 # Argument pointing to parent directory.
604 with open(wks_file, 'w') as wks:
605 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..")
606 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
607 % (wks_file, self.resultdir), ignore_status=True).status)
608 os.remove(wks_file)
609
610 # 3 Argument pointing to parent directory.
611 with open(wks_file, 'w') as wks:
612 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy")
613 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
614 % (wks_file, self.resultdir), ignore_status=True).status)
615 os.remove(wks_file)
616
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500617 def test_exclude_path_errors(self):
618 """Test --exclude-path wks option error handling."""
619 wks_file = 'temp.wks'
620
621 # Absolute argument.
622 with open(wks_file, 'w') as wks:
623 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr")
624 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
625 % (wks_file, self.resultdir), ignore_status=True).status)
626 os.remove(wks_file)
627
628 # Argument pointing to parent directory.
629 with open(wks_file, 'w') as wks:
630 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..")
631 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
632 % (wks_file, self.resultdir), ignore_status=True).status)
633 os.remove(wks_file)
634
Andrew Geissler82c905d2020-04-13 13:39:40 -0500635 def test_permissions(self):
636 """Test permissions are respected"""
637
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600638 # prepare wicenv and rootfs
639 bitbake('core-image-minimal core-image-minimal-mtdutils -c do_rootfs_wicenv')
640
Andrew Geissler82c905d2020-04-13 13:39:40 -0500641 oldpath = os.environ['PATH']
642 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
643
644 t_normal = """
645part / --source rootfs --fstype=ext4
646"""
647 t_exclude = """
648part / --source rootfs --fstype=ext4 --exclude-path=home
649"""
650 t_multi = """
651part / --source rootfs --ondisk sda --fstype=ext4
652part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4
653"""
654 t_change = """
655part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/   
656part /etc --source rootfs --fstype=ext4 --change-directory=etc
657"""
658 tests = [t_normal, t_exclude, t_multi, t_change]
659
660 try:
661 for test in tests:
662 include_path = os.path.join(self.resultdir, 'test-include')
663 os.makedirs(include_path)
664 wks_file = os.path.join(include_path, 'temp.wks')
665 with open(wks_file, 'w') as wks:
666 wks.write(test)
667 runCmd("wic create %s -e core-image-minimal -o %s" \
668 % (wks_file, self.resultdir))
669
670 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')):
671 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
672 self.assertEqual(True, files_own_by_root(res.output))
673
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600674 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "%s"\n' % wks_file
675 self.append_config(config)
676 bitbake('core-image-minimal')
677 tmpdir = os.path.join(get_bb_var('WORKDIR', 'core-image-minimal'),'build-wic')
678
679 # check each partition for permission
680 for part in glob(os.path.join(tmpdir, 'temp-*.direct.p*')):
681 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part))
682 self.assertTrue(files_own_by_root(res.output)
683 ,msg='Files permission incorrect using wks set "%s"' % test)
684
685 # clean config and result directory for next cases
686 self.remove_config(config)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500687 rmtree(self.resultdir, ignore_errors=True)
688
689 finally:
690 os.environ['PATH'] = oldpath
691
692 def test_change_directory(self):
693 """Test --change-directory wks option."""
694
695 oldpath = os.environ['PATH']
696 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
697
698 try:
699 include_path = os.path.join(self.resultdir, 'test-include')
700 os.makedirs(include_path)
701 wks_file = os.path.join(include_path, 'temp.wks')
702 with open(wks_file, 'w') as wks:
703 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc")
704 runCmd("wic create %s -e core-image-minimal -o %s" \
705 % (wks_file, self.resultdir))
706
707 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0]
708
709 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1))
710 files = extract_files(res.output)
711 self.assertIn('passwd', files)
712
713 finally:
714 os.environ['PATH'] = oldpath
715
716 def test_change_directory_errors(self):
717 """Test --change-directory wks option error handling."""
718 wks_file = 'temp.wks'
719
720 # Absolute argument.
721 with open(wks_file, 'w') as wks:
722 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr")
723 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
724 % (wks_file, self.resultdir), ignore_status=True).status)
725 os.remove(wks_file)
726
727 # Argument pointing to parent directory.
728 with open(wks_file, 'w') as wks:
729 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..")
730 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
731 % (wks_file, self.resultdir), ignore_status=True).status)
732 os.remove(wks_file)
733
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500734 def test_no_fstab_update(self):
735 """Test --no-fstab-update wks option."""
736
737 oldpath = os.environ['PATH']
738 os.environ['PATH'] = get_bb_var("PATH", "wic-tools")
739
740 # Get stock fstab from base-files recipe
Patrick Williams45852732022-04-02 08:58:32 -0500741 bitbake('base-files -c do_install')
742 bf_fstab = os.path.join(get_bb_var('D', 'base-files'), 'etc', 'fstab')
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500743 self.assertEqual(True, os.path.exists(bf_fstab))
744 bf_fstab_md5sum = runCmd('md5sum %s 2>/dev/null' % bf_fstab).output.split(" ")[0]
745
746 try:
747 no_fstab_update_path = os.path.join(self.resultdir, 'test-no-fstab-update')
748 os.makedirs(no_fstab_update_path)
749 wks_file = os.path.join(no_fstab_update_path, 'temp.wks')
750 with open(wks_file, 'w') as wks:
751 wks.writelines(['part / --source rootfs --fstype=ext4 --label rootfs\n',
752 'part /mnt/p2 --source rootfs --rootfs-dir=core-image-minimal ',
753 '--fstype=ext4 --label p2 --no-fstab-update\n'])
754 runCmd("wic create %s -e core-image-minimal -o %s" \
755 % (wks_file, self.resultdir))
756
757 part_fstab_md5sum = []
758 for i in range(1, 3):
759 part = glob(os.path.join(self.resultdir, 'temp-*.direct.p') + str(i))[0]
760 part_fstab = runCmd("debugfs -R 'cat etc/fstab' %s 2>/dev/null" % (part))
761 part_fstab_md5sum.append(hashlib.md5((part_fstab.output + "\n\n").encode('utf-8')).hexdigest())
762
763 # '/etc/fstab' in partition 2 should contain the same stock fstab file
764 # as the one installed by the base-file recipe.
765 self.assertEqual(bf_fstab_md5sum, part_fstab_md5sum[1])
766
767 # '/etc/fstab' in partition 1 should contain an updated fstab file.
768 self.assertNotEqual(bf_fstab_md5sum, part_fstab_md5sum[0])
769
770 finally:
771 os.environ['PATH'] = oldpath
772
773 def test_no_fstab_update_errors(self):
774 """Test --no-fstab-update wks option error handling."""
775 wks_file = 'temp.wks'
776
777 # Absolute argument.
778 with open(wks_file, 'w') as wks:
779 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update /etc")
780 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
781 % (wks_file, self.resultdir), ignore_status=True).status)
782 os.remove(wks_file)
783
784 # Argument pointing to parent directory.
785 with open(wks_file, 'w') as wks:
786 wks.write("part / --source rootfs --fstype=ext4 --no-fstab-update ././..")
787 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \
788 % (wks_file, self.resultdir), ignore_status=True).status)
789 os.remove(wks_file)
790
Andrew Geissler5199d832021-09-24 16:47:35 -0500791 def test_extra_space(self):
792 """Test --extra-space wks option."""
793 extraspace = 1024**3
794 runCmd("wic create wictestdisk "
795 "--image-name core-image-minimal "
796 "--extra-space %i -o %s" % (extraspace ,self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500797 wicout = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Andrew Geissler5199d832021-09-24 16:47:35 -0500798 self.assertEqual(1, len(wicout))
799 size = os.path.getsize(wicout[0])
Andrew Geissler220dafd2023-10-04 10:18:08 -0500800 self.assertTrue(size > extraspace, msg="Extra space not present (%s vs %s)" % (size, extraspace))
Andrew Geissler5199d832021-09-24 16:47:35 -0500801
Andrew Geissler5082cc72023-09-11 08:41:39 -0400802 def test_no_table(self):
803 """Test --no-table wks option."""
804 wks_file = 'temp.wks'
805
806 # Absolute argument.
807 with open(wks_file, 'w') as wks:
808 wks.write("part testspace --no-table --fixed-size 16k --offset 4080k")
809 runCmd("wic create %s --image-name core-image-minimal -o %s" % (wks_file, self.resultdir))
810
811 wicout = glob(os.path.join(self.resultdir, "*.*"))
812
813 self.assertEqual(1, len(wicout))
814 size = os.path.getsize(wicout[0])
815 self.assertEqual(size, 4 * 1024 * 1024)
816
817 os.remove(wks_file)
818
Patrick Williamsda295312023-12-05 16:48:56 -0600819 def test_partition_hidden_attributes(self):
820 """Test --hidden wks option."""
821 wks_file = 'temp.wks'
822 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
823 try:
824 with open(wks_file, 'w') as wks:
825 wks.write("""
826part / --source rootfs --fstype=ext4
827part / --source rootfs --fstype=ext4 --hidden
828bootloader --ptable gpt""")
829
830 runCmd("wic create %s -e core-image-minimal -o %s" \
831 % (wks_file, self.resultdir))
832 wicout = os.path.join(self.resultdir, "*.direct")
833
834 result = runCmd("%s/usr/sbin/sfdisk --part-attrs %s 1" % (sysroot, wicout))
835 self.assertEqual('', result.output)
836 result = runCmd("%s/usr/sbin/sfdisk --part-attrs %s 2" % (sysroot, wicout))
837 self.assertEqual('RequiredPartition', result.output)
838
839 finally:
840 os.remove(wks_file)
841
842
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800843class Wic2(WicTestCase):
844
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500845 def test_bmap_short(self):
846 """Test generation of .bmap file -m option"""
847 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800848 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500849 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
850 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500851
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500852 def test_bmap_long(self):
853 """Test generation of .bmap file --bmap option"""
854 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800855 runCmd(cmd)
Patrick Williams45852732022-04-02 08:58:32 -0500856 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
857 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct.bmap"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500858
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500859 def test_image_env(self):
860 """Test generation of <image>.env files."""
861 image = 'core-image-minimal'
862 imgdatadir = self._get_image_env_path(image)
863
864 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image)
865 basename = bb_vars['IMAGE_BASENAME']
866 self.assertEqual(basename, image)
867 path = os.path.join(imgdatadir, basename) + '.env'
Andrew Geissler220dafd2023-10-04 10:18:08 -0500868 self.assertTrue(os.path.isfile(path), msg="File %s wasn't generated as expected" % path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500869
870 wicvars = set(bb_vars['WICVARS'].split())
871 # filter out optional variables
872 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES',
Brad Bishop96ff1982019-08-19 13:50:42 -0400873 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE',
Andrew Geissler82c905d2020-04-13 13:39:40 -0500874 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME',
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500875 'APPEND', 'IMAGE_EFI_BOOT_FILES'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500876 with open(path) as envfile:
877 content = dict(line.split("=", 1) for line in envfile)
878 # test if variables used by wic present in the .env file
879 for var in wicvars:
880 self.assertTrue(var in content, "%s is not in .env file" % var)
Andrew Geissler220dafd2023-10-04 10:18:08 -0500881 self.assertTrue(content[var], "%s doesn't have a value (%s)" % (var, content[var]))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500882
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500883 def test_image_vars_dir_short(self):
884 """Test image vars directory selection -v option"""
885 image = 'core-image-minimal'
886 imgenvdir = self._get_image_env_path(image)
887 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
888
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800889 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500890 "--image-name=%s -v %s -n %s -o %s"
891 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800892 self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500893 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500894
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500895 def test_image_vars_dir_long(self):
896 """Test image vars directory selection --vars option"""
897 image = 'core-image-minimal'
898 imgenvdir = self._get_image_env_path(image)
899 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
900
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800901 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500902 "--image-name=%s "
903 "--vars %s "
904 "--native-sysroot %s "
905 "--outdir %s"
906 % (image, imgenvdir, native_sysroot,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800907 self.resultdir))
Patrick Williams45852732022-04-02 08:58:32 -0500908 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "wictestdisk-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500909
Patrick Williams7784c422022-11-17 07:29:11 -0600910 # TODO this test could also work on aarch64
911 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500912 def test_wic_image_type(self):
913 """Test building wic images by bitbake"""
914 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500915 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500916 self.append_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500917 image = 'wic-image-minimal'
918 bitbake(image)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500919 self.remove_config(config)
920
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500921 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
922 prefix = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.' % bb_vars['IMAGE_LINK_NAME'])
923
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500924 # check if we have result image and manifests symlinks
925 # pointing to existing files
926 for suffix in ('wic', 'manifest'):
927 path = prefix + suffix
Andrew Geissler220dafd2023-10-04 10:18:08 -0500928 self.assertTrue(os.path.islink(path), msg="Link %s wasn't generated as expected" % path)
929 self.assertTrue(os.path.isfile(os.path.realpath(path)), msg="File linked to by %s wasn't generated as expected" % path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500930
Patrick Williams45852732022-04-02 08:58:32 -0500931 # TODO this should work on aarch64
Patrick Williams7784c422022-11-17 07:29:11 -0600932 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -0500933 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500934 def test_qemu(self):
935 """Test wic-image-minimal under qemu"""
936 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\
Patrick Williams213cb262021-08-07 19:21:33 -0500937 'MACHINE_FEATURES:append = " efi"\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500938 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -0500939 bitbake('wic-image-minimal')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500940 self.remove_config(config)
941
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000942 with runqemu('wic-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
Andrew Geissler99467da2019-02-25 18:54:23 -0600943 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \
944 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400945 status, output = qemu.run_serial(cmd)
Andrew Geissler99467da2019-02-25 18:54:23 -0600946 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
947 self.assertEqual(output, '4')
Andrew Geissler78b72792022-06-14 06:47:25 -0500948 cmd = "grep UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba /etc/fstab"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500949 status, output = qemu.run_serial(cmd)
950 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400951 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500952
Patrick Williams7784c422022-11-17 07:29:11 -0600953 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -0500954 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500955 def test_qemu_efi(self):
956 """Test core-image-minimal efi image under qemu"""
957 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n'
958 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -0500959 bitbake('core-image-minimal ovmf')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500960 self.remove_config(config)
961
962 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000963 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500964 cmd = "grep sda. /proc/partitions |wc -l"
965 status, output = qemu.run_serial(cmd)
966 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
967 self.assertEqual(output, '3')
968
969 @staticmethod
970 def _make_fixed_size_wks(size):
971 """
972 Create a wks of an image with a single partition. Size of the partition is set
973 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name)
974 """
975 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf:
976 wkspath = tempf.name
977 tempf.write("part " \
978 "--source rootfs --ondisk hda --align 4 --fixed-size %d "
979 "--fstype=ext4\n" % size)
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500980
981 return wkspath
982
983 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False):
984 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir),
985 ignore_status=ignore_status)
986
987 if p.status:
988 return (p, None)
989
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500990 wksname = os.path.splitext(os.path.basename(wkspath))[0]
991
Patrick Williams45852732022-04-02 08:58:32 -0500992 wicout = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500993
994 if not wicout:
995 return (p, None)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500996
997 wicimg = wicout[0]
998
Andrew Geissler4ed12e12020-06-05 18:00:41 -0500999 if not native_sysroot:
1000 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001001
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001002 # verify partition size with wic
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001003 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg,
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001004 native_sysroot=native_sysroot)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001005
1006 # parse parted output which looks like this:
1007 # BYT;\n
1008 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n
1009 # 1:0.00MiB:200MiB:200MiB:ext4::;\n
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001010 return (p, res.output.splitlines()[2:])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001011
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001012 def test_fixed_size(self):
1013 """
1014 Test creation of a simple image with partition size controlled through
1015 --fixed-size flag
1016 """
1017 wkspath = Wic2._make_fixed_size_wks(200)
1018 _, partlns = self._get_wic_partitions(wkspath)
1019 os.remove(wkspath)
1020
1021 self.assertEqual(partlns, [
1022 "1:4.00kiB:204804kiB:204800kiB:ext4::;",
1023 ])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001024
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001025 def test_fixed_size_error(self):
1026 """
1027 Test creation of a simple image with partition size controlled through
1028 --fixed-size flag. The size of partition is intentionally set to 1MiB
1029 in order to trigger an error in wic.
1030 """
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001031 wkspath = Wic2._make_fixed_size_wks(1)
1032 p, _ = self._get_wic_partitions(wkspath, ignore_status=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001033 os.remove(wkspath)
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001034
1035 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1036
1037 def test_offset(self):
1038 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
1039
1040 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1041 # Test that partitions are placed at the correct offsets, default KB
1042 tempf.write("bootloader --ptable gpt\n" \
1043 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
1044 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
1045 tempf.flush()
1046
1047 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1048 self.assertEqual(partlns, [
1049 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
1050 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
1051 ])
1052
1053 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1054 # Test that partitions are placed at the correct offsets, same with explicit KB
1055 tempf.write("bootloader --ptable gpt\n" \
1056 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
1057 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n")
1058 tempf.flush()
1059
1060 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1061 self.assertEqual(partlns, [
1062 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
1063 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
1064 ])
1065
1066 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1067 # Test that partitions are placed at the correct offsets using MB
1068 tempf.write("bootloader --ptable gpt\n" \
1069 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \
1070 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n")
1071 tempf.flush()
1072
1073 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1074 self.assertEqual(partlns, [
1075 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;",
1076 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;",
1077 ])
1078
1079 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001080 # Test that partitions can be placed on a 512 byte sector boundary
1081 tempf.write("bootloader --ptable gpt\n" \
1082 "part / --source rootfs --ondisk hda --offset 65s --fixed-size 99M --fstype=ext4\n" \
1083 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n")
1084 tempf.flush()
1085
1086 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1087 self.assertEqual(partlns, [
1088 "1:32.5kiB:101408kiB:101376kiB:ext4:primary:;",
1089 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;",
1090 ])
1091
1092 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1093 # Test that a partition can be placed immediately after a MSDOS partition table
1094 tempf.write("bootloader --ptable msdos\n" \
1095 "part / --source rootfs --ondisk hda --offset 1s --fixed-size 100M --fstype=ext4\n")
1096 tempf.flush()
1097
1098 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1099 self.assertEqual(partlns, [
1100 "1:0.50kiB:102400kiB:102400kiB:ext4::;",
1101 ])
1102
1103 with NamedTemporaryFile("w", suffix=".wks") as tempf:
Andrew Geissler4ed12e12020-06-05 18:00:41 -05001104 # Test that image creation fails if the partitions would overlap
1105 tempf.write("bootloader --ptable gpt\n" \
1106 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \
1107 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n")
1108 tempf.flush()
1109
1110 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1111 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
1112
1113 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1114 # Test that partitions are not allowed to overlap with the booloader
1115 tempf.write("bootloader --ptable gpt\n" \
1116 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n")
1117 tempf.flush()
1118
1119 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True)
1120 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001121
Andrew Geissler5a43b432020-06-13 10:46:56 -05001122 def test_extra_space(self):
1123 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
1124
1125 with NamedTemporaryFile("w", suffix=".wks") as tempf:
1126 tempf.write("bootloader --ptable gpt\n" \
1127 "part / --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n")
1128 tempf.flush()
1129
1130 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot)
1131 self.assertEqual(len(partlns), 1)
1132 size = partlns[0].split(':')[3]
1133 self.assertRegex(size, r'^[0-9]+kiB$')
1134 size = int(size[:-3])
1135 self.assertGreaterEqual(size, 204800)
1136
Patrick Williams7784c422022-11-17 07:29:11 -06001137 # TODO this test could also work on aarch64
1138 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001139 @OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001140 def test_rawcopy_plugin_qemu(self):
1141 """Test rawcopy plugin in qemu"""
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001142 # build ext4 and then use it for a wic image
1143 config = 'IMAGE_FSTYPES = "ext4"\n'
1144 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001145 bitbake('core-image-minimal')
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001146 image_link_name = get_bb_var('IMAGE_LINK_NAME', 'core-image-minimal')
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001147 self.remove_config(config)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001148
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001149 config = 'IMAGE_FSTYPES = "wic"\n' \
1150 'IMAGE_LINK_NAME_CORE_IMAGE_MINIMAL = "%s"\n'\
1151 'WKS_FILE = "test_rawcopy_plugin.wks.in"\n'\
1152 % image_link_name
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001153 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001154 bitbake('core-image-minimal-mtdutils')
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001155 self.remove_config(config)
1156
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001157 with runqemu('core-image-minimal-mtdutils', ssh=False,
1158 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001159 cmd = "grep sda. /proc/partitions |wc -l"
1160 status, output = qemu.run_serial(cmd)
1161 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1162 self.assertEqual(output, '2')
1163
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001164 def _rawcopy_plugin(self, fstype):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001165 """Test rawcopy plugin"""
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001166 image = 'core-image-minimal'
1167 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001168 params = ',unpack' if fstype.endswith('.gz') else ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001169 with NamedTemporaryFile("w", suffix=".wks") as wks:
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001170 wks.write('part / --source rawcopy --sourceparams="file=%s.%s%s"\n'\
1171 % (bb_vars['IMAGE_LINK_NAME'], fstype, params))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001172 wks.flush()
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001173 cmd = "wic create %s -e %s -o %s" % (wks.name, image, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001174 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001175 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001176 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001177 self.assertEqual(1, len(out))
1178
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001179 def test_rawcopy_plugin(self):
1180 self._rawcopy_plugin('ext4')
1181
1182 def test_rawcopy_plugin_unpack(self):
1183 fstype = 'ext4.gz'
1184 config = 'IMAGE_FSTYPES = "%s"\n' % fstype
1185 self.append_config(config)
1186 self.assertEqual(0, bitbake('core-image-minimal').status)
1187 self.remove_config(config)
1188 self._rawcopy_plugin(fstype)
1189
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001190 def test_empty_plugin(self):
1191 """Test empty plugin"""
1192 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n'
1193 self.append_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001194 image = 'core-image-minimal'
1195 bitbake(image)
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001196 self.remove_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001197 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
1198 image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.wic' % bb_vars['IMAGE_LINK_NAME'])
Andrew Geissler220dafd2023-10-04 10:18:08 -05001199 self.assertTrue(os.path.exists(image_path), msg="Image file %s wasn't generated as expected" % image_path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001200
1201 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1202
1203 # Fstype column from 'wic ls' should be empty for the second partition
1204 # as listed in test_empty_plugin.wks
1205 result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot))
1206 self.assertEqual('1', result.output)
1207
Patrick Williams7784c422022-11-17 07:29:11 -06001208 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001209 @OETestTag("runqemu")
Brad Bishop96ff1982019-08-19 13:50:42 -04001210 def test_biosplusefi_plugin_qemu(self):
1211 """Test biosplusefi plugin in qemu"""
Patrick Williams213cb262021-08-07 19:21:33 -05001212 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001213 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001214 bitbake('core-image-minimal')
Brad Bishop08902b02019-08-20 09:16:51 -04001215 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001216
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001217 with runqemu('core-image-minimal', ssh=False,
1218 runqemuparams='nographic', image_fstype='wic') as qemu:
Brad Bishop96ff1982019-08-19 13:50:42 -04001219 # Check that we have ONLY two /dev/sda* partitions (/boot and /)
1220 cmd = "grep sda. /proc/partitions | wc -l"
1221 status, output = qemu.run_serial(cmd)
1222 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1223 self.assertEqual(output, '2')
1224 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is /
1225 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'"
1226 status, output = qemu.run_serial(cmd)
1227 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1228 self.assertEqual(output, '2')
1229 # Check that /boot has EFI bootx64.efi (required for EFI)
1230 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1231 status, output = qemu.run_serial(cmd)
1232 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1233 self.assertEqual(output, '1')
1234 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios)
1235 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command
1236 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat"
1237 status, output = qemu.run_serial(cmd)
1238 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1239 self.assertEqual(output, '*')
1240
Patrick Williams7784c422022-11-17 07:29:11 -06001241 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishop96ff1982019-08-19 13:50:42 -04001242 def test_biosplusefi_plugin(self):
1243 """Test biosplusefi plugin"""
1244 # Wic generation below may fail depending on the order of the unittests
1245 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory
1246 # which may or may not exists depending on what was built already
1247 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir()
1248 # will raise with "Couldn't find correct bootimg_dir"
1249 # 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 -05001250 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES:append = " efi"\n'
Brad Bishop08902b02019-08-20 09:16:51 -04001251 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001252 bitbake('core-image-minimal')
Brad Bishop08902b02019-08-20 09:16:51 -04001253 self.remove_config(config)
Brad Bishop96ff1982019-08-19 13:50:42 -04001254
1255 img = 'core-image-minimal'
1256 with NamedTemporaryFile("w", suffix=".wks") as wks:
1257 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n',
1258 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1259 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1260 wks.flush()
1261 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1262 runCmd(cmd)
1263 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001264 out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
Brad Bishop96ff1982019-08-19 13:50:42 -04001265 self.assertEqual(1, len(out))
1266
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06001267 @skipIfNotArch(['i586', 'i686', 'x86_64', 'aarch64'])
1268 def test_uefi_kernel(self):
1269 """ Test uefi-kernel in wic """
1270 config = 'IMAGE_EFI_BOOT_FILES="/etc/fstab;testfile"\nIMAGE_FSTYPES = "wic"\nWKS_FILE = "test_uefikernel.wks"\nMACHINE_FEATURES:append = " efi"\n'
1271 self.append_config(config)
1272 bitbake('core-image-minimal')
1273 self.remove_config(config)
1274
1275 img = 'core-image-minimal'
1276 with NamedTemporaryFile("w", suffix=".wks") as wks:
1277 wks.writelines(['part /boot --source bootimg-efi --sourceparams="loader=uefi-kernel"\n'
1278 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\
1279 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n'])
1280 wks.flush()
1281 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1282 runCmd(cmd)
1283 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1284 out = glob(os.path.join(self.resultdir, "%s-*.direct" % wksname))
1285 self.assertEqual(1, len(out))
1286
Patrick Williams45852732022-04-02 08:58:32 -05001287 # TODO this test could also work on aarch64
Patrick Williams7784c422022-11-17 07:29:11 -06001288 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001289 @OETestTag("runqemu")
Patrick Williams93c203f2021-10-06 16:15:23 -05001290 def test_efi_plugin_unified_kernel_image_qemu(self):
1291 """Test efi plugin's Unified Kernel Image feature in qemu"""
1292 config = 'IMAGE_FSTYPES = "wic"\n'\
1293 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\
1294 'WKS_FILE = "test_efi_plugin.wks"\n'\
1295 'MACHINE_FEATURES:append = " efi"\n'
1296 self.append_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001297 bitbake('core-image-minimal core-image-minimal-initramfs ovmf')
Patrick Williams93c203f2021-10-06 16:15:23 -05001298 self.remove_config(config)
1299
1300 with runqemu('core-image-minimal', ssh=False,
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001301 runqemuparams='nographic ovmf', image_fstype='wic') as qemu:
Patrick Williams93c203f2021-10-06 16:15:23 -05001302 # Check that /boot has EFI bootx64.efi (required for EFI)
1303 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l"
1304 status, output = qemu.run_serial(cmd)
1305 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1306 self.assertEqual(output, '1')
1307 # Check that /boot has EFI/Linux/linux.efi (required for Unified Kernel Images auto detection)
1308 cmd = "ls /boot/EFI/Linux/linux.efi | wc -l"
1309 status, output = qemu.run_serial(cmd)
1310 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1311 self.assertEqual(output, '1')
1312 # Check that /boot doesn't have loader/entries/boot.conf (Unified Kernel Images are auto detected by the bootloader)
1313 cmd = "ls /boot/loader/entries/boot.conf 2&>/dev/null | wc -l"
1314 status, output = qemu.run_serial(cmd)
1315 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1316 self.assertEqual(output, '0')
1317
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001318 def test_fs_types(self):
1319 """Test filesystem types for empty and not empty partitions"""
1320 img = 'core-image-minimal'
1321 with NamedTemporaryFile("w", suffix=".wks") as wks:
1322 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n',
1323 'part btrfs --fstype btrfs --source rootfs --size 40M\n',
1324 'part squash --fstype squashfs --source rootfs\n',
1325 'part swap --fstype swap --size 1M\n',
1326 'part emptyvfat --fstype vfat --size 1M\n',
1327 'part emptymsdos --fstype msdos --size 1M\n',
1328 'part emptyext2 --fstype ext2 --size 1M\n',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001329 'part emptybtrfs --fstype btrfs --size 150M\n'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001330 wks.flush()
1331 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001332 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001333 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001334 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001335 self.assertEqual(1, len(out))
1336
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001337 def test_kickstart_parser(self):
1338 """Test wks parser options"""
1339 with NamedTemporaryFile("w", suffix=".wks") as wks:
1340 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\
1341 '--overhead-factor 1.2 --size 100k\n'])
1342 wks.flush()
1343 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001344 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001345 wksname = os.path.splitext(os.path.basename(wks.name))[0]
Patrick Williams45852732022-04-02 08:58:32 -05001346 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001347 self.assertEqual(1, len(out))
1348
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001349 def test_image_bootpart_globbed(self):
1350 """Test globbed sources with image-bootpart plugin"""
1351 img = "core-image-minimal"
1352 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir)
1353 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img)
1354 self.append_config(config)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001355 runCmd(cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001356 self.remove_config(config)
Patrick Williams45852732022-04-02 08:58:32 -05001357 self.assertEqual(1, len(glob(os.path.join(self.resultdir, "sdimage-bootpart-*direct"))))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001358
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001359 def test_sparse_copy(self):
1360 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs"""
Patrick Williams45852732022-04-02 08:58:32 -05001361 libpath = os.path.join(self.td['COREBASE'], 'scripts', 'lib', 'wic')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001362 sys.path.insert(0, libpath)
1363 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp
1364 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse:
1365 src_name = sparse.name
1366 src_size = 1024 * 10
1367 sparse.truncate(src_size)
1368 # write one byte to the file
1369 with open(src_name, 'r+b') as sfile:
1370 sfile.seek(1024 * 4)
1371 sfile.write(b'\x00')
1372 dest = sparse.name + '.out'
1373 # copy src file to dest using different filemap APIs
1374 for api in (FilemapFiemap, FilemapSeek, None):
1375 if os.path.exists(dest):
1376 os.unlink(dest)
1377 try:
1378 sparse_copy(sparse.name, dest, api=api)
1379 except ErrorNotSupp:
1380 continue # skip unsupported API
1381 dest_stat = os.stat(dest)
1382 self.assertEqual(dest_stat.st_size, src_size)
1383 # 8 blocks is 4K (physical sector size)
1384 self.assertEqual(dest_stat.st_blocks, 8)
1385 os.unlink(dest)
1386
Patrick Williams45852732022-04-02 08:58:32 -05001387 def test_mkfs_extraopts(self):
1388 """Test wks option --mkfs-extraopts for empty and not empty partitions"""
1389 img = 'core-image-minimal'
1390 with NamedTemporaryFile("w", suffix=".wks") as wks:
1391 wks.writelines(
1392 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n',
1393 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n",
1394 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n',
1395 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1396 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n',
1397 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n',
1398 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n'])
1399 wks.flush()
1400 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir)
1401 runCmd(cmd)
1402 wksname = os.path.splitext(os.path.basename(wks.name))[0]
1403 out = glob(os.path.join(self.resultdir, "%s-*direct" % wksname))
1404 self.assertEqual(1, len(out))
1405
Patrick Williams7784c422022-11-17 07:29:11 -06001406 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Patrick Williams45852732022-04-02 08:58:32 -05001407 @OETestTag("runqemu")
1408 def test_expand_mbr_image(self):
1409 """Test wic write --expand command for mbr image"""
1410 # build an image
1411 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
1412 self.append_config(config)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001413 image = 'core-image-minimal'
1414 bitbake(image)
Patrick Williams45852732022-04-02 08:58:32 -05001415
1416 # get path to the image
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001417 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
1418 image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.wic' % bb_vars['IMAGE_LINK_NAME'])
Patrick Williams45852732022-04-02 08:58:32 -05001419
1420 self.remove_config(config)
1421
1422 try:
1423 # expand image to 1G
1424 new_image_path = None
1425 with NamedTemporaryFile(mode='wb', suffix='.wic.exp',
Andrew Geisslerfc113ea2023-03-31 09:59:46 -05001426 dir=bb_vars['DEPLOY_DIR_IMAGE'], delete=False) as sparse:
Patrick Williams45852732022-04-02 08:58:32 -05001427 sparse.truncate(1024 ** 3)
1428 new_image_path = sparse.name
1429
1430 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1431 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path)
1432 runCmd(cmd)
1433
1434 # check if partitions are expanded
1435 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot))
1436 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot))
1437 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]]
1438 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]]
1439 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized
Andrew Geissler220dafd2023-10-04 10:18:08 -05001440 self.assertTrue(orig_sizes[1] < exp_sizes[1], msg="Parition size wasn't enlarged (%s vs %s)" % (orig_sizes[1], exp_sizes[1]))
Patrick Williams45852732022-04-02 08:58:32 -05001441
1442 # Check if all free space is partitioned
1443 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001444 self.assertIn("0 B, 0 bytes, 0 sectors", result.output)
Patrick Williams45852732022-04-02 08:58:32 -05001445
1446 os.rename(image_path, image_path + '.bak')
1447 os.rename(new_image_path, image_path)
1448
1449 # Check if it boots in qemu
1450 with runqemu('core-image-minimal', ssh=False, runqemuparams='nographic') as qemu:
1451 cmd = "ls /etc/"
1452 status, output = qemu.run_serial('true')
1453 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output))
1454 finally:
1455 if os.path.exists(new_image_path):
1456 os.unlink(new_image_path)
1457 if os.path.exists(image_path + '.bak'):
1458 os.rename(image_path + '.bak', image_path)
1459
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001460 def test_gpt_partition_name(self):
1461 """Test --part-name argument to set partition name in GPT table"""
1462 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "test_gpt_partition_name.wks"\n'
1463 self.append_config(config)
Patrick Williamsb542dec2023-06-09 01:26:37 -05001464 image = 'core-image-minimal'
1465 bitbake(image)
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001466 self.remove_config(config)
1467 deploy_dir = get_bb_var('DEPLOY_DIR_IMAGE')
Patrick Williamsb542dec2023-06-09 01:26:37 -05001468 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'], image)
1469 image_path = os.path.join(bb_vars['DEPLOY_DIR_IMAGE'], '%s.wic' % bb_vars['IMAGE_LINK_NAME'])
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001470
Patrick Williamsb542dec2023-06-09 01:26:37 -05001471 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1472
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001473 # Image is created
Patrick Williamsb542dec2023-06-09 01:26:37 -05001474 self.assertTrue(os.path.exists(image_path), "image file %s doesn't exist" % image_path)
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001475
1476 # Check the names of the three partitions
1477 # as listed in test_gpt_partition_name.wks
Patrick Williamsb542dec2023-06-09 01:26:37 -05001478 result = runCmd("%s/usr/sbin/sfdisk --part-label %s 1" % (sysroot, image_path))
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001479 self.assertEqual('boot-A', result.output)
Patrick Williamsb542dec2023-06-09 01:26:37 -05001480 result = runCmd("%s/usr/sbin/sfdisk --part-label %s 2" % (sysroot, image_path))
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001481 self.assertEqual('root-A', result.output)
1482 # When the --part-name is not defined, the partition name is equal to the --label
Patrick Williamsb542dec2023-06-09 01:26:37 -05001483 result = runCmd("%s/usr/sbin/sfdisk --part-label %s 3" % (sysroot, image_path))
Patrick Williams8e7b46e2023-05-01 14:19:06 -05001484 self.assertEqual('ext-space', result.output)
1485
Patrick Williams45852732022-04-02 08:58:32 -05001486class ModifyTests(WicTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001487 def test_wic_ls(self):
1488 """Test listing image content using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001489 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001490 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001491 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001492 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001493 self.assertEqual(1, len(images))
1494
1495 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1496
1497 # list partitions
1498 result = runCmd("wic ls %s -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001499 self.assertEqual(3, len(result.output.split('\n')))
1500
1501 # list directory content of the first partition
1502 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001503 self.assertEqual(6, len(result.output.split('\n')))
1504
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001505 def test_wic_cp(self):
1506 """Test copy files and directories to the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001507 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001508 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001509 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001510 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001511 self.assertEqual(1, len(images))
1512
1513 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1514
1515 # list directory content of the first partition
1516 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001517 self.assertEqual(6, len(result.output.split('\n')))
1518
1519 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1520 testfile.write("test")
1521
1522 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001523 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001524
1525 # check if file is there
1526 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001527 self.assertEqual(7, len(result.output.split('\n')))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001528 self.assertIn(os.path.basename(testfile.name), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001529
1530 # prepare directory
1531 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir')
1532 testsubdir = os.path.join(testdir, 'subdir')
1533 os.makedirs(os.path.join(testsubdir))
1534 copy(testfile.name, testdir)
1535
1536 # copy directory to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001537 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001538
1539 # check if directory is there
1540 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001541 self.assertEqual(8, len(result.output.split('\n')))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001542 self.assertIn(os.path.basename(testdir), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001543
Andrew Geissler82c905d2020-04-13 13:39:40 -05001544 # copy the file from the partition and check if it success
1545 dest = '%s-cp' % testfile.name
1546 runCmd("wic cp %s:1/%s %s -n %s" % (images[0],
1547 os.path.basename(testfile.name), dest, sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001548 self.assertTrue(os.path.exists(dest), msg="File %s wasn't generated as expected" % dest)
Andrew Geissler82c905d2020-04-13 13:39:40 -05001549
1550
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001551 def test_wic_rm(self):
1552 """Test removing files and directories from the the wic image."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001553 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001554 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001555 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001556 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001557 self.assertEqual(1, len(images))
1558
1559 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
Patrick Williams45852732022-04-02 08:58:32 -05001560 # Not bulletproof but hopefully sufficient
1561 kerneltype = get_bb_var('KERNEL_IMAGETYPE', 'virtual/kernel')
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001562
1563 # list directory content of the first partition
1564 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Patrick Williams92b42cb2022-09-03 06:53:57 -05001565 self.assertIn('\n%s ' % kerneltype.upper(), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001566 self.assertIn('\nEFI <DIR> ', result.output)
1567
Patrick Williams45852732022-04-02 08:58:32 -05001568 # remove file. EFI partitions are case-insensitive so exercise that too
1569 runCmd("wic rm %s:1/%s -n %s" % (images[0], kerneltype.lower(), sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001570
1571 # remove directory
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001572 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001573
1574 # check if they're removed
1575 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot))
Patrick Williams45852732022-04-02 08:58:32 -05001576 self.assertNotIn('\n%s ' % kerneltype.upper(), result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001577 self.assertNotIn('\nEFI <DIR> ', result.output)
1578
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001579 def test_wic_ls_ext(self):
1580 """Test listing content of the ext partition using 'wic ls'"""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001581 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001582 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001583 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001584 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001585 self.assertEqual(1, len(images))
1586
1587 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1588
1589 # list directory content of the second ext4 partition
1590 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001591 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(
Andrew Geissler220dafd2023-10-04 10:18:08 -05001592 set(line.split()[-1] for line in result.output.split('\n') if line)), msg="Expected directories not present %s" % result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001593
1594 def test_wic_cp_ext(self):
1595 """Test copy files and directories to the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001596 runCmd("wic create wictestdisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001597 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001598 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001599 images = glob(os.path.join(self.resultdir, "wictestdisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001600 self.assertEqual(1, len(images))
1601
1602 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1603
1604 # list directory content of the ext4 partition
1605 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001606 dirs = set(line.split()[-1] for line in result.output.split('\n') if line)
Andrew Geissler220dafd2023-10-04 10:18:08 -05001607 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs), msg="Expected directories not present %s" % dirs)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001608
1609 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile:
1610 testfile.write("test")
1611
1612 # copy file to the partition
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001613 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001614
1615 # check if file is there
1616 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001617 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line)
1618 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)]))
1619
Andrew Geissler82c905d2020-04-13 13:39:40 -05001620 # check if the file to copy is in the partition
1621 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001622 self.assertIn('fstab', [line.split()[-1] for line in result.output.split('\n') if line])
Andrew Geissler82c905d2020-04-13 13:39:40 -05001623
1624 # copy file from the partition, replace the temporary file content with it and
1625 # check for the file size to validate the copy
1626 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001627 self.assertTrue(os.stat(testfile.name).st_size > 0, msg="Filesize not as expected %s" % os.stat(testfile.name).st_size)
Andrew Geissler82c905d2020-04-13 13:39:40 -05001628
1629
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001630 def test_wic_rm_ext(self):
1631 """Test removing files from the ext partition."""
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001632 runCmd("wic create mkefidisk "
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001633 "--image-name=core-image-minimal "
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001634 "-D -o %s" % self.resultdir)
Patrick Williams45852732022-04-02 08:58:32 -05001635 images = glob(os.path.join(self.resultdir, "mkefidisk-*.direct"))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001636 self.assertEqual(1, len(images))
1637
1638 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools')
1639
1640 # list directory content of the /etc directory on ext4 partition
1641 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001642 self.assertIn('fstab', [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001643
1644 # remove file
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001645 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot))
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001646
1647 # check if it's removed
1648 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001649 self.assertNotIn('fstab', [line.split()[-1] for line in result.output.split('\n') if line])
Brad Bishop6dbb3162019-11-25 09:41:34 -05001650
1651 # remove non-empty directory
1652 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
1653
1654 # check if it's removed
1655 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
Andrew Geissler220dafd2023-10-04 10:18:08 -05001656 self.assertNotIn('etc', [line.split()[-1] for line in result.output.split('\n') if line])