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_sdimage_bootpart(self): 239 """Test creation of sdimage-bootpart image""" 240 cmd = "wic create sdimage-bootpart -e core-image-minimal -o %s" % self.resultdir 241 kimgtype = get_bb_var('KERNEL_IMAGETYPE', 'core-image-minimal') 242 self.write_config('IMAGE_BOOT_FILES = "%s"\n' % kimgtype) 243 runCmd(cmd) 244 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct"))) 245 246 @only_for_arch(['i586', 'i686', 'x86_64']) 247 def test_default_output_dir(self): 248 """Test default output location""" 249 for fname in glob("directdisk-*.direct"): 250 os.remove(fname) 251 config = 'DEPENDS_pn-core-image-minimal += "syslinux"\n' 252 self.append_config(config) 253 bitbake('core-image-minimal') 254 self.remove_config(config) 255 cmd = "wic create directdisk -e core-image-minimal" 256 runCmd(cmd) 257 self.assertEqual(1, len(glob("directdisk-*.direct"))) 258 259 @only_for_arch(['i586', 'i686', 'x86_64']) 260 def test_build_artifacts(self): 261 """Test wic create directdisk providing all artifacts.""" 262 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'], 263 'wic-tools') 264 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'], 265 'core-image-minimal')) 266 bbvars = {key.lower(): value for key, value in bb_vars.items()} 267 bbvars['resultdir'] = self.resultdir 268 runCmd("wic create directdisk " 269 "-b %(staging_datadir)s " 270 "-k %(deploy_dir_image)s " 271 "-n %(recipe_sysroot_native)s " 272 "-r %(image_rootfs)s " 273 "-o %(resultdir)s" % bbvars) 274 self.assertEqual(1, len(glob(self.resultdir + "directdisk-*.direct"))) 275 276 def test_compress_gzip(self): 277 """Test compressing an image with gzip""" 278 runCmd("wic create wictestdisk " 279 "--image-name core-image-minimal " 280 "-c gzip -o %s" % self.resultdir) 281 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.gz"))) 282 283 def test_compress_bzip2(self): 284 """Test compressing an image with bzip2""" 285 runCmd("wic create wictestdisk " 286 "--image-name=core-image-minimal " 287 "-c bzip2 -o %s" % self.resultdir) 288 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.bz2"))) 289 290 def test_compress_xz(self): 291 """Test compressing an image with xz""" 292 runCmd("wic create wictestdisk " 293 "--image-name=core-image-minimal " 294 "--compress-with=xz -o %s" % self.resultdir) 295 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct.xz"))) 296 297 def test_wrong_compressor(self): 298 """Test how wic breaks if wrong compressor is provided""" 299 self.assertEqual(2, runCmd("wic create wictestdisk " 300 "--image-name=core-image-minimal " 301 "-c wrong -o %s" % self.resultdir, 302 ignore_status=True).status) 303 304 def test_debug_short(self): 305 """Test -D option""" 306 runCmd("wic create wictestdisk " 307 "--image-name=core-image-minimal " 308 "-D -o %s" % self.resultdir) 309 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 310 311 def test_debug_long(self): 312 """Test --debug option""" 313 runCmd("wic create wictestdisk " 314 "--image-name=core-image-minimal " 315 "--debug -o %s" % self.resultdir) 316 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 317 318 def test_skip_build_check_short(self): 319 """Test -s option""" 320 runCmd("wic create wictestdisk " 321 "--image-name=core-image-minimal " 322 "-s -o %s" % self.resultdir) 323 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 324 325 def test_skip_build_check_long(self): 326 """Test --skip-build-check option""" 327 runCmd("wic create wictestdisk " 328 "--image-name=core-image-minimal " 329 "--skip-build-check " 330 "--outdir %s" % self.resultdir) 331 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 332 333 def test_build_rootfs_short(self): 334 """Test -f option""" 335 runCmd("wic create wictestdisk " 336 "--image-name=core-image-minimal " 337 "-f -o %s" % self.resultdir) 338 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 339 340 def test_build_rootfs_long(self): 341 """Test --build-rootfs option""" 342 runCmd("wic create wictestdisk " 343 "--image-name=core-image-minimal " 344 "--build-rootfs " 345 "--outdir %s" % self.resultdir) 346 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*.direct"))) 347 348 @only_for_arch(['i586', 'i686', 'x86_64']) 349 def test_rootfs_indirect_recipes(self): 350 """Test usage of rootfs plugin with rootfs recipes""" 351 runCmd("wic create directdisk-multi-rootfs " 352 "--image-name=core-image-minimal " 353 "--rootfs rootfs1=core-image-minimal " 354 "--rootfs rootfs2=core-image-minimal " 355 "--outdir %s" % self.resultdir) 356 self.assertEqual(1, len(glob(self.resultdir + "directdisk-multi-rootfs*.direct"))) 357 358 @only_for_arch(['i586', 'i686', 'x86_64']) 359 def test_rootfs_artifacts(self): 360 """Test usage of rootfs plugin with rootfs paths""" 361 bb_vars = get_bb_vars(['STAGING_DATADIR', 'RECIPE_SYSROOT_NATIVE'], 362 'wic-tools') 363 bb_vars.update(get_bb_vars(['DEPLOY_DIR_IMAGE', 'IMAGE_ROOTFS'], 364 'core-image-minimal')) 365 bbvars = {key.lower(): value for key, value in bb_vars.items()} 366 bbvars['wks'] = "directdisk-multi-rootfs" 367 bbvars['resultdir'] = self.resultdir 368 runCmd("wic create %(wks)s " 369 "--bootimg-dir=%(staging_datadir)s " 370 "--kernel-dir=%(deploy_dir_image)s " 371 "--native-sysroot=%(recipe_sysroot_native)s " 372 "--rootfs-dir rootfs1=%(image_rootfs)s " 373 "--rootfs-dir rootfs2=%(image_rootfs)s " 374 "--outdir %(resultdir)s" % bbvars) 375 self.assertEqual(1, len(glob(self.resultdir + "%(wks)s-*.direct" % bbvars))) 376 377 def test_exclude_path(self): 378 """Test --exclude-path wks option.""" 379 380 oldpath = os.environ['PATH'] 381 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 382 383 try: 384 wks_file = 'temp.wks' 385 with open(wks_file, 'w') as wks: 386 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal') 387 wks.write(""" 388part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path usr 389part /usr --source rootfs --ondisk mmcblk0 --fstype=ext4 --rootfs-dir %s/usr 390part /etc --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path bin/ --rootfs-dir %s/usr""" 391 % (rootfs_dir, rootfs_dir)) 392 runCmd("wic create %s -e core-image-minimal -o %s" \ 393 % (wks_file, self.resultdir)) 394 395 os.remove(wks_file) 396 wicout = glob(self.resultdir + "%s-*direct" % 'temp') 397 self.assertEqual(1, len(wicout)) 398 399 wicimg = wicout[0] 400 401 # verify partition size with wic 402 res = runCmd("parted -m %s unit b p 2>/dev/null" % wicimg) 403 404 # parse parted output which looks like this: 405 # BYT;\n 406 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n 407 # 1:0.00MiB:200MiB:200MiB:ext4::;\n 408 partlns = res.output.splitlines()[2:] 409 410 self.assertEqual(3, len(partlns)) 411 412 for part in [1, 2, 3]: 413 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part) 414 partln = partlns[part-1].split(":") 415 self.assertEqual(7, len(partln)) 416 start = int(partln[1].rstrip("B")) / 512 417 length = int(partln[3].rstrip("B")) / 512 418 runCmd("dd if=%s of=%s skip=%d count=%d" % 419 (wicimg, part_file, start, length)) 420 421 # Test partition 1, should contain the normal root directories, except 422 # /usr. 423 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 424 os.path.join(self.resultdir, "selftest_img.part1")) 425 files = extract_files(res.output) 426 self.assertIn("etc", files) 427 self.assertNotIn("usr", files) 428 429 # Partition 2, should contain common directories for /usr, not root 430 # directories. 431 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 432 os.path.join(self.resultdir, "selftest_img.part2")) 433 files = extract_files(res.output) 434 self.assertNotIn("etc", files) 435 self.assertNotIn("usr", files) 436 self.assertIn("share", files) 437 438 # Partition 3, should contain the same as partition 2, including the bin 439 # directory, but not the files inside it. 440 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % \ 441 os.path.join(self.resultdir, "selftest_img.part3")) 442 files = extract_files(res.output) 443 self.assertNotIn("etc", files) 444 self.assertNotIn("usr", files) 445 self.assertIn("share", files) 446 self.assertIn("bin", files) 447 res = runCmd("debugfs -R 'ls -p bin' %s 2>/dev/null" % \ 448 os.path.join(self.resultdir, "selftest_img.part3")) 449 files = extract_files(res.output) 450 self.assertIn(".", files) 451 self.assertIn("..", files) 452 self.assertEqual(2, len(files)) 453 454 for part in [1, 2, 3]: 455 part_file = os.path.join(self.resultdir, "selftest_img.part%d" % part) 456 os.remove(part_file) 457 458 finally: 459 os.environ['PATH'] = oldpath 460 461 def test_include_path(self): 462 """Test --include-path wks option.""" 463 464 oldpath = os.environ['PATH'] 465 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 466 467 try: 468 include_path = os.path.join(self.resultdir, 'test-include') 469 os.makedirs(include_path) 470 with open(os.path.join(include_path, 'test-file'), 'w') as t: 471 t.write("test\n") 472 wks_file = os.path.join(include_path, 'temp.wks') 473 with open(wks_file, 'w') as wks: 474 rootfs_dir = get_bb_var('IMAGE_ROOTFS', 'core-image-minimal') 475 wks.write(""" 476part /part1 --source rootfs --ondisk mmcblk0 --fstype=ext4 477part /part2 --source rootfs --ondisk mmcblk0 --fstype=ext4 --include-path %s""" 478 % (include_path)) 479 runCmd("wic create %s -e core-image-minimal -o %s" \ 480 % (wks_file, self.resultdir)) 481 482 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] 483 part2 = glob(os.path.join(self.resultdir, 'temp-*.direct.p2'))[0] 484 485 # Test partition 1, should not contain 'test-file' 486 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) 487 files = extract_files(res.output) 488 self.assertNotIn('test-file', files) 489 self.assertEqual(True, files_own_by_root(res.output)) 490 491 # Test partition 2, should contain 'test-file' 492 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part2)) 493 files = extract_files(res.output) 494 self.assertIn('test-file', files) 495 self.assertEqual(True, files_own_by_root(res.output)) 496 497 finally: 498 os.environ['PATH'] = oldpath 499 500 def test_include_path_embeded(self): 501 """Test --include-path wks option.""" 502 503 oldpath = os.environ['PATH'] 504 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 505 506 try: 507 include_path = os.path.join(self.resultdir, 'test-include') 508 os.makedirs(include_path) 509 with open(os.path.join(include_path, 'test-file'), 'w') as t: 510 t.write("test\n") 511 wks_file = os.path.join(include_path, 'temp.wks') 512 with open(wks_file, 'w') as wks: 513 wks.write(""" 514part / --source rootfs --fstype=ext4 --include-path %s --include-path core-image-minimal-mtdutils export/""" 515 % (include_path)) 516 runCmd("wic create %s -e core-image-minimal -o %s" \ 517 % (wks_file, self.resultdir)) 518 519 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] 520 521 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) 522 files = extract_files(res.output) 523 self.assertIn('test-file', files) 524 self.assertEqual(True, files_own_by_root(res.output)) 525 526 res = runCmd("debugfs -R 'ls -p /export/etc/' %s 2>/dev/null" % (part1)) 527 files = extract_files(res.output) 528 self.assertIn('passwd', files) 529 self.assertEqual(True, files_own_by_root(res.output)) 530 531 finally: 532 os.environ['PATH'] = oldpath 533 534 def test_include_path_errors(self): 535 """Test --include-path wks option error handling.""" 536 wks_file = 'temp.wks' 537 538 # Absolute argument. 539 with open(wks_file, 'w') as wks: 540 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils /export") 541 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 542 % (wks_file, self.resultdir), ignore_status=True).status) 543 os.remove(wks_file) 544 545 # Argument pointing to parent directory. 546 with open(wks_file, 'w') as wks: 547 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils ././..") 548 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 549 % (wks_file, self.resultdir), ignore_status=True).status) 550 os.remove(wks_file) 551 552 # 3 Argument pointing to parent directory. 553 with open(wks_file, 'w') as wks: 554 wks.write("part / --source rootfs --fstype=ext4 --include-path core-image-minimal-mtdutils export/ dummy") 555 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 556 % (wks_file, self.resultdir), ignore_status=True).status) 557 os.remove(wks_file) 558 559 def test_exclude_path_errors(self): 560 """Test --exclude-path wks option error handling.""" 561 wks_file = 'temp.wks' 562 563 # Absolute argument. 564 with open(wks_file, 'w') as wks: 565 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path /usr") 566 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 567 % (wks_file, self.resultdir), ignore_status=True).status) 568 os.remove(wks_file) 569 570 # Argument pointing to parent directory. 571 with open(wks_file, 'w') as wks: 572 wks.write("part / --source rootfs --ondisk mmcblk0 --fstype=ext4 --exclude-path ././..") 573 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 574 % (wks_file, self.resultdir), ignore_status=True).status) 575 os.remove(wks_file) 576 577 def test_permissions(self): 578 """Test permissions are respected""" 579 580 oldpath = os.environ['PATH'] 581 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 582 583 t_normal = """ 584part / --source rootfs --fstype=ext4 585""" 586 t_exclude = """ 587part / --source rootfs --fstype=ext4 --exclude-path=home 588""" 589 t_multi = """ 590part / --source rootfs --ondisk sda --fstype=ext4 591part /export --source rootfs --rootfs=core-image-minimal-mtdutils --fstype=ext4 592""" 593 t_change = """ 594part / --source rootfs --ondisk sda --fstype=ext4 --exclude-path=etc/ 595part /etc --source rootfs --fstype=ext4 --change-directory=etc 596""" 597 tests = [t_normal, t_exclude, t_multi, t_change] 598 599 try: 600 for test in tests: 601 include_path = os.path.join(self.resultdir, 'test-include') 602 os.makedirs(include_path) 603 wks_file = os.path.join(include_path, 'temp.wks') 604 with open(wks_file, 'w') as wks: 605 wks.write(test) 606 runCmd("wic create %s -e core-image-minimal -o %s" \ 607 % (wks_file, self.resultdir)) 608 609 for part in glob(os.path.join(self.resultdir, 'temp-*.direct.p*')): 610 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part)) 611 self.assertEqual(True, files_own_by_root(res.output)) 612 613 rmtree(self.resultdir, ignore_errors=True) 614 615 finally: 616 os.environ['PATH'] = oldpath 617 618 def test_change_directory(self): 619 """Test --change-directory wks option.""" 620 621 oldpath = os.environ['PATH'] 622 os.environ['PATH'] = get_bb_var("PATH", "wic-tools") 623 624 try: 625 include_path = os.path.join(self.resultdir, 'test-include') 626 os.makedirs(include_path) 627 wks_file = os.path.join(include_path, 'temp.wks') 628 with open(wks_file, 'w') as wks: 629 wks.write("part /etc --source rootfs --fstype=ext4 --change-directory=etc") 630 runCmd("wic create %s -e core-image-minimal -o %s" \ 631 % (wks_file, self.resultdir)) 632 633 part1 = glob(os.path.join(self.resultdir, 'temp-*.direct.p1'))[0] 634 635 res = runCmd("debugfs -R 'ls -p' %s 2>/dev/null" % (part1)) 636 files = extract_files(res.output) 637 self.assertIn('passwd', files) 638 639 finally: 640 os.environ['PATH'] = oldpath 641 642 def test_change_directory_errors(self): 643 """Test --change-directory wks option error handling.""" 644 wks_file = 'temp.wks' 645 646 # Absolute argument. 647 with open(wks_file, 'w') as wks: 648 wks.write("part / --source rootfs --fstype=ext4 --change-directory /usr") 649 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 650 % (wks_file, self.resultdir), ignore_status=True).status) 651 os.remove(wks_file) 652 653 # Argument pointing to parent directory. 654 with open(wks_file, 'w') as wks: 655 wks.write("part / --source rootfs --fstype=ext4 --change-directory ././..") 656 self.assertNotEqual(0, runCmd("wic create %s -e core-image-minimal -o %s" \ 657 % (wks_file, self.resultdir), ignore_status=True).status) 658 os.remove(wks_file) 659 660class Wic2(WicTestCase): 661 662 def test_bmap_short(self): 663 """Test generation of .bmap file -m option""" 664 cmd = "wic create wictestdisk -e core-image-minimal -m -o %s" % self.resultdir 665 runCmd(cmd) 666 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 667 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap"))) 668 669 def test_bmap_long(self): 670 """Test generation of .bmap file --bmap option""" 671 cmd = "wic create wictestdisk -e core-image-minimal --bmap -o %s" % self.resultdir 672 runCmd(cmd) 673 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 674 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct.bmap"))) 675 676 def test_image_env(self): 677 """Test generation of <image>.env files.""" 678 image = 'core-image-minimal' 679 imgdatadir = self._get_image_env_path(image) 680 681 bb_vars = get_bb_vars(['IMAGE_BASENAME', 'WICVARS'], image) 682 basename = bb_vars['IMAGE_BASENAME'] 683 self.assertEqual(basename, image) 684 path = os.path.join(imgdatadir, basename) + '.env' 685 self.assertTrue(os.path.isfile(path)) 686 687 wicvars = set(bb_vars['WICVARS'].split()) 688 # filter out optional variables 689 wicvars = wicvars.difference(('DEPLOY_DIR_IMAGE', 'IMAGE_BOOT_FILES', 690 'INITRD', 'INITRD_LIVE', 'ISODIR','INITRAMFS_IMAGE', 691 'INITRAMFS_IMAGE_BUNDLE', 'INITRAMFS_LINK_NAME', 692 'APPEND')) 693 with open(path) as envfile: 694 content = dict(line.split("=", 1) for line in envfile) 695 # test if variables used by wic present in the .env file 696 for var in wicvars: 697 self.assertTrue(var in content, "%s is not in .env file" % var) 698 self.assertTrue(content[var]) 699 700 def test_image_vars_dir_short(self): 701 """Test image vars directory selection -v option""" 702 image = 'core-image-minimal' 703 imgenvdir = self._get_image_env_path(image) 704 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 705 706 runCmd("wic create wictestdisk " 707 "--image-name=%s -v %s -n %s -o %s" 708 % (image, imgenvdir, native_sysroot, 709 self.resultdir)) 710 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 711 712 def test_image_vars_dir_long(self): 713 """Test image vars directory selection --vars option""" 714 image = 'core-image-minimal' 715 imgenvdir = self._get_image_env_path(image) 716 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 717 718 runCmd("wic create wictestdisk " 719 "--image-name=%s " 720 "--vars %s " 721 "--native-sysroot %s " 722 "--outdir %s" 723 % (image, imgenvdir, native_sysroot, 724 self.resultdir)) 725 self.assertEqual(1, len(glob(self.resultdir + "wictestdisk-*direct"))) 726 727 @only_for_arch(['i586', 'i686', 'x86_64']) 728 def test_wic_image_type(self): 729 """Test building wic images by bitbake""" 730 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\ 731 'MACHINE_FEATURES_append = " efi"\n' 732 self.append_config(config) 733 self.assertEqual(0, bitbake('wic-image-minimal').status) 734 self.remove_config(config) 735 736 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 737 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 738 machine = bb_vars['MACHINE'] 739 prefix = os.path.join(deploy_dir, 'wic-image-minimal-%s.' % machine) 740 # check if we have result image and manifests symlinks 741 # pointing to existing files 742 for suffix in ('wic', 'manifest'): 743 path = prefix + suffix 744 self.assertTrue(os.path.islink(path)) 745 self.assertTrue(os.path.isfile(os.path.realpath(path))) 746 747 @only_for_arch(['i586', 'i686', 'x86_64']) 748 def test_qemu(self): 749 """Test wic-image-minimal under qemu""" 750 config = 'IMAGE_FSTYPES += "wic"\nWKS_FILE = "wic-image-minimal"\n'\ 751 'MACHINE_FEATURES_append = " efi"\n' 752 self.append_config(config) 753 self.assertEqual(0, bitbake('wic-image-minimal').status) 754 self.remove_config(config) 755 756 with runqemu('wic-image-minimal', ssh=False) as qemu: 757 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' " \ 758 "-e '/dev/root /|/dev/sda2 /' -e '/dev/sda3 /media' -e '/dev/sda4 /mnt'" 759 status, output = qemu.run_serial(cmd) 760 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 761 self.assertEqual(output, '4') 762 cmd = "grep UUID= /etc/fstab" 763 status, output = qemu.run_serial(cmd) 764 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 765 self.assertEqual(output, 'UUID=2c71ef06-a81d-4735-9d3a-379b69c6bdba\t/media\text4\tdefaults\t0\t0') 766 767 @only_for_arch(['i586', 'i686', 'x86_64']) 768 def test_qemu_efi(self): 769 """Test core-image-minimal efi image under qemu""" 770 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "mkefidisk.wks"\n' 771 self.append_config(config) 772 self.assertEqual(0, bitbake('core-image-minimal ovmf').status) 773 self.remove_config(config) 774 775 with runqemu('core-image-minimal', ssh=False, 776 runqemuparams='ovmf', image_fstype='wic') as qemu: 777 cmd = "grep sda. /proc/partitions |wc -l" 778 status, output = qemu.run_serial(cmd) 779 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 780 self.assertEqual(output, '3') 781 782 @staticmethod 783 def _make_fixed_size_wks(size): 784 """ 785 Create a wks of an image with a single partition. Size of the partition is set 786 using --fixed-size flag. Returns a tuple: (path to wks file, wks image name) 787 """ 788 with NamedTemporaryFile("w", suffix=".wks", delete=False) as tempf: 789 wkspath = tempf.name 790 tempf.write("part " \ 791 "--source rootfs --ondisk hda --align 4 --fixed-size %d " 792 "--fstype=ext4\n" % size) 793 794 return wkspath 795 796 def _get_wic_partitions(self, wkspath, native_sysroot=None, ignore_status=False): 797 p = runCmd("wic create %s -e core-image-minimal -o %s" % (wkspath, self.resultdir), 798 ignore_status=ignore_status) 799 800 if p.status: 801 return (p, None) 802 803 wksname = os.path.splitext(os.path.basename(wkspath))[0] 804 805 wicout = glob(self.resultdir + "%s-*direct" % wksname) 806 807 if not wicout: 808 return (p, None) 809 810 wicimg = wicout[0] 811 812 if not native_sysroot: 813 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 814 815 # verify partition size with wic 816 res = runCmd("parted -m %s unit kib p 2>/dev/null" % wicimg, 817 native_sysroot=native_sysroot) 818 819 # parse parted output which looks like this: 820 # BYT;\n 821 # /var/tmp/wic/build/tmpfwvjjkf_-201611101222-hda.direct:200MiB:file:512:512:msdos::;\n 822 # 1:0.00MiB:200MiB:200MiB:ext4::;\n 823 return (p, res.output.splitlines()[2:]) 824 825 def test_fixed_size(self): 826 """ 827 Test creation of a simple image with partition size controlled through 828 --fixed-size flag 829 """ 830 wkspath = Wic2._make_fixed_size_wks(200) 831 _, partlns = self._get_wic_partitions(wkspath) 832 os.remove(wkspath) 833 834 self.assertEqual(partlns, [ 835 "1:4.00kiB:204804kiB:204800kiB:ext4::;", 836 ]) 837 838 def test_fixed_size_error(self): 839 """ 840 Test creation of a simple image with partition size controlled through 841 --fixed-size flag. The size of partition is intentionally set to 1MiB 842 in order to trigger an error in wic. 843 """ 844 wkspath = Wic2._make_fixed_size_wks(1) 845 p, _ = self._get_wic_partitions(wkspath, ignore_status=True) 846 os.remove(wkspath) 847 848 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output) 849 850 def test_offset(self): 851 native_sysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "wic-tools") 852 853 with NamedTemporaryFile("w", suffix=".wks") as tempf: 854 # Test that partitions are placed at the correct offsets, default KB 855 tempf.write("bootloader --ptable gpt\n" \ 856 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \ 857 "part /bar --ondisk hda --offset 102432 --fixed-size 100M --fstype=ext4\n") 858 tempf.flush() 859 860 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 861 self.assertEqual(partlns, [ 862 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;", 863 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;", 864 ]) 865 866 with NamedTemporaryFile("w", suffix=".wks") as tempf: 867 # Test that partitions are placed at the correct offsets, same with explicit KB 868 tempf.write("bootloader --ptable gpt\n" \ 869 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \ 870 "part /bar --ondisk hda --offset 102432K --fixed-size 100M --fstype=ext4\n") 871 tempf.flush() 872 873 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 874 self.assertEqual(partlns, [ 875 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;", 876 "2:102432kiB:204832kiB:102400kiB:ext4:primary:;", 877 ]) 878 879 with NamedTemporaryFile("w", suffix=".wks") as tempf: 880 # Test that partitions are placed at the correct offsets using MB 881 tempf.write("bootloader --ptable gpt\n" \ 882 "part / --source rootfs --ondisk hda --offset 32K --fixed-size 100M --fstype=ext4\n" \ 883 "part /bar --ondisk hda --offset 101M --fixed-size 100M --fstype=ext4\n") 884 tempf.flush() 885 886 _, partlns = self._get_wic_partitions(tempf.name, native_sysroot) 887 self.assertEqual(partlns, [ 888 "1:32.0kiB:102432kiB:102400kiB:ext4:primary:;", 889 "2:103424kiB:205824kiB:102400kiB:ext4:primary:;", 890 ]) 891 892 with NamedTemporaryFile("w", suffix=".wks") as tempf: 893 # Test that image creation fails if the partitions would overlap 894 tempf.write("bootloader --ptable gpt\n" \ 895 "part / --source rootfs --ondisk hda --offset 32 --fixed-size 100M --fstype=ext4\n" \ 896 "part /bar --ondisk hda --offset 102431 --fixed-size 100M --fstype=ext4\n") 897 tempf.flush() 898 899 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True) 900 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output) 901 902 with NamedTemporaryFile("w", suffix=".wks") as tempf: 903 # Test that partitions are not allowed to overlap with the booloader 904 tempf.write("bootloader --ptable gpt\n" \ 905 "part / --source rootfs --ondisk hda --offset 8 --fixed-size 100M --fstype=ext4\n") 906 tempf.flush() 907 908 p, _ = self._get_wic_partitions(tempf.name, ignore_status=True) 909 self.assertNotEqual(p.status, 0, "wic exited successfully when an error was expected:\n%s" % p.output) 910 911 @only_for_arch(['i586', 'i686', 'x86_64']) 912 def test_rawcopy_plugin_qemu(self): 913 """Test rawcopy plugin in qemu""" 914 # build ext4 and wic images 915 for fstype in ("ext4", "wic"): 916 config = 'IMAGE_FSTYPES = "%s"\nWKS_FILE = "test_rawcopy_plugin.wks.in"\n' % fstype 917 self.append_config(config) 918 self.assertEqual(0, bitbake('core-image-minimal').status) 919 self.remove_config(config) 920 921 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu: 922 cmd = "grep sda. /proc/partitions |wc -l" 923 status, output = qemu.run_serial(cmd) 924 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 925 self.assertEqual(output, '2') 926 927 def test_rawcopy_plugin(self): 928 """Test rawcopy plugin""" 929 img = 'core-image-minimal' 930 machine = get_bb_var('MACHINE', img) 931 with NamedTemporaryFile("w", suffix=".wks") as wks: 932 wks.writelines(['part /boot --active --source bootimg-pcbios\n', 933 'part / --source rawcopy --sourceparams="file=%s-%s.ext4" --use-uuid\n'\ 934 % (img, machine), 935 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n']) 936 wks.flush() 937 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 938 runCmd(cmd) 939 wksname = os.path.splitext(os.path.basename(wks.name))[0] 940 out = glob(self.resultdir + "%s-*direct" % wksname) 941 self.assertEqual(1, len(out)) 942 943 @only_for_arch(['i586', 'i686', 'x86_64']) 944 def test_biosplusefi_plugin_qemu(self): 945 """Test biosplusefi plugin in qemu""" 946 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n' 947 self.append_config(config) 948 self.assertEqual(0, bitbake('core-image-minimal').status) 949 self.remove_config(config) 950 951 with runqemu('core-image-minimal', ssh=False, image_fstype='wic') as qemu: 952 # Check that we have ONLY two /dev/sda* partitions (/boot and /) 953 cmd = "grep sda. /proc/partitions | wc -l" 954 status, output = qemu.run_serial(cmd) 955 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 956 self.assertEqual(output, '2') 957 # Check that /dev/sda1 is /boot and that either /dev/root OR /dev/sda2 is / 958 cmd = "mount | grep '^/dev/' | cut -f1,3 -d ' ' | egrep -c -e '/dev/sda1 /boot' -e '/dev/root /|/dev/sda2 /'" 959 status, output = qemu.run_serial(cmd) 960 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 961 self.assertEqual(output, '2') 962 # Check that /boot has EFI bootx64.efi (required for EFI) 963 cmd = "ls /boot/EFI/BOOT/bootx64.efi | wc -l" 964 status, output = qemu.run_serial(cmd) 965 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 966 self.assertEqual(output, '1') 967 # Check that "BOOTABLE" flag is set on boot partition (required for PC-Bios) 968 # Trailing "cat" seems to be required; otherwise run_serial() sends back echo of the input command 969 cmd = "fdisk -l /dev/sda | grep /dev/sda1 | awk {print'$2'} | cat" 970 status, output = qemu.run_serial(cmd) 971 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 972 self.assertEqual(output, '*') 973 974 @only_for_arch(['i586', 'i686', 'x86_64']) 975 def test_biosplusefi_plugin(self): 976 """Test biosplusefi plugin""" 977 # Wic generation below may fail depending on the order of the unittests 978 # This is because bootimg-pcbios (that bootimg-biosplusefi uses) generate its MBR inside STAGING_DATADIR directory 979 # which may or may not exists depending on what was built already 980 # If an image hasn't been built yet, directory ${STAGING_DATADIR}/syslinux won't exists and _get_bootimg_dir() 981 # will raise with "Couldn't find correct bootimg_dir" 982 # The easiest way to work-around this issue is to make sure we already built an image here, hence the bitbake call 983 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "test_biosplusefi_plugin.wks"\nMACHINE_FEATURES_append = " efi"\n' 984 self.append_config(config) 985 self.assertEqual(0, bitbake('core-image-minimal').status) 986 self.remove_config(config) 987 988 img = 'core-image-minimal' 989 with NamedTemporaryFile("w", suffix=".wks") as wks: 990 wks.writelines(['part /boot --active --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\n', 991 'part / --source rootfs --fstype=ext4 --align 1024 --use-uuid\n'\ 992 'bootloader --timeout=0 --append="console=ttyS0,115200n8"\n']) 993 wks.flush() 994 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 995 runCmd(cmd) 996 wksname = os.path.splitext(os.path.basename(wks.name))[0] 997 out = glob(self.resultdir + "%s-*.direct" % wksname) 998 self.assertEqual(1, len(out)) 999 1000 def test_fs_types(self): 1001 """Test filesystem types for empty and not empty partitions""" 1002 img = 'core-image-minimal' 1003 with NamedTemporaryFile("w", suffix=".wks") as wks: 1004 wks.writelines(['part ext2 --fstype ext2 --source rootfs\n', 1005 'part btrfs --fstype btrfs --source rootfs --size 40M\n', 1006 'part squash --fstype squashfs --source rootfs\n', 1007 'part swap --fstype swap --size 1M\n', 1008 'part emptyvfat --fstype vfat --size 1M\n', 1009 'part emptymsdos --fstype msdos --size 1M\n', 1010 'part emptyext2 --fstype ext2 --size 1M\n', 1011 'part emptybtrfs --fstype btrfs --size 150M\n']) 1012 wks.flush() 1013 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 1014 runCmd(cmd) 1015 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1016 out = glob(self.resultdir + "%s-*direct" % wksname) 1017 self.assertEqual(1, len(out)) 1018 1019 def test_kickstart_parser(self): 1020 """Test wks parser options""" 1021 with NamedTemporaryFile("w", suffix=".wks") as wks: 1022 wks.writelines(['part / --fstype ext3 --source rootfs --system-id 0xFF '\ 1023 '--overhead-factor 1.2 --size 100k\n']) 1024 wks.flush() 1025 cmd = "wic create %s -e core-image-minimal -o %s" % (wks.name, self.resultdir) 1026 runCmd(cmd) 1027 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1028 out = glob(self.resultdir + "%s-*direct" % wksname) 1029 self.assertEqual(1, len(out)) 1030 1031 def test_image_bootpart_globbed(self): 1032 """Test globbed sources with image-bootpart plugin""" 1033 img = "core-image-minimal" 1034 cmd = "wic create sdimage-bootpart -e %s -o %s" % (img, self.resultdir) 1035 config = 'IMAGE_BOOT_FILES = "%s*"' % get_bb_var('KERNEL_IMAGETYPE', img) 1036 self.append_config(config) 1037 runCmd(cmd) 1038 self.remove_config(config) 1039 self.assertEqual(1, len(glob(self.resultdir + "sdimage-bootpart-*direct"))) 1040 1041 def test_sparse_copy(self): 1042 """Test sparse_copy with FIEMAP and SEEK_HOLE filemap APIs""" 1043 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'wic') 1044 sys.path.insert(0, libpath) 1045 from filemap import FilemapFiemap, FilemapSeek, sparse_copy, ErrorNotSupp 1046 with NamedTemporaryFile("w", suffix=".wic-sparse") as sparse: 1047 src_name = sparse.name 1048 src_size = 1024 * 10 1049 sparse.truncate(src_size) 1050 # write one byte to the file 1051 with open(src_name, 'r+b') as sfile: 1052 sfile.seek(1024 * 4) 1053 sfile.write(b'\x00') 1054 dest = sparse.name + '.out' 1055 # copy src file to dest using different filemap APIs 1056 for api in (FilemapFiemap, FilemapSeek, None): 1057 if os.path.exists(dest): 1058 os.unlink(dest) 1059 try: 1060 sparse_copy(sparse.name, dest, api=api) 1061 except ErrorNotSupp: 1062 continue # skip unsupported API 1063 dest_stat = os.stat(dest) 1064 self.assertEqual(dest_stat.st_size, src_size) 1065 # 8 blocks is 4K (physical sector size) 1066 self.assertEqual(dest_stat.st_blocks, 8) 1067 os.unlink(dest) 1068 1069 def test_wic_ls(self): 1070 """Test listing image content using 'wic ls'""" 1071 runCmd("wic create wictestdisk " 1072 "--image-name=core-image-minimal " 1073 "-D -o %s" % self.resultdir) 1074 images = glob(self.resultdir + "wictestdisk-*.direct") 1075 self.assertEqual(1, len(images)) 1076 1077 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1078 1079 # list partitions 1080 result = runCmd("wic ls %s -n %s" % (images[0], sysroot)) 1081 self.assertEqual(3, len(result.output.split('\n'))) 1082 1083 # list directory content of the first partition 1084 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1085 self.assertEqual(6, len(result.output.split('\n'))) 1086 1087 def test_wic_cp(self): 1088 """Test copy files and directories to the the wic image.""" 1089 runCmd("wic create wictestdisk " 1090 "--image-name=core-image-minimal " 1091 "-D -o %s" % self.resultdir) 1092 images = glob(self.resultdir + "wictestdisk-*.direct") 1093 self.assertEqual(1, len(images)) 1094 1095 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1096 1097 # list directory content of the first partition 1098 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1099 self.assertEqual(6, len(result.output.split('\n'))) 1100 1101 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile: 1102 testfile.write("test") 1103 1104 # copy file to the partition 1105 runCmd("wic cp %s %s:1/ -n %s" % (testfile.name, images[0], sysroot)) 1106 1107 # check if file is there 1108 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1109 self.assertEqual(7, len(result.output.split('\n'))) 1110 self.assertTrue(os.path.basename(testfile.name) in result.output) 1111 1112 # prepare directory 1113 testdir = os.path.join(self.resultdir, 'wic-test-cp-dir') 1114 testsubdir = os.path.join(testdir, 'subdir') 1115 os.makedirs(os.path.join(testsubdir)) 1116 copy(testfile.name, testdir) 1117 1118 # copy directory to the partition 1119 runCmd("wic cp %s %s:1/ -n %s" % (testdir, images[0], sysroot)) 1120 1121 # check if directory is there 1122 result = runCmd("wic ls %s:1/ -n %s" % (images[0], sysroot)) 1123 self.assertEqual(8, len(result.output.split('\n'))) 1124 self.assertTrue(os.path.basename(testdir) in result.output) 1125 1126 # copy the file from the partition and check if it success 1127 dest = '%s-cp' % testfile.name 1128 runCmd("wic cp %s:1/%s %s -n %s" % (images[0], 1129 os.path.basename(testfile.name), dest, sysroot)) 1130 self.assertTrue(os.path.exists(dest)) 1131 1132 1133 def test_wic_rm(self): 1134 """Test removing files and directories from the the wic image.""" 1135 runCmd("wic create mkefidisk " 1136 "--image-name=core-image-minimal " 1137 "-D -o %s" % self.resultdir) 1138 images = glob(self.resultdir + "mkefidisk-*.direct") 1139 self.assertEqual(1, len(images)) 1140 1141 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1142 1143 # list directory content of the first partition 1144 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot)) 1145 self.assertIn('\nBZIMAGE ', result.output) 1146 self.assertIn('\nEFI <DIR> ', result.output) 1147 1148 # remove file 1149 runCmd("wic rm %s:1/bzimage -n %s" % (images[0], sysroot)) 1150 1151 # remove directory 1152 runCmd("wic rm %s:1/efi -n %s" % (images[0], sysroot)) 1153 1154 # check if they're removed 1155 result = runCmd("wic ls %s:1 -n %s" % (images[0], sysroot)) 1156 self.assertNotIn('\nBZIMAGE ', result.output) 1157 self.assertNotIn('\nEFI <DIR> ', result.output) 1158 1159 def test_mkfs_extraopts(self): 1160 """Test wks option --mkfs-extraopts for empty and not empty partitions""" 1161 img = 'core-image-minimal' 1162 with NamedTemporaryFile("w", suffix=".wks") as wks: 1163 wks.writelines( 1164 ['part ext2 --fstype ext2 --source rootfs --mkfs-extraopts "-D -F -i 8192"\n', 1165 "part btrfs --fstype btrfs --source rootfs --size 40M --mkfs-extraopts='--quiet'\n", 1166 'part squash --fstype squashfs --source rootfs --mkfs-extraopts "-no-sparse -b 4096"\n', 1167 'part emptyvfat --fstype vfat --size 1M --mkfs-extraopts "-S 1024 -s 64"\n', 1168 'part emptymsdos --fstype msdos --size 1M --mkfs-extraopts "-S 1024 -s 64"\n', 1169 'part emptyext2 --fstype ext2 --size 1M --mkfs-extraopts "-D -F -i 8192"\n', 1170 'part emptybtrfs --fstype btrfs --size 100M --mkfs-extraopts "--mixed -K"\n']) 1171 wks.flush() 1172 cmd = "wic create %s -e %s -o %s" % (wks.name, img, self.resultdir) 1173 runCmd(cmd) 1174 wksname = os.path.splitext(os.path.basename(wks.name))[0] 1175 out = glob(self.resultdir + "%s-*direct" % wksname) 1176 self.assertEqual(1, len(out)) 1177 1178 def test_expand_mbr_image(self): 1179 """Test wic write --expand command for mbr image""" 1180 # build an image 1181 config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n' 1182 self.append_config(config) 1183 self.assertEqual(0, bitbake('core-image-minimal').status) 1184 1185 # get path to the image 1186 bb_vars = get_bb_vars(['DEPLOY_DIR_IMAGE', 'MACHINE']) 1187 deploy_dir = bb_vars['DEPLOY_DIR_IMAGE'] 1188 machine = bb_vars['MACHINE'] 1189 image_path = os.path.join(deploy_dir, 'core-image-minimal-%s.wic' % machine) 1190 1191 self.remove_config(config) 1192 1193 try: 1194 # expand image to 1G 1195 new_image_path = None 1196 with NamedTemporaryFile(mode='wb', suffix='.wic.exp', 1197 dir=deploy_dir, delete=False) as sparse: 1198 sparse.truncate(1024 ** 3) 1199 new_image_path = sparse.name 1200 1201 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1202 cmd = "wic write -n %s --expand 1:0 %s %s" % (sysroot, image_path, new_image_path) 1203 runCmd(cmd) 1204 1205 # check if partitions are expanded 1206 orig = runCmd("wic ls %s -n %s" % (image_path, sysroot)) 1207 exp = runCmd("wic ls %s -n %s" % (new_image_path, sysroot)) 1208 orig_sizes = [int(line.split()[3]) for line in orig.output.split('\n')[1:]] 1209 exp_sizes = [int(line.split()[3]) for line in exp.output.split('\n')[1:]] 1210 self.assertEqual(orig_sizes[0], exp_sizes[0]) # first partition is not resized 1211 self.assertTrue(orig_sizes[1] < exp_sizes[1]) 1212 1213 # Check if all free space is partitioned 1214 result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path)) 1215 self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output) 1216 1217 os.rename(image_path, image_path + '.bak') 1218 os.rename(new_image_path, image_path) 1219 1220 # Check if it boots in qemu 1221 with runqemu('core-image-minimal', ssh=False) as qemu: 1222 cmd = "ls /etc/" 1223 status, output = qemu.run_serial('true') 1224 self.assertEqual(1, status, 'Failed to run command "%s": %s' % (cmd, output)) 1225 finally: 1226 if os.path.exists(new_image_path): 1227 os.unlink(new_image_path) 1228 if os.path.exists(image_path + '.bak'): 1229 os.rename(image_path + '.bak', image_path) 1230 1231 def test_wic_ls_ext(self): 1232 """Test listing content of the ext partition using 'wic ls'""" 1233 runCmd("wic create wictestdisk " 1234 "--image-name=core-image-minimal " 1235 "-D -o %s" % self.resultdir) 1236 images = glob(self.resultdir + "wictestdisk-*.direct") 1237 self.assertEqual(1, len(images)) 1238 1239 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1240 1241 # list directory content of the second ext4 partition 1242 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1243 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset( 1244 set(line.split()[-1] for line in result.output.split('\n') if line))) 1245 1246 def test_wic_cp_ext(self): 1247 """Test copy files and directories to the ext partition.""" 1248 runCmd("wic create wictestdisk " 1249 "--image-name=core-image-minimal " 1250 "-D -o %s" % self.resultdir) 1251 images = glob(self.resultdir + "wictestdisk-*.direct") 1252 self.assertEqual(1, len(images)) 1253 1254 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1255 1256 # list directory content of the ext4 partition 1257 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1258 dirs = set(line.split()[-1] for line in result.output.split('\n') if line) 1259 self.assertTrue(set(['bin', 'home', 'proc', 'usr', 'var', 'dev', 'lib', 'sbin']).issubset(dirs)) 1260 1261 with NamedTemporaryFile("w", suffix=".wic-cp") as testfile: 1262 testfile.write("test") 1263 1264 # copy file to the partition 1265 runCmd("wic cp %s %s:2/ -n %s" % (testfile.name, images[0], sysroot)) 1266 1267 # check if file is there 1268 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1269 newdirs = set(line.split()[-1] for line in result.output.split('\n') if line) 1270 self.assertEqual(newdirs.difference(dirs), set([os.path.basename(testfile.name)])) 1271 1272 # check if the file to copy is in the partition 1273 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1274 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line]) 1275 1276 # copy file from the partition, replace the temporary file content with it and 1277 # check for the file size to validate the copy 1278 runCmd("wic cp %s:2/etc/fstab %s -n %s" % (images[0], testfile.name, sysroot)) 1279 self.assertTrue(os.stat(testfile.name).st_size > 0) 1280 1281 1282 def test_wic_rm_ext(self): 1283 """Test removing files from the ext partition.""" 1284 runCmd("wic create mkefidisk " 1285 "--image-name=core-image-minimal " 1286 "-D -o %s" % self.resultdir) 1287 images = glob(self.resultdir + "mkefidisk-*.direct") 1288 self.assertEqual(1, len(images)) 1289 1290 sysroot = get_bb_var('RECIPE_SYSROOT_NATIVE', 'wic-tools') 1291 1292 # list directory content of the /etc directory on ext4 partition 1293 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1294 self.assertTrue('fstab' in [line.split()[-1] for line in result.output.split('\n') if line]) 1295 1296 # remove file 1297 runCmd("wic rm %s:2/etc/fstab -n %s" % (images[0], sysroot)) 1298 1299 # check if it's removed 1300 result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot)) 1301 self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line]) 1302 1303 # remove non-empty directory 1304 runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot)) 1305 1306 # check if it's removed 1307 result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot)) 1308 self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line]) 1309