summaryrefslogtreecommitdiffstats
path: root/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib')
-rw-r--r--scripts/lib/wic/plugins/imager/direct.py83
1 files changed, 43 insertions, 40 deletions
diff --git a/scripts/lib/wic/plugins/imager/direct.py b/scripts/lib/wic/plugins/imager/direct.py
index 481d24d5ba..2e2d26ad7d 100644
--- a/scripts/lib/wic/plugins/imager/direct.py
+++ b/scripts/lib/wic/plugins/imager/direct.py
@@ -23,14 +23,16 @@
23# AUTHORS 23# AUTHORS
24# Tom Zanussi <tom.zanussi (at] linux.intel.com> 24# Tom Zanussi <tom.zanussi (at] linux.intel.com>
25# 25#
26
27import logging
26import os 28import os
27import shutil 29import shutil
28import uuid 30import sys
29import tempfile 31import tempfile
32import uuid
30 33
31from time import strftime 34from time import strftime
32 35
33from wic import msger
34from wic.filemap import sparse_copy 36from wic.filemap import sparse_copy
35from wic.ksparser import KickStart, KickStartError 37from wic.ksparser import KickStart, KickStartError
36from wic.plugin import pluginmgr 38from wic.plugin import pluginmgr
@@ -38,6 +40,8 @@ from wic.pluginbase import ImagerPlugin
38from wic.utils.errors import ImageError 40from wic.utils.errors import ImageError
39from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd 41from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
40 42
43logger = logging.getLogger('wic')
44
41class DirectPlugin(ImagerPlugin): 45class DirectPlugin(ImagerPlugin):
42 """ 46 """
43 Install a system into a file containing a partitioned disk image. 47 Install a system into a file containing a partitioned disk image.
@@ -54,7 +58,8 @@ class DirectPlugin(ImagerPlugin):
54 try: 58 try:
55 self.ks = KickStart(wks_file) 59 self.ks = KickStart(wks_file)
56 except KickStartError as err: 60 except KickStartError as err:
57 msger.error(str(err)) 61 logger.error(str(err))
62 sys.exit(1)
58 63
59 # parse possible 'rootfs=name' items 64 # parse possible 'rootfs=name' items
60 self.rootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' ')) 65 self.rootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' '))
@@ -204,12 +209,12 @@ class DirectPlugin(ImagerPlugin):
204 full_path = self._image.path 209 full_path = self._image.path
205 # Generate .bmap 210 # Generate .bmap
206 if self.bmap: 211 if self.bmap:
207 msger.debug("Generating bmap file for %s" % disk_name) 212 logger.debug("Generating bmap file for %s", disk_name)
208 exec_native_cmd("bmaptool create %s -o %s.bmap" % (full_path, full_path), 213 exec_native_cmd("bmaptool create %s -o %s.bmap" % (full_path, full_path),
209 self.native_sysroot) 214 self.native_sysroot)
210 # Compress the image 215 # Compress the image
211 if self.compressor: 216 if self.compressor:
212 msger.debug("Compressing disk %s with %s" % (disk_name, self.compressor)) 217 logger.debug("Compressing disk %s with %s", disk_name, self.compressor)
213 exec_cmd("%s %s" % (self.compressor, full_path)) 218 exec_cmd("%s %s" % (self.compressor, full_path))
214 219
215 def print_info(self): 220 def print_info(self):
@@ -239,7 +244,7 @@ class DirectPlugin(ImagerPlugin):
239 msg += ' KERNEL_DIR: %s\n' % self.kernel_dir 244 msg += ' KERNEL_DIR: %s\n' % self.kernel_dir
240 msg += ' NATIVE_SYSROOT: %s\n' % self.native_sysroot 245 msg += ' NATIVE_SYSROOT: %s\n' % self.native_sysroot
241 246
242 msger.info(msg) 247 logger.info(msg)
243 248
244 @property 249 @property
245 def rootdev(self): 250 def rootdev(self):
@@ -342,7 +347,7 @@ class PartitionedImage():
342 partition on the disk. The 'ptable_format' parameter defines the 347 partition on the disk. The 'ptable_format' parameter defines the
343 partition table format and may be "msdos". """ 348 partition table format and may be "msdos". """
344 349
345 msger.debug("Assigning %s partitions to disks" % self.ptable_format) 350 logger.debug("Assigning %s partitions to disks", self.ptable_format)
346 351
347 # Go through partitions in the order they are added in .ks file 352 # Go through partitions in the order they are added in .ks file
348 for num in range(len(self.partitions)): 353 for num in range(len(self.partitions)):
@@ -389,10 +394,10 @@ class PartitionedImage():
389 # to move forward to the next alignment point 394 # to move forward to the next alignment point
390 align_sectors = (part.align * 1024 // self.sector_size) - align_sectors 395 align_sectors = (part.align * 1024 // self.sector_size) - align_sectors
391 396
392 msger.debug("Realignment for %s%s with %s sectors, original" 397 logger.debug("Realignment for %s%s with %s sectors, original"
393 " offset %s, target alignment is %sK." % 398 " offset %s, target alignment is %sK.",
394 (part.disk, self.numpart, align_sectors, 399 part.disk, self.numpart, align_sectors,
395 self.offset, part.align)) 400 self.offset, part.align)
396 401
397 # increase the offset so we actually start the partition on right alignment 402 # increase the offset so we actually start the partition on right alignment
398 self.offset += align_sectors 403 self.offset += align_sectors
@@ -413,11 +418,10 @@ class PartitionedImage():
413 part.type = 'logical' 418 part.type = 'logical'
414 part.num = self.realpart + 1 419 part.num = self.realpart + 1
415 420
416 msger.debug("Assigned %s to %s%d, sectors range %d-%d size %d " 421 logger.debug("Assigned %s to %s%d, sectors range %d-%d size %d "
417 "sectors (%d bytes)." \ 422 "sectors (%d bytes).", part.mountpoint, part.disk,
418 % (part.mountpoint, part.disk, part.num, 423 part.num, part.start, self.offset - 1, part.size_sec,
419 part.start, self.offset - 1, 424 part.size_sec * self.sector_size)
420 part.size_sec, part.size_sec * self.sector_size))
421 425
422 # Once all the partitions have been layed out, we can calculate the 426 # Once all the partitions have been layed out, we can calculate the
423 # minumim disk size 427 # minumim disk size
@@ -432,8 +436,8 @@ class PartitionedImage():
432 436
433 # Start is included to the size so we need to substract one from the end. 437 # Start is included to the size so we need to substract one from the end.
434 end = start + size - 1 438 end = start + size - 1
435 msger.debug("Added '%s' partition, sectors %d-%d, size %d sectors" % 439 logger.debug("Added '%s' partition, sectors %d-%d, size %d sectors",
436 (parttype, start, end, size)) 440 parttype, start, end, size)
437 441
438 cmd = "parted -s %s unit s mkpart %s" % (device, parttype) 442 cmd = "parted -s %s unit s mkpart %s" % (device, parttype)
439 if fstype: 443 if fstype:
@@ -443,20 +447,20 @@ class PartitionedImage():
443 return exec_native_cmd(cmd, self.native_sysroot) 447 return exec_native_cmd(cmd, self.native_sysroot)
444 448
445 def create(self): 449 def create(self):
446 msger.debug("Creating sparse file %s" % self.path) 450 logger.debug("Creating sparse file %s", self.path)
447 with open(self.path, 'w') as sparse: 451 with open(self.path, 'w') as sparse:
448 os.ftruncate(sparse.fileno(), self.min_size) 452 os.ftruncate(sparse.fileno(), self.min_size)
449 453
450 msger.debug("Initializing partition table for %s" % self.path) 454 logger.debug("Initializing partition table for %s", self.path)
451 exec_native_cmd("parted -s %s mklabel %s" % 455 exec_native_cmd("parted -s %s mklabel %s" %
452 (self.path, self.ptable_format), self.native_sysroot) 456 (self.path, self.ptable_format), self.native_sysroot)
453 457
454 msger.debug("Set disk identifier %x" % self.identifier) 458 logger.debug("Set disk identifier %x", self.identifier)
455 with open(self.path, 'r+b') as img: 459 with open(self.path, 'r+b') as img:
456 img.seek(0x1B8) 460 img.seek(0x1B8)
457 img.write(self.identifier.to_bytes(4, 'little')) 461 img.write(self.identifier.to_bytes(4, 'little'))
458 462
459 msger.debug("Creating partitions") 463 logger.debug("Creating partitions")
460 464
461 for part in self.partitions: 465 for part in self.partitions:
462 if part.num == 0: 466 if part.num == 0:
@@ -494,39 +498,39 @@ class PartitionedImage():
494 # even number of sectors. 498 # even number of sectors.
495 if part.mountpoint == "/boot" and part.fstype in ["vfat", "msdos"] \ 499 if part.mountpoint == "/boot" and part.fstype in ["vfat", "msdos"] \
496 and part.size_sec % 2: 500 and part.size_sec % 2:
497 msger.debug("Subtracting one sector from '%s' partition to " \ 501 logger.debug("Subtracting one sector from '%s' partition to "
498 "get even number of sectors for the partition" % \ 502 "get even number of sectors for the partition",
499 part.mountpoint) 503 part.mountpoint)
500 part.size_sec -= 1 504 part.size_sec -= 1
501 505
502 self._create_partition(self.path, part.type, 506 self._create_partition(self.path, part.type,
503 parted_fs_type, part.start, part.size_sec) 507 parted_fs_type, part.start, part.size_sec)
504 508
505 if part.part_type: 509 if part.part_type:
506 msger.debug("partition %d: set type UID to %s" % \ 510 logger.debug("partition %d: set type UID to %s",
507 (part.num, part.part_type)) 511 part.num, part.part_type)
508 exec_native_cmd("sgdisk --typecode=%d:%s %s" % \ 512 exec_native_cmd("sgdisk --typecode=%d:%s %s" % \
509 (part.num, part.part_type, 513 (part.num, part.part_type,
510 self.path), self.native_sysroot) 514 self.path), self.native_sysroot)
511 515
512 if part.uuid and self.ptable_format == "gpt": 516 if part.uuid and self.ptable_format == "gpt":
513 msger.debug("partition %d: set UUID to %s" % \ 517 logger.debug("partition %d: set UUID to %s",
514 (part.num, part.uuid)) 518 part.num, part.uuid)
515 exec_native_cmd("sgdisk --partition-guid=%d:%s %s" % \ 519 exec_native_cmd("sgdisk --partition-guid=%d:%s %s" % \
516 (part.num, part.uuid, self.path), 520 (part.num, part.uuid, self.path),
517 self.native_sysroot) 521 self.native_sysroot)
518 522
519 if part.label and self.ptable_format == "gpt": 523 if part.label and self.ptable_format == "gpt":
520 msger.debug("partition %d: set name to %s" % \ 524 logger.debug("partition %d: set name to %s",
521 (part.num, part.label)) 525 part.num, part.label)
522 exec_native_cmd("parted -s %s name %d %s" % \ 526 exec_native_cmd("parted -s %s name %d %s" % \
523 (self.path, part.num, part.label), 527 (self.path, part.num, part.label),
524 self.native_sysroot) 528 self.native_sysroot)
525 529
526 if part.active: 530 if part.active:
527 flag_name = "legacy_boot" if self.ptable_format == 'gpt' else "boot" 531 flag_name = "legacy_boot" if self.ptable_format == 'gpt' else "boot"
528 msger.debug("Set '%s' flag for partition '%s' on disk '%s'" % \ 532 logger.debug("Set '%s' flag for partition '%s' on disk '%s'",
529 (flag_name, part.num, self.path)) 533 flag_name, part.num, self.path)
530 exec_native_cmd("parted -s %s set %d %s on" % \ 534 exec_native_cmd("parted -s %s set %d %s on" % \
531 (self.path, part.num, flag_name), 535 (self.path, part.num, flag_name),
532 self.native_sysroot) 536 self.native_sysroot)
@@ -540,8 +544,8 @@ class PartitionedImage():
540 # isn't necessary). 544 # isn't necessary).
541 if parted_fs_type == "fat16": 545 if parted_fs_type == "fat16":
542 if self.ptable_format == 'msdos': 546 if self.ptable_format == 'msdos':
543 msger.debug("Disable 'lba' flag for partition '%s' on disk '%s'" % \ 547 logger.debug("Disable 'lba' flag for partition '%s' on disk '%s'",
544 (part.num, self.path)) 548 part.num, self.path)
545 exec_native_cmd("parted -s %s set %d lba off" % \ 549 exec_native_cmd("parted -s %s set %d lba off" % \
546 (self.path, part.num), 550 (self.path, part.num),
547 self.native_sysroot) 551 self.native_sysroot)
@@ -552,7 +556,7 @@ class PartitionedImage():
552 os.remove(image) 556 os.remove(image)
553 557
554 def assemble(self): 558 def assemble(self):
555 msger.debug("Installing partitions") 559 logger.debug("Installing partitions")
556 560
557 for part in self.partitions: 561 for part in self.partitions:
558 source = part.source_file 562 source = part.source_file
@@ -560,10 +564,9 @@ class PartitionedImage():
560 # install source_file contents into a partition 564 # install source_file contents into a partition
561 sparse_copy(source, self.path, part.start * self.sector_size) 565 sparse_copy(source, self.path, part.start * self.sector_size)
562 566
563 msger.debug("Installed %s in partition %d, sectors %d-%d, " 567 logger.debug("Installed %s in partition %d, sectors %d-%d, "
564 "size %d sectors" % \ 568 "size %d sectors", source, part.num, part.start,
565 (source, part.num, part.start, 569 part.start + part.size_sec - 1, part.size_sec)
566 part.start + part.size_sec - 1, part.size_sec))
567 570
568 partimage = self.path + '.p%d' % part.num 571 partimage = self.path + '.p%d' % part.num
569 os.rename(source, partimage) 572 os.rename(source, partimage)