1# 2# Copyright (c) 2015, Intel Corporation. 3# 4# SPDX-License-Identifier: GPL-2.0-only 5# 6# AUTHORS 7# Ed Bartosh <ed.bartosh@linux.intel.com> 8 9"""Test cases for wic.""" 10 11import os 12import sys 13import unittest 14 15from glob import glob 16from shutil import rmtree, copy 17from functools import wraps, lru_cache 18from tempfile import NamedTemporaryFile 19 20from oeqa.selftest.case import OESelftestTestCase 21from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, runqemu 22 23 24@lru_cache(maxsize=32) 25def get_host_arch(recipe): 26 """A cached call to get_bb_var('HOST_ARCH', <recipe>)""" 27 return get_bb_var('HOST_ARCH', recipe) 28 29 30def only_for_arch(archs, image='core-image-minimal'): 31 """Decorator for wrapping test cases that can be run only for specific target 32 architectures. A list of compatible architectures is passed in `archs`. 33 Current architecture will be determined by parsing bitbake output for 34 `image` recipe. 35 """ 36 def wrapper(func): 37 @wraps(func) 38 def wrapped_f(*args, **kwargs): 39 arch = get_host_arch(image) 40 if archs and arch not in archs: 41 raise unittest.SkipTest("Testcase arch dependency not met: %s" % arch) 42 return func(*args, **kwargs) 43 wrapped_f.__name__ = func.__name__ 44 return wrapped_f 45 return wrapper 46 47def extract_files(debugfs_output): 48 """ 49 extract file names from the output of debugfs -R 'ls -p', 50 which looks like this: 51 52 /2/040755/0/0/.//\n 53 /2/040755/0/0/..//\n 54 /11/040700/0/0/lost+found^M//\n 55 /12/040755/1002/1002/run//\n 56 /13/040755/1002/1002/sys//\n 57 /14/040755/1002/1002/bin//\n 58 /80/040755/1002/1002/var//\n 59 /92/040755/1002/1002/tmp//\n 60 """ 61 # NOTE the occasional ^M in file names 62 return [line.split('/')[5].strip() for line in \ 63 debugfs_output.strip().split('/\n')] 64 65def files_own_by_root(debugfs_output): 66 for line in debugfs_output.strip().split('/\n'): 67 if line.split('/')[3:5] != ['0', '0']: 68 print(debugfs_output) 69 return False 70 return True 71 72class WicTestCase(OESelftestTestCase): 73 """Wic test class.""" 74 75 image_is_ready = False 76 wicenv_cache = {} 77 78 def setUpLocal(self): 79 """This code is executed before each test method.""" 80 self.resultdir = self.builddir + "/wic-tmp/" 81 super(WicTestCase, self).setUpLocal() 82 83 # Do this here instead of in setUpClass as the base setUp does some 84 # clean up which can result in the native tools built earlier in 85 # setUpClass being unavailable. 86 if not WicTestCase.image_is_ready: 87 if get_bb_var('USE_NLS') == 'yes': 88 bitbake('wic-tools') 89 else: 90 self.skipTest('wic-tools cannot be built due its (intltool|gettext)-native dependency and NLS disable') 91 92 bitbake('core-image-minimal') 93 bitbake('core-image-minimal-mtdutils') 94 WicTestCase.image_is_ready = True 95 96 rmtree(self.resultdir, ignore_errors=True) 97 98 def tearDownLocal(self): 99 """Remove resultdir as it may contain images.""" 100 rmtree(self.resultdir, ignore_errors=True) 101 super(WicTestCase, self).tearDownLocal() 102 103 def _get_image_env_path(self, image): 104 """Generate and obtain the path to <image>.env""" 105 if image not in WicTestCase.wicenv_cache: 106 self.assertEqual(0, bitbake('%s -c do_rootfs_wicenv' % image).status) 107 bb_vars = get_bb_vars(['STAGING_DIR', 'MACHINE'], image) 108 stdir = bb_vars['STAGING_DIR'] 109 machine = bb_vars['MACHINE'] 110 WicTestCase.wicenv_cache[image] = os.path.join(stdir, machine, 'imgdata') 111 return WicTestCase.wicenv_cache[image] 112 113class Wic(WicTestCase): 114 115 def test_version(self): 116 """Test wic --version""" 117 runCmd('wic --version') 118 119 def test_help(self): 120 """Test wic --help and wic -h""" 121 runCmd('wic --help') 122 runCmd('wic -h') 123 124 def test_createhelp(self): 125 """Test wic create --help""" 126 runCmd('wic create --help') 127 128 def test_listhelp(self): 129 """Test wic list --help""" 130 runCmd('wic list --help') 131 132 def test_help_create(self): 133 """Test wic help create""" 134 runCmd('wic help create') 135 136 def test_help_list(self): 137 """Test wic help list""" 138 runCmd('wic help list') 139 140 def test_help_overview(self): 141 """Test wic help overview""" 142 runCmd('wic help overview') 143 144 def test_help_plugins(self): 145 """Test wic help plugins""" 146 runCmd('wic help plugins') 147 148 def test_help_kickstart(self): 149 """Test wic help kickstart""" 150 runCmd('wic help kickstart') 151 152 def test_list_images(self): 153 """Test wic list images""" 154 runCmd('wic list images') 155 156 def test_list_source_plugins(self): 157 """Test wic list source-plugins""" 158 runCmd('wic list source-plugins') 159 160 def test_listed_images_help(self): 161 """Test wic listed images help""" 162 output = runCmd('wic list images').output 163 imagelist = [line.split()[0] for line in output.splitlines()] 164 for image in imagelist: 165 runCmd('wic list %s help' % image) 166 167 def test_unsupported_subcommand(self): 168 """Test unsupported subcommand""" 169 self.assertNotEqual(0, runCmd('wic unsupported', ignore_status=True).status) 170 171 def test_no_command(self): 172 """Test wic without command""" 173 self.assertEqual(1, runCmd('wic', ignore_status=True).status) 174 175 def test_build_image_name(self): 176 """Test wic create wictestdisk --image-name=core-image-minimal""" 177 cmd = "wic create wictestdisk --image-name=core-image-minimal -o %s" % self.resultdir 178 runCmd(cmd) 179 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 180 181 @only_for_arch(['i586', 'i686', 'x86_64']) 182 def test_gpt_image(self): 183 """Test creation of core-image-minimal with gpt table and UUID boot""" 184 cmd = "wic create directdisk-gpt --image-name core-image-minimal -o %s" % self.resultdir 185 runCmd(cmd) 186 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct"))) 187 188 @only_for_arch(['i586', 'i686', 'x86_64']) 189 def test_iso_image(self): 190 """Test creation of hybrid iso image with legacy and EFI boot""" 191 config = 'INITRAMFS_IMAGE = "core-image-minimal-initramfs"\n'\ 192 'MACHINE_FEATURES_append = " efi"\n'\ 193 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 194 self.append_config(config) 195 bitbake('core-image-minimal core-image-minimal-initramfs') 196 self.remove_config(config) 197 cmd = "wic create mkhybridiso --image-name core-image-minimal -o %s" % self.resultdir 198 runCmd(cmd) 199 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.direct"))) 200 self.assertEqual(1, len(glob(self.resultdir + "HYBRID_ISO_IMG-*.iso"))) 201 202 @only_for_arch(['i586', 'i686', 'x86_64']) 203 def test_qemux86_directdisk(self): 204 """Test creation of qemux-86-directdisk image""" 205 cmd = "wic create qemux86-directdisk -e core-image-minimal -o %s" % self.resultdir 206 runCmd(cmd) 207 self.assertEqual(1, len(glob(self.resultdir + "qemux86-directdisk-*direct"))) 208 209 @only_for_arch(['i586', 'i686', 'x86_64']) 210 def test_mkefidisk(self): 211 """Test creation of mkefidisk image""" 212 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir 213 runCmd(cmd) 214 self.assertEqual(1, len(glob(self.resultdir + "mkefidisk-*direct"))) 215 216 @only_for_arch(['i586', 'i686', 'x86_64']) 217 def test_bootloader_config(self): 218 """Test creation of directdisk-bootloader-config image""" 219 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 220 self.append_config(config) 221 bitbake('core-image-minimal') 222 self.remove_config(config) 223 cmd = "wic create directdisk-bootloader-config -e core-image-minimal -o %s" % self.resultdir 224 runCmd(cmd) 225 self.assertEqual(1, len(glob(self.resultdir + "directdisk-bootloader-config-*direct"))) 226 227 @only_for_arch(['i586', 'i686', 'x86_64']) 228 def test_systemd_bootdisk(self): 229 """Test creation of systemd-bootdisk image""" 230 config = 'MACHINE_FEATURES_append = " efi"\n' 231 self.append_config(config) 232 bitbake('core-image-minimal') 233 self.remove_config(config) 234 cmd = "wic create systemd-bootdisk -e core-image-minimal -o %s" % self.resultdir 235 runCmd(cmd) 236 self.assertEqual(1, len(glob(self.resultdir + "systemd-bootdisk-*direct"))) 237 238 def test_efi_bootpart(self): 239 """Test creation of efi-bootpart image""" 240 cmd = "wic create mkefidisk -e core-image-minimal -o %s" % self.resultdir 241 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal') 242 self.append_config('IMAGE_EFI_BOOT_FILES = "%s;kernel"\n' % kimgtype) 243 runCmd(cmd) 244 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 245 images = glob(self.resultdir + "mkefidisk-*.direct") 246 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 247 self.assertIn("kernel",result.output) 248 249 def test_sdimage_bootpart(self): 250 """Test creation of sdimage-bootpart image""" 251 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir 252 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal') 253 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype) 254 runCmd(cmd) 255 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct"))) 256 257 @only_for_arch(['i586', 'i686', 'x86_64']) 258 def test_default_output_dir(self): 259 """Test default output location""" 260 for fname in glob("directdisk-*.direct"): 261 os.remove(fname) 262 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 263 self.append_config(config) 264 bitbake('core-image-minimal') 265 self.remove_config(config) 266 cmd = "wic create directdisk -e core-image-minimal" 267 runCmd(cmd) 268 self.assertEqual(1, len(glob("directdisk-*.direct"))) 269 270 @only_for_arch(['i586', 'i686', 'x86_64']) 271 def test_build_artifacts(self): 272 """Test wic create directdisk providing all artifacts.""" 273 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'], 274 'wic-tools') 275 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'], 276 'core-image-minimal')) 277 bbvars = {key.lower(): value for key, value in bb_vars.items()} 278 bbvars['resultdir'] = self.resultdir 279 runCmd("wic create directdisk " 280 "-b %(staging_datadir)s " 281 "-k %(deploy_dir_image)s " 282 "-n %(recipe_sysroot_native)s " 283 "-r %(image_rootfs)s " 284 "-o %(resultdir)s" % bbvars) 285 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct"))) 286 287 def test_compress_gzip(self): 288 """Test compressing an image with gzip""" 289 runCmd("wic create wictestdisk " 290 "--image-name core-image-minimal " 291 "-c gzip -o %s" % self.resultdir) 292 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz"))) 293 294 def test_compress_bzip2(self): 295 """Test compressing an image with bzip2""" 296 runCmd("wic create wictestdisk " 297 "--image-name=core-image-minimal " 298 "-c bzip2 -o %s" % self.resultdir) 299 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2"))) 300 301 def test_compress_xz(self): 302 """Test compressing an image with xz""" 303 runCmd("wic create wictestdisk " 304 "--image-name=core-image-minimal " 305 "--compress-with=xz -o %s" % self.resultdir) 306 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz"))) 307 308 def test_wrong_compressor(self): 309 """Test how wic breaks if wrong compressor is provided""" 310 self.assertEqual(2, runCmd("wic create wictestdisk " 311 "--image-name=core-image-minimal " 312 "-c wrong -o %s" % self.resultdir, 313 ignore_status=True).status) 314 315 def test_debug_short(self): 316 """Test -D option""" 317 runCmd("wic create wictestdisk " 318 "--image-name=core-image-minimal " 319 "-D -o %s" % self.resultdir) 320 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 321 322 def test_debug_long(self): 323 """Test --debug option""" 324 runCmd("wic create wictestdisk " 325 "--image-name=core-image-minimal " 326 "--debug -o %s" % self.resultdir) 327 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 328 329 def test_skip_build_check_short(self): 330 """Test -s option""" 331 runCmd("wic create wictestdisk " 332 "--image-name=core-image-minimal " 333 "-s -o %s" % self.resultdir) 334 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 335 336 def test_skip_build_check_long(self): 337 """Test --skip-build-check option""" 338 runCmd("wic create wictestdisk " 339 "--image-name=core-image-minimal " 340 "--skip-build-check " 341 "--outdir %s" % self.resultdir) 342 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 343 344 def test_build_rootfs_short(self): 345 """Test -f option""" 346 runCmd("wic create wictestdisk " 347 "--image-name=core-image-minimal " 348 "-f -o %s" % self.resultdir) 349 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 350 351 def test_build_rootfs_long(self): 352 """Test --build-rootfs option""" 353 runCmd("wic create wictestdisk " 354 "--image-name=core-image-minimal " 355 "--build-rootfs " 356 "--outdir %s" % self.resultdir) 357 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 358 359 @only_for_arch(['i586', 'i686', 'x86_64']) 360 def test_rootfs_indirect_recipes(self): 361 """Test usage of rootfs plugin with rootfs recipes""" 362 runCmd("wic create directdisk-multi-rootfs " 363 "--image-name=core-image-minimal " 364 "--rootfs rootfs1=core-image-minimal " 365 "--rootfs rootfs2=core-image-minimal " 366 "--outdir %s" % self.resultdir) 367 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct"))) 368 369 @only_for_arch(['i586', 'i686', 'x86_64']) 370 def test_rootfs_artifacts(self): 371 """Test usage of rootfs plugin with rootfs paths""" 372 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'], 373 'wic-tools') 374 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'], 375 'core-image-minimal')) 376 bbvars = {key.lower(): value for key, value in bb_vars.items()} 377 bbvars['wks'] = "directdisk-multi-rootfs" 378 bbvars['resultdir'] = self.resultdir 379 runCmd("wic create %(wks)s " 380 "--bootimg-dir=%(staging_datadir)s " 381 "--kernel-dir=%(deploy_dir_image)s " 382 "--native-sysroot=%(recipe_sysroot_native)s " 383 "--rootfs-dir rootfs1=%(image_rootfs)s " 384 "--rootfs-dir rootfs2=%(image_rootfs)s " 385 "--outdir %(resultdir)s" % bbvars) 386 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars))) 387 388 def test_exclude_path(self): 389 """Test --exclude-path wks option.""" 390 391 oldpath = os.environ['PATH'] 392 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 393 394 try: 395 wks_file = 'temp.wks' 396 with open(wks_file, 'w') as wks: 397 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal') 398 wks.write(""" 399part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr 400part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr 401part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr""" 402 % (rootfs_dir, rootfs_dir)) 403 runCmd("wic create %s -e core-image-minimal -o %s" \ 404 % (wks_file, self.resultdir)) 405 406 os.remove(wks_file) 407 wicout = glob(self.resultdir + "%s-*direct" % 'temp') 408 self.assertEqual(1, len(wicout)) 409 410 wicimg = wicout[0] 411 412 # verify partition size with wic 413 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg) 414 415 # parse parted output which looks like this: 416 # BYT;\n 417 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n 418 # 1:0.00MiB:200MiB:200MiB:ext4::;\n 419 partlns = res.output.splitlines()[2:] 420 421 self.assertEqual(3, len(partlns)) 422 423 for part in [1, 2, 3]: 424 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part) 425 partln = partlns[part-1].split(":") 426 self.assertEqual(7, len(partln)) 427 start = int(partln[1].rstrip("B")) / 512 428 length = int(partln[3].rstrip("B")) / 512 429 runCmd("dd if=%s of=%s skip=%d count=%d" % 430 (wicimg, part_file, start, length)) 431 432 # Test partition 1, should contain the normal root directories, except 433 # /usr. 434 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 435 os.path.join(self.resultdir, "selftest_img.part1")) 436 files = extract_files(res.output) 437 self.assertIn("etc", files) 438 self.assertNotIn("usr", files) 439 440 # Partition 2, should contain common directories for /usr, not root 441 # directories. 442 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 443 os.path.join(self.resultdir, "selftest_img.part2")) 444 files = extract_files(res.output) 445 self.assertNotIn("etc", files) 446 self.assertNotIn("usr", files) 447 self.assertIn("share", files) 448 449 # Partition 3, should contain the same as partition 2, including the bin 450 # directory, but not the files inside it. 451 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 452 os.path.join(self.resultdir, "selftest_img.part3")) 453 files = extract_files(res.output) 454 self.assertNotIn("etc", files) 455 self.assertNotIn("usr", files) 456 self.assertIn("share", files) 457 self.assertIn("bin", files) 458 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \ 459 os.path.join(self.resultdir, "selftest_img.part3")) 460 files = extract_files(res.output) 461 self.assertIn(".", files) 462 self.assertIn("..", files) 463 self.assertEqual(2, len(files)) 464 465 for part in [1, 2, 3]: 466 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part) 467 os.remove(part_file) 468 469 finally: 470 os.environ['PATH'] = oldpath 471 472 def test_include_path(self): 473 """Test --include-path wks option.""" 474 475 oldpath = os.environ['PATH'] 476 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 477 478 try: 479 include_path = os.path.join(self.resultdir, 'test-include') 480 os.makedirs(include_path) 481 with open(os.path.join(include_path, 'test-file'), 'w') as t: 482 t.write("test\n") 483 wks_file = os.path.join(include_path, 'temp.wks') 484 with open(wks_file, 'w') as wks: 485 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal') 486 wks.write(""" 487part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4 488part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s""" 489 % (include_path)) 490 runCmd("wic create %s -e core-image-minimal -o %s" \ 491 % (wks_file, self.resultdir)) 492 493 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] 494 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0] 495 496 # Test partition 1, should not contain 'test-file' 497 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) 498 files = extract_files(res.output) 499 self.assertNotIn('test-file', files) 500 self.assertEqual(True, files_own_by_root(res.output)) 501 502 # Test partition 2, should contain 'test-file' 503 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2)) 504 files = extract_files(res.output) 505 self.assertIn('test-file', files) 506 self.assertEqual(True, files_own_by_root(res.output)) 507 508 finally: 509 os.environ['PATH'] = oldpath 510 511 def test_include_path_embeded(self): 512 """Test --include-path wks option.""" 513 514 oldpath = os.environ['PATH'] 515 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 516 517 try: 518 include_path = os.path.join(self.resultdir, 'test-include') 519 os.makedirs(include_path) 520 with open(os.path.join(include_path, 'test-file'), 'w') as t: 521 t.write("test\n") 522 wks_file = os.path.join(include_path, 'temp.wks') 523 with open(wks_file, 'w') as wks: 524 wks.write(""" 525part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/""" 526 % (include_path)) 527 runCmd("wic create %s -e core-image-minimal -o %s" \ 528 % (wks_file, self.resultdir)) 529 530 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] 531 532 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) 533 files = extract_files(res.output) 534 self.assertIn('test-file', files) 535 self.assertEqual(True, files_own_by_root(res.output)) 536 537 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1)) 538 files = extract_files(res.output) 539 self.assertIn('passwd', files) 540 self.assertEqual(True, files_own_by_root(res.output)) 541 542 finally: 543 os.environ['PATH'] = oldpath 544 545 def test_include_path_errors(self): 546 """Test --include-path wks option error handling.""" 547 wks_file = 'temp.wks' 548 549 # Absolute argument. 550 with open(wks_file, 'w') as wks: 551 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export") 552 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 553 % (wks_file, self.resultdir), ignore_status=True).status) 554 os.remove(wks_file) 555 556 # Argument pointing to parent directory. 557 with open(wks_file, 'w') as wks: 558 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..") 559 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 560 % (wks_file, self.resultdir), ignore_status=True).status) 561 os.remove(wks_file) 562 563 # 3 Argument pointing to parent directory. 564 with open(wks_file, 'w') as wks: 565 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy") 566 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 567 % (wks_file, self.resultdir), ignore_status=True).status) 568 os.remove(wks_file) 569 570 def test_exclude_path_errors(self): 571 """Test --exclude-path wks option error handling.""" 572 wks_file = 'temp.wks' 573 574 # Absolute argument. 575 with open(wks_file, 'w') as wks: 576 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr") 577 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 578 % (wks_file, self.resultdir), ignore_status=True).status) 579 os.remove(wks_file) 580 581 # Argument pointing to parent directory. 582 with open(wks_file, 'w') as wks: 583 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..") 584 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 585 % (wks_file, self.resultdir), ignore_status=True).status) 586 os.remove(wks_file) 587 588 def test_permissions(self): 589 """Test permissions are respected""" 590 591 oldpath = os.environ['PATH'] 592 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 593 594 t_normal = """ 595part / --source rootfs --fstype=ext4 596""" 597 t_exclude = """ 598part / --source rootfs --fstype=ext4 --exclude-path=home 599""" 600 t_multi = """ 601part / --source rootfs --ondisk sda --fstype=ext4 602part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4 603""" 604 t_change = """ 605part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/ 606part /etc --source rootfs --fstype=ext4 --change-directory=etc 607""" 608 tests = [t_normal, t_exclude, t_multi, t_change] 609 610 try: 611 for test in tests: 612 include_path = os.path.join(self.resultdir, 'test-include') 613 os.makedirs(include_path) 614 wks_file = os.path.join(include_path, 'temp.wks') 615 with open(wks_file, 'w') as wks: 616 wks.write(test) 617 runCmd("wic create %s -e core-image-minimal -o %s" \ 618 % (wks_file, self.resultdir)) 619 620 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')): 621 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part)) 622 self.assertEqual(True, files_own_by_root(res.output)) 623 624 rmtree(self.resultdir, ignore_errors=True) 625 626 finally: 627 os.environ['PATH'] = oldpath 628 629 def test_change_directory(self): 630 """Test --change-directory wks option.""" 631 632 oldpath = os.environ['PATH'] 633 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 634 635 try: 636 include_path = os.path.join(self.resultdir, 'test-include') 637 os.makedirs(include_path) 638 wks_file = os.path.join(include_path, 'temp.wks') 639 with open(wks_file, 'w') as wks: 640 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc") 641 runCmd("wic create %s -e core-image-minimal -o %s" \ 642 % (wks_file, self.resultdir)) 643 644 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] 645 646 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) 647 files = extract_files(res.output) 648 self.assertIn('passwd', files) 649 650 finally: 651 os.environ['PATH'] = oldpath 652 653 def test_change_directory_errors(self): 654 """Test --change-directory wks option error handling.""" 655 wks_file = 'temp.wks' 656 657 # Absolute argument. 658 with open(wks_file, 'w') as wks: 659 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr") 660 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 661 % (wks_file, self.resultdir), ignore_status=True).status) 662 os.remove(wks_file) 663 664 # Argument pointing to parent directory. 665 with open(wks_file, 'w') as wks: 666 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..") 667 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 668 % (wks_file, self.resultdir), ignore_status=True).status) 669 os.remove(wks_file) 670 671class Wic2(WicTestCase): 672 673 def test_bmap_short(self): 674 """Test generation of .bmap file -m option""" 675 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir 676 runCmd(cmd) 677 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 678 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap"))) 679 680 def test_bmap_long(self): 681 """Test generation of .bmap file --bmap option""" 682 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir 683 runCmd(cmd) 684 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 685 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap"))) 686 687 def test_image_env(self): 688 """Test generation of <image>.env files.""" 689 image = 'core-image-minimal' 690 imgdatadir = self._get_image_env_path(image) 691 692 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image) 693 basename = bb_vars['IMAGE_BASENAME'] 694 self.assertEqual(basename, image) 695 path = os.path.join(imgdatadir, basename) + '.env' 696 self.assertTrue(os.path.isfile(path)) 697 698 wicvars = set(bb_vars['WICVARS'].split()) 699 # filter out optional variables 700 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES', 701 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE', 702 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME', 703 'APPEND', 'IMAGE_EFI_BOOT_FILES')) 704 with open(path) as envfile: 705 content = dict(line.split("=", 1) for line in envfile) 706 # test if variables used by wic present in the .env file 707 for var in wicvars: 708 self.assertTrue(var in content, "%s is not in .env file" % var) 709 self.assertTrue(content[var]) 710 711 def test_image_vars_dir_short(self): 712 """Test image vars directory selection -v option""" 713 image = 'core-image-minimal' 714 imgenvdir = self._get_image_env_path(image) 715 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 716 717 runCmd("wic create wictestdisk " 718 "--image-name=%s -v %s -n %s -o %s" 719 % (image, imgenvdir, native_sysroot, 720 self.resultdir)) 721 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 722 723 def test_image_vars_dir_long(self): 724 """Test image vars directory selection --vars option""" 725 image = 'core-image-minimal' 726 imgenvdir = self._get_image_env_path(image) 727 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 728 729 runCmd("wic create wictestdisk " 730 "--image-name=%s " 731 "--vars %s " 732 "--native-sysroot %s " 733 "--outdir %s" 734 % (image, imgenvdir, native_sysroot, 735 self.resultdir)) 736 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 737 738 @only_for_arch(['i586', 'i686', 'x86_64']) 739 def test_wic_image_type(self): 740 """Test building wic images by bitbake""" 741 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\ 742 'MACHINE_FEATURES_append = " efi"\n' 743 self.append_config(config) 744 self.assertEqual(0, bitbake('wic-image-minimal').status) 745 self.remove_config(config) 746 747 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 748 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 749 machine = bb_vars['MACHINE'] 750 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine) 751 # check if we have result image and manifests symlinks 752 # pointing to existing files 753 for suffix in ('wic', 'manifest'): 754 path = prefix + suffix 755 self.assertTrue(os.path.islink(path)) 756 self.assertTrue(os.path.isfile(os.path.realpath(path))) 757 758 @only_for_arch(['i586', 'i686', 'x86_64']) 759 def test_qemu(self): 760 """Test wic-image-minimal under qemu""" 761 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\ 762 'MACHINE_FEATURES_append = " efi"\n' 763 self.append_config(config) 764 self.assertEqual(0, bitbake('wic-image-minimal').status) 765 self.remove_config(config) 766 767 with runqemu('wic-image-minimal', ssh=False) as qemu: 768 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \ 769 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'" 770 status, output = qemu.run_serial(cmd) 771 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 772 self.assertEqual(output, '4') 773 cmd = "grep UUID= /etc/fstab" 774 status, output = qemu.run_serial(cmd) 775 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 776 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0') 777 778 @only_for_arch(['i586', 'i686', 'x86_64']) 779 def test_qemu_efi(self): 780 """Test core-image-minimal efi image under qemu""" 781 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n' 782 self.append_config(config) 783 self.assertEqual(0, bitbake('core-image-minimal ovmf').status) 784 self.remove_config(config) 785 786 with runqemu('core-image-minimal', ssh=False, 787 runqemuparams='ovmf', image_fstype='wic') as qemu: 788 cmd = "grep sda. /proc/partitions |wc -l" 789 status, output = qemu.run_serial(cmd) 790 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 791 self.assertEqual(output, '3') 792 793 @staticmethod 794 def _make_fixed_size_wks(size): 795 """ 796 Create a wks of an image with a single partition. Size of the partition is set 797 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name) 798 """ 799 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf: 800 wkspath = tempf.name 801 tempf.write("part " \ 802 "--source rootfs --ondisk hda --align 4 --fixed-size %d " 803 "--fstype=ext4\n" % size) 804 805 return wkspath 806 807 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False): 808 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir), 809 ignore_status=ignore_status) 810 811 if p.status: 812 return (p, None) 813 814 wksname = os.path.splitext(os.path.basename(wkspath))[0] 815 816 wicout = glob(self.resultdir + "%s-*direct" % wksname) 817 818 if not wicout: 819 return (p, None) 820 821 wicimg = wicout[0] 822 823 if not native_sysroot: 824 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 825 826 # verify partition size with wic 827 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg, 828 native_sysroot=native_sysroot) 829 830 # parse parted output which looks like this: 831 # BYT;\n 832 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n 833 # 1:0.00MiB:200MiB:200MiB:ext4::;\n 834 return (p, res.output.splitlines()[2:]) 835 836 def test_fixed_size(self): 837 """ 838 Test creation of a simple image with partition size controlled through 839 --fixed-size flag 840 """ 841 wkspath = Wic2._make_fixed_size_wks(200) 842 _, partlns = self._get_wic_partitions(wkspath) 843 os.remove(wkspath) 844 845 self.assertEqual(partlns, [ 846 "1:4.00kiB:204804kiB:204800kiB:ext4::;", 847 ]) 848 849 def test_fixed_size_error(self): 850 """ 851 Test creation of a simple image with partition size controlled through 852 --fixed-size flag. The size of partition is intentionally set to 1MiB 853 in order to trigger an error in wic. 854 """ 855 wkspath = Wic2._make_fixed_size_wks(1) 856 p, _ = self._get_wic_partitions(wkspath, ignore_status=True) 857 os.remove(wkspath) 858 859 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output) 860 861 def test_offset(self): 862 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 863 864 with NamedTemporaryFile("w", suffix=".wks") as tempf: 865 # Test that partitions are placed at the correct offsets, default KB 866 tempf.write("bootloader --ptable gpt\n" \ 867 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \ 868 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n") 869 tempf.flush() 870 871 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 872 self.assertEqual(partlns, [ 873 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;", 874 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;", 875 ]) 876 877 with NamedTemporaryFile("w", suffix=".wks") as tempf: 878 # Test that partitions are placed at the correct offsets, same with explicit KB 879 tempf.write("bootloader --ptable gpt\n" \ 880 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \ 881 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n") 882 tempf.flush() 883 884 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 885 self.assertEqual(partlns, [ 886 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;", 887 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;", 888 ]) 889 890 with NamedTemporaryFile("w", suffix=".wks") as tempf: 891 # Test that partitions are placed at the correct offsets using MB 892 tempf.write("bootloader --ptable gpt\n" \ 893 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \ 894 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n") 895 tempf.flush() 896 897 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 898 self.assertEqual(partlns, [ 899 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;", 900 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;", 901 ]) 902 903 with NamedTemporaryFile("w", suffix=".wks") as tempf: 904 # Test that partitions can be placed on a 512 byte sector boundary 905 tempf.write("bootloader --ptable gpt\n" \ 906 "part / --source rootfs --ondisk hda --offset 65s --fixed-size 99M --fstype=ext4\n" \ 907 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n") 908 tempf.flush() 909 910 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 911 self.assertEqual(partlns, [ 912 "1:32.5kiB:101408kiB:101376kiB:ext4:primary:;", 913 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;", 914 ]) 915 916 with NamedTemporaryFile("w", suffix=".wks") as tempf: 917 # Test that a partition can be placed immediately after a MSDOS partition table 918 tempf.write("bootloader --ptable msdos\n" \ 919 "part / --source rootfs --ondisk hda --offset 1s --fixed-size 100M --fstype=ext4\n") 920 tempf.flush() 921 922 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 923 self.assertEqual(partlns, [ 924 "1:0.50kiB:102400kiB:102400kiB:ext4::;", 925 ]) 926 927 with NamedTemporaryFile("w", suffix=".wks") as tempf: 928 # Test that image creation fails if the partitions would overlap 929 tempf.write("bootloader --ptable gpt\n" \ 930 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \ 931 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n") 932 tempf.flush() 933 934 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True) 935 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output) 936 937 with NamedTemporaryFile("w", suffix=".wks") as tempf: 938 # Test that partitions are not allowed to overlap with the booloader 939 tempf.write("bootloader --ptable gpt\n" \ 940 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n") 941 tempf.flush() 942 943 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True) 944 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output) 945 946 def test_extra_space(self): 947 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 948 949 with NamedTemporaryFile("w", suffix=".wks") as tempf: 950 tempf.write("bootloader --ptable gpt\n" \ 951 "part / --source rootfs --ondisk hda --extra-space 200M --fstype=ext4\n") 952 tempf.flush() 953 954 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 955 self.assertEqual(len(partlns), 1) 956 size = partlns[0].split(':')[3] 957 self.assertRegex(size, r'^[0-9]+kiB$') 958 size = int(size[:-3]) 959 self.assertGreaterEqual(size, 204800) 960 961 @only_for_arch(['i586', 'i686', 'x86_64']) 962 def test_rawcopy_plugin_qemu(self): 963 """Test rawcopy plugin in qemu""" 964 # build ext4 and wic images 965 for fstype in ("ext4", "wic"): 966 config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype 967 self.append_config(config) 968 self.assertEqual(0, bitbake('core-image-minimal').status) 969 self.remove_config(config) 970 971 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu: 972 cmd = "grep sda. /proc/partitions |wc -l" 973 status, output = qemu.run_serial(cmd) 974 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 975 self.assertEqual(output, '2') 976 977 def test_rawcopy_plugin(self): 978 """Test rawcopy plugin""" 979 img = 'core-image-minimal' 980 machine = get_bb_var('MACHINE', img) 981 with NamedTemporaryFile("w", suffix=".wks") as wks: 982 wks.writelines(['part /boot --active --source bootimg-pcbios\n', 983 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\ 984 % (img, machine), 985 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n']) 986 wks.flush() 987 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 988 runCmd(cmd) 989 wksname = os.path.splitext(os.path.basename(wks.name))[0] 990 out = glob(self.resultdir + "%s-*direct" % wksname) 991 self.assertEqual(1, len(out)) 992 993 def test_empty_plugin(self): 994 """Test empty plugin""" 995 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_empty_plugin.wks"\n' 996 self.append_config(config) 997 self.assertEqual(0, bitbake('core-image-minimal').status) 998 self.remove_config(config) 999 1000 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 1001 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 1002 machine = bb_vars['MACHINE'] 1003 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine) 1004 self.assertEqual(True, os.path.exists(image_path)) 1005 1006 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1007 1008 # Fstype column from 'wic ls' should be empty for the second partition 1009 # as listed in test_empty_plugin.wks 1010 result = runCmd("wic ls %s -n %s | awk -F ' ' '{print $1 \" \" $5}' | grep '^2' | wc -w" % (image_path, sysroot)) 1011 self.assertEqual('1', result.output) 1012 1013 @only_for_arch(['i586', 'i686', 'x86_64']) 1014 def test_biosplusefi_plugin_qemu(self): 1015 """Test biosplusefi plugin in qemu""" 1016 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n' 1017 self.append_config(config) 1018 self.assertEqual(0, bitbake('core-image-minimal').status) 1019 self.remove_config(config) 1020 1021 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu: 1022 # Check that we have ONLY two /dev/sda* partitions (/boot and /) 1023 cmd = "grep sda. /proc/partitions | wc -l" 1024 status, output = qemu.run_serial(cmd) 1025 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 1026 self.assertEqual(output, '2') 1027 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is / 1028 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'" 1029 status, output = qemu.run_serial(cmd) 1030 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 1031 self.assertEqual(output, '2') 1032 # Check that /boot has EFI bootx64.efi (required for EFI) 1033 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l" 1034 status, output = qemu.run_serial(cmd) 1035 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 1036 self.assertEqual(output, '1') 1037 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios) 1038 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command 1039 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat" 1040 status, output = qemu.run_serial(cmd) 1041 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 1042 self.assertEqual(output, '*') 1043 1044 @only_for_arch(['i586', 'i686', 'x86_64']) 1045 def test_biosplusefi_plugin(self): 1046 """Test biosplusefi plugin""" 1047 # Wic generation below may fail depending on the order of the unittests 1048 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory 1049 # which may or may not exists depending on what was built already 1050 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir() 1051 # will raise with "Couldn't find correct bootimg_dir" 1052 # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call 1053 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n' 1054 self.append_config(config) 1055 self.assertEqual(0, bitbake('core-image-minimal').status) 1056 self.remove_config(config) 1057 1058 img = 'core-image-minimal' 1059 with NamedTemporaryFile("w", suffix=".wks") as wks: 1060 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n', 1061 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\ 1062 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n']) 1063 wks.flush() 1064 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 1065 runCmd(cmd) 1066 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1067 out = glob(self.resultdir + "%s-*.direct" % wksname) 1068 self.assertEqual(1, len(out)) 1069 1070 def test_fs_types(self): 1071 """Test filesystem types for empty and not empty partitions""" 1072 img = 'core-image-minimal' 1073 with NamedTemporaryFile("w", suffix=".wks") as wks: 1074 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n', 1075 'part btrfs --fstype btrfs --source rootfs --size 40M\n', 1076 'part squash --fstype squashfs --source rootfs\n', 1077 'part swap --fstype swap --size 1M\n', 1078 'part emptyvfat --fstype vfat --size 1M\n', 1079 'part emptymsdos --fstype msdos --size 1M\n', 1080 'part emptyext2 --fstype ext2 --size 1M\n', 1081 'part emptybtrfs --fstype btrfs --size 150M\n']) 1082 wks.flush() 1083 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 1084 runCmd(cmd) 1085 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1086 out = glob(self.resultdir + "%s-*direct" % wksname) 1087 self.assertEqual(1, len(out)) 1088 1089 def test_kickstart_parser(self): 1090 """Test wks parser options""" 1091 with NamedTemporaryFile("w", suffix=".wks") as wks: 1092 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\ 1093 '--overhead-factor 1.2 --size 100k\n']) 1094 wks.flush() 1095 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir) 1096 runCmd(cmd) 1097 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1098 out = glob(self.resultdir + "%s-*direct" % wksname) 1099 self.assertEqual(1, len(out)) 1100 1101 def test_image_bootpart_globbed(self): 1102 """Test globbed sources with image-bootpart plugin""" 1103 img = "core-image-minimal" 1104 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir) 1105 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img) 1106 self.append_config(config) 1107 runCmd(cmd) 1108 self.remove_config(config) 1109 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct"))) 1110 1111 def test_sparse_copy(self): 1112 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs""" 1113 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic') 1114 sys.path.insert(0, libpath) 1115 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp 1116 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse: 1117 src_name = sparse.name 1118 src_size = 1024 * 10 1119 sparse.truncate(src_size) 1120 # write one byte to the file 1121 with open(src_name, 'r+b') as sfile: 1122 sfile.seek(1024 * 4) 1123 sfile.write(b'\x00') 1124 dest = sparse.name + '.out' 1125 # copy src file to dest using different filemap APIs 1126 for api in (FilemapFiemap, FilemapSeek, None): 1127 if os.path.exists(dest): 1128 os.unlink(dest) 1129 try: 1130 sparse_copy(sparse.name, dest, api=api) 1131 except ErrorNotSupp: 1132 continue # skip unsupported API 1133 dest_stat = os.stat(dest) 1134 self.assertEqual(dest_stat.st_size, src_size) 1135 # 8 blocks is 4K (physical sector size) 1136 self.assertEqual(dest_stat.st_blocks, 8) 1137 os.unlink(dest) 1138 1139 def test_wic_ls(self): 1140 """Test listing image content using 'wic ls'""" 1141 runCmd("wic create wictestdisk " 1142 "--image-name=core-image-minimal " 1143 "-D -o %s" % self.resultdir) 1144 images = glob(self.resultdir + "wictestdisk-*.direct") 1145 self.assertEqual(1, len(images)) 1146 1147 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1148 1149 # list partitions 1150 result = runCmd("wic ls %s -n %s" % (images[0], sysroot)) 1151 self.assertEqual(3, len(result.output.split('\n'))) 1152 1153 # list directory content of the first partition 1154 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1155 self.assertEqual(6, len(result.output.split('\n'))) 1156 1157 def test_wic_cp(self): 1158 """Test copy files and directories to the the wic image.""" 1159 runCmd("wic create wictestdisk " 1160 "--image-name=core-image-minimal " 1161 "-D -o %s" % self.resultdir) 1162 images = glob(self.resultdir + "wictestdisk-*.direct") 1163 self.assertEqual(1, len(images)) 1164 1165 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1166 1167 # list directory content of the first partition 1168 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1169 self.assertEqual(6, len(result.output.split('\n'))) 1170 1171 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile: 1172 testfile.write("test") 1173 1174 # copy file to the partition 1175 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot)) 1176 1177 # check if file is there 1178 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1179 self.assertEqual(7, len(result.output.split('\n'))) 1180 self.assertTrue(os.path.basename(testfile.name) in result.output) 1181 1182 # prepare directory 1183 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir') 1184 testsubdir = os.path.join(testdir, 'subdir') 1185 os.makedirs(os.path.join(testsubdir)) 1186 copy(testfile.name, testdir) 1187 1188 # copy directory to the partition 1189 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot)) 1190 1191 # check if directory is there 1192 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1193 self.assertEqual(8, len(result.output.split('\n'))) 1194 self.assertTrue(os.path.basename(testdir) in result.output) 1195 1196 # copy the file from the partition and check if it success 1197 dest = '%s-cp' % testfile.name 1198 runCmd("wic cp %s:1/%s %s -n %s" % (images[0], 1199 os.path.basename(testfile.name), dest, sysroot)) 1200 self.assertTrue(os.path.exists(dest)) 1201 1202 1203 def test_wic_rm(self): 1204 """Test removing files and directories from the the wic image.""" 1205 runCmd("wic create mkefidisk " 1206 "--image-name=core-image-minimal " 1207 "-D -o %s" % self.resultdir) 1208 images = glob(self.resultdir + "mkefidisk-*.direct") 1209 self.assertEqual(1, len(images)) 1210 1211 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1212 1213 # list directory content of the first partition 1214 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot)) 1215 self.assertIn('\nBZIMAGE ', result.output) 1216 self.assertIn('\nEFI <DIR> ', result.output) 1217 1218 # remove file 1219 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot)) 1220 1221 # remove directory 1222 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot)) 1223 1224 # check if they're removed 1225 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot)) 1226 self.assertNotIn('\nBZIMAGE ', result.output) 1227 self.assertNotIn('\nEFI <DIR> ', result.output) 1228 1229 def test_mkfs_extraopts(self): 1230 """Test wks option --mkfs-extraopts for empty and not empty partitions""" 1231 img = 'core-image-minimal' 1232 with NamedTemporaryFile("w", suffix=".wks") as wks: 1233 wks.writelines( 1234 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n', 1235 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n", 1236 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n', 1237 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n', 1238 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n', 1239 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n', 1240 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n']) 1241 wks.flush() 1242 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 1243 runCmd(cmd) 1244 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1245 out = glob(self.resultdir + "%s-*direct" % wksname) 1246 self.assertEqual(1, len(out)) 1247 1248 def test_expand_mbr_image(self): 1249 """Test wic write --expand command for mbr image""" 1250 # build an image 1251 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n' 1252 self.append_config(config) 1253 self.assertEqual(0, bitbake('core-image-minimal').status) 1254 1255 # get path to the image 1256 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 1257 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 1258 machine = bb_vars['MACHINE'] 1259 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine) 1260 1261 self.remove_config(config) 1262 1263 try: 1264 # expand image to 1G 1265 new_image_path = None 1266 with NamedTemporaryFile(mode='wb', suffix='.wic.exp', 1267 dir=deploy_dir, delete=False) as sparse: 1268 sparse.truncate(1024 ** 3) 1269 new_image_path = sparse.name 1270 1271 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1272 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path) 1273 runCmd(cmd) 1274 1275 # check if partitions are expanded 1276 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot)) 1277 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot)) 1278 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]] 1279 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]] 1280 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized 1281 self.assertTrue(orig_sizes[1] < exp_sizes[1]) 1282 1283 # Check if all free space is partitioned 1284 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path)) 1285 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output) 1286 1287 os.rename(image_path, image_path + '.bak') 1288 os.rename(new_image_path, image_path) 1289 1290 # Check if it boots in qemu 1291 with runqemu('core-image-minimal', ssh=False) as qemu: 1292 cmd = "ls /etc/" 1293 status, output = qemu.run_serial('true') 1294 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 1295 finally: 1296 if os.path.exists(new_image_path): 1297 os.unlink(new_image_path) 1298 if os.path.exists(image_path + '.bak'): 1299 os.rename(image_path + '.bak', image_path) 1300 1301 def test_wic_ls_ext(self): 1302 """Test listing content of the ext partition using 'wic ls'""" 1303 runCmd("wic create wictestdisk " 1304 "--image-name=core-image-minimal " 1305 "-D -o %s" % self.resultdir) 1306 images = glob(self.resultdir + "wictestdisk-*.direct") 1307 self.assertEqual(1, len(images)) 1308 1309 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1310 1311 # list directory content of the second ext4 partition 1312 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1313 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset( 1314 set(line.split()[-1] for line in result.output.split('\n') if line))) 1315 1316 def test_wic_cp_ext(self): 1317 """Test copy files and directories to the ext partition.""" 1318 runCmd("wic create wictestdisk " 1319 "--image-name=core-image-minimal " 1320 "-D -o %s" % self.resultdir) 1321 images = glob(self.resultdir + "wictestdisk-*.direct") 1322 self.assertEqual(1, len(images)) 1323 1324 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1325 1326 # list directory content of the ext4 partition 1327 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1328 dirs = set(line.split()[-1] for line in result.output.split('\n') if line) 1329 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs)) 1330 1331 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile: 1332 testfile.write("test") 1333 1334 # copy file to the partition 1335 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot)) 1336 1337 # check if file is there 1338 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1339 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line) 1340 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)])) 1341 1342 # check if the file to copy is in the partition 1343 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1344 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line]) 1345 1346 # copy file from the partition, replace the temporary file content with it and 1347 # check for the file size to validate the copy 1348 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot)) 1349 self.assertTrue(os.stat(testfile.name).st_size > 0) 1350 1351 1352 def test_wic_rm_ext(self): 1353 """Test removing files from the ext partition.""" 1354 runCmd("wic create mkefidisk " 1355 "--image-name=core-image-minimal " 1356 "-D -o %s" % self.resultdir) 1357 images = glob(self.resultdir + "mkefidisk-*.direct") 1358 self.assertEqual(1, len(images)) 1359 1360 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1361 1362 # list directory content of the /etc directory on ext4 partition 1363 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1364 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line]) 1365 1366 # remove file 1367 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot)) 1368 1369 # check if it's removed 1370 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1371 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line]) 1372 1373 # remove non-empty directory 1374 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot)) 1375 1376 # check if it's removed 1377 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1378 self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line]) 1379